mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Migrate tests/integration/states/test_cron.py
to PyTest
This commit is contained in:
parent
7433a8a022
commit
a521ff1785
5 changed files with 106 additions and 76 deletions
|
@ -50,7 +50,7 @@ salt/modules/dockermod.py:
|
|||
- integration.states.test_docker_network
|
||||
|
||||
salt/modules/file.py:
|
||||
- integration.states.test_cron
|
||||
- pytests.integration.states.test_cron
|
||||
|
||||
salt/modules/influxdb08mod.py:
|
||||
- unit.states.test_influxdb08_database
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
# Lines below here are managed by Salt, do not edit
|
||||
@hourly touch /tmp/test-file
|
|
@ -1,73 +0,0 @@
|
|||
"""
|
||||
Tests for the cron state
|
||||
"""
|
||||
|
||||
import logging
|
||||
import pprint
|
||||
|
||||
import salt.utils.platform
|
||||
from tests.support.case import ModuleCase
|
||||
from tests.support.helpers import skip_if_binaries_missing, slowTest
|
||||
from tests.support.unit import skipIf
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), "minion is windows")
|
||||
@skip_if_binaries_missing("crontab")
|
||||
class CronTest(ModuleCase):
|
||||
"""
|
||||
Validate the file state
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Setup
|
||||
"""
|
||||
ret = self.run_state("user.present", name="test_cron_user")
|
||||
assert ret
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Teardown
|
||||
"""
|
||||
# Remove cron file
|
||||
if salt.utils.platform.is_freebsd():
|
||||
self.run_function("cmd.run", cmd="crontab -u test_cron_user -rf")
|
||||
else:
|
||||
self.run_function("cmd.run", cmd="crontab -u test_cron_user -r")
|
||||
|
||||
# Delete user
|
||||
self.run_state("user.absent", name="test_cron_user")
|
||||
|
||||
@slowTest
|
||||
def test_managed(self):
|
||||
"""
|
||||
file.managed
|
||||
"""
|
||||
ret = self.run_state(
|
||||
"cron.file", name="salt://issue-46881/cron", user="test_cron_user"
|
||||
)
|
||||
assert ret
|
||||
self.assertIn(
|
||||
"cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file",
|
||||
ret,
|
||||
msg="Assertion failed. run_state retuned: {}".format(pprint.pformat(ret)),
|
||||
)
|
||||
state = ret["cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file"]
|
||||
self.assertIn(
|
||||
"changes",
|
||||
state,
|
||||
msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
|
||||
)
|
||||
self.assertIn(
|
||||
"diff",
|
||||
state["changes"],
|
||||
msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
|
||||
)
|
||||
expected = "--- \n+++ \n@@ -1 +1,2 @@\n-\n+# Lines below here are managed by Salt, do not edit\n+@hourly touch /tmp/test-file\n"
|
||||
self.assertEqual(
|
||||
expected,
|
||||
state["changes"]["diff"],
|
||||
msg="Assertion failed. ret: {}".format(pprint.pformat(ret)),
|
||||
)
|
51
tests/pytests/integration/states/test_cron.py
Normal file
51
tests/pytests/integration/states/test_cron.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
"""
|
||||
Tests for the cron state
|
||||
"""
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
import salt.utils.platform
|
||||
from tests.support.helpers import slowTest
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cron_account():
|
||||
with pytest.helpers.create_account() as system_account:
|
||||
try:
|
||||
yield system_account
|
||||
finally:
|
||||
command = ["crontab", "-u", system_account.username, "-r"]
|
||||
if salt.utils.platform.is_freebsd():
|
||||
command.append("-f")
|
||||
subprocess.run(command, check=False)
|
||||
|
||||
|
||||
@slowTest
|
||||
@pytest.mark.skip_on_windows
|
||||
@pytest.mark.skip_if_not_root
|
||||
@pytest.mark.skip_if_binaries_missing("crontab")
|
||||
def test_managed(cron_account, salt_cli, salt_minion, base_env_state_tree_root_dir):
|
||||
"""
|
||||
file.managed
|
||||
"""
|
||||
cron_contents = "# Lines below here are managed by Salt, do not edit\n@hourly touch /tmp/test-file\n"
|
||||
expected = "--- \n+++ \n@@ -1 +1,2 @@\n-\n+# Lines below here are managed by Salt, do not edit\n+@hourly touch /tmp/test-file\n"
|
||||
with pytest.helpers.temp_file(
|
||||
"issue-46881/cron", cron_contents, base_env_state_tree_root_dir
|
||||
):
|
||||
ret = salt_cli.run(
|
||||
"state.single",
|
||||
"cron.file",
|
||||
name="salt://issue-46881/cron",
|
||||
user=cron_account.username,
|
||||
minion_tgt=salt_minion.id,
|
||||
)
|
||||
assert ret.exitcode == 0, ret
|
||||
state = ret.json["cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file"]
|
||||
assert "changes" in state
|
||||
assert "diff" in state["changes"]
|
||||
assert state["changes"]["diff"] == expected
|
|
@ -14,9 +14,14 @@ import types
|
|||
import warnings
|
||||
from contextlib import contextmanager
|
||||
|
||||
import attr
|
||||
import pytest
|
||||
import salt.utils.platform
|
||||
import salt.utils.pycrypto
|
||||
from saltfactories.utils import random_string
|
||||
from tests.support.pytest.loader import LoaderModuleMock
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.sminion import create_sminion
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -287,6 +292,55 @@ def remove_stale_minion_key(master, minion_id):
|
|||
log.debug("The minion(id=%r) key was not found at %s", minion_id, key_path)
|
||||
|
||||
|
||||
@attr.s(kw_only=True, slots=True)
|
||||
class TestAccount:
|
||||
sminion = attr.ib(default=None, repr=False)
|
||||
username = attr.ib(default=None)
|
||||
password = attr.ib(default=None)
|
||||
hashed_password = attr.ib(default=None, repr=False)
|
||||
groups = attr.ib(default=None)
|
||||
|
||||
def __attrs_post_init__(self):
|
||||
if self.sminion is None:
|
||||
self.sminion = create_sminion()
|
||||
if self.username is None:
|
||||
self.username = random_string("account-", uppercase=False)
|
||||
if self.password is None:
|
||||
self.password = self.username
|
||||
if self.hashed_password is None:
|
||||
self.hashed_password = salt.utils.pycrypto.gen_hash(password=self.password)
|
||||
|
||||
def __enter__(self):
|
||||
log.debug("Creating system account: %s", self)
|
||||
ret = self.sminion.functions.user.add(self.username)
|
||||
assert ret
|
||||
ret = self.sminion.functions.shadow.set_password(
|
||||
self.username,
|
||||
self.password if salt.utils.platform.is_darwin() else self.hashed_password,
|
||||
)
|
||||
assert ret
|
||||
assert self.username in self.sminion.functions.user.list_users()
|
||||
log.debug("Created system account: %s", self)
|
||||
# Run tests
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
self.sminion.functions.user.delete(self.username, remove=True, force=True)
|
||||
log.debug("Deleted system account: %s", self.username)
|
||||
|
||||
|
||||
@pytest.helpers.register
|
||||
@contextmanager
|
||||
def create_account(username=None, password=None, hashed_password=None, sminion=None):
|
||||
with TestAccount(
|
||||
sminion=sminion,
|
||||
username=username,
|
||||
password=password,
|
||||
hashed_password=hashed_password,
|
||||
) as account:
|
||||
yield account
|
||||
|
||||
|
||||
# Only allow star importing the functions defined in this module
|
||||
__all__ = [
|
||||
name
|
||||
|
|
Loading…
Add table
Reference in a new issue