Skip to content

Commit

Permalink
add monkeyble_shared_tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed Feb 2, 2023
1 parent 6a29424 commit 85fac79
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
37 changes: 37 additions & 0 deletions docs/scenarios.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ ANSIBLE_CONFIG="ansible.cfg" ansible-playbook playbook.yml \

## Re-using test code

### Using Jinja in extra vars

As the configuration is passed as an extra var, usage of jinja template is available to reuse some part of your configuration

E.g:
Expand Down Expand Up @@ -108,6 +110,41 @@ ANSIBLE_CONFIG="ansible.cfg" ansible-playbook playbook.yml \
-e "monkeyble_scenario=validate_test_1"
```

### Using 'monkeyble_shared_tasks'

The 'monkeyble_shared_tasks' is a variable that can be used to place test configuration that will always be available in every executed scenario.

As an example. We have a role, which is used in a lot of playbook, that call Hashicorp Vault server to get a token from a Github token. Here is the ansible code of the task:

```yaml
- name: Get a vault token from github token
uri:
url: "{{ vault_address }}/v1/auth/github/login"
method: POST
headers:
Content-Type: application/json
body_format: json
body:
token: "{{ vault_github_token }}"
register: get_vault_token
```
The mock configuration of this particular task can be placed in the `monkeyble_shared_tasks` variable and passed as extra var, so it's always available
without having to declare it in the `tasks_to_test` list of each scenario.
```yaml
monkeyble_shared_tasks:
- task: "Get a vault token from github token"
mock:
config:
monkeyble_module:
consider_changed: true
result_dict:
json:
auth:
client_token: fake_token
```


## Ansible native test components

The following Ansible components may be placed into playbooks and roles to prevent from failure:
Expand Down
3 changes: 3 additions & 0 deletions plugins/callback/monkeyble_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def v2_playbook_on_play_start(self, play):
self.extra_vars = vm.extra_vars
monkeyble_scenario = self.extra_vars.get("monkeyble_scenario")
monkeyble_scenarios = self.extra_vars.get("monkeyble_scenarios")
monkeyble_shared_tasks = self.extra_vars.get("monkeyble_shared_tasks", [])
if monkeyble_scenario is None:
raise MonkeybleException("'monkeyble_scenario' need to be passed as extra_vars")
if monkeyble_scenarios is None:
Expand All @@ -75,6 +76,8 @@ def v2_playbook_on_play_start(self, play):
raise MonkeybleException(f"The Monkeyble scenario name '{monkeyble_scenario}' "
f"not found in 'monkeyble_scenarios'")

# add shared task to the config
loaded_monkeyble_config["monkeyble_shared_tasks"] = monkeyble_shared_tasks
# variable placed into the monkeyble config need to be instantiated with extra vars
templar = Templar(loader=DataLoader(), variables=self.extra_vars)
try:
Expand Down
3 changes: 3 additions & 0 deletions plugins/module_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ def get_task_config(ansible_task: Task, monkeyble_config: dict):
role_name = ansible_task._role._role_name
# print(f"get_task_config role: {role_name}")
if "tasks_to_test" in monkeyble_config:
# we add global task at the end the list of task
if "monkeyble_shared_tasks" in monkeyble_config:
monkeyble_config["tasks_to_test"].extend(monkeyble_config["monkeyble_shared_tasks"])
for task_config in monkeyble_config["tasks_to_test"]:
if "task" not in task_config:
raise MonkeybleException(message=str("Monkeyble error: Task name need to be set"))
Expand Down
10 changes: 9 additions & 1 deletion tests/units/test_callback/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def test_v2_playbook_on_play_start(self):
self.var_manager.extra_vars = {
"my_extra_variable": "value1",
"monkeyble_scenario": "test_scenario",
"monkeyble_shared_tasks": [
{"task": "shared_task_1"},
{"task": "{{ my_extra_variable }}"},
],
"monkeyble_scenarios": {
"test_scenario": {
"name": "{{ my_extra_variable }}"
Expand All @@ -70,7 +74,11 @@ def test_v2_playbook_on_play_start(self):
}
self.test_callback.v2_playbook_on_play_start(self.play)
expected_monkeyble_config = {
"name": "value1"
"name": "value1",
"monkeyble_shared_tasks": [
{"task": "shared_task_1"},
{"task": "value1"},
],
}
self.assertDictEqual(expected_monkeyble_config, self.test_callback.monkeyble_config)

Expand Down
16 changes: 16 additions & 0 deletions tests/units/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,19 @@ def test_get_task_config_task_play_filter(self):
ansible_task.play.name = "another_play_name"
result = get_task_config(ansible_task=ansible_task, monkeyble_config=monkeyble_config)
self.assertIsNone(result)

def test_get_task_config_task_from_shared_task(self):
ansible_task = MagicMock()
ansible_task._role._role_name = None
ansible_task.name = "task_name_test"
ansible_task.play.name = "play_name_test"
monkeyble_config = {"name": "Validate this",
"tasks_to_test": [],
"monkeyble_shared_tasks": [{"task": "task_name_test",
"play": "play_name_test"}]
}

result = get_task_config(ansible_task=ansible_task, monkeyble_config=monkeyble_config)
expected = {"task": "task_name_test",
"play": "play_name_test"}
self.assertDictEqual(result, expected)

0 comments on commit 85fac79

Please sign in to comment.