diff --git a/contributing/contr_pb.yaml b/contributing/contr_pb.yaml index 602fd5f..837bc70 100644 --- a/contributing/contr_pb.yaml +++ b/contributing/contr_pb.yaml @@ -6,7 +6,7 @@ ping: # - name: Bad task (will fail) -# shell: "mv biba boba" +# shell: "mv biba boba" - name: Creates very important directory file: diff --git a/src/cotea_run_files/inv b/src/cotea_run_files/inv deleted file mode 100644 index d0162e9..0000000 --- a/src/cotea_run_files/inv +++ /dev/null @@ -1,2 +0,0 @@ -[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' diff --git a/src/cotea_test_utils.py b/src/cotea_test_utils.py deleted file mode 100644 index f8c7f1b..0000000 --- a/src/cotea_test_utils.py +++ /dev/null @@ -1,2 +0,0 @@ -def generate_error_msg(method_name, should_be, returned): - return "{} worked wrong\nshould be {}, returned {}".format(method_name, should_be, returned) \ No newline at end of file diff --git a/src/test/__init__.py b/src/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/test/ansible/docker.ini b/src/test/ansible/docker.ini new file mode 100644 index 0000000..3663719 --- /dev/null +++ b/src/test/ansible/docker.ini @@ -0,0 +1,9 @@ +[cotea-docker-host] +localhost + +[cotea-docker-host:vars] +ansible_connection=ssh +ansible_ssh_port=2222 +ansible_user=root +ansible_password=amella +ansible_ssh_common_args='-o StrictHostKeyChecking=no' diff --git a/src/cotea_run_files/error.yaml b/src/test/ansible/error.yaml similarity index 100% rename from src/cotea_run_files/error.yaml rename to src/test/ansible/error.yaml diff --git a/src/cotea_run_files/incorrect.yaml b/src/test/ansible/incorrect.yaml similarity index 100% rename from src/cotea_run_files/incorrect.yaml rename to src/test/ansible/incorrect.yaml diff --git a/src/cotea_run_files/ok.yaml b/src/test/ansible/ok.yaml similarity index 100% rename from src/cotea_run_files/ok.yaml rename to src/test/ansible/ok.yaml diff --git a/src/cotea_ansible_error_case.py b/src/test/test_ansible_error.py similarity index 90% rename from src/cotea_ansible_error_case.py rename to src/test/test_ansible_error.py index 83a8d1d..c0f6dc6 100644 --- a/src/cotea_ansible_error_case.py +++ b/src/test/test_ansible_error.py @@ -1,12 +1,12 @@ +import os import unittest from cotea.runner import runner from cotea.arguments_maker import argument_maker +from test.utils import ANSIBLE_DIR, INVENTORY_PATH def run_ansible_error_case(pb_path, inv_path): - msg = "" - arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) @@ -20,13 +20,11 @@ def run_ansible_error_case(pb_path, inv_path): if r.was_error(): return True - + return False def run_ansible_error_case_with_ignore(pb_path, inv_path): - msg = "" - arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) @@ -42,27 +40,26 @@ def run_ansible_error_case_with_ignore(pb_path, inv_path): if r.was_error(): return True - + return False class TestCotea(unittest.TestCase): def test_ansible_task_error_case(self): - pb_path = "cotea_run_files/error.yaml" - inv_path = "cotea_run_files/inv" + pb_path = os.path.join(ANSIBLE_DIR, "error.yaml") + inv_path = INVENTORY_PATH was_exception = False exception_msg = None was_error_in_tests = False - test_fail_msg = None try: was_error_in_tests = run_ansible_error_case(pb_path, inv_path) except Exception as e: was_exception = True exception_msg = str(e) - + self.assertFalse(was_exception, msg=exception_msg) msg = "Ansible should be failed, there was an error in playbook, but cotea didn't catch that" @@ -73,13 +70,12 @@ def test_ansible_task_error_case(self): except Exception as e: was_exception = True exception_msg = str(e) - + self.assertFalse(was_exception, msg=exception_msg) msg = "runner.ignore_errors_of_next_task works wrong. The Ansible task failure wasn't ignored" self.assertFalse(was_error_in_tests, msg=msg) - if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/src/cotea_internal_error_case.py b/src/test/test_internal_error.py similarity index 90% rename from src/cotea_internal_error_case.py rename to src/test/test_internal_error.py index e495dc4..461edcd 100644 --- a/src/cotea_internal_error_case.py +++ b/src/test/test_internal_error.py @@ -1,5 +1,8 @@ +import os import unittest +from test.utils import ANSIBLE_DIR, INVENTORY_PATH + class TestCotea(unittest.TestCase): @@ -14,8 +17,8 @@ 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" + pb_path = os.path.join(ANSIBLE_DIR, "#%|&") + inv_path = INVENTORY_PATH arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) @@ -39,8 +42,8 @@ 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" + pb_path = os.path.join(ANSIBLE_DIR, "incorrect.yaml") + inv_path = INVENTORY_PATH arg_maker = argument_maker() arg_maker.add_arg("-i", inv_path) diff --git a/src/cotea_ok_case.py b/src/test/test_ok.py similarity index 87% rename from src/cotea_ok_case.py rename to src/test/test_ok.py index 5f3fa6a..2c99f5b 100644 --- a/src/cotea_ok_case.py +++ b/src/test/test_ok.py @@ -1,23 +1,24 @@ +import os import unittest from cotea.runner import runner from cotea.arguments_maker import argument_maker -from cotea_test_utils import generate_error_msg +from test.utils import generate_error_msg, ANSIBLE_DIR, INVENTORY_PATH # almost every cotea's function(method) is strongly connected # with Ansible launch and at the moment we didn't -# implement normal testing scheme (including unit testing and etc) +# implement normal testing scheme (including unit testing etc.) def run_cotea_ok_case(pb_path, inv_path): msg = "" play_names = ["Play1", "Play2"] plays_tasks = [] - + play1_tasks = ["Gathering Facts", "meta", "Pinge Ponge", "Creates directory", - "Creating an empty file", "Delete file", - "Delete content & directory", "meta", "meta", "None"] + "Creating an empty file", "Delete file", + "Delete content & directory", "meta", "meta", "None"] plays_tasks.append(play1_tasks) - + play2_tasks = ["Gathering Facts", "meta", "Pinge Ponge", "meta", "meta", "None"] plays_tasks.append(play2_tasks) @@ -26,7 +27,6 @@ def run_cotea_ok_case(pb_path, inv_path): r = runner(pb_path, arg_maker, show_progress_bar=True) plays_ind = 0 - tasks_ind = 0 while r.has_next_play(): tasks_ind = 0 @@ -45,7 +45,7 @@ def run_cotea_ok_case(pb_path, inv_path): msg = generate_error_msg("runner.get_next_task_name", next_task_name_should_be, next_task_name) r.finish_ansible() return True, msg - + if tasks_ind > 0: prev_task_name = r.get_prev_task_name() prev_task_name_should_be = plays_tasks[plays_ind][tasks_ind - 1] @@ -64,7 +64,7 @@ def run_cotea_ok_case(pb_path, inv_path): return True, msg tasks_ind += 1 - + plays_ind += 1 r.finish_ansible() @@ -73,14 +73,14 @@ def run_cotea_ok_case(pb_path, inv_path): ansible_err_msg = r.get_error_msg() msg += "\nAnsible task error:\n{}".format(ansible_err_msg) return True, msg - + return False, msg class TestCotea(unittest.TestCase): def test_cotea_ok_case(self): - pb_path = "cotea_run_files/ok.yaml" - inv_path = "cotea_run_files/inv" + pb_path = os.path.join(ANSIBLE_DIR, "ok.yaml") + inv_path = INVENTORY_PATH was_exception = False exception_msg = None @@ -93,10 +93,10 @@ def test_cotea_ok_case(self): except Exception as e: was_exception = True exception_msg = str(e) - + self.assertFalse(was_exception, msg=exception_msg) self.assertFalse(was_error_in_tests, msg=test_fail_msg) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/src/test/utils.py b/src/test/utils.py new file mode 100644 index 0000000..9eba6e1 --- /dev/null +++ b/src/test/utils.py @@ -0,0 +1,9 @@ +import os + +TEST_DIR = os.path.dirname(__file__) +ANSIBLE_DIR = os.path.join(TEST_DIR, "ansible") +INVENTORY_PATH = os.path.join(ANSIBLE_DIR, "docker.ini") + + +def generate_error_msg(method_name, should_be, returned): + return "{} worked wrong\nshould be {}, returned {}".format(method_name, should_be, returned) \ No newline at end of file