Don't group fixtures, and improve them

Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2023-11-30 10:36:49 +00:00 committed by Daniel Wozniak
parent e19400c36b
commit 4b56ff11f3
11 changed files with 709 additions and 577 deletions

View file

@ -18,40 +18,39 @@ def assistive(modules):
return modules.assistive
@pytest.fixture(scope="function")
def osa_script():
yield "/usr/bin/osascript"
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(assistive, osa_script):
@pytest.fixture
def osa_script(assistive):
osa_script_path = "/usr/bin/osascript"
try:
ret = assistive.install(osa_script, True)
ret = assistive.install(osa_script_path, True)
yield osa_script_path
except CommandExecutionError as exc:
pytest.skip(f"Unable to install {osa_script} - {str(exc.value)}")
try:
yield
pytest.skip(f"Unable to install {osa_script}: {exc}")
finally:
osa_script_ret = assistive.installed(osa_script)
osa_script_ret = assistive.installed(osa_script_path)
if osa_script_ret:
assistive.remove(osa_script)
assistive.remove(osa_script_path)
smile_bundle = "com.smileonmymac.textexpander"
@pytest.fixture
def install_remove_pkg_name(assistive):
smile_bundle = "com.smileonmymac.textexpander"
try:
yield smile_bundle
finally:
smile_bundle_present = assistive.installed(smile_bundle)
if smile_bundle_present:
assistive.remove(smile_bundle)
@pytest.mark.slow_test
def test_install_and_remove(assistive, osa_script):
def test_install_and_remove(assistive, install_remove_pkg_name):
"""
Tests installing and removing a bundled ID or command to use assistive access.
"""
new_bundle = "com.smileonmymac.textexpander"
ret = assistive.install(new_bundle)
ret = assistive.install(install_remove_pkg_name)
assert ret
ret = assistive.remove(new_bundle)
ret = assistive.remove(install_remove_pkg_name)
assert ret

View file

@ -19,83 +19,69 @@ def pkg(modules):
return modules.pkg
# Brew doesn't support local package installation - So, let's
# Grab some small packages available online for brew
@pytest.fixture(scope="function")
def add_pkg():
yield "algol68g"
@pytest.fixture(scope="function")
def del_pkg():
yield "acme"
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(pkg, add_pkg, del_pkg):
@pytest.fixture
def pkg_1_name(pkg):
pkg_name = "algol68g"
try:
yield
yield pkg_name
finally:
pkg_list = pkg.list_pkgs()
# Remove any installed packages
if add_pkg in pkg_list:
pkg.remove(add_pkg)
if del_pkg in pkg_list:
pkg.remove(del_pkg)
# Remove package if installed
if pkg_name in pkg_list:
pkg.remove(pkg_name)
def test_brew_install(pkg, add_pkg):
@pytest.fixture
def pkg_2_name(pkg):
pkg_name = "acme"
try:
pkg.install(pkg_name)
pkg_list = pkg.list_pkgs()
if pkg_name not in pkg_list:
pytest.skip(f"Failed to install the '{pkg_name}' package to delete")
yield pkg_name
finally:
pkg_list = pkg.list_pkgs()
# Remove package if still installed
if pkg_name in pkg_list:
pkg.remove(pkg_name)
def test_brew_install(pkg, pkg_1_name):
"""
Tests the installation of packages
"""
pkg.install(add_pkg)
pkg.install(pkg_1_name)
pkg_list = pkg.list_pkgs()
assert add_pkg in pkg_list
assert pkg_1_name in pkg_list
def test_remove(pkg, del_pkg):
def test_remove(pkg, pkg_2_name):
"""
Tests the removal of packages
"""
# Install a package to delete - If unsuccessful, skip the test
pkg.install(del_pkg)
pkg.remove(pkg_2_name)
pkg_list = pkg.list_pkgs()
if del_pkg not in pkg_list:
pkg.install(del_pkg)
pytest.skip("Failed to install a package to delete")
# Now remove the installed package
pkg.remove(del_pkg)
del_list = pkg.list_pkgs()
assert del_pkg not in del_list
assert pkg_2_name not in pkg_list
def test_version(pkg, add_pkg):
def test_version(pkg, pkg_1_name):
"""
Test pkg.version for mac. Installs a package and then checks we can get
a version for the installed package.
"""
pkg.install(add_pkg)
pkg.install(pkg_1_name)
pkg_list = pkg.list_pkgs()
version = pkg.version(add_pkg)
assert version, f"version: {version} is empty, or other issue is present"
assert (
add_pkg in pkg_list
), "package: {} is not in the list of installed packages: {}".format(
add_pkg, pkg_list
)
version = pkg.version(pkg_1_name)
assert version
assert pkg_1_name in pkg_list
# make sure the version is accurate and is listed in the pkg_list
assert version in str(
pkg_list[add_pkg]
), "The {} version: {} is not listed in the pkg_list: {}".format(
add_pkg, version, pkg_list[add_pkg]
)
assert version in str(pkg_list[pkg_1_name])
def test_latest_version(pkg, add_pkg):
def test_latest_version(pkg, pkg_1_name):
"""
Test pkg.latest_version:
- get the latest version available
@ -103,12 +89,12 @@ def test_latest_version(pkg, add_pkg):
- get the latest version available
- check that the latest version is empty after installing it
"""
pkg.remove(add_pkg)
uninstalled_latest = pkg.latest_version(add_pkg)
pkg.remove(pkg_1_name)
uninstalled_latest = pkg.latest_version(pkg_1_name)
pkg.install(add_pkg)
installed_latest = pkg.latest_version(add_pkg)
version = pkg.version(add_pkg)
pkg.install(pkg_1_name)
installed_latest = pkg.latest_version(pkg_1_name)
version = pkg.version(pkg_1_name)
assert isinstance(uninstalled_latest, str)
assert installed_latest == version
@ -121,10 +107,9 @@ def test_refresh_db(pkg):
assert refresh_brew
def test_list_upgrades(pkg, add_pkg):
def test_list_upgrades(pkg, pkg_1_name):
"""
Test pkg.list_upgrades: data is in the form {'name1': 'version1',
'name2': 'version2', ... }
Test pkg.list_upgrades: data is in the form {'name1': 'version1', 'name2': 'version2', ... }
"""
upgrades = pkg.list_upgrades()
assert isinstance(upgrades, dict)
@ -134,14 +119,14 @@ def test_list_upgrades(pkg, add_pkg):
assert isinstance(upgrades[name], str)
def test_info_installed(pkg, add_pkg):
def test_info_installed(pkg, pkg_1_name):
"""
Test pkg.info_installed: info returned has certain fields used by
mac_brew.latest_version
"""
pkg.install(add_pkg)
info = pkg.info_installed(add_pkg)
assert add_pkg in info
assert "versions" in info[add_pkg]
assert "revision" in info[add_pkg]
assert "stable" in info[add_pkg]["versions"]
pkg.install(pkg_1_name)
info = pkg.info_installed(pkg_1_name)
assert pkg_1_name in info
assert "versions" in info[pkg_1_name]
assert "revision" in info[pkg_1_name]
assert "stable" in info[pkg_1_name]["versions"]

