-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_host.py
More file actions
806 lines (702 loc) · 28.8 KB
/
Copy pathtest_host.py
File metadata and controls
806 lines (702 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2020
import datetime
import json
import logging
import os
from typing import Any, Dict, Generator
from unittest.mock import Mock
import pytest
import requests
from mock import MagicMock, patch
from instana.agent.host import AnnounceData, HostAgent
from instana.collector.host import HostCollector
from instana.fsm import TheMachine
from instana.options import StandardOptions
from instana.recorder import StanRecorder
from instana.singletons import get_agent
from instana.span.span import InstanaSpan
from instana.span_context import SpanContext
from instana.util.process_discovery import Discovery
from instana.util.runtime import is_windows
class TestHostAgent:
@pytest.fixture(autouse=True)
def _resource(
self,
caplog: pytest.LogCaptureFixture,
) -> Generator[None, None, None]:
self.agent = get_agent()
self.span_recorder = None
self.tracer = None
yield
caplog.clear()
variable_names = (
"AWS_EXECUTION_ENV",
"INSTANA_EXTRA_HTTP_HEADERS",
"INSTANA_ENDPOINT_URL",
"INSTANA_ENDPOINT_PROXY",
"INSTANA_AGENT_KEY",
"INSTANA_LOG_LEVEL",
"INSTANA_SERVICE_NAME",
"INSTANA_SECRETS",
"INSTANA_TAGS",
)
for variable_name in variable_names:
if variable_name in os.environ:
os.environ.pop(variable_name)
def test_secrets(self) -> None:
assert hasattr(self.agent.options, "secrets_matcher")
assert self.agent.options.secrets_matcher == "contains-ignore-case"
assert hasattr(self.agent.options, "secrets_list")
assert self.agent.options.secrets_list == ["key", "pass", "secret"]
def test_options_have_extra_http_headers(self) -> None:
assert hasattr(self.agent, "options")
assert hasattr(self.agent.options, "extra_http_headers")
def test_has_options(self) -> None:
assert hasattr(self.agent, "options")
assert isinstance(self.agent.options, StandardOptions)
def test_agent_default_log_level(self) -> None:
assert self.agent.options.log_level == logging.WARNING
def test_agent_instana_debug(self) -> None:
os.environ["INSTANA_DEBUG"] = "asdf"
self.agent.options = StandardOptions()
assert self.agent.options.log_level == logging.DEBUG
def test_agent_instana_service_name(self) -> None:
os.environ["INSTANA_SERVICE_NAME"] = "greycake"
self.agent.options = StandardOptions()
assert self.agent.options.service_name == "greycake"
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_is_successful(
self,
mock_requests_session_put: MagicMock,
) -> None:
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a"
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = (
"{" f' "pid": {test_pid}, ' f' "agentUuid": "{test_agent_uuid}"' "}"
)
# This mocks the call to self.agent.client.put
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
payload = self.agent.announce(d)
assert "pid" in payload
assert test_pid == payload["pid"]
assert "agentUuid" in payload
assert test_agent_uuid == payload["agentUuid"]
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_fails_with_non_200(
self,
mock_requests_session_put: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
mock_response = MagicMock()
mock_response.status_code = 404
mock_response.content = ""
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
caplog.set_level(logging.DEBUG, logger="instana")
caplog.clear()
payload = self.agent.announce(d)
assert payload is None
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert "response status code" in caplog.messages[0]
assert "is NOT 200" in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_fails_with_non_json(
self,
mock_requests_session_put: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = ""
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
caplog.set_level(logging.DEBUG, logger="instana")
caplog.clear()
payload = self.agent.announce(d)
assert payload is None
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert "response is not JSON" in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_fails_with_empty_list_json(
self,
mock_requests_session_put: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = "[]"
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
caplog.set_level(logging.DEBUG, logger="instana")
caplog.clear()
payload = self.agent.announce(d)
assert payload is None
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert "payload has no fields" in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_fails_with_missing_pid(
self,
mock_requests_session_put: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a"
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = "{" f' "agentUuid": "{test_agent_uuid}"' "}"
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
caplog.clear()
payload = self.agent.announce(d)
assert payload is None
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert "response payload has no pid" in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "put")
def test_announce_fails_with_missing_uuid(
self,
mock_requests_session_put: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
test_pid = 4242
test_process_name = "test_process"
test_process_args = ["-v", "-d"]
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.content = "{" f' "pid": {test_pid} ' "}"
mock_requests_session_put.return_value = mock_response
d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args)
caplog.clear()
payload = self.agent.announce(d)
assert payload is None
assert len(caplog.messages) == 1
assert len(caplog.records) == 1
assert "response payload has no agentUuid" in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "get")
def test_agent_connection_attempt(
self,
mock_requests_session_get: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
mock_response = MagicMock()
mock_response.status_code = 200
mock_requests_session_get.return_value = mock_response
host = self.agent.options.agent_host
port = self.agent.options.agent_port
msg = f"Instana host agent found on {host}:{port}"
result = self.agent.is_agent_listening(host, port)
assert result
assert msg in caplog.messages[0]
@pytest.mark.original
@patch.object(requests.Session, "get")
def test_agent_connection_attempt_fails_with_404(
self,
mock_requests_session_get: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
caplog.set_level(logging.DEBUG, logger="instana")
mock_response = MagicMock()
mock_response.status_code = 404
mock_requests_session_get.return_value = mock_response
host = self.agent.options.agent_host
port = self.agent.options.agent_port
msg = (
"The attempt to connect to the Instana host agent on "
f"{host}:{port} has failed with an unexpected status code. "
f"Expected HTTP 200 but received: {mock_response.status_code}"
)
caplog.clear()
result = self.agent.is_agent_listening(host, port)
assert not result
assert msg in caplog.messages[0]
@pytest.mark.skipif(
is_windows(),
reason='Avoiding "psutil.NoSuchProcess: process PID not found (pid=12345)"',
)
def test_init(self) -> None:
with patch(
"instana.agent.base.BaseAgent.update_log_level"
) as mock_update, patch.object(os, "getpid", return_value=12345):
agent = HostAgent()
assert not agent.announce_data
assert not agent.last_seen
assert not agent.last_fork_check
assert agent._boot_pid == 12345
mock_update.assert_called_once()
assert isinstance(agent.options, StandardOptions)
assert isinstance(agent.collector, HostCollector)
assert isinstance(agent.machine, TheMachine)
def test_start(
self,
) -> None:
with patch("instana.collector.host.HostCollector.start") as mock_start:
agent = HostAgent()
agent.start()
mock_start.assert_called_once()
def test_handle_fork(
self,
) -> None:
with patch.object(HostAgent, "reset") as mock_reset:
agent = HostAgent()
agent.handle_fork()
mock_reset.assert_called_once()
def test_reset(
self,
) -> None:
with patch(
"instana.collector.host.HostCollector.shutdown"
) as mock_shutdown, patch("instana.fsm.TheMachine.reset") as mock_reset:
agent = HostAgent()
agent.reset()
assert not agent.last_seen
assert not agent.announce_data
mock_shutdown.assert_called_once_with(report_final=False)
mock_reset.assert_called_once()
def test_is_timed_out(
self,
) -> None:
agent = HostAgent()
assert not agent.is_timed_out()
agent.last_seen = datetime.datetime.now() - datetime.timedelta(minutes=5)
agent.can_send = True
assert agent.is_timed_out()
def test_can_send_test_env(
self,
) -> None:
agent = HostAgent()
with patch.dict("os.environ", {"INSTANA_TEST": "sample-data"}):
if "INSTANA_TEST" in os.environ:
assert agent.can_send()
@pytest.mark.original
def test_can_send(
self,
) -> None:
agent = HostAgent()
agent._boot_pid = 12345
with patch.object(os, "getpid", return_value=12344), patch(
"instana.agent.host.HostAgent.handle_fork"
) as mock_handle, patch.dict("os.environ", {}, clear=True):
agent.can_send()
assert agent._boot_pid == 12344
mock_handle.assert_called_once()
with patch.object(agent.machine.fsm, "current", "wait4init"):
assert agent.can_send() is True
@pytest.mark.original
def test_can_send_default(
self,
) -> None:
agent = HostAgent()
with patch.dict("os.environ", {}, clear=True):
assert not agent.can_send()
def test_set_from(
self,
) -> None:
agent = HostAgent()
sample_res_data = {
"secrets": {"matcher": "value-1", "list": ["value-2"]},
"extraHeaders": ["value-3"],
"agentUuid": "value-4",
"pid": 1234,
}
agent.options.extra_http_headers = None
agent.set_from(sample_res_data)
assert agent.options.secrets_matcher == "value-1"
assert agent.options.secrets_list == ["value-2"]
assert agent.options.extra_http_headers == ["value-3"]
agent.options.extra_http_headers = ["value"]
agent.set_from(sample_res_data)
assert "value" in agent.options.extra_http_headers
assert agent.announce_data.agentUuid == "value-4"
assert agent.announce_data.pid == 1234
@pytest.mark.original
def test_get_from_structure(
self,
) -> None:
agent = HostAgent()
agent.announce_data = AnnounceData(pid=1234, agentUuid="value")
assert agent.get_from_structure() == {"e": 1234, "h": "value"}
@pytest.mark.original
def test_is_agent_listening(
self,
caplog,
) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
with patch.object(requests.Session, "get", return_value=mock_response):
assert agent.is_agent_listening("sample", 1234)
mock_response.status_code = 404
with patch.object(
requests.Session, "get", return_value=mock_response, clear=True
):
assert not agent.is_agent_listening("sample", 1234)
host = "localhost"
port = 123
with patch.object(requests.Session, "get", side_effect=Exception()):
caplog.set_level(logging.DEBUG, logger="instana")
agent.is_agent_listening(host, port)
assert f"Instana Host Agent not found on {host}:{port}" in caplog.messages
@pytest.mark.original
def test_announce(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.content = json.dumps(
{"get": "value", "pid": "value", "agentUuid": "value"}
)
response = json.loads(mock_response.content)
with patch.object(requests.Session, "put", return_value=mock_response):
assert agent.announce("sample-data") == response
mock_response.content = mock_response.content.encode("UTF-8")
with patch.object(requests.Session, "put", return_value=mock_response):
assert agent.announce("sample-data") == response
mock_response.content = json.dumps(
{"get": "value", "pid": "value", "agentUuid": "value"}
)
with patch.object(requests.Session, "put", side_effect=Exception()):
caplog.set_level(logging.DEBUG, logger="instana")
assert not agent.announce("sample-data")
assert (
f"announce: connection error ({type(Exception())})" in caplog.messages
)
mock_response.content = json.dumps("key")
with patch.object(
requests.Session, "put", return_value=mock_response, clear=True
):
caplog.set_level(logging.DEBUG, logger="instana")
assert not agent.announce("sample-data")
assert "announce: response payload has no fields: (key)" in caplog.messages
mock_response.content = json.dumps({"key": "value"})
with patch.object(
requests.Session, "put", return_value=mock_response, clear=True
):
caplog.set_level(logging.DEBUG, logger="instana")
assert not agent.announce("sample-data")
assert (
"announce: response payload has no pid: ({'key': 'value'})"
in caplog.messages
)
mock_response.content = json.dumps({"pid": "value"})
with patch.object(
requests.Session, "put", return_value=mock_response, clear=True
):
caplog.set_level(logging.DEBUG, logger="instana")
assert not agent.announce("sample-data")
assert (
"announce: response payload has no agentUuid: ({'pid': 'value'})"
in caplog.messages
)
mock_response.status_code = 404
with patch.object(
requests.Session, "put", return_value=mock_response, clear=True
):
assert not agent.announce("sample-data")
assert "announce: response status code (404) is NOT 200" in caplog.messages
def test_log_message_to_host_agent(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.return_value = "sample"
mock_datetime = datetime.datetime(2022, 1, 1, 12, 0, 0)
with patch.object(requests.Session, "post", return_value=mock_response), patch(
"instana.agent.host.datetime"
) as mock_date:
mock_date.now.return_value = mock_datetime
mock_date.side_effect = lambda *args, **kwargs: datetime(*args, **kwargs)
agent.log_message_to_host_agent("sample")
assert agent.last_seen == mock_datetime
with patch.object(requests.Session, "post", side_effect=Exception()):
caplog.set_level(logging.DEBUG, logger="instana")
agent.log_message_to_host_agent("sample")
assert (
f"agent logging: connection error ({type(Exception())})"
in caplog.messages
)
def test_is_agent_ready(
self,
caplog: pytest.LogCaptureFixture,
) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.return_value = {"key": "value"}
agent.AGENT_DATA_PATH = "sample_path"
agent.announce_data = AnnounceData(pid=1234, agentUuid="sample")
with patch.object(requests.Session, "head", return_value=mock_response), patch(
"instana.agent.host.HostAgent._HostAgent__data_url",
return_value="localhost",
):
assert agent.is_agent_ready()
with patch.object(requests.Session, "head", side_effect=Exception()):
caplog.set_level(logging.DEBUG, logger="instana")
agent.is_agent_ready()
assert (
f"is_agent_ready: connection error ({type(Exception())})"
in caplog.messages
)
def test_report_data_payload(
self,
span_context: SpanContext,
span_processor: StanRecorder,
) -> None:
agent = HostAgent()
span_name = "test-span"
span_1 = InstanaSpan(span_name, span_context, span_processor)
span_2 = InstanaSpan(span_name, span_context, span_processor)
payload = {
"spans": [span_1, span_2],
"profiles": ["profile-1", "profile-2"],
"metrics": {
"plugins": [
{"data": "sample data"},
]
},
}
sample_response = {"key": "value"}
mock_response = Mock()
mock_response.status_code = 200
mock_response.content = sample_response
with patch.object(requests.Session, "post", return_value=mock_response), patch(
"instana.agent.host.HostAgent._HostAgent__traces_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__profiles_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__data_url",
return_value="localhost",
):
test_response = agent.report_data_payload(payload)
assert isinstance(agent.last_seen, datetime.datetime)
assert test_response.content == sample_response
def test_report_metrics(self) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.return_value = "Success"
payload = {
"metrics": {
"plugins": [
{"data": "sample data"},
]
},
}
with patch.object(requests.Session, "post", return_value=mock_response), patch(
"instana.agent.host.HostAgent._HostAgent__traces_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__profiles_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__data_url",
return_value="localhost",
):
test_response = agent.report_metrics(payload)
assert test_response.return_value == "Success"
def test_report_profiles(self) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.return_value = "Success"
payload = {
"profiles": ["profile-1", "profile-2"],
}
with patch.object(requests.Session, "post", return_value=mock_response), patch(
"instana.agent.host.HostAgent._HostAgent__traces_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__profiles_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__data_url",
return_value="localhost",
):
test_response = agent.report_profiles(payload)
assert test_response.return_value == "Success"
def test_report_spans(
self,
span_context: SpanContext,
span_processor: StanRecorder,
) -> None:
agent = HostAgent()
mock_response = Mock()
mock_response.status_code = 200
mock_response.return_value = "Success"
span_name = "test_span"
span_1 = InstanaSpan(span_name, span_context, span_processor)
span_2 = InstanaSpan(span_name, span_context, span_processor)
payload = {
"spans": [span_1, span_2],
}
with patch.object(requests.Session, "post", return_value=mock_response), patch(
"instana.agent.host.HostAgent._HostAgent__traces_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__profiles_url",
return_value="localhost",
), patch(
"instana.agent.host.HostAgent._HostAgent__data_url",
return_value="localhost",
):
test_response = agent.report_spans(payload)
assert test_response.return_value == "Success"
def test_diagnostics(self, caplog: pytest.LogCaptureFixture) -> None:
caplog.set_level(logging.WARNING, logger="instana")
agent = HostAgent()
agent.diagnostics()
assert (
"====> Instana Python Language Agent Diagnostics <====" in caplog.messages
)
assert "----> Agent <----" in caplog.messages
assert f"is_agent_ready: {agent.is_agent_ready()}" in caplog.messages
assert f"is_timed_out: {agent.is_timed_out()}" in caplog.messages
assert "last_seen: None" in caplog.messages
sample_date = datetime.datetime(2022, 7, 25, 14, 30, 0)
agent.last_seen = sample_date
agent.diagnostics()
assert "last_seen: 2022-07-25 14:30:00" in caplog.messages
assert "announce_data: None" in caplog.messages
agent.announce_data = AnnounceData(pid=1234, agentUuid="value")
agent.diagnostics()
assert f"announce_data: {agent.announce_data.__dict__}" in caplog.messages
assert f"Options: {agent.options.__dict__}" in caplog.messages
assert "----> StateMachine <----" in caplog.messages
assert f"State: {agent.machine.fsm.current}" in caplog.messages
assert "----> Collector <----" in caplog.messages
assert f"Collector: {agent.collector}" in caplog.messages
assert f"ready_to_start: {agent.collector.ready_to_start}" in caplog.messages
assert "reporting_thread: None" in caplog.messages
assert f"report_interval: {agent.collector.report_interval}" in caplog.messages
assert "should_send_snapshot_data: True" in caplog.messages
def test_is_service_or_endpoint_ignored(self) -> None:
self.agent.options.span_filters = {
"include": [],
"exclude": [
{
"name": "service1-all",
"suppression": True,
"attributes": [
{"key": "type", "values": ["service1"], "match_type": "strict"}
],
},
{
"name": "service2-method1",
"suppression": True,
"attributes": [
{"key": "type", "values": ["service2"], "match_type": "strict"},
{
"key": "endpoint",
"values": ["method1"],
"match_type": "strict",
},
],
},
],
}
# ignore all endpoints of service1
assert self.agent._HostAgent__is_endpoint_ignored({"type": "service1"})
assert self.agent._HostAgent__is_endpoint_ignored(
{"type": "service1", "endpoint": "method1"}
)
assert self.agent._HostAgent__is_endpoint_ignored(
{"type": "service1", "endpoint": "method2"}
)
# ignore only endpoint1 of service2
assert self.agent._HostAgent__is_endpoint_ignored(
{"type": "service2", "endpoint": "method1"}
)
assert not self.agent._HostAgent__is_endpoint_ignored(
{"type": "service2", "endpoint": "method2"}
)
# don't ignore other services
assert not self.agent._HostAgent__is_endpoint_ignored({"type": "service3"})
assert not self.agent._HostAgent__is_endpoint_ignored(
{"type": "service3", "endpoint": "method1"}
)
@pytest.mark.parametrize(
"input_data",
[
{
"agentUuid": "test-uuid",
},
{
"pid": 1234,
},
{
"extraHeaders": ["value-3"],
},
],
ids=["missing_pid", "missing_agent_uuid", "missing_both_required_keys"],
)
def test_set_from_missing_required_keys(
self, input_data: Dict[str, Any], caplog: pytest.LogCaptureFixture
) -> None:
"""Test set_from when required keys are missing in res_data."""
agent = HostAgent()
caplog.set_level(logging.DEBUG, logger="instana")
res_data = {
"secrets": {"matcher": "value-1", "list": ["value-2"]},
}
res_data.update(input_data)
agent.set_from(res_data)
assert agent.announce_data is None
assert "Missing required keys in announce response" in caplog.messages[-1]
def test_filter_spans_with_empty_service_name(self) -> None:
"""Test that filter_spans handles spans with empty service_name gracefully."""
# Create a mock span with no valid service name in data
mock_span = Mock()
mock_span.n = "test"
mock_span.k = 1
mock_span.data = {
"invalid_key": "value"
} # No dict value, so service_name stays empty
# Should not crash and should include the span
filtered = self.agent.filter_spans([mock_span])
assert len(filtered) == 1
assert filtered[0] == mock_span
def test_filter_spans_with_none_kind(self) -> None:
"""Test that filter_spans handles spans with None kind gracefully."""
# Create a mock span without 'k' attribute
mock_span = Mock()
mock_span.n = "http"
del mock_span.k # Remove k attribute
mock_span.data = {"http": {"method": "GET", "url": "http://example.com"}}
# Should not crash - getattr will return None for missing k
filtered = self.agent.filter_spans([mock_span])
assert len(filtered) == 1