|
1 | | -# Copyright (c) Microsoft. All rights reserved. |
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
2 | 3 |
|
| 4 | +import unittest |
| 5 | +from unittest.mock import Mock, patch |
3 | 6 |
|
4 | 7 | from microsoft_agents_a365.observability.core import configure |
| 8 | +from microsoft_agents_a365.observability.core.exporters.agent365_exporter_options import ( |
| 9 | + Agent365ExporterOptions, |
| 10 | +) |
5 | 11 | from microsoft_agents_a365.observability.core.trace_processor import SpanProcessor |
6 | 12 |
|
7 | 13 |
|
8 | | -def test_basic_functionality(): |
9 | | - """Test basic Microsoft Agent 365 SDK functionality""" |
10 | | - print("Testing Microsoft Agent 365 SDK...") |
| 14 | +class TestAgent365Configure(unittest.TestCase): |
| 15 | + """Test suite for Agent365 configuration functionality.""" |
11 | 16 |
|
12 | | - # Test configure function |
13 | | - try: |
14 | | - configure( |
| 17 | + def setUp(self): |
| 18 | + """Set up test fixtures.""" |
| 19 | + self.mock_token_resolver = Mock() |
| 20 | + self.mock_token_resolver.return_value = "test_token_123" |
| 21 | + |
| 22 | + def test_configure_basic_functionality(self): |
| 23 | + """Test configure function with basic parameters and legacy parameters.""" |
| 24 | + # Test basic configuration without exporter_options |
| 25 | + result = configure( |
| 26 | + service_name="test-service", |
| 27 | + service_namespace="test-namespace", |
| 28 | + ) |
| 29 | + self.assertTrue(result, "configure() should return True") |
| 30 | + |
| 31 | + # Test configuration with legacy parameters |
| 32 | + result = configure( |
| 33 | + service_name="test-service", |
| 34 | + service_namespace="test-namespace", |
| 35 | + token_resolver=self.mock_token_resolver, |
| 36 | + cluster_category="test", |
| 37 | + ) |
| 38 | + self.assertTrue(result, "configure() should return True with legacy parameters") |
| 39 | + |
| 40 | + @patch("microsoft_agents_a365.observability.core.config.is_agent365_exporter_enabled") |
| 41 | + def test_configure_with_exporter_options_and_parameter_precedence(self, mock_is_enabled): |
| 42 | + """Test configure function with exporter_options and verify parameter precedence.""" |
| 43 | + # Enable Agent365 exporter for this test |
| 44 | + mock_is_enabled.return_value = True |
| 45 | + |
| 46 | + # Test 1: Basic exporter_options functionality |
| 47 | + exporter_options = Agent365ExporterOptions( |
| 48 | + cluster_category="dev", |
| 49 | + token_resolver=self.mock_token_resolver, |
| 50 | + use_s2s_endpoint=True, |
| 51 | + max_queue_size=1024, |
| 52 | + scheduled_delay_ms=2500, |
| 53 | + exporter_timeout_ms=15000, |
| 54 | + max_export_batch_size=256, |
| 55 | + ) |
| 56 | + |
| 57 | + result = configure( |
15 | 58 | service_name="test-service", |
16 | 59 | service_namespace="test-namespace", |
| 60 | + exporter_options=exporter_options, |
| 61 | + ) |
| 62 | + self.assertTrue(result, "configure() should return True with exporter_options") |
| 63 | + |
| 64 | + @patch("microsoft_agents_a365.observability.core.config.Agent365Exporter") |
| 65 | + @patch("microsoft_agents_a365.observability.core.config.BatchSpanProcessor") |
| 66 | + @patch("microsoft_agents_a365.observability.core.config.is_agent365_exporter_enabled") |
| 67 | + def test_batch_span_processor_and_exporter_called_with_correct_values( |
| 68 | + self, mock_is_enabled, mock_batch_processor, mock_exporter |
| 69 | + ): |
| 70 | + """Test that BatchSpanProcessor and Agent365Exporter are called with correct values from exporter_options.""" |
| 71 | + # Enable Agent365 exporter for this test |
| 72 | + mock_is_enabled.return_value = True |
| 73 | + |
| 74 | + # Create exporter options with specific values |
| 75 | + exporter_options = Agent365ExporterOptions( |
| 76 | + cluster_category="staging", |
| 77 | + token_resolver=self.mock_token_resolver, |
| 78 | + use_s2s_endpoint=True, |
| 79 | + max_queue_size=512, |
| 80 | + scheduled_delay_ms=1000, |
| 81 | + exporter_timeout_ms=10000, |
| 82 | + max_export_batch_size=128, |
| 83 | + ) |
| 84 | + |
| 85 | + # Configure with exporter_options |
| 86 | + result = configure( |
| 87 | + service_name="test-service", |
| 88 | + service_namespace="test-namespace", |
| 89 | + exporter_options=exporter_options, |
| 90 | + ) |
| 91 | + |
| 92 | + # Verify configuration succeeded |
| 93 | + self.assertTrue(result, "configure() should return True") |
| 94 | + |
| 95 | + # Verify Agent365Exporter was called with correct parameters |
| 96 | + mock_exporter.assert_called_once_with( |
| 97 | + token_resolver=self.mock_token_resolver, |
| 98 | + cluster_category="staging", |
| 99 | + use_s2s_endpoint=True, |
17 | 100 | ) |
18 | | - print("✅ configure() executed successfully") |
19 | | - except Exception as e: |
20 | | - print(f"❌ configure() failed: {e}") |
21 | | - return False |
22 | 101 |
|
23 | | - # Test SpanProcessor class |
24 | | - try: |
25 | | - SpanProcessor() |
26 | | - print("✅ SpanProcessor created successfully") |
27 | | - except Exception as e: |
28 | | - print(f"❌ SpanProcessor creation failed: {e}") |
29 | | - return False |
| 102 | + # Verify BatchSpanProcessor was called with correct parameters from exporter_options |
| 103 | + mock_batch_processor.assert_called_once() |
| 104 | + call_args = mock_batch_processor.call_args |
| 105 | + self.assertEqual(call_args.kwargs["max_queue_size"], 512) |
| 106 | + self.assertEqual(call_args.kwargs["schedule_delay_millis"], 1000) |
| 107 | + self.assertEqual(call_args.kwargs["export_timeout_millis"], 10000) |
| 108 | + self.assertEqual(call_args.kwargs["max_export_batch_size"], 128) |
30 | 109 |
|
31 | | - print("✅ All tests passed!") |
32 | | - return True |
| 110 | + def test_span_processor_creation(self): |
| 111 | + """Test SpanProcessor class creation.""" |
| 112 | + processor = SpanProcessor() |
| 113 | + self.assertIsNotNone(processor, "SpanProcessor should be created successfully") |
33 | 114 |
|
34 | 115 |
|
35 | 116 | if __name__ == "__main__": |
36 | | - test_basic_functionality() |
| 117 | + unittest.main() |
0 commit comments