From 8c4154e1682ff30ee940d53380f8a7e83dd32618 Mon Sep 17 00:00:00 2001 From: SegFaulti4 Date: Tue, 16 Jan 2024 21:18:48 +0300 Subject: [PATCH] WIP: Added tests for internal errors --- .github/workflows/cotea_testing_workflow.yaml | 1 + src/cotea_internal_error_case.py | 66 +++++++++++++++++++ src/cotea_run_files/incorrect.yaml | 6 ++ 3 files changed, 73 insertions(+) create mode 100644 src/cotea_internal_error_case.py create mode 100644 src/cotea_run_files/incorrect.yaml diff --git a/.github/workflows/cotea_testing_workflow.yaml b/.github/workflows/cotea_testing_workflow.yaml index e2ec3f6..4bea0de 100644 --- a/.github/workflows/cotea_testing_workflow.yaml +++ b/.github/workflows/cotea_testing_workflow.yaml @@ -31,3 +31,4 @@ jobs: cd ./src python cotea_ok_case.py python cotea_ansible_error_case.py + python cotea_internal_error_case.py diff --git a/src/cotea_internal_error_case.py b/src/cotea_internal_error_case.py new file mode 100644 index 0000000..de22c32 --- /dev/null +++ b/src/cotea_internal_error_case.py @@ -0,0 +1,66 @@ +import unittest + +from cotea.runner import runner +from cotea.arguments_maker import argument_maker + + +def run_ansible_error_case(pb_path, inv_path): + arg_maker = argument_maker() + arg_maker.add_arg("-i", inv_path) + r = runner(pb_path, arg_maker, show_progress_bar=True) + + while r.has_next_play(): + while r.has_next_task(): + r.run_next_task() + r.finish_ansible() + + if r.was_error(): + return True + return False + + +class TestCotea(unittest.TestCase): + + def test_incorrect_playbook_path_case(self): + 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: + self.assertTrue(e.message.startswith(f"the playbook: {pb_path} could not be found")) + 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): + 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: + # NOTE: e should be AnsibleParserError, but "isinstance" returns False for some reason + self.assertTrue(e.message.startswith("couldn't resolve module/action"), + msg="Unexpected exception") + 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() diff --git a/src/cotea_run_files/incorrect.yaml b/src/cotea_run_files/incorrect.yaml new file mode 100644 index 0000000..1beadab --- /dev/null +++ b/src/cotea_run_files/incorrect.yaml @@ -0,0 +1,6 @@ +--- +- name: Play1 + hosts: all + tasks: + - name: Syntactically incorrect command + battle_star_engine: