Skip to content

Commit

Permalink
stop monkeyble callback if arg name not found during test input
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed Nov 29, 2022
1 parent 837b1f3 commit afb9c24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 8 additions & 1 deletion plugins/callback/monkeyble_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,14 @@ def test_input(self, ansible_task, playbook_vars=dict):
}
for arg_to_test in self._last_task_config["test_input"]:
for test_name, value_and_expected in arg_to_test.items():
argument_value = templated_module_args[value_and_expected['arg_name']]
try:
argument_value = templated_module_args[value_and_expected['arg_name']]
except KeyError:
# the key specified in arg name does not exist. exit with error monkeyble
raise MonkeybleException(message=str(f"arg_name '{value_and_expected['arg_name']}' not present in "
f"the list of argument when executing the "
f"module '{ansible_task.action}'"),
scenario_description=self.monkeyble_scenario_description)
try:
expected = value_and_expected['expected']
except KeyError:
Expand Down
25 changes: 23 additions & 2 deletions tests/units/test_callback/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,16 @@ def test_check_input_ok(self):
@patch('sys.exit')
def test_check_input_fail(self, mock_exit):
# test fail test
self.ansible_task_test.args = {
"msg": "another_value"
self.test_callback._last_task_config = {
"task": "test_task",
"test_output": [
{
"assert_equal": {
"result_key": "result.key1",
"expected": "value1"
}
}
]
}
expected = {'monkeyble_passed_test': [],
'monkeyble_failed_test': [{'test_name': 'assert_equal',
Expand All @@ -44,3 +52,16 @@ def test_check_input_fail(self, mock_exit):
self.test_callback.test_input(self.ansible_task_test)
self.assertDictEqual(self.test_callback._last_check_input_result, expected)
mock_exit.assert_called()

@patch('sys.exit')
def test_check_input_fail_arg_does_not_exist(self, mock_exit):
self.test_input_list = [{
"assert_equal": {
"arg_name": "does_not_exist_key",
"expected": "value1"
}
}]
self.test_callback._last_task_config["test_input"] = self.test_input_list
with self.assertRaises(MonkeybleException):
self.test_callback.test_input(self.ansible_task_test)
mock_exit.assert_called()

0 comments on commit afb9c24

Please sign in to comment.