|
| 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() |
0 commit comments