Skip to content

Commit

Permalink
ci: Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
titom73 committed Nov 20, 2023
1 parent b5fa87b commit d98a3db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
1 change: 1 addition & 0 deletions anta/cli/get/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def from_ansible(ctx: click.Context, output: Path, ansible_inventory: Path, ansi
except ValueError as e:
logger.error(str(e))
ctx.exit(ExitCode.USAGE_ERROR)
ctx.exit(ExitCode.OK)


@click.command()
Expand Down
23 changes: 13 additions & 10 deletions tests/units/cli/get/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from cvprac.cvp_client_errors import CvpApiError

from anta.cli import anta
from anta.cli.get.commands import from_ansible, from_cvp
from anta.cli.get.commands import from_cvp
from tests.lib.utils import default_anta_env

if TYPE_CHECKING:
Expand Down Expand Up @@ -132,9 +132,11 @@ def test_from_ansible(
out_dir = Path() / output
else:
# Get inventory-directory default
default_dir: Path = cast(Path, from_ansible.params[2].default)
default_dir: Path = cast(Path, f"{tmp_path}/output.yml")
out_dir = Path() / default_dir

cli_args.extend(["--output", str(out_dir)])

if ansible_inventory is not None:
ansible_inventory_path = DATA_DIR / ansible_inventory
cli_args.extend(["--ansible-inventory", str(ansible_inventory_path)])
Expand All @@ -146,24 +148,25 @@ def test_from_ansible(
print(cli_args)
result = click_runner.invoke(anta, cli_args, env=env, auto_envvar_prefix="ANTA")

print(result)
print(f"Runner args: {cli_args}")
print(f"Runner result is: {result}")

assert result.exit_code == expected_exit
print(caplog.records)
if expected_exit != 0:
assert len(caplog.records) == 2
assert len(caplog.records) in {2, 3}
else:
assert out_dir.exists()


@pytest.mark.parametrize(
"ansible_inventory, ansible_group, output_option, expected_exit",
[
pytest.param("ansible_inventory.yml", None, "--no-overwrite", 3, id="no group-no-overwrite"),
pytest.param("ansible_inventory.yml", None, "--confirm-overwrite", 0, id="no group-overwrite"),
pytest.param("ansible_inventory.yml", "ATD_LEAFS", "--no-overwrite", 3, id="group found"),
pytest.param("ansible_inventory.yml", "DUMMY", "--no-overwrite", 3, id="group not found"),
pytest.param("empty_ansible_inventory.yml", None, "--no-overwrite", 3, id="empty inventory"),
pytest.param("ansible_inventory.yml", None, "--no-overwrite", 1, id="no group-no-overwrite"),
pytest.param("ansible_inventory.yml", None, "--confirm-overwrite", 1, id="no group-overwrite"),
pytest.param("ansible_inventory.yml", "ATD_LEAFS", "--no-overwrite", 1, id="group found"),
pytest.param("ansible_inventory.yml", "DUMMY", "--no-overwrite", 1, id="group not found"),
pytest.param("empty_ansible_inventory.yml", None, "--no-overwrite", 1, id="empty inventory"),
],
)
# pylint: disable-next=too-many-arguments
Expand Down Expand Up @@ -218,5 +221,5 @@ def custom_anta_env(tmp_path: Path) -> dict[str, str]:
assert result.exit_code == expected_exit
print(caplog.records)
if expected_exit != 0:
assert len(caplog.records) in {2, 3}
assert len(caplog.records) == 1
Path(env["ANTA_INVENTORY"]).unlink(missing_ok=True)

0 comments on commit d98a3db

Please sign in to comment.