Skip to content

Commit

Permalink
add monkeyble list + test limit to the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Sispheor committed Mar 6, 2023
1 parent f6a1a29 commit 7eb6a7b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 10 deletions.
29 changes: 27 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ pipx inject ansible --include-apps monkeyble

### From sources

Run the Python setup
Run the Python setup
```bash
python3 setup.py install
```

## Configuration file

The CLI expect to find monkeyble configuration file. The file will be searched for in the following order:
The CLI expect to find Monkeyble configuration file. The file will be searched for in the following order:

- `-c CONFIG` as a cli argument
- `MONKEYBLE_CONFIG` placed as an environment variable
Expand Down Expand Up @@ -81,6 +81,22 @@ monkeyble_test_suite:

## Commands

### monkeyble list

List playbook and scenario declared in the Monkeyble configuration file.

Command:
```
ANSIBLE_CONFIG='monkeyble.cfg' monkeyble list
```

```
Playbook | Scenario
-------------------+-----------------
test_playbook.yml | validate_test_1
| validate_test_2
```
### monkeyble test
The monkeyble test command executes all the test declared in the `monkeyble_test_suite` configuration flag and provides
Expand All @@ -104,3 +120,12 @@ Playbook | Scenario | Test passed

🐵 Monkeyble test result - Tests passed: 4 of 4 tests
```
#### limit
Limit the execution to a list of scenario name. Use the `--limit` flag by passing space separated list of scenario.
Command:
```
ANSIBLE_CONFIG='monkeyble.cfg' monkeyble test --limit validate_this validate_that
```
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: hpe
name: monkeyble

# The version of the collection. Must be compatible with semantic versioning
version: 1.3.0
version: 1.3.1.dev1

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
2 changes: 1 addition & 1 deletion monkeyble/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# https://www.python.org/dev/peps/pep-0440/
__version__ = "1.3.0"
__version__ = "1.3.1.dev1"
25 changes: 21 additions & 4 deletions monkeyble/cli/monkeyble_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')

# actions available
ACTION_LIST = ["test"]
ACTION_LIST = ["test", "list"]


def run_ansible(ansible_cmd, playbook, inventory, extra_vars, scenario):
Expand Down Expand Up @@ -60,7 +60,8 @@ def run_ansible(ansible_cmd, playbook, inventory, extra_vars, scenario):
return TEST_FAILED


def run_monkeyble_test(monkeyble_config):
def run_monkeyble_test(monkeyble_config, scenario_name_limit=None):
scenario_name_limit = scenario_name_limit if scenario_name_limit else list()
ansible_cmd = MONKEYBLE_DEFAULT_ANSIBLE_CMD
if "monkeyble_test_suite" not in monkeyble_config:
raise MonkeybleCLIException(message="No 'monkeyble_test_suite' variable defined")
Expand All @@ -84,6 +85,8 @@ def run_monkeyble_test(monkeyble_config):
Utils.print_info(f"Monkeyble - current path: {pathlib.Path().resolve()}")
list_scenario_result = list()
for scenario in scenarios:
if len(scenario_name_limit) > 0 and scenario not in scenario_name_limit:
continue
scenario_result = ScenarioResult(scenario)
scenario_result.result = run_ansible(ansible_cmd, playbook, inventory, extra_vars, scenario)
list_scenario_result.append(scenario_result)
Expand Down Expand Up @@ -166,7 +169,8 @@ def parse_args(args):
"""
# create arguments
parser = argparse.ArgumentParser(description=MONKEYBLE)
parser.add_argument("action", help="[test]")
parser.add_argument("action", help="[test,list]")
parser.add_argument("--limit", nargs="+")
parser.add_argument("-c", "--config",
help="Path to the monkeyble config")
parser.add_argument('-v', '--version', action='version',
Expand All @@ -176,6 +180,16 @@ def parse_args(args):
return parser.parse_args(args)


def list_config(config):
headers = ["Playbook", "Scenario"]
table = list()
for monkeyble_test in config["monkeyble_test_suite"]:
row = [monkeyble_test["playbook"], '\n'.join(str(x) for x in monkeyble_test["scenarios"])]
table.append(row)
print("")
print(tabulate(table, headers=headers, tablefmt="presto"))


def main():
try:
parser = parse_args(sys.argv[1:]) # script name removed from args
Expand All @@ -191,10 +205,13 @@ def main():

if parser.action == "test":
start_time = time.monotonic()
test_results = run_monkeyble_test(config)
test_results = run_monkeyble_test(config, parser.limit)
print_result_table(test_results)
do_exit(test_results, start_time)

if parser.action == "list":
list_config(config)


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions plugins/callback/monkeyble_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ def test_output(self, result_dict):
templar = Templar(loader=DataLoader(), variables=context)
try:
templated_value = templar.template(template_string)
if templated_value == "" and test_name == "assert_is_none":
templated_value = None
except AnsibleUndefinedVariable as e:
raise MonkeybleException(message=str(e),
scenario_description=self.monkeyble_scenario_description)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "monkeyble"
version = "1.3.0" # /!\ version placed in monkeyble._version and in galaxy.yml files too
version = "1.3.1.dev1" # /!\ version placed in monkeyble._version and in galaxy.yml files too
description = "End-to-end testing framework for Ansible"
authors = ["Nicolas Marcq <[email protected]>"]
license = "GNU General Public License v3 (GPLv3)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ monkeyble_scenarios:
should_fail: true

skipped_but_should_have_failed:
task_to_tests:
tasks_to_test:
- task: "skipped_but_should_have_failed"
should_fail: true
16 changes: 16 additions & 0 deletions tests/units/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ def test_run_monkeyble_test_run_ansible_called(self):
"scenario3")
mock_run_ansible.assert_has_calls([call_1, call_2, call_3])

def test_run_monkeyble_test_with_limit_run_ansible_called(self):
monkeyble_config = {
"monkeyble_global_extra_vars": ['mocks.yml'],
"monkeyble_test_suite": [
{
"playbook": "playbook1.yml",
"inventory": "my_inventory1",
"extra_vars": ["extra_vars1.yml", "extra_vars2.yml"],
"scenarios": ["scenario1", "scenario2"]
}
]
}
with mock.patch("monkeyble.cli.monkeyble_cli.run_ansible") as mock_run_ansible:
run_monkeyble_test(monkeyble_config, scenario_name_limit="scenario1")
self.assertEqual(mock_run_ansible.call_count, 1)

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

0 comments on commit 7eb6a7b

Please sign in to comment.