Skip to content

Commit

Permalink
Added tests for internal errors, simplified inventory file for tests,…
Browse files Browse the repository at this point in the history
… minor changes in excepthook
  • Loading branch information
SegFaulti4 committed Jan 17, 2024
1 parent e51704d commit 92d5f1d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/cotea_testing_workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
cd ./src
python cotea_ok_case.py
python cotea_ansible_error_case.py
python cotea_internal_error_case.py
16 changes: 7 additions & 9 deletions src/cotea/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,12 @@ def _set_wrappers_back(self):
PlaybookExecutor.__init__ = self.playbook_executor_wrp.func

def _except_hook(self, args, /):
exc_type, exc_value, exc_traceback, thread = \
args.exc_type, args.exc_value, args.exc_traceback, args.thread

if (exc_type == SystemExit or
if (args.exc_type == SystemExit or
# NOTE: this probably should never happen
thread != self.ansible_thread):
args.thread != self.ansible_thread):
return self._old_except_hook(args)

self.sync_obj.exception = exc_value
self.sync_obj.exception = args.exc_value
self.sync_obj.continue_runner()

def _start_ansible(self):
Expand Down Expand Up @@ -318,10 +315,11 @@ def get_already_ignore_unrch(self):


def finish_ansible(self):
while self.sync_obj.curr_breakpoint_label != self.breakpoint_labeles["after_playbook"]:
self.sync_obj.continue_ansible_with_stop()
if self.sync_obj.exception is None:
while self.sync_obj.curr_breakpoint_label != self.breakpoint_labeles["after_playbook"]:
self.sync_obj.continue_ansible_with_stop()
self.sync_obj.continue_ansible()

self.sync_obj.continue_ansible()
self.ansible_thread.join(timeout=5)
self._set_wrappers_back()

Expand Down
66 changes: 66 additions & 0 deletions src/cotea_internal_error_case.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest


class TestCotea(unittest.TestCase):

def tearDown(self) -> None:
from cotea.utils import remove_modules_from_imported

# Remove any Ansible-related objects from memory
# to clear previous execution context
remove_modules_from_imported(module_name_like="cotea")

def test_incorrect_playbook_path_case(self):
from cotea.runner import runner
from cotea.arguments_maker import argument_maker

pb_path = "cotea_run_files/#%|&"
inv_path = "cotea_run_files/inv"

arg_maker = argument_maker()
arg_maker.add_arg("-i", inv_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

try:
while r.has_next_play():
while r.has_next_task():
r.run_next_task()
r.finish_ansible()
except Exception as e:
r.finish_ansible()
self.assertTrue(hasattr(e, "message"), msg="Exception is expected to have 'message' attribute")
self.assertTrue(e.message.startswith(f"the playbook: {pb_path} could not be found"),
msg="Unexpected exception message")
else:
self.assertFalse(True, msg="Ansible is supposed to fail due to syntax error "
"and its' exception should be passed to main thread")

def test_incorrect_syntax_case(self):
from cotea.runner import runner
from cotea.arguments_maker import argument_maker

pb_path = "cotea_run_files/incorrect.yaml"
inv_path = "cotea_run_files/inv"

arg_maker = argument_maker()
arg_maker.add_arg("-i", inv_path)
r = runner(pb_path, arg_maker, show_progress_bar=True)

try:
while r.has_next_play():
while r.has_next_task():
r.run_next_task()
r.finish_ansible()
except Exception as e:
r.finish_ansible()
# NOTE: e should be AnsibleParserError, but "isinstance" returns False for some reason
self.assertTrue(hasattr(e, "message"), msg="Exception is expected to have 'message' attribute")
self.assertTrue(e.message.startswith("couldn't resolve module/action"),
msg="Unexpected exception message")
else:
self.assertFalse(True, msg="Ansible is supposed to fail due to syntax error "
"and its' exception should be passed to main thread")


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions src/cotea_run_files/incorrect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: Play1
hosts: all
tasks:
- name: Syntactically incorrect command
battle_star_engine:
5 changes: 1 addition & 4 deletions src/cotea_run_files/inv
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[host1]
host1 ansible_host=localhost ansible_connection=ssh ansible_ssh_port=2222 ansible_user=root ansible_password=amella

[targets]
host1
localhost ansible_connection=ssh ansible_ssh_port=2222 ansible_user=root ansible_password=amella ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'

0 comments on commit 92d5f1d

Please sign in to comment.