|
| 1 | +# (c) Copyright IBM Corp. 2026 |
| 2 | + |
| 3 | +""" |
| 4 | +Unit tests for AWSLambdaCollector. |
| 5 | +This test suite validates the payload preparation and metric reporting |
| 6 | +for the AWS Lambda instrumentation collector. |
| 7 | +""" |
| 8 | + |
| 9 | +from collections import defaultdict |
| 10 | +from typing import Generator |
| 11 | +from unittest.mock import MagicMock, patch |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from instana.collector.aws_lambda import AWSLambdaCollector |
| 16 | + |
| 17 | + |
| 18 | +class TestAWSLambdaCollector: |
| 19 | + """Test suite for AWSLambdaCollector base class.""" |
| 20 | + |
| 21 | + @pytest.fixture(autouse=True) |
| 22 | + def _resource( |
| 23 | + self, |
| 24 | + caplog: pytest.LogCaptureFixture, |
| 25 | + ) -> Generator[None, None, None]: |
| 26 | + |
| 27 | + self.agent = MagicMock() |
| 28 | + self.collector = AWSLambdaCollector(self.agent) |
| 29 | + yield |
| 30 | + |
| 31 | + def test_prepare_payload_empty_span_queue(self) -> None: |
| 32 | + """Test that prepare_payload returns an empty list for spans when queue is empty.""" |
| 33 | + payload = self.collector.prepare_payload() |
| 34 | + |
| 35 | + assert isinstance(payload, defaultdict) |
| 36 | + |
| 37 | + assert "spans" in payload |
| 38 | + assert payload["spans"] == [] |
| 39 | + |
| 40 | + assert "metrics" in payload |
| 41 | + assert "plugins" in payload["metrics"] |
| 42 | + |
| 43 | + def test_prepare_payload_with_spans(self) -> None: |
| 44 | + """Test that prepare_payload formats queued spans and includes them in the payload.""" |
| 45 | + mock_span = MagicMock() |
| 46 | + self.collector.span_queue.put(mock_span) |
| 47 | + |
| 48 | + # Mock queued_spans and format_span |
| 49 | + self.collector.queued_spans = MagicMock(return_value=[mock_span]) |
| 50 | + |
| 51 | + with patch("instana.collector.aws_lambda.format_span") as mock_format: |
| 52 | + mock_format.return_value = [{"n": "test_span"}] |
| 53 | + payload = self.collector.prepare_payload() |
| 54 | + |
| 55 | + assert payload["spans"] == [{"n": "test_span"}] |
| 56 | + assert payload["metrics"]["plugins"] == [] |
| 57 | + |
| 58 | + def test_prepare_payload_with_snapshot_data(self) -> None: |
| 59 | + """Test that prepare_payload includes snapshot data when it should be sent.""" |
| 60 | + self.collector.snapshot_data = { |
| 61 | + "plugins": [{"name": "com.instana.plugin.aws.lambda"}] |
| 62 | + } |
| 63 | + self.collector.snapshot_data_sent = False |
| 64 | + |
| 65 | + payload = self.collector.prepare_payload() |
| 66 | + |
| 67 | + assert payload["metrics"] == { |
| 68 | + "plugins": [{"name": "com.instana.plugin.aws.lambda"}] |
| 69 | + } |
| 70 | + assert self.collector.snapshot_data_sent is True |
0 commit comments