Skip to content

Commit

Permalink
remove "original_module_args" from Monkeyble mock module
Browse files Browse the repository at this point in the history
Ansible tries to interpret lookup placed in it. Blocking the execution
  • Loading branch information
Sispheor committed Feb 2, 2023
1 parent 0baaea8 commit 6a29424
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
2 changes: 0 additions & 2 deletions plugins/callback/monkeyble_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,10 @@ def mock_task_module(self, ansible_task):
self.display_message_ok(msg=str(message))
ansible_task.action = new_action_name
if new_action_name == "monkeyble_module":
original_module_args = ansible_task.args
consider_changed = self._last_task_config["mock"]["config"][new_action_name].get("consider_changed", False)
result_dict = self._last_task_config["mock"]["config"][new_action_name].get("result_dict", {})
ansible_task.args = {"task_name": ansible_task.name,
"original_module_name": original_module_name,
"original_module_args": original_module_args,
"consider_changed": consider_changed,
"result_dict": result_dict
}
Expand Down
1 change: 0 additions & 1 deletion plugins/module/monkeyble_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def run_module():
module_args = {
'task_name': dict(type='str', required=True),
'original_module_name': dict(type='str', required=True),
'original_module_args': dict(type='dict', required=True),
'consider_changed': dict(type='bool', required=False, default=False),
'result_dict': dict(type='dict', required=False),
}
Expand Down
15 changes: 10 additions & 5 deletions tests/local_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
def main():
host_list = ['localhost']
# since the API is constructed for CLI it expects certain options to always be set in the context object
context.CLIARGS = ImmutableDict(connection='local', module_path=['/to/mymodules', '/usr/share/ansible'], forks=10,
context.CLIARGS = ImmutableDict(connection='local', module_path=['../plugins/module'], forks=10,
become=None, become_method=None, become_user=None, check=False, diff=False,
verbosity=0)
# required for
Expand All @@ -43,6 +43,10 @@ def main():
extra_vars = {
"task_name": "test_name2",
"test_var": "updated !",
"list_var": [
"item1",
"item2",
],
"test_input_config_1": [
{
"assert_equal": {
Expand All @@ -65,12 +69,12 @@ def main():
"name": "Validate that bla bla",
"tasks_to_test": [
{
"task": "test_name2 with test_name2",
"task": "test_name",
# "should_be_changed": True,
# "should_be_skipped": False,
# "should_fail": False,
"test_input": "{{ test_input_config_1 }}",
# "mock": {"config": "{{ replace_debug_mock }}"}
# "test_input": "{{ test_input_config_1 }}",
"mock": {"config": "{{ replace_debug_mock }}"}
}
]
# "tasks_to_test": [
Expand Down Expand Up @@ -121,7 +125,8 @@ def main():
tasks=[
# dict(action=dict(module='shell', args='ls'), register='shell_out'),
# dict(name="test_name2", action=dict(module='debug', args=dict(msg='{{ my_var }}')), when="my_var == 'to_be_updated'"),
dict(name="other", action=dict(module='debug', args=dict(msg='{{ bla }}'))),
dict(name="test_name", action=dict(module='set_fact', args=dict(key_test="{{ lookup('community.hashi_vault.hashi_vault', item) }}")), loop="{{ list_var }}", register="register1"),
dict(name="print register", action=dict(module='debug', args=dict(msg='{{ register1.results }}'))),
# dict(name="test_name3", action=dict(module='find', args=dict(path='/tmp'))),
# dict(name="test_name3", action=dict(module='set_fact', args=dict(new_var='{{ test_var }}'))),
# dict(name="test_name1", action=dict(module='debug', args=dict(msg='{{ my_var }}'))),
Expand Down
5 changes: 0 additions & 5 deletions tests/units/test_callback/test_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def test_mock_task_module(self):
expected_original_module_args = {
"msg": "my_message"
}
self.assertDictEqual(expected_original_module_args, update_ansible_task.args["original_module_args"])

def test_mock_task_module_default_config(self):
mock_config_test = {
Expand All @@ -54,7 +53,3 @@ def test_mock_task_module_default_config(self):
self.assertFalse(update_ansible_task.args["consider_changed"])
expected_result_dict = {}
self.assertDictEqual(expected_result_dict, update_ansible_task.args["result_dict"])
expected_original_module_args = {
"msg": "my_message"
}
self.assertDictEqual(expected_original_module_args, update_ansible_task.args["original_module_args"])
12 changes: 0 additions & 12 deletions tests/units/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ def test_argument_ok(self):
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
'original_module_name': 'copy',
'original_module_args': {
'datacenter': 'my_dc'
},
'task_name': "task1"
})
monkeyble_module.main()
Expand All @@ -32,9 +29,6 @@ def test_result_with_changed_defined(self):
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
'original_module_name': 'copy',
'original_module_args': {
'datacenter': 'my_dc'
},
'task_name': "task1",
'consider_changed': True
})
Expand All @@ -45,9 +39,6 @@ def test_result_with_changed_defined(self):
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
'original_module_name': 'copy',
'original_module_args': {
'datacenter': 'my_dc'
},
'task_name': "task1",
'consider_changed': False
})
Expand All @@ -59,9 +50,6 @@ def test_with_return_dict_defined(self):
with self.assertRaises(AnsibleExitJson) as result:
set_module_args({
'original_module_name': 'copy',
'original_module_args': {
'datacenter': 'my_dc'
},
'task_name': "task1",
'result_dict': {'my_key': 'my_val'}

Expand Down

0 comments on commit 6a29424

Please sign in to comment.