-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_agents.py
More file actions
647 lines (558 loc) · 21 KB
/
test_agents.py
File metadata and controls
647 lines (558 loc) · 21 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
import json
from datetime import datetime
from urllib.parse import urlencode
import pytest
import requests
from pytest_httpserver import HTTPServer
from _incydr_cli.main import incydr
from incydr import Client
from incydr.enums import SortDirection
from incydr.enums.agents import SortKeys
from incydr.models import Agent
from incydr.models import AgentsPage
TEST_AGENT_ID = "agent-1"
TEST_AGENT_1 = {
"agentId": TEST_AGENT_ID,
"name": "DESKTOP-H6V9R95",
"userId": "user-1",
"osHostname": "DESKTOP-H6V9R95",
"osName": "Win",
"machineId": "123",
"serialNumber": "A12B34",
"active": True,
"agentType": "COMBINED",
"agentHealthIssueTypes": ["NOT_CONNECTING"],
"agentHealthModificationDate": "2022-07-14T17:03:22.123000Z",
"appVersion": "1.0",
"productVersion": "2.0",
"lastConnected": "2022-07-14T17:05:44.524000Z",
"externalReference": None,
"creationDate": "2022-07-14T16:49:11.166000Z",
"modificationDate": "2022-07-14T17:05:44.524000Z",
}
TEST_AGENT_2 = {
"agentId": TEST_AGENT_ID,
"name": "DESKTOP-H6V9R95",
"userId": "user-1",
"osHostname": "DESKTOP-H6V9R95",
"osName": "Win",
"machineId": "456",
"serialNumber": "A45B67",
"active": True,
"agentType": "COMBINED",
"agentHealthIssueTypes": [],
"agentHealthModificationDate": "2022-07-14T17:02:15.456000Z",
"appVersion": "1.0",
"productVersion": "2.0",
"lastConnected": "2022-07-14T17:05:44.524000Z",
"externalReference": None,
"creationDate": "2022-07-14T16:49:11.166000Z",
"modificationDate": "2022-07-14T17:05:44.524000Z",
}
TEST_AGENT_3 = {
"agentId": TEST_AGENT_ID,
"name": "DESKTOP-H6V9R95",
"userId": "user-1",
"osHostname": "DESKTOP-H6V9R95",
"osName": "Win",
"machineId": "1",
"serialNumber": "C42",
"active": True,
"agentType": "COMBINED",
"agentHealthIssueTypes": [],
"agentHealthModificationDate": "2022-07-14T17:01:30.789000Z",
"appVersion": "1.0",
"productVersion": "2.0",
"lastConnected": "2022-07-14T17:05:44.524000Z",
"externalReference": None,
"creationDate": "2022-07-14T16:49:11.166000Z",
"modificationDate": "2022-07-14T17:05:44.524000Z",
}
@pytest.fixture
def mock_get_all_agents_default(httpserver_auth: HTTPServer):
query = {
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 500,
"page": 1,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
"/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
@pytest.fixture
def mock_get_agent(httpserver_auth: HTTPServer):
httpserver_auth.expect_request(
uri=f"/v1/agents/{TEST_AGENT_ID}", method="GET"
).respond_with_json(TEST_AGENT_1)
def test_get_agent_returns_expected_data(mock_get_agent):
client = Client()
agent = client.agents.v1.get_agent("agent-1")
assert isinstance(agent, Agent)
assert agent.agent_id == "agent-1"
assert agent.json() == json.dumps(TEST_AGENT_1, separators=(",", ":"))
# test timestamp conversion
assert agent.last_connected == datetime.fromisoformat(
TEST_AGENT_1["lastConnected"].replace("Z", "+00:00")
)
assert agent.creation_date == datetime.fromisoformat(
TEST_AGENT_1["creationDate"].replace("Z", "+00:00")
)
assert agent.modification_date == datetime.fromisoformat(
TEST_AGENT_1["modificationDate"].replace("Z", "+00:00")
)
assert agent.agent_health_modification_date == datetime.fromisoformat(
TEST_AGENT_1["agentHealthModificationDate"].replace("Z", "+00:00")
)
def test_get_page_when_default_query_params_returns_expected_data(
mock_get_all_agents_default,
):
client = Client()
page = client.agents.v1.get_page()
assert isinstance(page, AgentsPage)
assert page.agents[0].json() == json.dumps(TEST_AGENT_1, separators=(",", ":"))
assert page.agents[1].json() == json.dumps(TEST_AGENT_2, separators=(",", ":"))
assert page.total_count == len(page.agents) == 2
def test_get_page_when_custom_query_params_returns_expected_data(
httpserver_auth: HTTPServer,
):
query = {
"active": True,
"srtKey": "LAST_CONNECTED",
"srtDir": "DESC",
"pageSize": 10,
"page": 2,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 10,
"page": 2,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
client = Client()
page = client.agents.v1.get_page(
active=True,
page_num=2,
page_size=10,
sort_dir=SortDirection.DESC,
sort_key=SortKeys.LAST_CONNECTED,
)
assert isinstance(page, AgentsPage)
assert page.agents[0].json() == json.dumps(TEST_AGENT_1, separators=(",", ":"))
assert page.agents[1].json() == json.dumps(TEST_AGENT_2, separators=(",", ":"))
assert page.total_count == len(page.agents) == 2
def test_get_page_when_agent_health_modified_in_last_days_passed_makes_expected_call(
httpserver_auth: HTTPServer,
):
query = {
"agentHealthModifiedInLastDays": 7,
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 500,
"page": 1,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
client = Client()
page = client.agents.v1.get_page(agent_health_modified_in_last_days=7)
assert isinstance(page, AgentsPage)
assert page.agents[0].json() == json.dumps(TEST_AGENT_1, separators=(",", ":"))
assert page.agents[1].json() == json.dumps(TEST_AGENT_2, separators=(",", ":"))
assert page.total_count == len(page.agents) == 2
def test_iter_all_when_default_params_returns_expected_data(
httpserver_auth: HTTPServer,
):
query_1 = {
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 2,
"page": 1,
}
query_2 = {
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 2,
"page": 2,
}
agents_data_1 = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 3,
"pageSize": 2,
"page": 1,
}
agents_data_2 = {
"agents": [TEST_AGENT_3],
"totalCount": 3,
"pageSize": 2,
"page": 2,
}
httpserver_auth.expect_ordered_request(
"/v1/agents", method="GET", query_string=urlencode(query_1)
).respond_with_json(agents_data_1)
httpserver_auth.expect_ordered_request(
"/v1/agents", method="GET", query_string=urlencode(query_2)
).respond_with_json(agents_data_2)
client = Client()
iterator = client.agents.v1.iter_all(page_size=2)
total_agents = 0
expected_agents = [TEST_AGENT_1, TEST_AGENT_2, TEST_AGENT_3]
for item in iterator:
total_agents += 1
assert isinstance(item, Agent)
assert item.json() == json.dumps(expected_agents.pop(0), separators=(",", ":"))
assert total_agents == 3
def test_update_makes_expected_request(httpserver_auth: HTTPServer):
agent_id = "1234"
new_name = "new"
new_reference = "new_ref"
httpserver_auth.expect_request(
f"/v1/agents/{agent_id}",
method="PUT",
json={"name": new_name, "externalReference": new_reference},
).respond_with_data(status=204)
client = Client()
response = client.agents.v1.update(
agent_id, name=new_name, external_reference=new_reference
)
assert isinstance(response, requests.Response)
def test_activate_makes_expected_request(httpserver_auth: HTTPServer):
agent_ids = ["1234", "5678"]
httpserver_auth.expect_request(
"/v1/agents/activate",
method="POST",
json={"agentIds": agent_ids},
).respond_with_data(status=204)
client = Client()
response = client.agents.v1.activate(agent_ids)
assert isinstance(response, requests.Response)
def test_deactivate_makes_expected_request(httpserver_auth: HTTPServer):
agent_ids = ["1234", "5678"]
httpserver_auth.expect_request(
"/v1/agents/deactivate",
method="POST",
json={"agentIds": agent_ids},
).respond_with_data(status=204)
client = Client()
response = client.agents.v1.deactivate(agent_ids)
assert isinstance(response, requests.Response)
# ************************************************ CLI ************************************************
def test_cli_list_when_default_params_makes_expected_call(
httpserver_auth: HTTPServer, runner, mock_get_all_agents_default
):
result = runner.invoke(incydr, ["agents", "list"])
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_list_when_custom_params_makes_expected_call(
httpserver_auth: HTTPServer, runner
):
query = {
"active": True,
"agentHealthy": True,
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 500,
"page": 1,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
result = runner.invoke(incydr, ["agents", "list", "--active", "--healthy"])
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_list_when_unhealthy_option_passed_with_default_value_passes_no_issue_types(
httpserver_auth: HTTPServer, runner
):
query = {
"agentHealthy": False,
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 500,
"page": 1,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
result = runner.invoke(incydr, ["agents", "list", "--unhealthy"])
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_list_when_unhealthy_option_passed_with_string_parses_issue_types_correctly(
httpserver_auth: HTTPServer, runner
):
query = [
("agentHealthy", False),
("anyOfAgentHealthIssueTypes", "NOT_CONNECTED"),
("anyOfAgentHealthIssueTypes", "TEST_VALUE"),
("srtKey", "NAME"),
("srtDir", "ASC"),
("pageSize", 500),
("page", 1),
]
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
result = runner.invoke(
incydr, ["agents", "list", "--unhealthy", "NOT_CONNECTED,TEST_VALUE"]
)
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_list_when_health_modified_days_option_passed_makes_expected_call(
httpserver_auth: HTTPServer, runner
):
query = {
"agentHealthModifiedInLastDays": 7,
"srtKey": "NAME",
"srtDir": "ASC",
"pageSize": 500,
"page": 1,
}
agents_data = {
"agents": [TEST_AGENT_1, TEST_AGENT_2],
"totalCount": 2,
"pageSize": 500,
"page": 1,
}
httpserver_auth.expect_request(
uri="/v1/agents", method="GET", query_string=urlencode(query)
).respond_with_json(agents_data)
result = runner.invoke(
incydr, ["agents", "list", "--agent-health-modified-within-days", "7"]
)
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_show_when_custom_params_makes_expected_call(
httpserver_auth: HTTPServer, runner, mock_get_agent
):
result = runner.invoke(incydr, ["agents", "show", TEST_AGENT_ID])
httpserver_auth.check()
assert result.exit_code == 0
def test_cli_bulk_activate_CSV_file_input(httpserver_auth: HTTPServer, runner):
csv_lines = (
"agent_id,some_extra_column\n",
"1234,not_relevant\n",
"2345,extra_data\n",
)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
tmpfile.writelines(csv_lines)
result = runner.invoke(incydr, ["agents", "bulk-activate", "tmpfile"])
httpserver_auth.check()
assert "Activating agents..." in result.output
def test_cli_bulk_activate_JSON_file_input(httpserver_auth: HTTPServer, runner):
json_lines = (
{"agent_id": "1234", "extra": "not_relevant"},
{"agentId": "2345", "extra": "ignore"},
)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in json_lines:
json.dump(line, tmpfile)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-activate", "--format", "json-lines", "tmpfile"]
)
httpserver_auth.check()
assert "Activating agents..." in result.output
def test_cli_bulk_deactivate_CSV_file_input(httpserver_auth: HTTPServer, runner):
csv_lines = (
"agent_id,some_extra_column\n",
"1234,not_relevant\n",
"2345,extra_data\n",
)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
tmpfile.writelines(csv_lines)
result = runner.invoke(incydr, ["agents", "bulk-deactivate", "tmpfile"])
httpserver_auth.check()
assert "Deactivating agents..." in result.output
def test_cli_bulk_deactivate_JSON_file_input(httpserver_auth: HTTPServer, runner):
json_lines = (
{"agent_id": "1234", "extra": "not_relevant"},
{"agentId": "2345", "extra": "ignore"},
)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in json_lines:
json.dump(line, tmpfile)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-deactivate", "--format", "json-lines", "tmpfile"]
)
httpserver_auth.check()
assert "Deactivating agents..." in result.output
def test_cli_bulk_activate_retries_with_agent_ids_not_found_removed(
httpserver_auth: HTTPServer, runner
):
input_lines = ("agent_id", "1234", "5678", "2345", "9876")
httpserver_auth.expect_request(
uri="/v1/agents/activate",
method="POST",
json={"agentIds": ["1234", "5678", "2345", "9876"]},
).respond_with_json(response_json={"agentsNotFound": ["5678", "9876"]}, status=404)
# order of agentIds in CLI requests aren't deterministic for retries because of usage of sets
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["2345", "1234"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in input_lines:
tmpfile.write(line)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-activate", "--format", "csv", "tmpfile"]
)
assert (
"404 Error processing batch of 4 agent activations, agent_ids not found: ['5678', '9876']"
in result.output
)
assert "Activating agents..." in result.output
def test_cli_bulk_activate_retries_ids_individually_when_unknown_error_occurs(
httpserver_auth: HTTPServer, runner
):
input_lines = ("agent_id", "1234", "5678", "2345", "9876")
httpserver_auth.expect_request(
uri="/v1/agents/activate",
method="POST",
json={"agentIds": ["1234", "5678", "2345", "9876"]},
).respond_with_data(response_data="Unknown Server Error", status=500)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["1234"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["2345"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["5678"]}
).respond_with_data(response_data="Unknown Server Error", status=500)
httpserver_auth.expect_request(
uri="/v1/agents/activate", method="POST", json={"agentIds": ["9876"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in input_lines:
tmpfile.write(line)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-activate", "--format", "csv", "tmpfile"]
)
assert "Unknown error processing batch of 4 agent activations" in result.output
assert "Trying agent activation for this batch individually" in result.output
assert "Activating agents..." in result.output
assert (
"Failed to process activation for 5678: Unknown Server Error"
in result.output
)
def test_cli_bulk_deactivate_retries_with_agent_ids_not_found_removed(
httpserver_auth: HTTPServer, runner
):
input_lines = ("agent_id", "1234", "5678", "2345", "9876")
httpserver_auth.expect_request(
uri="/v1/agents/deactivate",
method="POST",
json={"agentIds": ["1234", "5678", "2345", "9876"]},
).respond_with_json(response_json={"agentsNotFound": ["5678", "9876"]}, status=404)
# order of agentIds in CLI requests aren't deterministic for retries because of usage of sets
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["1234", "2345"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["2345", "1234"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in input_lines:
tmpfile.write(line)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-deactivate", "--format", "csv", "tmpfile"]
)
assert (
"404 Error processing batch of 4 agent deactivations, agent_ids not found: ['5678', '9876']"
in result.output
)
assert "Deactivating agents..." in result.output
def test_cli_bulk_deactivate_retries_ids_individually_when_unknown_error_occurs(
httpserver_auth: HTTPServer, runner
):
input_lines = ("agent_id", "1234", "5678", "2345", "9876")
httpserver_auth.expect_request(
uri="/v1/agents/deactivate",
method="POST",
json={"agentIds": ["1234", "5678", "2345", "9876"]},
).respond_with_data(response_data="Unknown Server Error", status=500)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["1234"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["2345"]}
).respond_with_data(status=204)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["5678"]}
).respond_with_data(response_data="Unknown Server Error", status=500)
httpserver_auth.expect_request(
uri="/v1/agents/deactivate", method="POST", json={"agentIds": ["9876"]}
).respond_with_data(status=204)
with runner.isolated_filesystem():
with open("tmpfile", "w") as tmpfile:
for line in input_lines:
tmpfile.write(line)
tmpfile.write("\n")
result = runner.invoke(
incydr, ["agents", "bulk-deactivate", "--format", "csv", "tmpfile"]
)
assert (
"Unknown error processing batch of 4 agent deactivations" in result.output
)
assert "Trying agent deactivation for this batch individually" in result.output
assert "Deactivating agents..." in result.output
assert (
"Failed to process deactivation for 5678: Unknown Server Error"
in result.output
)