Skip to content

Commit 9ecebcc

Browse files
authored
Clean up generated YML files from unit tests (#3237)
* remove ability files created from unit test * remove planner file after using fixture * fix comment * cleanup adversary files * style fix * add path exists checks to cleanup
1 parent 1a19420 commit 9ecebcc

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

tests/api/v2/handlers/test_abilities_api.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pytest
23

34
from http import HTTPStatus
@@ -11,23 +12,31 @@
1112
@pytest.fixture
1213
def new_ability_payload():
1314
test_executor_linux = Executor(name='sh', platform='linux', command='whoami')
14-
return {'name': 'new test ability',
15-
'ability_id': '456',
16-
'tactic': 'collection',
17-
'technique_name': 'collection',
18-
'technique_id': '1',
19-
'executors': [ExecutorSchema().dump(test_executor_linux)],
20-
'access': {},
21-
'additional_info': {},
22-
'buckets': ['collection'],
23-
'description': '',
24-
'privilege': '',
25-
'repeatable': False,
26-
'requirements': [],
27-
'singleton': False,
28-
'plugin': '',
29-
'delete_payload': True,
30-
}
15+
yield {
16+
'name': 'new test ability',
17+
'ability_id': '456',
18+
'tactic': 'collection',
19+
'technique_name': 'collection',
20+
'technique_id': '1',
21+
'executors': [ExecutorSchema().dump(test_executor_linux)],
22+
'access': {},
23+
'additional_info': {},
24+
'buckets': ['collection'],
25+
'description': '',
26+
'privilege': '',
27+
'repeatable': False,
28+
'requirements': [],
29+
'singleton': False,
30+
'plugin': '',
31+
'delete_payload': True,
32+
}
33+
34+
# Ability cleanup
35+
if os.path.exists('data/abilities/collection/456.yml'):
36+
try:
37+
os.remove('data/abilities/collection/456.yml')
38+
except OSError:
39+
pass
3140

3241

3342
@pytest.fixture
@@ -56,7 +65,14 @@ def test_ability(event_loop, api_v2_client, executor):
5665
technique_name='collection', technique_id='1', description='', privilege='', tactic='discovery',
5766
plugin='testplugin')
5867
event_loop.run_until_complete(BaseService.get_service('data_svc').store(ability))
59-
return ability
68+
yield ability
69+
70+
# cleanup
71+
if os.path.exists('data/abilities/collection/123.yml'):
72+
try:
73+
os.remove('data/abilities/collection/123.yml')
74+
except OSError:
75+
pass
6076

6177

6278
class TestAbilitiesApi:

tests/api/v2/handlers/test_adversaries_api.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pytest
23

34
from http import HTTPStatus
@@ -6,6 +7,15 @@
67
from app.utility.base_service import BaseService
78

89

10+
def adversary_file_cleanup(adversary_id):
11+
file_path = f'data/adversaries/{adversary_id}.yml'
12+
if os.path.exists(file_path):
13+
try:
14+
os.remove(file_path)
15+
except OSError:
16+
pass
17+
18+
919
@pytest.fixture
1020
def updated_adversary_payload():
1121
return {
@@ -33,7 +43,7 @@ def expected_updated_adversary_dump(test_adversary, updated_adversary_payload):
3343

3444
@pytest.fixture
3545
def new_adversary_payload():
36-
return {
46+
yield {
3747
'name': 'test new adversary',
3848
'description': 'a new adversary',
3949
'adversary_id': '456',
@@ -43,6 +53,8 @@ def new_adversary_payload():
4353
'plugin': ''
4454
}
4555

56+
adversary_file_cleanup('456')
57+
4658

4759
@pytest.fixture
4860
def expected_new_adversary_dump(new_adversary_payload):
@@ -52,7 +64,7 @@ def expected_new_adversary_dump(new_adversary_payload):
5264

5365
@pytest.fixture
5466
def new_adversary_duplicate_id_payload():
55-
return {
67+
yield {
5668
'name': 'test new adversary',
5769
'description': 'a new adversary with an invalid payload',
5870
'adversary_id': '456',
@@ -63,6 +75,8 @@ def new_adversary_duplicate_id_payload():
6375
'plugin': ''
6476
}
6577

78+
adversary_file_cleanup('456')
79+
6680

6781
@pytest.fixture
6882
def expected_new_duplicate_id_adversary_dump(new_adversary_duplicate_id_payload):
@@ -83,7 +97,9 @@ def test_adversary(event_loop):
8397
'plugin': ''}
8498
test_adversary = AdversarySchema().load(expected_adversary)
8599
event_loop.run_until_complete(BaseService.get_service('data_svc').store(test_adversary))
86-
return test_adversary
100+
yield test_adversary
101+
102+
adversary_file_cleanup(test_adversary.adversary_id)
87103

88104

89105
class TestAdversariesApi:

tests/api/v2/handlers/test_planners_api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pytest
23

34
from http import HTTPStatus
@@ -10,7 +11,14 @@
1011
def test_planner(event_loop, api_v2_client):
1112
planner = Planner(name="123test planner", planner_id="123", description="a test planner", plugin="planner")
1213
event_loop.run_until_complete(BaseService.get_service('data_svc').store(planner))
13-
return planner
14+
yield planner
15+
16+
# Planner cleanup
17+
if os.path.exists('data/planners/123.yml'):
18+
try:
19+
os.remove('data/planners/123.yml')
20+
except OSError:
21+
pass
1422

1523

1624
@pytest.fixture

0 commit comments

Comments
 (0)