View file

@ -20,23 +20,57 @@ def group(modules):
# Create group name strings for tests
@pytest.fixture(scope="module")
def add_group():
yield random_string("RS-", lowercase=False)
def non_existing_group_name(group):
group_name = random_string("group-", lowercase=False)
try:
yield group_name
finally:
# Delete the added group
group_info = group.info(group_name)
if group_info:
group.delete(group_name)
@pytest.fixture(scope="module")
def del_group():
yield random_string("RS-", lowercase=False)
def existing_group_name(group):
group_name = random_string("group-", lowercase=False)
try:
ret = group.add(group_name, 4567)
if ret is not True:
pytest.skip("Failed to create a group to delete")
yield group_name
finally:
# Delete the added group
group_info = group.info(group_name)
if group_info:
group.delete(group_name)
@pytest.fixture(scope="module")
def change_group():
yield random_string("RS-", lowercase=False)
def non_existing_user(group):
group_name = random_string("user-", lowercase=False)
try:
yield group_name
finally:
# Delete the added group
group_info = group.info(group_name)
if group_info:
group.delete(group_name)
@pytest.fixture(scope="module")
def add_user():
yield random_string("RS-", lowercase=False)
def existing_user(group, existing_group_name):
group_name = random_string("user-", lowercase=False)
try:
ret = group.adduser(existing_group_name, group_name)
if ret is not True:
pytest.skip("Failed to create an existing group member")
yield group_name
finally:
# Delete the added group
group_info = group.info(group_name)
if group_info:
group.delete(group_name)
@pytest.fixture(scope="module")
@ -44,138 +78,99 @@ def rep_user_group():
yield random_string("RS-", lowercase=False)
@pytest.fixture(autouse=True)
def _setup_teardown_vars(group, add_group, change_group, del_group):
@pytest.fixture(scope="module")
def non_existing_group_member(group):
group_name = random_string("user-", lowercase=False)
try:
yield
yield group_name
finally:
# Delete ADD_GROUP
add_info = group.info(add_group)
if add_info:
group.delete(add_group)
# Delete DEL_GROUP if something failed
del_info = group.info(del_group)
if del_info:
group.delete(del_group)
# Delete CHANGE_GROUP
change_info = group.info(change_group)
if change_info:
group.delete(change_group)
# Delete the added group
group_info = group.info(group_name)
if group_info:
group.delete(group_name)
def test_mac_group_add(group, add_group):
def test_mac_group_add(group, non_existing_group_name):
"""
Tests the add group function
"""
group.add(add_group, 3456)
group_info = group.info(add_group)
assert group_info["name"] == add_group
group.add(non_existing_group_name, 3456)
group_info = group.info(non_existing_group_name)
assert group_info["name"] == non_existing_group_name
def test_mac_group_delete(group, del_group):
def test_mac_group_delete(group, existing_group_name):
"""
Tests the delete group function
"""
# Create a group to delete - If unsuccessful, skip the test
group_add_ret = group.add(del_group, 4567)
if group_add_ret is not True:
group.delete(del_group)
pytest.skip("Failed to create a group to delete")
# Now try to delete the added group
ret = group.delete(del_group)
ret = group.delete(existing_group_name)
assert ret
def test_mac_group_chgid(group, change_group):
def test_mac_group_chgid(group, existing_group_name):
"""
Tests changing the group id
"""
# Create a group to delete - If unsuccessful, skip the test
ret = group.add(change_group, 5678)
if ret is not True:
group.delete(change_group)
pytest.skip("Failed to create a group to manipulate")
group.chgid(change_group, 6789)
group_info = group.info(change_group)
assert group_info["gid"] == 6789
gid = 6789
group_info = group.info(existing_group_name)
assert group_info["gid"] != gid
group.chgid(existing_group_name, gid)
group_info = group.info(existing_group_name)
assert group_info["gid"] == gid
def test_mac_adduser(group, add_group, add_user):
def test_mac_adduser(group, non_existing_group_name, non_existing_user):
"""
Tests adding user to the group
"""
# Create a group to use for test - If unsuccessful, skip the test
ret = group.add(add_group, 5678)
ret = group.add(non_existing_group_name, 5678)
if ret is not True:
group.delete(add_group)
group.delete(non_existing_group_name)
pytest.skip("Failed to create a group to manipulate")
group.adduser(add_group, add_user)
group_info = group.info(add_group)
assert add_user == "".join(group_info["members"])
group.adduser(non_existing_group_name, non_existing_user)
group_info = group.info(non_existing_group_name)
assert non_existing_user in group_info["members"]
assert group_info["members"] == [non_existing_user]
def test_mac_deluser(group, add_group, add_user):
def test_mac_deluser(group, existing_group_name, existing_user):
"""
Test deleting user from a group
"""
# Create a group to use for test - If unsuccessful, skip the test
group_add_ret = group.add(add_group, 5678)
user_add_ret = group.adduser(add_group, add_user)
if group_add_ret and user_add_ret is not True:
group.delete(add_group)
pytest.skip("Failed to create a group to manipulate")
delusr = group.deluser(add_group, add_user)
delusr = group.deluser(existing_group_name, existing_user)
assert delusr
group_info = group.info(add_group)
assert add_user not in "".join(group_info["members"])
group_info = group.info(existing_group_name)
assert existing_user not in group_info["members"]
def test_mac_members(group, add_group, add_user, rep_user_group):
def test_mac_members(
group, existing_group_name, existing_user, non_existing_group_member
):
"""
Test replacing members of a group
"""
group_add_ret = group.add(add_group, 5678)
user_add_ret = group.adduser(add_group, add_user)
group_info = group.info(existing_group_name)
assert non_existing_group_member not in group_info["members"]
assert non_existing_user in group_info["members"]
if group_add_ret and user_add_ret is not True:
group.delete(add_group)
pytest.skip(
"Failed to create the {} group or add user {} to group "
"to manipulate".format(add_group, add_user)
)
rep_group_mem = group.members(add_group, rep_user_group)
# Replace group members
rep_group_mem = group.members(existing_group_name, non_existing_group_member)
assert rep_group_mem
# ensure new user is added to group and previous user is removed
group_info = group.info(add_group)
assert rep_user_group in str(group_info["members"])
assert add_user not in str(group_info["members"])
group_info = group.info(existing_group_name)
assert non_existing_group_member in group_info["members"]
assert non_existing_user not in group_info["members"]
def test_mac_getent(group, add_group, add_user):
def test_mac_getent(group, existing_user, existing_group_name):
"""
Test returning info on all groups
"""
group_add_ret = group.add(add_group, 5678)
user_add_ret = group.adduser(add_group, add_user)
if group_add_ret and user_add_ret is not True:
group.delete(add_group)
pytest.skip(
"Failed to create the {} group or add user {} to group "
"to manipulate".format(add_group, add_user)
)
getinfo = group.getent()
assert getinfo
assert add_group in str(getinfo)
assert add_user in str(getinfo)
assert existing_group_name in str(getinfo)
assert existing_user in str(getinfo)

