Skip to content

Commit

Permalink
Moved test files to separate module, rewrote inventory file, added fi…
Browse files Browse the repository at this point in the history
…le paths to utils
  • Loading branch information
SegFaulti4 committed Feb 7, 2024
1 parent e030d3e commit a080edf
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 36 deletions.
2 changes: 1 addition & 1 deletion contributing/contr_pb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ping:

# - name: Bad task (will fail)
# shell: "mv biba boba"
# shell: "mv biba boba"

- name: Creates very important directory
file:
Expand Down
2 changes: 0 additions & 2 deletions src/cotea_run_files/inv

This file was deleted.

2 changes: 0 additions & 2 deletions src/cotea_test_utils.py

This file was deleted.

Empty file added src/test/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions src/test/ansible/docker.ini
Original file line number Diff line number Diff line change
@@ -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'
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 9 additions & 13 deletions src/cotea_ansible_error_case.py → src/test/test_ansible_error.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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)

Expand All @@ -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"
Expand All @@ -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()
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import unittest

from test.utils import ANSIBLE_DIR, INVENTORY_PATH


class TestCotea(unittest.TestCase):

Expand All @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions src/cotea_ok_case.py → src/test/test_ok.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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()
unittest.main()
9 changes: 9 additions & 0 deletions src/test/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a080edf

Please sign in to comment.