Adjust some tests to get passing nightly builds again

This commit is contained in:
Pedro Algarvio 2024-03-20 10:10:30 +00:00
parent 16bead7bcc
commit ebb02d6c3a
2 changed files with 93 additions and 88 deletions

View file

@ -318,41 +318,35 @@ def test_listen_requisite_resolution_names(state, state_tree):
assert "test_|-listener_service_|-crond_|-mod_watch" in ret
def test_onlyif_req(state, subtests):
onlyif = [{}]
with subtests.test(onlyif=onlyif):
ret = state.single(
name="onlyif test", fun="test.succeed_with_changes", onlyif=onlyif
)
assert ret.result is True
assert ret.comment == "Success!"
onlyif = [{"fun": "test.true"}]
with subtests.test(onlyif=onlyif):
ret = state.single(
name="onlyif test", fun="test.succeed_without_changes", onlyif=onlyif
)
assert ret.result is True
assert not ret.changes
assert ret.comment == "Success!"
onlyif = [{"fun": "test.false"}]
with subtests.test(onlyif=onlyif):
ret = state.single(
name="onlyif test", fun="test.fail_with_changes", onlyif=onlyif
)
assert ret.result is True
assert not ret.changes
assert ret.comment == "onlyif condition is false"
onlyif = [{"fun": "test.true"}]
with subtests.test(onlyif=onlyif):
ret = state.single(
name="onlyif test", fun="test.fail_with_changes", onlyif=onlyif
)
assert ret.result is False
@pytest.mark.parametrize(
"fun,onlyif,result,comment,assert_changes",
(
("test.succeed_with_changes", [{}], True, "Success!", None),
(
"test.succeed_without_changes",
[{"fun": "test.true"}],
True,
"Success!",
False,
),
(
"test.fail_with_changes",
[{"fun": "test.false"}],
True,
"onlyif condition is false",
False,
),
("test.fail_with_changes", [{"fun": "test.true"}], False, "Failure!", True),
),
)
def test_onlyif_req(state, fun, onlyif, result, comment, assert_changes):
ret = state.single(name="onlyif test", fun=fun, onlyif=onlyif)
assert ret.result is result
assert ret.comment == comment
if assert_changes is True:
assert ret.changes
assert ret.comment == "Failure!"
elif assert_changes is False:
assert not ret.changes
def test_listen_requisite_not_exist(state, state_tree):

View file

@ -6,6 +6,7 @@ user present with custom homedir
"""
import pathlib
import random
import shutil
import sys
@ -43,6 +44,11 @@ def username(sminion):
pass
@pytest.fixture
def guid():
return random.randint(60000, 61000)
@pytest.fixture
def user_home(username, tmp_path):
if salt.utils.platform.is_windows():
@ -429,43 +435,52 @@ def test_user_present_change_optional_groups(
assert user_info["groups"] == [group_1.name]
@pytest.fixture
def user_present_groups(states):
groups = ["testgroup1", "testgroup2"]
try:
yield groups
finally:
for group in groups:
ret = states.group.absent(name=group)
assert ret.result is True
@pytest.mark.skip_unless_on_linux(reason="underlying functionality only runs on Linux")
def test_user_present_no_groups(modules, states, username):
def test_user_present_no_groups(modules, states, username, user_present_groups, guid):
"""
test user.present when groups arg is not
included by the group is created in another
state. Re-run the states to ensure there are
not changes and it is idempotent.
"""
groups = ["testgroup1", "testgroup2"]
try:
ret = states.group.present(name=username, gid=61121)
ret = states.group.present(name=username, gid=guid)
assert ret.result is True
ret = states.user.present(
name=username,
uid=61121,
gid=61121,
uid=guid,
gid=guid,
)
assert ret.result is True
assert ret.changes["groups"] == [username]
assert ret.changes["name"] == username
ret = states.group.present(
name=groups[0],
name=user_present_groups[0],
members=[username],
)
assert ret.changes["members"] == [username]
ret = states.group.present(
name=groups[1],
name=user_present_groups[1],
members=[username],
)
assert ret.changes["members"] == [username]
user_info = modules.user.info(username)
assert user_info
assert user_info["groups"] == [username, groups[0], groups[1]]
assert user_info["groups"] == [username, *user_present_groups]
# run again, expecting no changes
ret = states.group.present(name=username)
@ -479,14 +494,14 @@ def test_user_present_no_groups(modules, states, username):
assert ret.changes == {}
ret = states.group.present(
name=groups[0],
name=user_present_groups[0],
members=[username],
)
assert ret.result is True
assert ret.changes == {}
ret = states.group.present(
name=groups[1],
name=user_present_groups[1],
members=[username],
)
assert ret.result is True
@ -494,8 +509,4 @@ def test_user_present_no_groups(modules, states, username):
user_info = modules.user.info(username)
assert user_info
assert user_info["groups"] == [username, groups[0], groups[1]]
finally:
for group in groups:
ret = states.group.absent(name=group)
assert ret.result is True
assert user_info["groups"] == [username, *user_present_groups]