View file

@ -18,89 +18,101 @@ def pkg(modules):
return modules.pkg
@pytest.fixture
def uninstalled_pkg_name(pkg):
pkgname = installed_pkg_name
try:
pkg.refresh_db()
yield pkgname
finally:
if pkgname in pkg.list_pkgs():
pkg.remove(pkgname)
@pytest.fixture
def installed_pkg_name(uninstalled_pkg_name):
pkg.install(uninstalled_pkg_name)
return uninstalled_pkg_name
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(pkg):
AGREE_INSTALLED = False
try:
ret = pkg.list_pkgs()
AGREE_INSTALLED = "agree" in ret
AGREE_INSTALLED = installed_pkg_name in ret
pkg.refresh_db()
yield
finally:
if AGREE_INSTALLED:
pkg.remove("agree")
pkg.remove(installed_pkg_name)
def test_list_pkgs(pkg):
def test_list_pkgs(pkg, installed_pkg_name):
"""
Test pkg.list_pkgs
"""
pkg.install("agree")
pkg_list_ret = pkg.list_pkgs()
assert isinstance(pkg_list_ret, dict)
assert "agree" in pkg_list_ret
assert installed_pkg_name in pkg_list_ret
def test_latest_version(pkg):
def test_latest_version(pkg, installed_pkg_name):
"""
Test pkg.latest_version
"""
pkg.install("agree")
result = pkg.latest_version("agree", refresh=False)
result = pkg.latest_version(installed_pkg_name, refresh=False)
assert isinstance(result, dict)
assert "agree" in result.data
assert installed_pkg_name in result.data
def test_remove(pkg):
def test_remove(pkg, installed_pkg_name):
"""
Test pkg.remove
"""
pkg.install("agree")
removed = pkg.remove("agree")
assert isinstance(removed, dict)
assert "agree" in removed
ret = pkg.remove(installed_pkg_name)
assert isinstance(ret, dict)
assert installed_pkg_name in ret
@pytest.mark.destructive_test
def test_install(pkg):
def test_install(pkg, uninstalled_pkg_name):
"""
Test pkg.install
"""
pkg.remove("agree")
installed = pkg.install("agree")
assert isinstance(installed, dict)
assert "agree" in installed
ret = pkg.install(uninstalled_pkg_name)
assert isinstance(ret, dict)
assert uninstalled_pkg_name in ret
def test_list_upgrades(pkg):
def test_list_upgrades_type(pkg):
"""
Test pkg.list_upgrades
Test pkg.list_upgrades return type
"""
upgrade = pkg.list_upgrades(refresh=False)
assert isinstance(upgrade, dict)
ret = pkg.list_upgrades(refresh=False)
assert isinstance(ret, dict)
def test_upgrade_available(pkg):
def test_upgrade_available(pkg, installed_pkg_name):
"""
Test pkg.upgrade_available
"""
pkg.install("agree")
upgrade_available = pkg.upgrade_available("agree", refresh=False)
assert not upgrade_available.data
ret = pkg.upgrade_available(installed_pkg_name, refresh=False)
assert not ret.data
def test_refresh_db(pkg):
"""
Test pkg.refresh_db
"""
refresh = pkg.refresh_db()
assert refresh
ret = pkg.refresh_db()
assert ret
def test_upgrade(pkg):
"""
Test pkg.upgrade
"""
results = pkg.upgrade(refresh=False)
assert isinstance(results, dict)
assert results.data["result"]
ret = pkg.upgrade(refresh=False)
assert isinstance(ret, dict)
assert ret.data["result"]

View file

