Fix and simplify tests/pytests/functional/modules/test_opkg.py

This commit is contained in:
Pedro Algarvio 2020-10-08 05:13:41 +01:00
parent 19479a1a28
commit 839be9afbd
3 changed files with 64 additions and 60 deletions

View file

View file

@ -1,83 +1,87 @@
import os
import tempfile
from pathlib import Path
import pytest
import salt.modules.cmdmod as cmd
import salt.modules.opkg as opkg
import salt.utils.platform
from tests.support.mock import patch
pytestmark = pytest.mark.skip_if_binaries_missing("stat", "md5sum", "uname")
@pytest.fixture(autouse=True)
def setup_loader(request):
def setup_loader():
setup_loader_modules = {
opkg: {"__salt__": {"cmd.shell": cmd.shell, "cmd.run_stdout": cmd.run_stdout}},
cmd: {},
}
with pytest.helpers.loader_mock(request, setup_loader_modules) as loader_mock:
with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
yield loader_mock
def test_when_nisysapi_conf_d_path_does_not_exist_it_should_not_be_created_by_update_restart_check():
with tempfile.TemporaryDirectory() as tempdir:
conf_d_path = Path(tempdir, "conf.d.path")
def test_conf_d_path_does_not_exist_not_created_by_restart_check(tmp_path):
"""
Test when nisysapi ``conf.d.path`` does not exist it should not be created by update restart check
"""
conf_d_path = tmp_path / "conf.d.path"
assert not conf_d_path.exists()
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", str(tmp_path)), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
assert not conf_d_path.exists()
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", tempdir), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
assert not conf_d_path.exists()
def test_when_nisysapi_conf_d_path_exists_and_no_files_exist_we_should_not_add_any_check_files():
with tempfile.TemporaryDirectory() as tempdir:
restartcheck_path = Path(tempdir)
conf_d_path = restartcheck_path / "conf.d.path"
conf_d_path.mkdir(parents=True, exist_ok=True)
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", tempdir), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
def test_conf_d_path_exists_with_no_files(tmp_path):
"""
Test when nisysapi ``conf.d.path`` exists and no files exist we should not add any check files
"""
conf_d_path = tmp_path / "conf.d.path"
conf_d_path.mkdir(parents=True, exist_ok=True)
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", str(tmp_path)), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
assert not [
path
for path in restartcheck_path.iterdir()
if path.suffix in (".timestamp", ".md5sum")
]
assert not [
path
for path in tmp_path.iterdir()
if path.suffix in (".timestamp", ".md5sum") and path.stem != "modules.dep"
]
@pytest.mark.skipif(
not salt.utils.platform.is_linux(),
reason="Test requires GNU stat - not found on macOS",
)
def test_when_nisysapi_conf_d_path_exists_with_files_then_we_should_fingerprint_the_files():
with tempfile.TemporaryDirectory() as tempdir:
restartcheck_path = Path(tempdir)
conf_d_path = restartcheck_path / "conf.d.path"
conf_d_path.mkdir(parents=True, exist_ok=True)
file_one = conf_d_path / "file_one"
expected_md5sum = "d41d8cd98f00b204e9800998ecf8427e {}\n".format(file_one)
expected_timestamp = "10000\n"
file_one.touch()
os.utime(str(file_one), (int(expected_timestamp), int(expected_timestamp)))
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", tempdir), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
@pytest.mark.skip_unless_on_linux(reason="Test requires GNU stat")
def test_conf_d_path_exists_with_files(tmp_path):
"""
Test when nisysapi ``conf.d.path`` exists with files then we should fingerprint the files
"""
conf_d_path = tmp_path / "conf.d.path"
conf_d_path.mkdir(parents=True, exist_ok=True)
file_one = conf_d_path / "file_one"
expected_md5sum = "d41d8cd98f00b204e9800998ecf8427e {}\n".format(file_one)
expected_timestamp = "10000\n"
file_one.touch()
os.utime(str(file_one), (int(expected_timestamp), int(expected_timestamp)))
with patch.object(opkg, "NILRT_RESTARTCHECK_STATE_PATH", str(tmp_path)), patch(
"salt.modules.opkg._get_nisysapi_conf_d_path",
autospec=True,
return_value=str(conf_d_path),
):
opkg._update_nilrt_restart_state()
timestamp = (
restartcheck_path / file_one.with_suffix(suffix=".timestamp").name
).read_text()
md5sum = (
restartcheck_path / file_one.with_suffix(suffix=".md5sum").name
).read_text()
assert timestamp == expected_timestamp
assert md5sum == expected_md5sum
assert [
path
for path in tmp_path.iterdir()
if path.suffix in (".timestamp", ".md5sum") and path.stem != "modules.dep"
]
timestamp = (
tmp_path / file_one.with_suffix(suffix=".timestamp").name
).read_text()
md5sum = (tmp_path / file_one.with_suffix(suffix=".md5sum").name).read_text()
assert timestamp == expected_timestamp
assert md5sum == expected_md5sum