Skip to content

Commit

Permalink
Small improvements to the formulas tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed May 9, 2024
1 parent 65a83ca commit dff783e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 111 deletions.
22 changes: 17 additions & 5 deletions tests/pytests/functional/formulas/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import shutil
import zipfile

import pytest

from tests.support.pytest.formulas import SaltStackFormula

@pytest.fixture(scope="module")
def formula():
pytest.fail("The module scoped 'formula' fixture should have been overridden.")


@pytest.fixture(scope="module")
def saltstack_formula(tmp_path_factory, base_env_state_tree_root_dir):
zipfiles_dir = tmp_path_factory.mktemp("fomulas-zips")
def modules(loaders, formula, tmp_path_factory, base_env_state_tree_root_dir):
url = f"https://github.com/saltstack-formulas/{formula.name}/archive/refs/tags/v{formula.tag}.zip"
zipfiles_dir = tmp_path_factory.mktemp(f"unzipped-{formula.name}-{formula.tag}")
try:
yield SaltStackFormula.with_default_paths(
zipfiles_dir, base_env_state_tree_root_dir
target_path = base_env_state_tree_root_dir / f"{formula.name}-{formula.tag}"
zipfile_path = pytest.helpers.download_file(
url, zipfiles_dir / url.split("/")[-1]
)
with zipfile.ZipFile(zipfile_path) as zip_obj:
zip_obj.extractall(zipfiles_dir)
shutil.move(zipfiles_dir / f"{formula.name}-{formula.tag}", target_path)

loaders.opts["file_roots"]["base"].append(str(target_path))
yield loaders.modules
finally:
shutil.rmtree(zipfiles_dir, ignore_errors=True)
23 changes: 8 additions & 15 deletions tests/pytests/functional/formulas/test_nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@
Tests using nginx formula
"""

import types

import pytest

pytestmark = [
pytest.mark.timeout_unless_on_windows(120),
pytest.mark.skip_on_windows,
pytest.mark.destructive_test,
pytest.mark.timeout_unless_on_windows(240),
]


@pytest.fixture(scope="module")
def _formula(saltstack_formula):
with saltstack_formula(name="nginx-formula", tag="2.8.1") as formula:
yield formula


@pytest.fixture(scope="module")
def modules(loaders, _formula):
loaders.opts["file_roots"]["base"].append(
str(_formula.state_tree_path / f"{_formula.name}-{_formula.tag}")
)
return loaders.modules
def formula():
return types.SimpleNamespace(name="nginx-formula", tag="2.8.1")


@pytest.mark.skip_on_windows
@pytest.mark.destructive_test
def test_formula(modules):
ret = modules.state.sls("nginx")
assert not ret.errors
assert not ret.failed
assert ret.failed is False
for staterun in ret:
assert staterun.result is True
20 changes: 8 additions & 12 deletions tests/pytests/functional/formulas/test_sudoers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@
Tests using sudoers formula
"""

import pytest
import types

import pytest

@pytest.fixture(scope="module")
def _formula(saltstack_formula):
with saltstack_formula(name="sudoers-formula", tag="0.25.0") as formula:
yield formula
pytestmark = [
pytest.mark.skip_on_windows,
pytest.mark.destructive_test,
]


@pytest.fixture(scope="module")
def modules(loaders, _formula):
loaders.opts["file_roots"]["base"].append(
str(_formula.state_tree_path / f"{_formula.name}-{_formula.tag}")
)
return loaders.modules
def formula():
return types.SimpleNamespace(name="sudoers-formula", tag="0.25.0")


@pytest.mark.skip_on_windows
@pytest.mark.destructive_test
def test_sudoers_formula(modules):
ret = modules.state.sls("sudoers")
assert not ret.errors
Expand Down
32 changes: 13 additions & 19 deletions tests/pytests/functional/formulas/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@
Tests using users formula
"""

import pytest
import types

import pytest

@pytest.fixture(scope="module")
def _formula(saltstack_formula):
with saltstack_formula(name="users-formula", tag="0.48.8") as formula:
yield formula
pytestmark = [
pytest.mark.skip_on_windows,
pytest.mark.destructive_test,
]


@pytest.fixture(scope="module")
def modules(loaders, _formula):
loaders.opts["file_roots"]["base"].append(
str(_formula.state_tree_path / f"{_formula.name}-{_formula.tag}")
)
return loaders.modules
def formula():
return types.SimpleNamespace(name="users-formula", tag="0.48.8")


@pytest.mark.skip_on_windows
@pytest.mark.destructive_test
def test_users_formula(modules):
# sudo
def test_users_sudo_formula(modules):
ret = modules.state.sls("users.sudo")
assert not ret.errors
assert not ret.failed
assert ret.failed is False
for staterun in ret:
assert staterun.result is True

# bashrc

def test_users_bashrc_formula(modules):
ret = modules.state.sls("users.bashrc")
for staterun in ret:
assert not staterun.result.failed
assert not ret.errors
assert not ret.failed
assert ret.failed is False
for staterun in ret:
assert staterun.result is True
26 changes: 10 additions & 16 deletions tests/pytests/functional/formulas/test_vim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@
Tests using vim formula
"""

import pytest
import types

import pytest

@pytest.fixture(scope="module")
def _formula(saltstack_formula):
with saltstack_formula(name="vim-formula", tag="0.15.5") as formula:
yield formula
pytestmark = [
pytest.mark.skip_on_windows,
pytest.mark.destructive_test,
]


@pytest.fixture(scope="module")
def modules(loaders, _formula):
loaders.opts["file_roots"]["base"].append(
str(_formula.state_tree_path / f"{_formula.name}-{_formula.tag}")
)
return loaders.modules
def formula(grains):
if grains["oscodename"] == "Photon":
pytest.skip(reason="vim package not available for this distribution")
return types.SimpleNamespace(name="vim-formula", tag="0.15.5")


@pytest.mark.skip_on_windows
@pytest.mark.destructive_test
@pytest.mark.skipif(
'grains["oscodename"] == "Photon"',
reason="vim package not available for this distrubition",
)
def test_vim_formula(modules):
ret = modules.state.sls("vim")
assert not ret.errors
Expand Down
44 changes: 0 additions & 44 deletions tests/support/pytest/formulas.py

This file was deleted.

0 comments on commit dff783e

Please sign in to comment.