@ -4,7 +4,7 @@ integration tests for mac_power
import pytest
from salt.exceptions import SaltInvocationError
from salt.exceptions import CommandExecutionError, SaltInvocationError
pytestmark = [
pytest.mark.skip_if_binaries_missing("systemsetup"),
@ -20,19 +20,114 @@ def power(modules):
return modules.power
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(power):
computer_sleep = power.get_computer_sleep()
display_sleep = power.get_display_sleep()
hard_disk_sleep = power.get_harddisk_sleep()
@pytest.fixture
def _reset_computer_sleep(power):
ret = power.get_computer_sleep()
try:
yield
finally:
power.set_computer_sleep(computer_sleep)
power.set_display_sleep(display_sleep)
power.set_harddisk_sleep(hard_disk_sleep)
power.set_computer_sleep(ret)
@pytest.fixture
def _reset_display_sleep(power):
ret = power.get_display_sleep()
try:
yield
finally:
power.set_display_sleep(ret)
@pytest.fixture
def _reset_harddisk_sleep(power):
ret = power.get_harddisk_sleep()
try:
yield
finally:
power.set_harddisk_sleep(ret)
@pytest.fixture
def _reset_restart_power_failure(power):
try:
ret = power.get_restart_power_failure()
if not isinstance(ret, bool):
assert "Error" in ret
pytest.skip(f"Error while calling `get_restart_power_failure()`: {ret}")
except CommandExecutionError as exc:
if "Not supported on this machine" in str(exc):
pytest.skip("Restart After Power Failure: Not supported on this machine.")
try:
yield
finally:
if isinstance(ret, bool):
if ret:
ret = power.set_restart_power_failure("On")
assert ret
else:
ret = power.set_restart_power_failure("Off")
assert ret
@pytest.fixture
def _reset_sleep_on_power_button(power):
try:
ret = power.get_sleep_on_power_button()
if not isinstance(ret, bool):
functionality_available = False
else:
functionality_available = True
except CommandExecutionError as exc:
functionality_available = False
if functionality_available is False:
pytest.skip("Skipping. sleep_on_power_button unavailable.")
try:
yield
finally:
power.set_sleep_on_power_button(ret)
@pytest.fixture
def _reset_wake_on_modem(power):
try:
ret = power.get_wake_on_modem()
if not isinstance(ret, bool):
functionality_available = False
else:
functionality_available = True
except CommandExecutionError as exc:
functionality_available = False
if functionality_available is False:
pytest.skip("Skipping. wake_on_modem unavailable.")
try:
yield
finally:
power.set_wake_on_modem(ret)
@pytest.fixture
def _reset_wake_on_network(power):
try:
ret = power.get_wake_on_network()
if not isinstance(ret, bool):
assert "Error" in ret
pytest.skip(f"Error while calling `get_wake_on_network()`: {ret}")
except CommandExecutionError as exc:
if "Not supported on this machine" in str(exc):
pytest.skip("Wake On Network Access: Not supported on this machine")
try:
yield
finally:
if isinstance(ret, bool):
ret = power.set_wake_on_network(ret)
assert ret
@pytest.mark.usefixtures("_reset_computer_sleep")
def test_computer_sleep(power):
"""
Test power.get_computer_sleep
@ -70,6 +165,7 @@ def test_computer_sleep(power):
assert "Invalid Boolean Value for Minutes" in str(exc.value)
@pytest.mark.usefixtures("_reset_display_sleep")
def test_display_sleep(power):
"""
Test power.get_display_sleep
@ -107,6 +203,7 @@ def test_display_sleep(power):
assert "Invalid Boolean Value for Minutes" in str(exc.value)
@pytest.mark.usefixtures("_reset_harddisk_sleep")
def test_harddisk_sleep(power):
"""
Test power.get_harddisk_sleep
@ -164,3 +261,79 @@ def test_restart_freeze(power):
# This is an apple bug
ret = power.get_restart_freeze()
assert ret
@pytest.mark.usefixtures("_reset_restart_power_failure")
def test_restart_power_failure(power):
"""
Test power.get_restart_power_failure
Test power.set_restart_power_failure
"""
ret = power.set_restart_power_failure("On")
assert ret
ret = power.get_restart_power_failure()
assert ret
ret = power.set_restart_power_failure("Off")
assert ret
ret = power.get_restart_power_failure()
assert not ret
@pytest.mark.usefixtures("_reset_sleep_on_power_button")
def test_sleep_on_power_button(power):
"""
Test power.get_sleep_on_power_button
Test power.set_sleep_on_power_button
"""
ret = power.set_sleep_on_power_button("on")
assert ret
ret = power.get_sleep_on_power_button()
assert ret
ret = power.set_sleep_on_power_button("off")
assert ret
ret = power.get_sleep_on_power_button()
assert not ret
@pytest.mark.usefixtures("_reset_wake_on_modem")
def test_wake_on_modem(power):
"""
Test power.get_wake_on_modem
Test power.set_wake_on_modem
"""
ret = power.set_wake_on_modem("on")
assert ret
ret = power.get_wake_on_modem()
assert ret
ret = power.set_wake_on_modem("off")
assert ret
ret = power.get_wake_on_modem()
assert not ret
@pytest.mark.usefixtures("_reset_wake_on_network")
def test_wake_on_network(power):
"""
Test power.get_wake_on_network
Test power.set_wake_on_network
"""
ret = power.set_wake_on_network("on")
assert ret
ret = power.get_wake_on_network()
assert ret
ret = power.set_wake_on_network("off")
assert ret
ret = power.get_wake_on_network()
assert not ret

View file

@ -23,11 +23,12 @@ def shadow(modules):
return modules.shadow
@pytest.fixture(scope="function")
@pytest.fixture
def accounts():
with pytest.helpers.create_account(create_group=True) as _account:
yield types.SimpleNamespace(
created=_account.username, not_created=random_string("RS-", lowercase=False)
existing=_account.username,
non_existing=random_string("account-", lowercase=False),
)
@ -36,28 +37,28 @@ def test_info(shadow, accounts):
Test shadow.info
"""
# Correct Functionality
ret = shadow.info(accounts.created)
assert ret["name"] == accounts.created
ret = shadow.info(accounts.existing)
assert ret["name"] == accounts.existing
# User does not exist
ret = shadow.info(accounts.not_created)
ret = shadow.info(accounts.non_existing)
assert ret["name"] == ""
def test_get_account_created(shadow, accounts):
def test_get_account_existing(shadow, accounts):
"""
Test shadow.get_account_created
Test shadow.get_account_existing
"""
# Correct Functionality
text_date = shadow.get_account_created(accounts.created)
text_date = shadow.get_account_existing(accounts.existing)
assert text_date != "Invalid Timestamp"
obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
assert isinstance(obj_date, datetime.date)
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.get_account_created(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_account_existing(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_last_change(shadow, accounts):
@ -65,15 +66,15 @@ def test_get_last_change(shadow, accounts):
Test shadow.get_last_change
"""
# Correct Functionality
text_date = shadow.get_last_change(accounts.created)
text_date = shadow.get_last_change(accounts.existing)
assert text_date != "Invalid Timestamp"
obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
assert isinstance(obj_date, datetime.date)
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.get_last_change(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_last_change(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_login_failed_last(shadow, accounts):
@ -81,7 +82,7 @@ def test_get_login_failed_last(shadow, accounts):
Test shadow.get_login_failed_last
"""
# Correct Functionality
text_date = shadow.get_login_failed_last(accounts.created)
text_date = shadow.get_login_failed_last(accounts.existing)
assert text_date != "Invalid Timestamp"
obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S")
assert isinstance(obj_date, datetime.date)
@ -89,7 +90,7 @@ def test_get_login_failed_last(shadow, accounts):
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.get_login_failed_last(accounts)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_login_failed_count(shadow, accounts):
@ -97,12 +98,12 @@ def test_get_login_failed_count(shadow, accounts):
Test shadow.get_login_failed_count
"""
# Correct Functionality
assert shadow.get_login_failed_count(accounts.created) == "0"
assert shadow.get_login_failed_count(accounts.existing) == "0"
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.get_login_failed_count(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_login_failed_count(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_set_maxdays(shadow, accounts):
@ -111,17 +112,17 @@ def test_get_set_maxdays(shadow, accounts):
Test shadow.set_maxdays
"""
# Correct Functionality
assert shadow.set_maxdays(accounts.created, 20)
assert shadow.get_maxdays(accounts.created) == 20
assert shadow.set_maxdays(accounts.existing, 20)
assert shadow.get_maxdays(accounts.existing) == 20
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.set_maxdays(accounts.not_created, 7)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.set_maxdays(accounts.non_existing, 7)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
with pytest.raises(CommandExecutionError) as exc:
shadow.get_maxdays(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_maxdays(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_set_change(shadow, accounts):
@ -130,17 +131,17 @@ def test_get_set_change(shadow, accounts):
Test shadow.set_change
"""
# Correct Functionality
assert shadow.set_change(accounts.created, "02/11/2011")
assert shadow.get_change(accounts.created) == "02/11/2011"
assert shadow.set_change(accounts.existing, "02/11/2011")
assert shadow.get_change(accounts.existing) == "02/11/2011"
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.set_change(accounts.not_created, "02/11/2012")
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.set_change(accounts.non_existing, "02/11/2012")
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
with pytest.raises(CommandExecutionError) as exc:
shadow.get_change(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_change(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_get_set_expire(shadow, accounts):
@ -149,17 +150,17 @@ def test_get_set_expire(shadow, accounts):
Test shadow.set_expire
"""
# Correct Functionality
assert shadow.set_expire(accounts.created, "02/11/2011")
assert shadow.get_expire(accounts.created) == "02/11/2011"
assert shadow.set_expire(accounts.existing, "02/11/2011")
assert shadow.get_expire(accounts.existing) == "02/11/2011"
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.set_expire(accounts.not_created, "02/11/2012")
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.set_expire(accounts.non_existing, "02/11/2012")
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
with pytest.raises(CommandExecutionError) as exc:
shadow.get_expire(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.get_expire(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_del_password(shadow, accounts):
@ -167,13 +168,13 @@ def test_del_password(shadow, accounts):
Test shadow.del_password
"""
# Correct Functionality
assert shadow.del_password(accounts.created)
assert shadow.info(accounts.created)["passwd"] == "*"
assert shadow.del_password(accounts.existing)
assert shadow.info(accounts.existing)["passwd"] == "*"
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.del_password(accounts.not_created)
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.del_password(accounts.non_existing)
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)
def test_set_password(shadow, accounts):
@ -181,9 +182,9 @@ def test_set_password(shadow, accounts):
Test shadow.set_password
"""
# Correct Functionality
assert shadow.set_password(accounts.created, "Pa$$W0rd")
assert shadow.set_password(accounts.existing, "Pa$$W0rd")
# User does not exist
with pytest.raises(CommandExecutionError) as exc:
shadow.set_password(accounts.not_created, "P@SSw0rd")
assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value)
shadow.set_password(accounts.non_existing, "P@SSw0rd")
assert f"ERROR: User not found: {accounts.non_existing}" in str(exc.value)

View file

@ -19,29 +19,35 @@ def softwareupdate(modules):
return modules.softwareupdate
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(softwareupdate):
IGNORED_LIST = softwareupdate.list_ignored()
SCHEDULE = softwareupdate.schedule_enabled()
CATALOG = softwareupdate.get_catalog()
@pytest.fixture
def _reset_schedule_enabled(softwareupdate):
ret = softwareupdate.schedule_enabled()
try:
yield IGNORED_LIST, SCHEDULE, CATALOG
yield
finally:
if IGNORED_LIST:
for item in IGNORED_LIST:
softwareupdate.ignore(item)
else:
softwareupdate.reset_ignored()
softwareupdate.schedule_enable(ret)
softwareupdate.schedule_enable(SCHEDULE)
if CATALOG == "Default":
@pytest.fixture
def _reset_catalog(softwareupdate):
ret = softwareupdate.get_catalog()
try:
yield
finally:
if ret == "Default":
softwareupdate.reset_catalog()
else:
softwareupdate.set_catalog(CATALOG)
softwareupdate.set_catalog(ret)
@pytest.fixture
def _reset_ignored(softwareupdate):
ret = softwareupdate.list_ignored() or ()
try:
yield
finally:
for item in ret:
softwareupdate.ignore(item)
def test_list_available(softwareupdate):
@ -54,6 +60,7 @@ def test_list_available(softwareupdate):
assert isinstance(ret, dict)
@pytest.mark.usefixtures("_reset_ignored")
@pytest.mark.skip(reason="Ignore removed from latest OS X.")
def test_ignore(softwareupdate):
"""
@ -83,6 +90,7 @@ def test_ignore(softwareupdate):
assert "squidward" in ret
@pytest.mark.usefixtures("_reset_schedule_enabled")
@pytest.mark.skip(reason="Ignore schedule support removed from latest OS X.")
def test_schedule(softwareupdate):
"""
@ -157,6 +165,7 @@ def test_download_all(softwareupdate):
assert isinstance(ret, list)
@pytest.mark.usefixtures("_reset_catalog")
@pytest.mark.skip(reason="Ignore catalog support removed from latest OS X.")
def test_get_set_reset_catalog(softwareupdate):
"""

View file

@ -56,10 +56,10 @@ def _remote_events_cleanup(system, grains):
@pytest.fixture
def _subnet_cleanup(system):
def subnet_name(system):
subnet_name = system.get_subnet_name()
try:
yield
yield random_string("subnet-", lowercase=False)
finally:
if system.get_subnet_name() != subnet_name:
system.set_subnet_name(subnet_name)
@ -76,10 +76,10 @@ def _keyboard_cleanup(system):
@pytest.fixture
def _computer_name_cleanup(system):
def computer_name(system):
computer_name = system.get_computer_name()
try:
yield
yield random_string("cmptr-", lowercase=False)
finally:
if system.get_computer_name() != computer_name:
system.set_computer_name(computer_name)
@ -197,19 +197,16 @@ def test_get_set_remote_events(system):
assert "Invalid String Value for Enabled" in str(exc.value)
@pytest.mark.usefixtures("_subnet_cleanup")
def test_get_set_subnet_name(system):
def test_get_set_subnet_name(system, subnet_name):
"""
Test system.get_subnet_name
Test system.set_subnet_name
"""
set_subnet_name = random_string("RS-", lowercase=False)
ret = system.set_subnet_name(set_subnet_name)
ret = system.set_subnet_name(subnet_name)
assert ret
ret = system.get_subnet_name()
assert ret == set_subnet_name
assert ret == subnet_name
@pytest.mark.skip_initial_gh_actions_failure
@ -336,21 +333,17 @@ def test_get_set_boot_arch(system):
# investigate
# @pytest.mark.skipif(salt.utils.platform.is_darwin() and six.PY3, reason='This test hangs on OS X on Py3. Skipping until #53566 is merged.')
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_computer_name_cleanup")
def test_get_set_computer_name(system):
def test_get_set_computer_name(system, computer_name):
"""
Test system.get_computer_name
Test system.set_computer_name
"""
set_computer_name = random_string("RS-", lowercase=False)
current_computer_name = system.get_computer_name()
assert current_computer_name
assert current_computer_name != computer_name
computer_name = system.get_computer_name()
log.debug("set name is %s", set_computer_name)
ret = system.set_computer_name(set_computer_name)
ret = system.set_computer_name(computer_name)
assert ret
ret = system.get_computer_name()
assert ret == set_computer_name
system.set_computer_name(computer_name)
assert ret == computer_name

View file

@ -29,29 +29,57 @@ def timezone(modules):
return modules.timezone
@pytest.fixture(scope="function", autouse=True)
def _setup_teardown_vars(timezone):
USE_NETWORK_TIME = timezone.get_using_network_time()
TIME_SERVER = timezone.get_time_server()
TIME_ZONE = timezone.get_zone()
CURRENT_DATE = timezone.get_date()
CURRENT_TIME = timezone.get_time()
timezone.set_using_network_time(False)
timezone.set_zone("America/Denver")
@pytest.fixture
def _reset_time_server(timezone):
ret = timezone.get_time_server()
try:
yield
finally:
timezone.set_time_server(TIME_SERVER)
timezone.set_using_network_time(USE_NETWORK_TIME)
timezone.set_zone(TIME_ZONE)
if not USE_NETWORK_TIME:
timezone.set_date(CURRENT_DATE)
timezone.set_time(CURRENT_TIME)
if timezone.get_time_server() != ret:
timezone.set_time_server(ret)
@pytest.mark.destructive_test
@pytest.fixture
def _reset_using_network_time(timezone):
ret = timezone.get_using_network_time()
try:
timezone.set_using_network_time(False)
yield ret
finally:
timezone.set_using_network_time(ret)
@pytest.fixture
def _reset_time(timezone, _reset_using_network_time):
ret = timezone.get_time()
try:
yield
finally:
if not _reset_using_network_time:
timezone.set_time(ret)
@pytest.fixture
def _reset_date(timezone, _reset_using_network_time):
ret = timezone.get_date()
try:
yield
finally:
if not _reset_using_network_time:
timezone.set_date(ret)
@pytest.fixture
def _reset_zone(timezone):
ret = timezone.get_zone()
try:
timezone.set_zone("America/Denver")
yield
finally:
timezone.set_zone(ret)
@pytest.mark.usefixtures("_reset_date")
def test_get_set_date(timezone):
"""
Test timezone.get_date
@ -83,7 +111,7 @@ def test_get_time(timezone):
assert isinstance(obj_date, datetime.date)
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_time")
def test_set_time(timezone):
"""
Test timezone.set_time
@ -101,7 +129,7 @@ def test_set_time(timezone):
)
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_zone")
def test_get_set_zone(timezone):
"""
Test timezone.get_zone
@ -123,7 +151,7 @@ def test_get_set_zone(timezone):
)
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_zone")
def test_get_offset(timezone):
"""
Test timezone.get_offset
@ -141,7 +169,7 @@ def test_get_offset(timezone):
assert ret == "-0800"
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_zone")
def test_get_set_zonecode(timezone):
"""
Test timezone.get_zonecode
@ -171,20 +199,18 @@ def test_list_zones(timezone):
assert "America/Los_Angeles" in zones
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_zone")
def test_zone_compare(timezone):
"""
Test timezone.zone_compare
"""
ret = timezone.set_zone("America/Denver")
assert ret
ret = timezone.zone_compare("America/Denver")
assert ret
ret = timezone.zone_compare("Pacific/Wake")
assert not ret
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_using_network_time")
def test_get_set_using_network_time(timezone):
"""
Test timezone.get_using_network_time
@ -203,7 +229,7 @@ def test_get_set_using_network_time(timezone):
assert not ret
@pytest.mark.destructive_test
@pytest.mark.usefixtures("_reset_time_server")
def test_get_set_time_server(timezone):
"""
Test timezone.get_time_server

View file

@ -22,165 +22,14 @@ def user(modules):
return modules.user
@pytest.fixture(scope="function")
def setup_teardown_vars(user):
ADD_USER = random_string("RS-", lowercase=False)
DEL_USER = random_string("RS-", lowercase=False)
PRIMARY_GROUP_USER = random_string("RS-", lowercase=False)
CHANGE_USER = random_string("RS-", lowercase=False)
try:
yield ADD_USER, DEL_USER, PRIMARY_GROUP_USER, CHANGE_USER
finally:
# Delete ADD_USER
add_info = user.info(ADD_USER)
if add_info:
user.delete(ADD_USER)
# Delete DEL_USER if something failed
del_info = user.info(DEL_USER)
if del_info:
user.delete(DEL_USER)
# Delete CHANGE_USER
change_info = user.info(CHANGE_USER)
if change_info:
user.delete(CHANGE_USER)
def test_mac_user_add(user, setup_teardown_vars):
"""
Tests the add function
"""
ADD_USER = setup_teardown_vars[0]
user.add(ADD_USER)
user_info = user.info(ADD_USER)
assert ADD_USER == user_info["name"]
@pytest.mark.slow_test
def test_mac_user_delete(user, setup_teardown_vars):
"""
Tests the delete function
"""
DEL_USER = setup_teardown_vars[1]
# Create a user to delete - If unsuccessful, skip the test
ret = user.add(DEL_USER)
if ret is not True:
user.delete(DEL_USER)
pytest.skip("Failed to create a user to delete")
# Now try to delete the added user
ret = user.delete(DEL_USER)
assert ret
@pytest.mark.slow_test
def test_mac_user_primary_group(user, setup_teardown_vars):
"""
Tests the primary_group function
"""
PRIMARY_GROUP_USER = setup_teardown_vars[2]
# Create a user to test primary group function
ret = user.add(PRIMARY_GROUP_USER)
if ret is not True:
user.delete(PRIMARY_GROUP_USER)
pytest.skip("Failed to create a user")
# Test mac_user.primary_group
primary_group = user.primary_group(PRIMARY_GROUP_USER)
uid_info = user.info(PRIMARY_GROUP_USER)
assert primary_group in uid_info["groups"]
@pytest.mark.slow_test
def test_mac_user_changes(user, setup_teardown_vars):
"""
Tests mac_user functions that change user properties
"""
CHANGE_USER = setup_teardown_vars[3]
# Create a user to manipulate - if unsuccessful, skip the test
ret = user.add(CHANGE_USER)
if ret is not True:
user.delete(CHANGE_USER)
pytest.skip("Failed to create a user")
# Test mac_user.chuid
user.chuid(CHANGE_USER, 4376)
uid_info = user.info(CHANGE_USER)
assert uid_info["uid"] == 4376
# Test mac_user.chgid
user.chgid(CHANGE_USER, 4376)
gid_info = user.info(CHANGE_USER)
assert gid_info["gid"] == 4376
# Test mac.user.chshell
user.chshell(CHANGE_USER, "/bin/zsh")
shell_info = user.info(CHANGE_USER)
assert shell_info["shell"] == "/bin/zsh"
# Test mac_user.chhome
user.chhome(CHANGE_USER, "/Users/foo")
home_info = user.info(CHANGE_USER)
assert home_info["home"] == "/Users/foo"
# Test mac_user.chfullname
user.chfullname(CHANGE_USER, "Foo Bar")
fullname_info = user.info(CHANGE_USER)
assert fullname_info["fullname"] == "Foo Bar"
# Test mac_user.chgroups
ret = user.info(CHANGE_USER)
pre_info = ret["groups"]
expected = pre_info + ["wheel"]
user.chgroups(CHANGE_USER, "wheel")
groups_info = user.info(CHANGE_USER)
assert groups_info["groups"] == expected
@pytest.mark.slow_test
def test_mac_user_enable_auto_login(user):
"""
Tests mac_user functions that enable auto login
"""
@pytest.fixture
def _reset_enable_auto_login(user):
# Make sure auto login is disabled before we start
if user.get_auto_login():
pytest.skip("Auto login already enabled")
try:
# Does enable return True
ret = user.enable_auto_login("Spongebob", "Squarepants")
assert ret
# Did it set the user entry in the plist file
ret = user.get_auto_login()
assert ret == "Spongebob"
# Did it generate the `/etc/kcpassword` file
assert os.path.exists("/etc/kcpassword")
# Are the contents of the file correct
test_data = bytes.fromhex("2e f8 27 42 a0 d9 ad 8b cd cd 6c 7d")
with salt.utils.files.fopen("/etc/kcpassword", "rb") as f:
file_data = f.read()
assert test_data == file_data
# Does disable return True
ret = user.disable_auto_login()
assert ret
# Does it remove the user entry in the plist file
ret = user.get_auto_login()
assert not ret
# Is the `/etc/kcpassword` file removed
assert not os.path.exists("/etc/kcpassword")
yield
finally:
# Make sure auto_login is disabled
ret = user.disable_auto_login()
@ -189,45 +38,152 @@ def test_mac_user_enable_auto_login(user):
# Make sure autologin is disabled
ret = user.get_auto_login()
if ret:
raise Exception("Failed to disable auto login")
pytest.fail("Failed to disable auto login")
@pytest.mark.slow_test
@pytest.fixture
def existing_user(user):
username = random_string("account-", uppercase=False)
try:
ret = user.add(username)
if ret is not True:
pytest.skip("Failed to create an account to manipulate")
yield username
finally:
user_info = user.info(username)
if user_info:
user.delete(username)
@pytest.fixture
def non_existing_user(user):
username = random_string("account-", uppercase=False)
try:
yield username
finally:
user_info = user.info(username)
if user_info:
user.delete(username)
def test_mac_user_add(user, non_existing_user):
"""
Tests the add function
"""
user.add(non_existing_user)
user_info = user.info(non_existing_user)
assert user_info["name"] == non_existing_user
def test_mac_user_delete(user, existing_user):
"""
Tests the delete function
"""
ret = user.delete(existing_user)
assert ret
def test_mac_user_primary_group(user, existing_user):
"""
Tests the primary_group function
"""
primary_group = user.primary_group(existing_user)
uid_info = user.info(existing_user)
assert primary_group in uid_info["groups"]
def test_mac_user_changes(user, existing_user):
"""
Tests mac_user functions that change user properties
"""
# Test mac_user.chuid
user.chuid(existing_user, 4376)
uid_info = user.info(existing_user)
assert uid_info["uid"] == 4376
# Test mac_user.chgid
user.chgid(existing_user, 4376)
gid_info = user.info(existing_user)
assert gid_info["gid"] == 4376
# Test mac.user.chshell
user.chshell(existing_user, "/bin/zsh")
shell_info = user.info(existing_user)
assert shell_info["shell"] == "/bin/zsh"
# Test mac_user.chhome
user.chhome(existing_user, "/Users/foo")
home_info = user.info(existing_user)
assert home_info["home"] == "/Users/foo"
# Test mac_user.chfullname
user.chfullname(existing_user, "Foo Bar")
fullname_info = user.info(existing_user)
assert fullname_info["fullname"] == "Foo Bar"
# Test mac_user.chgroups
ret = user.info(existing_user)
pre_info = ret["groups"]
expected = pre_info + ["wheel"]
user.chgroups(existing_user, "wheel")
groups_info = user.info(existing_user)
assert groups_info["groups"] == expected
@pytest.mark.usefixtures("_reset_enable_auto_login")
def test_mac_user_enable_auto_login(user):
"""
Tests mac_user functions that enable auto login
"""
# Does enable return True
ret = user.enable_auto_login("Spongebob", "Squarepants")
assert ret
# Did it set the user entry in the plist file
ret = user.get_auto_login()
assert ret == "Spongebob"
# Did it generate the `/etc/kcpassword` file
assert os.path.exists("/etc/kcpassword")
# Are the contents of the file correct
test_data = bytes.fromhex("2e f8 27 42 a0 d9 ad 8b cd cd 6c 7d")
with salt.utils.files.fopen("/etc/kcpassword", "rb") as f:
file_data = f.read()
assert test_data == file_data
# Does disable return True
ret = user.disable_auto_login()
assert ret
# Does it remove the user entry in the plist file
ret = user.get_auto_login()
assert not ret
# Is the `/etc/kcpassword` file removed
assert not os.path.exists("/etc/kcpassword")
@pytest.mark.usefixtures("_reset_enable_auto_login")
def test_mac_user_disable_auto_login(user):
"""
Tests mac_user functions that disable auto login
"""
# Make sure auto login is enabled before we start
# Is there an existing setting
if user.get_auto_login():
pytest.skip("Auto login already enabled")
# Enable auto login for the test
user.enable_auto_login("Spongebob", "Squarepants")
try:
# Enable auto login for the test
user.enable_auto_login("Spongebob", "Squarepants")
# Make sure auto login got set up
ret = user.get_auto_login()
if not ret == "Spongebob":
raise pytest.fail("Failed to enable auto login")
# Make sure auto login got set up
ret = user.get_auto_login()
if not ret == "Spongebob":
raise Exception("Failed to enable auto login")
# Does disable return True
ret = user.disable_auto_login()
assert ret
# Does disable return True
ret = user.disable_auto_login()
assert ret
# Does it remove the user entry in the plist file
ret = user.get_auto_login()
assert not ret
# Does it remove the user entry in the plist file
ret = user.get_auto_login()
assert not ret
# Is the `/etc/kcpassword` file removed
assert not os.path.exists("/etc/kcpassword")
finally:
# Make sure auto login is disabled
ret = user.disable_auto_login()
assert ret
# Make sure auto login is disabled
ret = user.get_auto_login()
if ret:
raise Exception("Failed to disable auto login")
# Is the `/etc/kcpassword` file removed
assert not os.path.exists("/etc/kcpassword")

View file

@ -19,64 +19,56 @@ def xattr(modules):
return modules.xattr
@pytest.fixture(scope="function")
def setup_teardown_vars(salt_call_cli, tmp_path):
test_file = tmp_path / "xattr_test_file.txt"
no_file = str(tmp_path / "xattr_no_file.txt")
test_file.touch()
try:
yield str(test_file), no_file
finally:
if test_file.exists():
test_file.unlink()
@pytest.fixture
def existing_file(tmp_path):
fpath = tmp_path / "xattr_test_file.txt"
fpath.touch()
return fpath
def test_list_no_xattr(xattr, setup_teardown_vars):
@pytest.fixture
def non_existing_file(tmp_path):
return tmp_path / "xattr_no_file"
def test_list_no_xattr(xattr, existing_file, non_existing_file):
"""
Make sure there are no attributes
"""
test_file = setup_teardown_vars[0]
no_file = setup_teardown_vars[1]
# Clear existing attributes
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Test no attributes
ret = xattr.list(test_file)
ret = xattr.list(existing_file)
assert ret == {}
# Test file not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.list(no_file)
assert f"File not found: {no_file}" in str(exc.value)
ret = xattr.list(non_existing_file)
assert f"File not found: {non_existing_file}" in str(exc.value)
def test_write(xattr, setup_teardown_vars):
def test_write(xattr, existing_file, non_existing_file):
"""
Write an attribute
"""
test_file = setup_teardown_vars[0]
no_file = setup_teardown_vars[1]
# Clear existing attributes
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Write some attributes
ret = xattr.write(test_file, "spongebob", "squarepants")
ret = xattr.write(existing_file, "spongebob", "squarepants")
assert ret
ret = xattr.write(test_file, "squidward", "plankton")
ret = xattr.write(existing_file, "squidward", "plankton")
assert ret
ret = xattr.write(test_file, "crabby", "patty")
ret = xattr.write(existing_file, "crabby", "patty")
assert ret
# Test that they were actually added
ret = xattr.list(test_file)
ret = xattr.list(existing_file)
assert ret == {
"spongebob": "squarepants",
"squidward": "plankton",
@ -85,67 +77,61 @@ def test_write(xattr, setup_teardown_vars):
# Test file not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.write(no_file, "patrick", "jellyfish")
assert f"File not found: {no_file}" in str(exc.value)
ret = xattr.write(non_existing_file, "patrick", "jellyfish")
assert f"File not found: {non_existing_file}" in str(exc.value)
def test_read(xattr, setup_teardown_vars):
def test_read(xattr, existing_file, non_existing_file):
"""
Test xattr.read
"""
test_file = setup_teardown_vars[0]
no_file = setup_teardown_vars[1]
# Clear existing attributes
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Write an attribute
ret = xattr.write(test_file, "spongebob", "squarepants")
ret = xattr.write(existing_file, "spongebob", "squarepants")
assert ret
# Read the attribute
ret = xattr.read(test_file, "spongebob")
ret = xattr.read(existing_file, "spongebob")
assert ret == "squarepants"
# Test file not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.read(no_file, "spongebob")
assert f"File not found: {no_file}" in str(exc.value)
ret = xattr.read(non_existing_file, "spongebob")
assert f"File not found: {non_existing_file}" in str(exc.value)
# Test attribute not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.read(test_file, "patrick")
ret = xattr.read(existing_file, "patrick")
assert "Attribute not found: patrick" in str(exc.value)
def test_delete(xattr, setup_teardown_vars):
def test_delete(xattr, existing_file, non_existing_file):
"""
Test xattr.delete
"""
test_file = setup_teardown_vars[0]
no_file = setup_teardown_vars[1]
# Clear existing attributes
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Write some attributes
ret = xattr.write(test_file, "spongebob", "squarepants")
ret = xattr.write(existing_file, "spongebob", "squarepants")
assert ret
ret = xattr.write(test_file, "squidward", "plankton")
ret = xattr.write(existing_file, "squidward", "plankton")
assert ret
ret = xattr.write(test_file, "crabby", "patty")
ret = xattr.write(existing_file, "crabby", "patty")
assert ret
# Delete an attribute
ret = xattr.delete(test_file, "squidward")
ret = xattr.delete(existing_file, "squidward")
assert ret
# Make sure it was actually deleted
ret = xattr.list(test_file)
ret = xattr.list(existing_file)
assert ret == {
"spongebob": "squarepants",
"crabby": "patty",
@ -153,41 +139,38 @@ def test_delete(xattr, setup_teardown_vars):
# Test file not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.delete(no_file, "spongebob")
assert f"File not found: {no_file}" in str(exc.value)
ret = xattr.delete(non_existing_file, "spongebob")
assert f"File not found: {non_existing_file}" in str(exc.value)
# Test attribute not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.delete(test_file, "patrick")
ret = xattr.delete(existing_file, "patrick")
assert "Attribute not found: patrick" in str(exc.value)
def test_clear(xattr, setup_teardown_vars):
def test_clear(xattr, existing_file, non_existing_file):
"""
Test xattr.clear
"""
test_file = setup_teardown_vars[0]
no_file = setup_teardown_vars[1]
# Clear existing attributes
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Write some attributes
ret = xattr.write(test_file, "spongebob", "squarepants")
ret = xattr.write(existing_file, "spongebob", "squarepants")
assert ret
ret = xattr.write(test_file, "squidward", "plankton")
ret = xattr.write(existing_file, "squidward", "plankton")
assert ret
ret = xattr.write(test_file, "crabby", "patty")
ret = xattr.write(existing_file, "crabby", "patty")
assert ret
# Test Clear
ret = xattr.clear(test_file)
ret = xattr.clear(existing_file)
assert ret
# Test file not found
with pytest.raises(CommandExecutionError) as exc:
ret = xattr.clear(no_file)
assert f"File not found: {no_file}" in str(exc.value)
ret = xattr.clear(non_existing_file)
assert f"File not found: {non_existing_file}" in str(exc.value)