|
| 1 | +# (c) Copyright IBM Corp. 2024 |
| 2 | + |
| 3 | +import os |
| 4 | +import json |
| 5 | +import unittest |
| 6 | + |
| 7 | +from instana.tracer import InstanaTracer |
| 8 | +from instana.recorder import StanRecorder |
| 9 | +from instana.agent.aws_eks_fargate import EKSFargateAgent |
| 10 | +from instana.singletons import get_agent, set_agent, get_tracer, set_tracer |
| 11 | + |
| 12 | + |
| 13 | +class TestFargateCollector(unittest.TestCase): |
| 14 | + def __init__(self, methodName='runTest'): |
| 15 | + super(TestFargateCollector, self).__init__(methodName) |
| 16 | + self.agent = None |
| 17 | + self.span_recorder = None |
| 18 | + self.tracer = None |
| 19 | + self.pwd = os.path.dirname(os.path.realpath(__file__)) |
| 20 | + |
| 21 | + self.original_agent = get_agent() |
| 22 | + self.original_tracer = get_tracer() |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + os.environ["INSTANA_TRACER_ENVIRONMENT"] = "AWS_EKS_FARGATE" |
| 26 | + os.environ["INSTANA_ENDPOINT_URL"] = "https://localhost/notreal" |
| 27 | + os.environ["INSTANA_AGENT_KEY"] = "Fake_Key" |
| 28 | + |
| 29 | + def tearDown(self): |
| 30 | + """ Reset all environment variables of consequence """ |
| 31 | + variable_names = ( |
| 32 | + "INSTANA_TRACER_ENVIRONMENT", |
| 33 | + "AWS_EXECUTION_ENV", "INSTANA_EXTRA_HTTP_HEADERS", |
| 34 | + "INSTANA_ENDPOINT_URL", "INSTANA_ENDPOINT_PROXY", |
| 35 | + "INSTANA_AGENT_KEY", "INSTANA_ZONE", "INSTANA_TAGS" |
| 36 | + ) |
| 37 | + |
| 38 | + for variable_name in variable_names: |
| 39 | + if variable_name in os.environ: |
| 40 | + os.environ.pop(variable_name) |
| 41 | + |
| 42 | + set_agent(self.original_agent) |
| 43 | + set_tracer(self.original_tracer) |
| 44 | + |
| 45 | + def create_agent_and_setup_tracer(self): |
| 46 | + self.agent = EKSFargateAgent() |
| 47 | + self.span_recorder = StanRecorder(self.agent) |
| 48 | + self.tracer = InstanaTracer(recorder=self.span_recorder) |
| 49 | + set_agent(self.agent) |
| 50 | + set_tracer(self.tracer) |
| 51 | + |
| 52 | + def test_prepare_payload_basics(self): |
| 53 | + self.create_agent_and_setup_tracer() |
| 54 | + |
| 55 | + payload = self.agent.collector.prepare_payload() |
| 56 | + self.assertTrue(payload) |
| 57 | + |
| 58 | + self.assertEqual(2, len(payload.keys())) |
| 59 | + self.assertIn('spans',payload) |
| 60 | + self.assertIsInstance(payload['spans'], list) |
| 61 | + self.assertEqual(0, len(payload['spans'])) |
| 62 | + self.assertIn('metrics', payload) |
| 63 | + self.assertEqual(1, len(payload['metrics'].keys())) |
| 64 | + self.assertIn('plugins', payload['metrics']) |
| 65 | + self.assertIsInstance(payload['metrics']['plugins'], list) |
| 66 | + self.assertEqual(2, len(payload['metrics']['plugins'])) |
| 67 | + |
| 68 | + |
| 69 | + process_plugin = payload['metrics']['plugins'][0] |
| 70 | + #self.assertIn('data', process_plugin) |
| 71 | + |
| 72 | + runtime_plugin = payload['metrics']['plugins'][1] |
| 73 | + self.assertIn('name', runtime_plugin) |
| 74 | + self.assertIn('entityId', runtime_plugin) |
| 75 | + self.assertIn('data', runtime_plugin) |
| 76 | + |
| 77 | + def test_no_instana_zone(self): |
| 78 | + self.create_agent_and_setup_tracer() |
| 79 | + self.assertIsNone(self.agent.options.zone) |
| 80 | + |
| 81 | + def test_instana_zone(self): |
| 82 | + os.environ["INSTANA_ZONE"] = "YellowDog" |
| 83 | + self.create_agent_and_setup_tracer() |
| 84 | + |
| 85 | + self.assertEqual(self.agent.options.zone, "YellowDog") |
| 86 | + |
| 87 | + payload = self.agent.collector.prepare_payload() |
| 88 | + self.assertTrue(payload) |
| 89 | + |
| 90 | + plugins = payload['metrics']['plugins'] |
| 91 | + self.assertIsInstance(plugins, list) |
| 92 | + |
| 93 | + process_plugin = payload['metrics']['plugins'][0] |
| 94 | + self.assertTrue(process_plugin) |
| 95 | + self.assertIn("data", process_plugin) |
| 96 | + self.assertIn("instanaZone", process_plugin["data"]) |
| 97 | + self.assertEqual(process_plugin["data"]["instanaZone"], "YellowDog") |
| 98 | + |
| 99 | + def test_custom_tags(self): |
| 100 | + os.environ["INSTANA_TAGS"] = "love,war=1,games" |
| 101 | + self.create_agent_and_setup_tracer() |
| 102 | + self.assertTrue(hasattr(self.agent.options, 'tags')) |
| 103 | + self.assertDictEqual(self.agent.options.tags, {"love": None, "war": "1", "games": None}) |
| 104 | + |
| 105 | + payload = self.agent.collector.prepare_payload() |
| 106 | + |
| 107 | + self.assertTrue(payload) |
| 108 | + task_plugin = None |
| 109 | + process_plugin = payload['metrics']['plugins'][0] |
| 110 | + self.assertTrue(process_plugin) |
| 111 | + self.assertIn("tags", process_plugin["data"]) |
| 112 | + tags = process_plugin["data"]["tags"] |
| 113 | + self.assertEqual(tags["war"], "1") |
| 114 | + self.assertIsNone(tags["love"]) |
| 115 | + self.assertIsNone(tags["games"]) |
0 commit comments