Small improvements to the formulas tests

This commit is contained in:
Pedro Algarvio 2024-05-03 16:18:46 +01:00
parent b979cc3754
commit d0b9bbee35
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF
6 changed files with 56 additions and 111 deletions

View file

@ -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)

View file

@ -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
def formula():
return types.SimpleNamespace(name="nginx-formula", tag="2.8.1")
@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
@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

View file

@ -2,25 +2,21 @@
Tests using sudoers formula
"""
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

View file

@ -2,38 +2,32 @@
Tests using users formula
"""
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

View file

@ -2,29 +2,23 @@
Tests using vim formula
"""
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

View file

@ -1,44 +0,0 @@
import functools
import pathlib
import shutil
import zipfile
import attr
import pytest
@attr.s(slots=True, frozen=True)
class SaltStackFormula:
"""
Class representing a saltstack formula.
"""
name: str = attr.ib()
tag: str = attr.ib()
tmp_path: pathlib.Path = attr.ib()
state_tree_path: pathlib.Path = attr.ib()
url: str = attr.ib()
@url.default
def _default_url(self):
return f"https://github.com/saltstack-formulas/{self.name}/archive/refs/tags/v{self.tag}.zip"
def __enter__(self):
target_path = self.state_tree_path / f"{self.name}-{self.tag}"
if not target_path.exists():
zipfile_path = pytest.helpers.download_file(
self.url, self.tmp_path / self.url.split("/")[-1]
)
with zipfile.ZipFile(zipfile_path) as zip_obj:
zip_obj.extractall(self.tmp_path)
shutil.move(self.tmp_path / f"{self.name}-{self.tag}", target_path)
return self
def __exit__(self, *_):
pass
@classmethod
def with_default_paths(cls, tmp_path, state_tree_path):
return functools.partial(
cls, tmp_path=tmp_path, state_tree_path=state_tree_path
)