From bb95169bec94a8461429a9b8b994ca25d9dccc50 Mon Sep 17 00:00:00 2001 From: Griffin Sullivan Date: Wed, 17 May 2023 07:49:39 -0400 Subject: [PATCH] Updating tests to use real and fake broker_settings.yaml --- broker_settings.yaml.example | 16 ------ tests/conftest.py | 20 +++++++ tests/data/broker_settings.yaml | 54 +++++++++++++++++++ .../satlab/checkout_latest_rhel.yaml | 2 +- .../satlab/checkout_latest_sat.yaml | 2 +- .../cli_scenarios/satlab/checkout_rhel78.yaml | 2 +- .../satlab/checkout_sat_613.yaml | 2 + .../cli_scenarios/satlab/checkout_sat_69.yaml | 2 - tests/functional/test_containers.py | 29 +++++----- tests/functional/test_satlab.py | 28 +++++----- tests/providers/test_ansible_tower.py | 6 +++ tests/providers/test_container.py | 6 +++ tests/test_broker.py | 4 ++ tests/test_helpers.py | 4 ++ tests/test_settings.py | 6 +++ 15 files changed, 132 insertions(+), 51 deletions(-) create mode 100644 tests/data/broker_settings.yaml create mode 100644 tests/data/cli_scenarios/satlab/checkout_sat_613.yaml delete mode 100644 tests/data/cli_scenarios/satlab/checkout_sat_69.yaml diff --git a/broker_settings.yaml.example b/broker_settings.yaml.example index f7ecb82f..308e8a31 100644 --- a/broker_settings.yaml.example +++ b/broker_settings.yaml.example @@ -40,25 +40,9 @@ Container: # name_prefix: test results_limit: 50 auto_map_ports: False -TestProvider: - instances: - - test1: - foo: "bar" - default: True - - test2: - foo: "baz" - override_envars: True - - bad: - nothing: False - config_value: "something" # You can set a nickname as a shortcut for arguments nicks: rhel7: workflow: "deploy-base-rhel" rhel_version: "7.9" notes: "Requested by broker" - test_nick: - test_action: "fake" - arg1: "abc" - arg2: 123 - arg3: True diff --git a/tests/conftest.py b/tests/conftest.py index 52e2e58f..0ff88b3e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,8 @@ import os import pytest +from broker.settings import settings_path, inventory_path +import sys +import shutil @pytest.fixture @@ -15,3 +18,20 @@ def set_envars(request): os.environ[request.param[0]] = request.param[1] yield del os.environ[request.param[0]] + +@pytest.fixture(scope="module") +def temp_settings_and_inventory(): + """Temporarily move the local inventory and settings files, then move them back when done""" + inv_backup_path = inventory_path.rename(f"{inventory_path.absolute()}.bak") + settings_backup_path = settings_path.rename(f"{settings_path.absolute()}.bak") + shutil.copyfile('tests/data/broker_settings.yaml', f'{settings_path.parent}/broker_settings.yaml') + inventory_path.touch() + del sys.modules['broker.settings'] + __import__('broker.settings') + yield + inventory_path.unlink() + settings_path.unlink() + inv_backup_path.rename(inventory_path) + settings_backup_path.rename(settings_path) + del sys.modules['broker.settings'] + __import__('broker.settings') diff --git a/tests/data/broker_settings.yaml b/tests/data/broker_settings.yaml new file mode 100644 index 00000000..e89f1f0d --- /dev/null +++ b/tests/data/broker_settings.yaml @@ -0,0 +1,54 @@ +# Broker settings +# different log levels for file and stdout +logging: + console_level: info + file_level: debug +# Host Settings +# These can be left alone if you're not using Broker as a library +host_username: root +host_password: "" +host_ssh_port: 22 +host_ssh_key_filename: "" +# Provider settings +TestProvider: + instances: + - test1: + foo: "bar" + default: True + - test2: + foo: "baz" + override_envars: True + - bad: + nothing: False + config_value: "something" +AnsibleTower: + base_url: "https:///" + # Username is required for both token and password-based authentication + username: "" + # token is the preferred authentication method + token: "" + # password: "" + inventory: "inventory name" + release_workflow: "remove-vm" + extend_workflow: "extend-vm" + new_expire_time: "+172800" + workflow_timeout: 3600 + results_limit: 50 +Container: + host_username: "" + host_password: "" + host_port: None + default: True + runtime: 'docker' + # name used to prefix container names, used to distinguish yourself + # if not set, then your local username will be used + # name_prefix: test + results_limit: 50 + auto_map_ports: False +# You can set a nickname as a shortcut for arguments +nicks: + test_nick: + test_action: "fake" + arg1: "abc" + arg2: 123 + arg3: True diff --git a/tests/data/cli_scenarios/satlab/checkout_latest_rhel.yaml b/tests/data/cli_scenarios/satlab/checkout_latest_rhel.yaml index 016c39dc..ba240cdd 100644 --- a/tests/data/cli_scenarios/satlab/checkout_latest_rhel.yaml +++ b/tests/data/cli_scenarios/satlab/checkout_latest_rhel.yaml @@ -1 +1 @@ -workflow: deploy-base-rhel +workflow: deploy-rhel diff --git a/tests/data/cli_scenarios/satlab/checkout_latest_sat.yaml b/tests/data/cli_scenarios/satlab/checkout_latest_sat.yaml index fae87fc0..60ad63bc 100644 --- a/tests/data/cli_scenarios/satlab/checkout_latest_sat.yaml +++ b/tests/data/cli_scenarios/satlab/checkout_latest_sat.yaml @@ -1 +1 @@ -workflow: deploy-sat-jenkins +workflow: deploy-satellite diff --git a/tests/data/cli_scenarios/satlab/checkout_rhel78.yaml b/tests/data/cli_scenarios/satlab/checkout_rhel78.yaml index b0579213..b34af6f6 100644 --- a/tests/data/cli_scenarios/satlab/checkout_rhel78.yaml +++ b/tests/data/cli_scenarios/satlab/checkout_rhel78.yaml @@ -1,2 +1,2 @@ -workflow: deploy-base-rhel +workflow: deploy-rhel deploy_rhel_version: "7.8" diff --git a/tests/data/cli_scenarios/satlab/checkout_sat_613.yaml b/tests/data/cli_scenarios/satlab/checkout_sat_613.yaml new file mode 100644 index 00000000..6ef6c8a0 --- /dev/null +++ b/tests/data/cli_scenarios/satlab/checkout_sat_613.yaml @@ -0,0 +1,2 @@ +workflow: deploy-satellite +deploy_sat_version: "6.13" diff --git a/tests/data/cli_scenarios/satlab/checkout_sat_69.yaml b/tests/data/cli_scenarios/satlab/checkout_sat_69.yaml deleted file mode 100644 index 754928a2..00000000 --- a/tests/data/cli_scenarios/satlab/checkout_sat_69.yaml +++ /dev/null @@ -1,2 +0,0 @@ -workflow: deploy-sat-jenkins -deploy_sat_version: "6.9" diff --git a/tests/functional/test_containers.py b/tests/functional/test_containers.py index f39f673a..61b3a92d 100644 --- a/tests/functional/test_containers.py +++ b/tests/functional/test_containers.py @@ -1,11 +1,12 @@ from pathlib import Path +import shutil from tempfile import NamedTemporaryFile import pytest from click.testing import CliRunner from broker import Broker from broker.commands import cli from broker.providers.container import Container -from broker.settings import inventory_path +from broker.settings import inventory_path, settings_path SCENARIO_DIR = Path("tests/data/cli_scenarios/containers") @@ -72,21 +73,20 @@ def test_container_e2e(): assert c_host._cont_inst.top()['Processes'] res = c_host.execute("hostname") assert res.stdout.strip() == c_host.hostname - loc_settings_path = Path("broker_settings.yaml") remote_dir = "/tmp/fake" - c_host.session.sftp_write(loc_settings_path.name, f"{remote_dir}/") + c_host.session.sftp_write(settings_path, f"{remote_dir}/") res = c_host.execute(f"ls {remote_dir}") - assert str(loc_settings_path) in res.stdout + assert str(settings_path.name) in res.stdout with NamedTemporaryFile() as tmp: - c_host.session.sftp_read(f"{remote_dir}/{loc_settings_path.name}", tmp.file.name) + c_host.session.sftp_read(f"{remote_dir}/{settings_path.name}", tmp.file.name) data = c_host.session.sftp_read( - f"{remote_dir}/{loc_settings_path.name}", return_data=True + f"{remote_dir}/{settings_path.name}", return_data=True ) assert ( - loc_settings_path.read_bytes() == Path(tmp.file.name).read_bytes() + settings_path.read_bytes() == Path(tmp.file.name).read_bytes() ), "Local file is different from the received one" assert ( - loc_settings_path.read_bytes() == data + settings_path.read_bytes() == data ), "Local file is different from the received one (return_data=True)" assert data == Path(tmp.file.name).read_bytes(), "Received files do not match" # test the tail_file context manager @@ -104,20 +104,19 @@ def test_container_e2e_mp(): assert c_host._cont_inst.top()['Processes'] res = c_host.execute("hostname") assert res.stdout.strip() == c_host.hostname - loc_settings_path = Path("broker_settings.yaml") remote_dir = "/tmp/fake" - c_host.session.sftp_write(loc_settings_path.name, f"{remote_dir}/") + c_host.session.sftp_write(settings_path, f"{remote_dir}/") res = c_host.execute(f"ls {remote_dir}") - assert str(loc_settings_path) in res.stdout + assert str(settings_path.name) in res.stdout with NamedTemporaryFile() as tmp: - c_host.session.sftp_read(f"{remote_dir}/{loc_settings_path.name}", tmp.file.name) + c_host.session.sftp_read(f"{remote_dir}/{settings_path.name}", tmp.file.name) data = c_host.session.sftp_read( - f"{remote_dir}/{loc_settings_path.name}", return_data=True + f"{remote_dir}/{settings_path.name}", return_data=True ) assert ( - loc_settings_path.read_bytes() == Path(tmp.file.name).read_bytes() + settings_path.read_bytes() == Path(tmp.file.name).read_bytes() ), "Local file is different from the received one" assert ( - loc_settings_path.read_bytes() == data + settings_path.read_bytes() == data ), "Local file is different from the received one (return_data=True)" assert data == Path(tmp.file.name).read_bytes(), "Received files do not match" diff --git a/tests/functional/test_satlab.py b/tests/functional/test_satlab.py index a8be4e3a..874a4058 100644 --- a/tests/functional/test_satlab.py +++ b/tests/functional/test_satlab.py @@ -5,7 +5,7 @@ from broker import Broker from broker.commands import cli from broker.providers.ansible_tower import AnsibleTower -from broker.settings import inventory_path +from broker.settings import inventory_path, settings_path SCENARIO_DIR = Path("tests/data/cli_scenarios/satlab") @@ -69,21 +69,20 @@ def test_tower_host(): with Broker(workflow="deploy-base-rhel") as r_host: res = r_host.execute("hostname") assert res.stdout.strip() == r_host.hostname - loc_settings_path = Path("broker_settings.yaml") remote_dir = "/tmp/fake" - r_host.session.sftp_write(loc_settings_path.name, f"{remote_dir}/") + r_host.session.sftp_write(settings_path, f"{remote_dir}/") res = r_host.execute(f"ls {remote_dir}") - assert str(loc_settings_path) in res.stdout + assert str(settings_path.name) in res.stdout with NamedTemporaryFile() as tmp: - r_host.session.sftp_read(f"{remote_dir}/{loc_settings_path.name}", tmp.file.name) + r_host.session.sftp_read(f"{remote_dir}/{settings_path.name}", tmp.file.name) data = r_host.session.sftp_read( - f"{remote_dir}/{loc_settings_path.name}", return_data=True + f"{remote_dir}/{settings_path.name}", return_data=True ) assert ( - loc_settings_path.read_bytes() == Path(tmp.file.name).read_bytes() + settings_path.read_bytes() == Path(tmp.file.name).read_bytes() ), "Local file is different from the received one" assert ( - loc_settings_path.read_bytes() == data + settings_path.read_bytes() == data ), "Local file is different from the received one (return_data=True)" assert data == Path(tmp.file.name).read_bytes(), "Received files do not match" # test the tail_file context manager @@ -100,20 +99,19 @@ def test_tower_host_mp(): for r_host in r_hosts: res = r_host.execute("hostname") assert res.stdout.strip() == r_host.hostname - loc_settings_path = Path("broker_settings.yaml") remote_dir = "/tmp/fake" - r_host.session.sftp_write(loc_settings_path.name, f"{remote_dir}/") + r_host.session.sftp_write(settings_path, f"{remote_dir}/") res = r_host.execute(f"ls {remote_dir}") - assert str(loc_settings_path) in res.stdout + assert str(settings_path.name) in res.stdout with NamedTemporaryFile() as tmp: - r_host.session.sftp_read(f"{remote_dir}/{loc_settings_path.name}", tmp.file.name) + r_host.session.sftp_read(f"{remote_dir}/{settings_path.name}", tmp.file.name) data = r_host.session.sftp_read( - f"{remote_dir}/{loc_settings_path.name}", return_data=True + f"{remote_dir}/{settings_path.name}", return_data=True ) assert ( - loc_settings_path.read_bytes() == Path(tmp.file.name).read_bytes() + settings_path.read_bytes() == Path(tmp.file.name).read_bytes() ), "Local file is different from the received one" assert ( - loc_settings_path.read_bytes() == data + settings_path.read_bytes() == data ), "Local file is different from the received one (return_data=True)" assert data == Path(tmp.file.name).read_bytes(), "Received files do not match" diff --git a/tests/providers/test_ansible_tower.py b/tests/providers/test_ansible_tower.py index 14143bc2..f724cd77 100644 --- a/tests/providers/test_ansible_tower.py +++ b/tests/providers/test_ansible_tower.py @@ -73,6 +73,12 @@ def pop(self, item=None): return super().pop(item) +@pytest.fixture(scope="module", autouse=True) +def _temp_settings_and_inventory(temp_settings_and_inventory): + """Temporarily move the local inventory and settings files, then move them back when done""" + pass + + @pytest.fixture(scope="function") def api_stub(): yield AwxkitApiStub() diff --git a/tests/providers/test_container.py b/tests/providers/test_container.py index e406adf3..002142a4 100644 --- a/tests/providers/test_container.py +++ b/tests/providers/test_container.py @@ -55,6 +55,12 @@ def create_container(self, container_host, **kwargs): return MockStub(container) +@pytest.fixture(scope="module", autouse=True) +def _temp_settings_and_inventory(temp_settings_and_inventory): + """Temporarily move the local inventory and settings files, then move them back when done""" + pass + + @pytest.fixture(scope="function") def api_stub(): yield ContainerApiStub() diff --git a/tests/test_broker.py b/tests/test_broker.py index 82312a81..245ee2b7 100644 --- a/tests/test_broker.py +++ b/tests/test_broker.py @@ -2,6 +2,10 @@ from broker.providers import test_provider import pytest +@pytest.fixture(scope="module", autouse=True) +def _temp_settings_and_inventory(temp_settings_and_inventory): + """Temporarily move the local inventory and settings files, then move them back when done""" + pass def test_empty_init(): """Broker should be able to init without any arguments""" diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 443cece4..f0c7b7fc 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -12,6 +12,10 @@ "my_second_arg": "foo", } +@pytest.fixture(scope="module", autouse=True) +def _temp_settings_and_inventory(temp_settings_and_inventory): + """Temporarily move the local inventory and settings files, then move them back when done""" + pass @pytest.fixture def tmp_file(tmp_path): diff --git a/tests/test_settings.py b/tests/test_settings.py index 1926babf..11949cfa 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -6,6 +6,12 @@ from broker.providers.test_provider import TestProvider +@pytest.fixture(scope="module", autouse=True) +def _temp_settings_and_inventory(temp_settings_and_inventory): + """Temporarily move the local inventory and settings files, then move them back when done""" + pass + + def test_default_settings(): test_provider = TestProvider() assert test_provider.instance == "test1"