From e48cbeb31fe6ca930bde65026effa8a7a065cdcf Mon Sep 17 00:00:00 2001 From: Joseph Hall Date: Thu, 20 Jul 2023 12:12:23 -0600 Subject: [PATCH] Switch to use loaders.modules instead of cli --- .../functional/formulas/test_docker.py | 57 ++++--------------- .../pytests/functional/formulas/test_nginx.py | 32 ++++------- .../pytests/functional/formulas/test_salt.py | 52 +++++------------ .../pytests/functional/formulas/test_users.py | 46 ++++++--------- 4 files changed, 55 insertions(+), 132 deletions(-) diff --git a/tests/pytests/functional/formulas/test_docker.py b/tests/pytests/functional/formulas/test_docker.py index 47764da5a68..32a14ba520c 100644 --- a/tests/pytests/functional/formulas/test_docker.py +++ b/tests/pytests/functional/formulas/test_docker.py @@ -9,6 +9,13 @@ from zipfile import ZipFile import pytest import requests +import salt.utils.files + + +@pytest.fixture(scope="module") +def modules(loaders): + return loaders.modules + @pytest.fixture(scope="module") def formula_tag(): @@ -20,7 +27,7 @@ def repo_url(formula_tag): return f"https://github.com/saltstack-formulas/docker-formula/archive/refs/tags/v{formula_tag}.zip" -@pytest.fixture(scope="module") +@pytest.fixture(scope="module", autouse=True) def docker_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): local_filename = Path(repo_url.split("/")[-1]) zip_path = state_tree / local_filename @@ -36,47 +43,7 @@ def docker_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url) return str(base_env_state_tree_root_dir) -def test_docker_formula(salt_call_cli, docker_repo): - out = salt_call_cli.run( - "--local", - "state.sls", - "docker", - "test=True", - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "archive_|-docker-software-docker-archive-install_|-/usr/local/docker-19.03.9/bin/_|-extracted", - "cmd_|-docker-software-docker-archive-install-managed-service_|-systemctl daemon-reload_|-run", - "file_|-docker-compose-software-binary-install-symlink-docker-compose_|-/usr/local/bin/docker-compose_|-symlink", - "file_|-docker-compose-software-binary-install_|-/usr/local/docker-compose-latest/bin//docker-compose_|-managed", - "file_|-docker-software-daemon-file-managed-daemon_file_|-/etc/docker/daemon.json_|-absent", - "file_|-docker-software-docker-archive-install-file-directory_|-/var/lib/docker_|-directory", - "file_|-docker-software-docker-archive-install-managed-service_|-/usr/lib/systemd/system/docker.service_|-managed", - "file_|-docker-software-docker-archive-install-symlink-containerd-shim_|-/usr/local/bin/containerd-shim_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-containerd_|-/usr/local/bin/containerd_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-ctr_|-/usr/local/bin/ctr_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-docker-init_|-/usr/local/bin/docker-init_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-docker-proxy_|-/usr/local/bin/docker-proxy_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-docker_|-/usr/local/bin/docker_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-dockerd_|-/usr/local/bin/dockerd_|-symlink", - "file_|-docker-software-docker-archive-install-symlink-runc_|-/usr/local/bin/runc_|-symlink", - "file_|-docker-software-docker-archive-install_|-/usr/local/docker-19.03.9/bin/_|-directory", - "pkg_|-docker-compose-software-binary-install_|-python-docker_|-installed", - "pkg_|-docker-compose-software-binary-install_|-python-pip_|-installed", - "pkg_|-docker-software-docker-archive-install_|-python-docker_|-installed", - "pkg_|-docker-software-docker-archive-install_|-python-pip_|-installed", - "service_|-docker-software-service-running-docker_|-docker_|-running", - "service_|-docker-software-service-running-unmasked_|-docker_|-unmasked", - "service_|-docker-software-service-running-docker-fail-notify_|-docker_|-enabled", - "test_|-docker-compose-package-install-other_|-docker-compose-package-install-other_|-show_notification", - "test_|-docker-software-desktop-install-other_|-docker-software-desktop-install-other_|-show_notification", - "test_|-docker-software-package-install-other_|-docker-software-package-install-other_|-show_notification", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False - - state_ids = [ - "test_|-docker-software-service-running-docker-fail-notify_|-docker-software-service-running-docker-fail-notify_|-fail_without_changes", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is False +def test_docker_formula(modules): + ret = modules.state.sls("docker", test=True) + for staterun in ret: + assert not staterun.result.failed diff --git a/tests/pytests/functional/formulas/test_nginx.py b/tests/pytests/functional/formulas/test_nginx.py index cd5f4344212..0c1542c4c48 100644 --- a/tests/pytests/functional/formulas/test_nginx.py +++ b/tests/pytests/functional/formulas/test_nginx.py @@ -9,6 +9,13 @@ from zipfile import ZipFile import pytest import requests +import salt.utils.files + + +@pytest.fixture(scope="module") +def modules(loaders): + return loaders.modules + @pytest.fixture(scope="module") def formula_tag(): @@ -20,7 +27,7 @@ def repo_url(formula_tag): return f"https://github.com/saltstack-formulas/nginx-formula/archive/refs/tags/v{formula_tag}.zip" -@pytest.fixture(scope="module") +@pytest.fixture(scope="module", autouse=True) def nginx_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): local_filename = Path(repo_url.split("/")[-1]) zip_path = state_tree / local_filename @@ -36,22 +43,7 @@ def nginx_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): return str(base_env_state_tree_root_dir) -def test_formula(salt_call_cli, nginx_repo): - out = salt_call_cli.run( - "--local", - "state.sls", - "nginx", - "test=True", - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "file_|-nginx_config_|-/etc/nginx/nginx.conf_|-managed", - "file_|-nginx_server_available_dir_|-/etc/nginx/sites-available_|-directory", - "file_|-nginx_server_enabled_dir_|-/etc/nginx/sites-enabled_|-directory", - "file_|-prepare_certificates_path_dir_|-/etc/nginx/ssl_|-directory", - "pkg_|-nginx_install_|-nginx_|-installed", - "service_|-listener_nginx_service_|-nginx_|-mod_watch", - "service_|-nginx_service_|-nginx_|-running", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False +def test_formula(modules): + ret = modules.state.sls("nginx", test=True) + for staterun in ret: + assert staterun.result is True diff --git a/tests/pytests/functional/formulas/test_salt.py b/tests/pytests/functional/formulas/test_salt.py index 994f8724270..f846aa3d997 100644 --- a/tests/pytests/functional/formulas/test_salt.py +++ b/tests/pytests/functional/formulas/test_salt.py @@ -9,6 +9,13 @@ from zipfile import ZipFile import pytest import requests +import salt.utils.files + + +@pytest.fixture(scope="module") +def modules(loaders): + return loaders.modules + @pytest.fixture(scope="module") def formula_tag(): @@ -20,7 +27,7 @@ def repo_url(formula_tag): return f"https://github.com/saltstack-formulas/salt-formula/archive/refs/tags/v{formula_tag}.zip" -@pytest.fixture(scope="module") +@pytest.fixture(scope="module", autouse=True) def salt_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): local_filename = Path(repo_url.split("/")[-1]) zip_path = state_tree / local_filename @@ -36,42 +43,13 @@ def salt_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): return str(base_env_state_tree_root_dir) -def test_salt_formula(salt_call_cli, salt_repo): +def test_salt_formula(modules): # Master Formula - out = salt_call_cli.run( - "--local", - "state.sls", - "salt.master", - "test=True", - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "pkg_|-salt-master_|-salt_|-installed", - "file_|-salt-master_|-/etc/salt/master.d_|-recurse", - "file_|-remove-old-master-conf-file_|-/etc/salt/master.d/_defaults.conf_|-absent", - "service_|-salt-master_|-salt-master_|-running", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False + ret = modules.state.sls("salt.master", test=True) + for staterun in ret: + assert not staterun.result.failed # Minion Formula - out = salt_call_cli.run( - "--local", - "state.sls", - "salt.minion", - "test=True", - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "pkg_|-salt-minion_|-salt_|-installed", - "file_|-salt-minion_|-/etc/salt/minion.d_|-recurse", - "file_|-remove-old-minion-conf-file_|-/etc/salt/minion.d/_defaults.conf_|-absent", - "cmd_|-salt-minion_|-salt-call --local service.restart salt-minion --out-file /dev/null_|-run", - "file_|-permissions-minion-config_|-/etc/salt/minion_|-managed", - "file_|-salt-minion-pki-dir_|-/etc/salt/pki/minion_|-directory", - "file_|-permissions-minion.pem_|-/etc/salt/pki/minion/minion.pem_|-managed", - "file_|-permissions-minion.pub_|-/etc/salt/pki/minion/minion.pub_|-managed", - "service_|-salt-minion_|-salt-minion_|-running", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False + ret = modules.state.sls("salt.minion", test=True) + for staterun in ret: + assert not staterun.result.failed diff --git a/tests/pytests/functional/formulas/test_users.py b/tests/pytests/functional/formulas/test_users.py index 0f319474599..a39a669bae3 100644 --- a/tests/pytests/functional/formulas/test_users.py +++ b/tests/pytests/functional/formulas/test_users.py @@ -9,6 +9,13 @@ from zipfile import ZipFile import pytest import requests +import salt.utils.files + + +@pytest.fixture(scope="module") +def modules(loaders): + return loaders.modules + @pytest.fixture(scope="module") def formula_tag(): @@ -20,7 +27,7 @@ def repo_url(formula_tag): return f"https://github.com/saltstack-formulas/users-formula/archive/refs/tags/v{formula_tag}.zip" -@pytest.fixture(scope="module") +@pytest.fixture(scope="module", autouse=True) def users_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): local_filename = Path(repo_url.split("/")[-1]) zip_path = state_tree / local_filename @@ -36,34 +43,13 @@ def users_repo(state_tree, base_env_state_tree_root_dir, formula_tag, repo_url): return str(base_env_state_tree_root_dir) -def test_users_formula(salt_call_cli, users_repo): +def test_users_formula(modules): # sudo - out = salt_call_cli.run( - "--local", - "state.sls", - "users.sudo", - "test=True", - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "pkg_|-users_bash-package_|-bash_|-installed", - "file_|-users_/etc/sudoers.d_|-/etc/sudoers.d_|-directory", - "pkg_|-users_sudo-package_|-sudo_|-installed", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False + ret = modules.state.sls("users.sudo", test=True) + for staterun in ret: + assert not staterun.result.failed + # bashrc - out = salt_call_cli.run( - "--local", - "state.sls", - "users.bashrc", - "test=True", - "pillar=" + json.dumps({"users": {"stan": {"fullname": "Stan Lee"}}}), - ) - ret = json.loads(str(out.stdout)) - state_ids = [ - "group_|-users_stan_user_|-stan_|-present", - "file_|-users_/etc/sudoers.d/stan_|-/etc/sudoers.d/stan_|-absent", - ] - for state_id in state_ids: - assert ret["local"][state_id]["result"] is not False + ret = modules.state.sls("users.bashrc", test=True) + for staterun in ret: + assert not staterun.result.failed