Skip to content

Commit 6e56bba

Browse files
committed
WIP: Added tests for internal errors
1 parent 4adaa11 commit 6e56bba

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

.github/workflows/cotea_testing_workflow.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ jobs:
3131
cd ./src
3232
python cotea_ok_case.py
3333
python cotea_ansible_error_case.py
34+
python cotea_internal_error_case.py

src/cotea/runner.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,14 @@ def _set_wrappers_back(self):
123123
PlaybookExecutor.__init__ = self.playbook_executor_wrp.func
124124

125125
def _except_hook(self, args, /):
126-
exc_type, exc_value, exc_traceback, thread = \
127-
args.exc_type, args.exc_value, args.exc_traceback, args.thread
128-
129-
if (exc_type == SystemExit or
126+
if (args.exc_type == SystemExit or
130127
# NOTE: this probably should never happen
131-
thread != self.ansible_thread):
128+
args.thread != self.ansible_thread):
132129
return self._old_except_hook(args)
133130

134-
self.sync_obj.exception = exc_value
131+
self.sync_obj.exception = args.exc_value
135132
self.sync_obj.continue_runner()
133+
return
136134

137135
def _start_ansible(self):
138136
args = self.arg_maker.args

src/cotea_internal_error_case.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import unittest
2+
3+
from cotea.runner import runner
4+
from cotea.arguments_maker import argument_maker
5+
6+
7+
def run_ansible_error_case(pb_path, inv_path):
8+
arg_maker = argument_maker()
9+
arg_maker.add_arg("-i", inv_path)
10+
r = runner(pb_path, arg_maker, show_progress_bar=True)
11+
12+
while r.has_next_play():
13+
while r.has_next_task():
14+
r.run_next_task()
15+
r.finish_ansible()
16+
17+
if r.was_error():
18+
return True
19+
return False
20+
21+
22+
class TestCotea(unittest.TestCase):
23+
24+
def test_incorrect_playbook_path_case(self):
25+
pb_path = "cotea_run_files/#%|&"
26+
inv_path = "cotea_run_files/inv"
27+
28+
arg_maker = argument_maker()
29+
arg_maker.add_arg("-i", inv_path)
30+
r = runner(pb_path, arg_maker, show_progress_bar=True)
31+
32+
try:
33+
while r.has_next_play():
34+
while r.has_next_task():
35+
r.run_next_task()
36+
r.finish_ansible()
37+
except Exception as e:
38+
self.assertTrue(hasattr(e, "message"), msg="Exception is expected to have 'message' attribute")
39+
self.assertTrue(e.message.startswith(f"the playbook: {pb_path} could not be found"),
40+
msg="Unexpected exception message")
41+
else:
42+
self.assertFalse(True, msg="Ansible is supposed to fail due to syntax error "
43+
"and its' exception should be passed to main thread")
44+
45+
def test_incorrect_syntax_case(self):
46+
pb_path = "cotea_run_files/incorrect.yaml"
47+
inv_path = "cotea_run_files/inv"
48+
49+
arg_maker = argument_maker()
50+
arg_maker.add_arg("-i", inv_path)
51+
r = runner(pb_path, arg_maker, show_progress_bar=True)
52+
53+
try:
54+
while r.has_next_play():
55+
while r.has_next_task():
56+
r.run_next_task()
57+
r.finish_ansible()
58+
except Exception as e:
59+
# NOTE: e should be AnsibleParserError, but "isinstance" returns False for some reason
60+
self.assertTrue(hasattr(e, "message"), msg="Exception is expected to have 'message' attribute")
61+
self.assertTrue(e.message.startswith("couldn't resolve module/action"),
62+
msg="Unexpected exception message")
63+
else:
64+
self.assertFalse(True, msg="Ansible is supposed to fail due to syntax error "
65+
"and its' exception should be passed to main thread")
66+
67+
68+
if __name__ == '__main__':
69+
unittest.main()

src/cotea_run_files/incorrect.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Play1
3+
hosts: all
4+
tasks:
5+
- name: Syntactically incorrect command
6+
battle_star_engine:

0 commit comments

Comments
 (0)