Skip to content

Commit

Permalink
exit error if Mokeyble callback has not started in the test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed Nov 29, 2022
1 parent 6b9cca6 commit a44d9fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions monkeyble/cli/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
TEST_FAILED = "FAILED"
MONKEYBLE_DEFAULT_CONFIG_PATH = "monkeyble.yml"
MONKEYBLE_DEFAULT_ANSIBLE_CMD = "ansible-playbook"
MONKEYBLE_CALLBACK_STARTED = "Starting Monkeyble callback"
10 changes: 8 additions & 2 deletions monkeyble/cli/monkeyble_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from tabulate import tabulate

from monkeyble.cli.const import MONKEYBLE_DEFAULT_CONFIG_PATH, TEST_PASSED, TEST_FAILED, MONKEYBLE, \
MONKEYBLE_DEFAULT_ANSIBLE_CMD
MONKEYBLE_DEFAULT_ANSIBLE_CMD, MONKEYBLE_CALLBACK_STARTED
from monkeyble.cli.exceptions import MonkeybleCLIException
from monkeyble.cli.models import MonkeybleResult, ScenarioResult
from monkeyble.cli.utils import Utils
Expand Down Expand Up @@ -40,9 +40,15 @@ def run_ansible(ansible_cmd, playbook, inventory, extra_vars, scenario):
pipes = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
monkeyble_callback_started_successfully = False
for line in iter(pipes.stdout.readline, b''):
print(f"{line.rstrip().decode('utf-8')}")
decoded_line = line.rstrip().decode('utf-8')
if MONKEYBLE_CALLBACK_STARTED in str(decoded_line):
monkeyble_callback_started_successfully = True
print(f"{decoded_line}")
pipes.wait()
if not monkeyble_callback_started_successfully:
raise MonkeybleCLIException(message="Seems that Monkeyble callback has not started. Check your Ansible config.")
if pipes.returncode == 0:
return TEST_PASSED
else:
Expand Down
18 changes: 16 additions & 2 deletions tests/units/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from unittest import mock
from unittest.mock import patch, mock_open, call

from monkeyble.cli.const import TEST_PASSED, TEST_FAILED, MONKEYBLE_DEFAULT_ANSIBLE_CMD
from monkeyble.cli.const import TEST_PASSED, TEST_FAILED, MONKEYBLE_DEFAULT_ANSIBLE_CMD, MONKEYBLE_CALLBACK_STARTED
from monkeyble.cli.exceptions import MonkeybleCLIException

from monkeyble.cli.models import MonkeybleResult, ScenarioResult
Expand Down Expand Up @@ -121,7 +121,7 @@ def test_run_monkeyble_test_run_ansible_called(self):

@patch("subprocess.Popen")
def test_run_ansible(self, mock_subproc_popen):
mock_subproc_popen.return_value.stdout = io.BytesIO(b"playbook output")
mock_subproc_popen.return_value.stdout = io.BytesIO(MONKEYBLE_CALLBACK_STARTED.encode("utf-8"))

run_ansible(MONKEYBLE_DEFAULT_ANSIBLE_CMD,
"playbook.yml",
Expand All @@ -134,3 +134,17 @@ def test_run_ansible(self, mock_subproc_popen):
'-e', '@extra_vars1.yml', '-e', '@extra_vars2.yml',
'-e', 'monkeyble_scenario=scenario1'], stdout=-1, stderr=-2)
mock_subproc_popen.assert_has_calls([expected_call])

@patch('sys.exit')
@patch("subprocess.Popen")
def test_run_ansible_callback_not_started(self, mock_subproc_popen, mock_exit):
mock_subproc_popen.return_value.stdout = io.BytesIO(b"playbook output without expected callback sentence")

with self.assertRaises(MonkeybleCLIException):
run_ansible(MONKEYBLE_DEFAULT_ANSIBLE_CMD,
"playbook.yml",
"my_inventory",
["extra_vars1.yml", "extra_vars2.yml"],
"scenario1")
self.assertTrue(mock_subproc_popen.called)
mock_exit.assert_called_with(1)

0 comments on commit a44d9fd

Please sign in to comment.