diff --git a/salt/modules/mac_keychain.py b/salt/modules/mac_keychain.py index 7fdc162b9aa2..978d214ebf26 100644 --- a/salt/modules/mac_keychain.py +++ b/salt/modules/mac_keychain.py @@ -122,14 +122,14 @@ def list_certs(keychain="/Library/Keychains/System.keychain"): salt '*' keychain.list_certs """ cmd = ( - 'security find-certificate -a {} | grep -o "alis".*\\" | ' + 'security find-certificate -a {} | grep -o "alis.*" | ' "grep -o '\\\"[-A-Za-z0-9.:() ]*\\\"'".format(shlex.quote(keychain)) ) out = __salt__["cmd.run"](cmd, python_shell=True) return out.replace('"', "").split("\n") -def get_friendly_name(cert, password): +def get_friendly_name(cert, password, legacy=False): """ Get the friendly name of the given certificate @@ -143,15 +143,26 @@ def get_friendly_name(cert, password): Note: The password given here will show up as plaintext in the returned job info. + legacy + Assume legacy format for certificate. + CLI Example: .. code-block:: bash salt '*' keychain.get_friendly_name /tmp/test.p12 test123 + + salt '*' keychain.get_friendly_name /tmp/test.p12 test123 legacy=True """ + openssl_cmd = "openssl pkcs12" + if legacy: + openssl_cmd = f"{openssl_cmd} -legacy" + cmd = ( - "openssl pkcs12 -in {} -passin pass:{} -info -nodes -nokeys 2> /dev/null | " - "grep friendlyName:".format(shlex.quote(cert), shlex.quote(password)) + "{} -in {} -passin pass:{} -info -nodes -nokeys 2> /dev/null | " + "grep friendlyName:".format( + openssl_cmd, shlex.quote(cert), shlex.quote(password) + ) ) out = __salt__["cmd.run"](cmd, python_shell=True) return out.replace("friendlyName: ", "").strip() diff --git a/salt/modules/mac_power.py b/salt/modules/mac_power.py index 01fc561e834b..efdca6528465 100644 --- a/salt/modules/mac_power.py +++ b/salt/modules/mac_power.py @@ -68,7 +68,7 @@ def _validate_sleep(minutes): ) raise SaltInvocationError(msg) else: - msg = "Unknown Variable Type Passed for Minutes.\nPassed: {}".format(minutes) + msg = f"Unknown Variable Type Passed for Minutes.\nPassed: {minutes}" raise SaltInvocationError(msg) @@ -115,7 +115,7 @@ def set_sleep(minutes): salt '*' power.set_sleep never """ value = _validate_sleep(minutes) - cmd = "systemsetup -setsleep {}".format(value) + cmd = f"systemsetup -setsleep {value}" salt.utils.mac_utils.execute_return_success(cmd) state = [] @@ -165,7 +165,7 @@ def set_computer_sleep(minutes): salt '*' power.set_computer_sleep off """ value = _validate_sleep(minutes) - cmd = "systemsetup -setcomputersleep {}".format(value) + cmd = f"systemsetup -setcomputersleep {value}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( @@ -210,7 +210,7 @@ def set_display_sleep(minutes): salt '*' power.set_display_sleep off """ value = _validate_sleep(minutes) - cmd = "systemsetup -setdisplaysleep {}".format(value) + cmd = f"systemsetup -setdisplaysleep {value}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( @@ -255,7 +255,7 @@ def set_harddisk_sleep(minutes): salt '*' power.set_harddisk_sleep off """ value = _validate_sleep(minutes) - cmd = "systemsetup -setharddisksleep {}".format(value) + cmd = f"systemsetup -setharddisksleep {value}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( @@ -303,12 +303,13 @@ def set_wake_on_modem(enabled): salt '*' power.set_wake_on_modem True """ state = salt.utils.mac_utils.validate_enabled(enabled) - cmd = "systemsetup -setwakeonmodem {}".format(state) + cmd = f"systemsetup -setwakeonmodem {state}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( state, get_wake_on_modem, + True, ) @@ -353,12 +354,13 @@ def set_wake_on_network(enabled): salt '*' power.set_wake_on_network True """ state = salt.utils.mac_utils.validate_enabled(enabled) - cmd = "systemsetup -setwakeonnetworkaccess {}".format(state) + cmd = f"systemsetup -setwakeonnetworkaccess {state}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( state, get_wake_on_network, + True, ) @@ -403,12 +405,13 @@ def set_restart_power_failure(enabled): salt '*' power.set_restart_power_failure True """ state = salt.utils.mac_utils.validate_enabled(enabled) - cmd = "systemsetup -setrestartpowerfailure {}".format(state) + cmd = f"systemsetup -setrestartpowerfailure {state}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( state, get_restart_power_failure, + True, ) @@ -453,7 +456,7 @@ def set_restart_freeze(enabled): salt '*' power.set_restart_freeze True """ state = salt.utils.mac_utils.validate_enabled(enabled) - cmd = "systemsetup -setrestartfreeze {}".format(state) + cmd = f"systemsetup -setrestartfreeze {state}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated(state, get_restart_freeze, True) @@ -502,10 +505,11 @@ def set_sleep_on_power_button(enabled): salt '*' power.set_sleep_on_power_button True """ state = salt.utils.mac_utils.validate_enabled(enabled) - cmd = "systemsetup -setallowpowerbuttontosleepcomputer {}".format(state) + cmd = f"systemsetup -setallowpowerbuttontosleepcomputer {state}" salt.utils.mac_utils.execute_return_success(cmd) return salt.utils.mac_utils.confirm_updated( state, get_sleep_on_power_button, + True, ) diff --git a/salt/modules/mac_system.py b/salt/modules/mac_system.py index 1dd0aa8ea29a..ad64bc6badc6 100644 --- a/salt/modules/mac_system.py +++ b/salt/modules/mac_system.py @@ -10,6 +10,7 @@ import getpass import shlex +import salt.utils.mac_utils import salt.utils.platform from salt.exceptions import CommandExecutionError, SaltInvocationError @@ -71,7 +72,7 @@ def _execute_command(cmd, at_time=None): Returns: bool """ if at_time: - cmd = "echo '{}' | at {}".format(cmd, shlex.quote(at_time)) + cmd = f"echo '{cmd}' | at {shlex.quote(at_time)}" return not bool(__salt__["cmd.retcode"](cmd, python_shell=True)) @@ -204,10 +205,10 @@ def get_remote_login(): salt '*' system.get_remote_login """ - ret = __utils__["mac_utils.execute_return_result"]("systemsetup -getremotelogin") + ret = salt.utils.mac_utils.execute_return_result("systemsetup -getremotelogin") - enabled = __utils__["mac_utils.validate_enabled"]( - __utils__["mac_utils.parse_return"](ret) + enabled = salt.utils.mac_utils.validate_enabled( + salt.utils.mac_utils.parse_return(ret) ) return enabled == "on" @@ -230,12 +231,12 @@ def set_remote_login(enable): salt '*' system.set_remote_login True """ - state = __utils__["mac_utils.validate_enabled"](enable) + state = salt.utils.mac_utils.validate_enabled(enable) - cmd = "systemsetup -f -setremotelogin {}".format(state) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f"systemsetup -f -setremotelogin {state}" + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( state, get_remote_login, normalize_ret=True ) @@ -253,12 +254,12 @@ def get_remote_events(): salt '*' system.get_remote_events """ - ret = __utils__["mac_utils.execute_return_result"]( + ret = salt.utils.mac_utils.execute_return_result( "systemsetup -getremoteappleevents" ) - enabled = __utils__["mac_utils.validate_enabled"]( - __utils__["mac_utils.parse_return"](ret) + enabled = salt.utils.mac_utils.validate_enabled( + salt.utils.mac_utils.parse_return(ret) ) return enabled == "on" @@ -282,12 +283,12 @@ def set_remote_events(enable): salt '*' system.set_remote_events On """ - state = __utils__["mac_utils.validate_enabled"](enable) + state = salt.utils.mac_utils.validate_enabled(enable) - cmd = "systemsetup -setremoteappleevents {}".format(state) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f"systemsetup -setremoteappleevents {state}" + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( state, get_remote_events, normalize_ret=True, @@ -307,9 +308,9 @@ def get_computer_name(): salt '*' system.get_computer_name """ - ret = __utils__["mac_utils.execute_return_result"]("scutil --get ComputerName") + ret = salt.utils.mac_utils.execute_return_result("scutil --get ComputerName") - return __utils__["mac_utils.parse_return"](ret) + return salt.utils.mac_utils.parse_return(ret) def set_computer_name(name): @@ -327,10 +328,10 @@ def set_computer_name(name): salt '*' system.set_computer_name "Mike's Mac" """ - cmd = 'scutil --set ComputerName "{}"'.format(name) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f'scutil --set ComputerName "{name}"' + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( name, get_computer_name, ) @@ -349,11 +350,9 @@ def get_subnet_name(): salt '*' system.get_subnet_name """ - ret = __utils__["mac_utils.execute_return_result"]( - "systemsetup -getlocalsubnetname" - ) + ret = salt.utils.mac_utils.execute_return_result("systemsetup -getlocalsubnetname") - return __utils__["mac_utils.parse_return"](ret) + return salt.utils.mac_utils.parse_return(ret) def set_subnet_name(name): @@ -375,10 +374,10 @@ def set_subnet_name(name): The following will be set as 'Mikes-Mac' salt '*' system.set_subnet_name "Mike's Mac" """ - cmd = 'systemsetup -setlocalsubnetname "{}"'.format(name) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f'systemsetup -setlocalsubnetname "{name}"' + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( name, get_subnet_name, ) @@ -397,9 +396,9 @@ def get_startup_disk(): salt '*' system.get_startup_disk """ - ret = __utils__["mac_utils.execute_return_result"]("systemsetup -getstartupdisk") + ret = salt.utils.mac_utils.execute_return_result("systemsetup -getstartupdisk") - return __utils__["mac_utils.parse_return"](ret) + return salt.utils.mac_utils.parse_return(ret) def list_startup_disks(): @@ -415,7 +414,7 @@ def list_startup_disks(): salt '*' system.list_startup_disks """ - ret = __utils__["mac_utils.execute_return_result"]("systemsetup -liststartupdisks") + ret = salt.utils.mac_utils.execute_return_result("systemsetup -liststartupdisks") return ret.splitlines() @@ -445,10 +444,10 @@ def set_startup_disk(path): ) raise SaltInvocationError(msg) - cmd = "systemsetup -setstartupdisk {}".format(path) - __utils__["mac_utils.execute_return_result"](cmd) + cmd = f"systemsetup -setstartupdisk {path}" + salt.utils.mac_utils.execute_return_result(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( path, get_startup_disk, ) @@ -469,11 +468,11 @@ def get_restart_delay(): salt '*' system.get_restart_delay """ - ret = __utils__["mac_utils.execute_return_result"]( + ret = salt.utils.mac_utils.execute_return_result( "systemsetup -getwaitforstartupafterpowerfailure" ) - return __utils__["mac_utils.parse_return"](ret) + return salt.utils.mac_utils.parse_return(ret) def set_restart_delay(seconds): @@ -512,10 +511,10 @@ def set_restart_delay(seconds): ) raise SaltInvocationError(msg) - cmd = "systemsetup -setwaitforstartupafterpowerfailure {}".format(seconds) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f"systemsetup -setwaitforstartupafterpowerfailure {seconds}" + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( seconds, get_restart_delay, ) @@ -535,12 +534,12 @@ def get_disable_keyboard_on_lock(): salt '*' system.get_disable_keyboard_on_lock """ - ret = __utils__["mac_utils.execute_return_result"]( + ret = salt.utils.mac_utils.execute_return_result( "systemsetup -getdisablekeyboardwhenenclosurelockisengaged" ) - enabled = __utils__["mac_utils.validate_enabled"]( - __utils__["mac_utils.parse_return"](ret) + enabled = salt.utils.mac_utils.validate_enabled( + salt.utils.mac_utils.parse_return(ret) ) return enabled == "on" @@ -564,12 +563,12 @@ def set_disable_keyboard_on_lock(enable): salt '*' system.set_disable_keyboard_on_lock False """ - state = __utils__["mac_utils.validate_enabled"](enable) + state = salt.utils.mac_utils.validate_enabled(enable) - cmd = "systemsetup -setdisablekeyboardwhenenclosurelockisengaged {}".format(state) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f"systemsetup -setdisablekeyboardwhenenclosurelockisengaged {state}" + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( state, get_disable_keyboard_on_lock, normalize_ret=True, @@ -589,11 +588,11 @@ def get_boot_arch(): salt '*' system.get_boot_arch """ - ret = __utils__["mac_utils.execute_return_result"]( + ret = salt.utils.mac_utils.execute_return_result( "systemsetup -getkernelbootarchitecturesetting" ) - arch = __utils__["mac_utils.parse_return"](ret) + arch = salt.utils.mac_utils.parse_return(ret) if "default" in arch: return "default" @@ -639,10 +638,10 @@ def set_boot_arch(arch="default"): ) raise SaltInvocationError(msg) - cmd = "systemsetup -setkernelbootarchitecture {}".format(arch) - __utils__["mac_utils.execute_return_success"](cmd) + cmd = f"systemsetup -setkernelbootarchitecture {arch}" + salt.utils.mac_utils.execute_return_success(cmd) - return __utils__["mac_utils.confirm_updated"]( + return salt.utils.mac_utils.confirm_updated( arch, get_boot_arch, ) diff --git a/tests/integration/modules/test_mac_assistive.py b/tests/integration/modules/test_mac_assistive.py deleted file mode 100644 index 5c435def9807..000000000000 --- a/tests/integration/modules/test_mac_assistive.py +++ /dev/null @@ -1,89 +0,0 @@ -""" - :codeauthor: Nicole Thomas -""" - -import pytest - -from tests.support.case import ModuleCase - -OSA_SCRIPT = "/usr/bin/osascript" - - -@pytest.mark.destructive_test -@pytest.mark.skip_if_not_root -@pytest.mark.skip_initial_gh_actions_failure -@pytest.mark.skip_unless_on_darwin -class MacAssistiveTest(ModuleCase): - """ - Integration tests for the mac_assistive module. - """ - - def setUp(self): - """ - Sets up test requirements - """ - # Let's install a bundle to use in tests - self.run_function("assistive.install", [OSA_SCRIPT, True]) - - def tearDown(self): - """ - Clean up after tests - """ - # Delete any bundles that were installed - osa_script = self.run_function("assistive.installed", [OSA_SCRIPT]) - if osa_script: - self.run_function("assistive.remove", [OSA_SCRIPT]) - - smile_bundle = "com.smileonmymac.textexpander" - smile_bundle_present = self.run_function("assistive.installed", [smile_bundle]) - if smile_bundle_present: - self.run_function("assistive.remove", [smile_bundle]) - - @pytest.mark.slow_test - def test_install_and_remove(self): - """ - Tests installing and removing a bundled ID or command to use assistive access. - """ - new_bundle = "com.smileonmymac.textexpander" - self.assertTrue(self.run_function("assistive.install", [new_bundle])) - self.assertTrue(self.run_function("assistive.remove", [new_bundle])) - - @pytest.mark.slow_test - def test_installed(self): - """ - Tests the True and False return of assistive.installed. - """ - # OSA script should have been installed in setUp function - self.assertTrue(self.run_function("assistive.installed", [OSA_SCRIPT])) - # Clean up install - self.run_function("assistive.remove", [OSA_SCRIPT]) - # Installed should now return False - self.assertFalse(self.run_function("assistive.installed", [OSA_SCRIPT])) - - @pytest.mark.slow_test - def test_enable(self): - """ - Tests setting the enabled status of a bundled ID or command. - """ - # OSA script should have been installed and enabled in setUp function - # Now let's disable it, which should return True. - self.assertTrue(self.run_function("assistive.enable", [OSA_SCRIPT, False])) - # Double check the script was disabled, as intended. - self.assertFalse(self.run_function("assistive.enabled", [OSA_SCRIPT])) - # Now re-enable - self.assertTrue(self.run_function("assistive.enable", [OSA_SCRIPT])) - # Double check the script was enabled, as intended. - self.assertTrue(self.run_function("assistive.enabled", [OSA_SCRIPT])) - - @pytest.mark.slow_test - def test_enabled(self): - """ - Tests if a bundled ID or command is listed in assistive access returns True. - """ - # OSA script should have been installed in setUp function, which sets - # enabled to True by default. - self.assertTrue(self.run_function("assistive.enabled", [OSA_SCRIPT])) - # Disable OSA Script - self.run_function("assistive.enable", [OSA_SCRIPT, False]) - # Assert against new disabled status - self.assertFalse(self.run_function("assistive.enabled", [OSA_SCRIPT])) diff --git a/tests/integration/modules/test_mac_brew_pkg.py b/tests/integration/modules/test_mac_brew_pkg.py deleted file mode 100644 index 59d2dcde1dee..000000000000 --- a/tests/integration/modules/test_mac_brew_pkg.py +++ /dev/null @@ -1,188 +0,0 @@ -""" - :codeauthor: Nicole Thomas -""" - -import pytest - -from salt.exceptions import CommandExecutionError -from tests.support.case import ModuleCase - -# Brew doesn't support local package installation - So, let's -# Grab some small packages available online for brew -ADD_PKG = "algol68g" -DEL_PKG = "acme" - - -@pytest.mark.skip_if_not_root -@pytest.mark.destructive_test -@pytest.mark.skip_if_binaries_missing("brew") -@pytest.mark.skip_unless_on_darwin -class BrewModuleTest(ModuleCase): - """ - Integration tests for the brew module - """ - - @pytest.mark.slow_test - def test_brew_install(self): - """ - Tests the installation of packages - """ - try: - self.run_function("pkg.install", [ADD_PKG]) - pkg_list = self.run_function("pkg.list_pkgs") - try: - self.assertIn(ADD_PKG, pkg_list) - except AssertionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - except CommandExecutionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - - @pytest.mark.slow_test - def test_remove(self): - """ - Tests the removal of packages - """ - try: - # Install a package to delete - If unsuccessful, skip the test - self.run_function("pkg.install", [DEL_PKG]) - pkg_list = self.run_function("pkg.list_pkgs") - if DEL_PKG not in pkg_list: - self.run_function("pkg.install", [DEL_PKG]) - self.skipTest("Failed to install a package to delete") - - # Now remove the installed package - self.run_function("pkg.remove", [DEL_PKG]) - del_list = self.run_function("pkg.list_pkgs") - self.assertNotIn(DEL_PKG, del_list) - except CommandExecutionError: - self.run_function("pkg.remove", [DEL_PKG]) - raise - - @pytest.mark.slow_test - def test_version(self): - """ - Test pkg.version for mac. Installs a package and then checks we can get - a version for the installed package. - """ - try: - self.run_function("pkg.install", [ADD_PKG]) - pkg_list = self.run_function("pkg.list_pkgs") - version = self.run_function("pkg.version", [ADD_PKG]) - try: - self.assertTrue( - version, - msg="version: {} is empty, or other issue is present".format( - version - ), - ) - self.assertIn( - ADD_PKG, - pkg_list, - msg="package: {} is not in the list of installed packages: {}".format( - ADD_PKG, pkg_list - ), - ) - # make sure the version is accurate and is listed in the pkg_list - self.assertIn( - version, - str(pkg_list[ADD_PKG]), - msg="The {} version: {} is not listed in the pkg_list: {}".format( - ADD_PKG, version, pkg_list[ADD_PKG] - ), - ) - except AssertionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - except CommandExecutionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - - @pytest.mark.slow_test - def test_latest_version(self): - """ - Test pkg.latest_version: - - get the latest version available - - install the package - - get the latest version available - - check that the latest version is empty after installing it - """ - try: - self.run_function("pkg.remove", [ADD_PKG]) - uninstalled_latest = self.run_function("pkg.latest_version", [ADD_PKG]) - - self.run_function("pkg.install", [ADD_PKG]) - installed_latest = self.run_function("pkg.latest_version", [ADD_PKG]) - version = self.run_function("pkg.version", [ADD_PKG]) - try: - self.assertTrue(isinstance(uninstalled_latest, str)) - self.assertEqual(installed_latest, version) - except AssertionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - except CommandExecutionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - - @pytest.mark.slow_test - def test_refresh_db(self): - """ - Integration test to ensure pkg.refresh_db works with brew - """ - refresh_brew = self.run_function("pkg.refresh_db") - self.assertTrue(refresh_brew) - - @pytest.mark.slow_test - def test_list_upgrades(self): - """ - Test pkg.list_upgrades: data is in the form {'name1': 'version1', - 'name2': 'version2', ... } - """ - try: - upgrades = self.run_function("pkg.list_upgrades") - try: - self.assertTrue(isinstance(upgrades, dict)) - if upgrades: - for name in upgrades: - self.assertTrue(isinstance(name, str)) - self.assertTrue(isinstance(upgrades[name], str)) - except AssertionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - except CommandExecutionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - - @pytest.mark.slow_test - def test_info_installed(self): - """ - Test pkg.info_installed: info returned has certain fields used by - mac_brew.latest_version - """ - try: - self.run_function("pkg.install", [ADD_PKG]) - info = self.run_function("pkg.info_installed", [ADD_PKG]) - try: - self.assertTrue(ADD_PKG in info) - self.assertTrue("versions" in info[ADD_PKG]) - self.assertTrue("revision" in info[ADD_PKG]) - self.assertTrue("stable" in info[ADD_PKG]["versions"]) - except AssertionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - except CommandExecutionError: - self.run_function("pkg.remove", [ADD_PKG]) - raise - - def tearDown(self): - """ - Clean up after tests - """ - pkg_list = self.run_function("pkg.list_pkgs") - - # Remove any installed packages - if ADD_PKG in pkg_list: - self.run_function("pkg.remove", [ADD_PKG]) - if DEL_PKG in pkg_list: - self.run_function("pkg.remove", [DEL_PKG]) diff --git a/tests/integration/modules/test_mac_desktop.py b/tests/integration/modules/test_mac_desktop.py deleted file mode 100644 index 73f12c18e539..000000000000 --- a/tests/integration/modules/test_mac_desktop.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Integration tests for the mac_desktop execution module. -""" - -import pytest - -from tests.support.case import ModuleCase - - -@pytest.mark.destructive_test -@pytest.mark.skip_if_not_root -@pytest.mark.skip_unless_on_darwin -class MacDesktopTestCase(ModuleCase): - """ - Integration tests for the mac_desktop module. - """ - - def test_get_output_volume(self): - """ - Tests the return of get_output_volume. - """ - ret = self.run_function("desktop.get_output_volume") - self.assertIsNotNone(ret) - - @pytest.mark.slow_test - def test_set_output_volume(self): - """ - Tests the return of set_output_volume. - """ - current_vol = self.run_function("desktop.get_output_volume") - to_set = 10 - if current_vol == str(to_set): - to_set += 2 - new_vol = self.run_function("desktop.set_output_volume", [str(to_set)]) - check_vol = self.run_function("desktop.get_output_volume") - self.assertEqual(new_vol, check_vol) - - # Set volume back to what it was before - self.run_function("desktop.set_output_volume", [current_vol]) - - def test_screensaver(self): - """ - Tests the return of the screensaver function. - """ - self.assertTrue(self.run_function("desktop.screensaver")) - - def test_lock(self): - """ - Tests the return of the lock function. - """ - self.assertTrue(self.run_function("desktop.lock")) - - @pytest.mark.slow_test - def test_say(self): - """ - Tests the return of the say function. - """ - self.assertTrue(self.run_function("desktop.say", ["hello", "world"])) diff --git a/tests/integration/modules/test_mac_group.py b/tests/integration/modules/test_mac_group.py deleted file mode 100644 index 46be79667f85..000000000000 --- a/tests/integration/modules/test_mac_group.py +++ /dev/null @@ -1,177 +0,0 @@ -""" - :codeauthor: Nicole Thomas -""" - -import pytest -from saltfactories.utils import random_string - -from salt.exceptions import CommandExecutionError -from tests.support.case import ModuleCase - -# Create group name strings for tests -ADD_GROUP = random_string("RS-", lowercase=False) -DEL_GROUP = random_string("RS-", lowercase=False) -CHANGE_GROUP = random_string("RS-", lowercase=False) -ADD_USER = random_string("RS-", lowercase=False) -REP_USER_GROUP = random_string("RS-", lowercase=False) - - -@pytest.mark.skip_if_not_root -@pytest.mark.destructive_test -@pytest.mark.skip_unless_on_darwin -class MacGroupModuleTest(ModuleCase): - """ - Integration tests for the mac_group module - """ - - def setUp(self): - """ - Sets up test requirements - """ - os_grain = self.run_function("grains.item", ["kernel"]) - if os_grain["kernel"] not in "Darwin": - self.skipTest("Test not applicable to '{kernel}' kernel".format(**os_grain)) - - @pytest.mark.slow_test - def test_mac_group_add(self): - """ - Tests the add group function - """ - try: - self.run_function("group.add", [ADD_GROUP, 3456]) - group_info = self.run_function("group.info", [ADD_GROUP]) - self.assertEqual(group_info["name"], ADD_GROUP) - except CommandExecutionError: - self.run_function("group.delete", [ADD_GROUP]) - raise - - @pytest.mark.slow_test - def test_mac_group_delete(self): - """ - Tests the delete group function - """ - # Create a group to delete - If unsuccessful, skip the test - if self.run_function("group.add", [DEL_GROUP, 4567]) is not True: - self.run_function("group.delete", [DEL_GROUP]) - self.skipTest("Failed to create a group to delete") - - # Now try to delete the added group - ret = self.run_function("group.delete", [DEL_GROUP]) - self.assertTrue(ret) - - @pytest.mark.slow_test - def test_mac_group_chgid(self): - """ - Tests changing the group id - """ - # Create a group to delete - If unsuccessful, skip the test - if self.run_function("group.add", [CHANGE_GROUP, 5678]) is not True: - self.run_function("group.delete", [CHANGE_GROUP]) - self.skipTest("Failed to create a group to manipulate") - - try: - self.run_function("group.chgid", [CHANGE_GROUP, 6789]) - group_info = self.run_function("group.info", [CHANGE_GROUP]) - self.assertEqual(group_info["gid"], 6789) - except AssertionError: - self.run_function("group.delete", [CHANGE_GROUP]) - raise - - @pytest.mark.slow_test - def test_mac_adduser(self): - """ - Tests adding user to the group - """ - # Create a group to use for test - If unsuccessful, skip the test - if self.run_function("group.add", [ADD_GROUP, 5678]) is not True: - self.run_function("group.delete", [ADD_GROUP]) - self.skipTest("Failed to create a group to manipulate") - - try: - self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) - group_info = self.run_function("group.info", [ADD_GROUP]) - self.assertEqual(ADD_USER, "".join(group_info["members"])) - except AssertionError: - self.run_function("group.delete", [ADD_GROUP]) - raise - - @pytest.mark.slow_test - def test_mac_deluser(self): - """ - Test deleting user from a group - """ - # Create a group to use for test - If unsuccessful, skip the test - if ( - self.run_function("group.add", [ADD_GROUP, 5678]) - and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True - ): - self.run_function("group.delete", [ADD_GROUP]) - self.skipTest("Failed to create a group to manipulate") - - delusr = self.run_function("group.deluser", [ADD_GROUP, ADD_USER]) - self.assertTrue(delusr) - - group_info = self.run_function("group.info", [ADD_GROUP]) - self.assertNotIn(ADD_USER, "".join(group_info["members"])) - - @pytest.mark.slow_test - def test_mac_members(self): - """ - Test replacing members of a group - """ - if ( - self.run_function("group.add", [ADD_GROUP, 5678]) - and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True - ): - self.run_function("group.delete", [ADD_GROUP]) - self.skipTest( - "Failed to create the {} group or add user {} to group " - "to manipulate".format(ADD_GROUP, ADD_USER) - ) - - rep_group_mem = self.run_function("group.members", [ADD_GROUP, REP_USER_GROUP]) - self.assertTrue(rep_group_mem) - - # ensure new user is added to group and previous user is removed - group_info = self.run_function("group.info", [ADD_GROUP]) - self.assertIn(REP_USER_GROUP, str(group_info["members"])) - self.assertNotIn(ADD_USER, str(group_info["members"])) - - @pytest.mark.slow_test - def test_mac_getent(self): - """ - Test returning info on all groups - """ - if ( - self.run_function("group.add", [ADD_GROUP, 5678]) - and self.run_function("group.adduser", [ADD_GROUP, ADD_USER]) is not True - ): - self.run_function("group.delete", [ADD_GROUP]) - self.skipTest( - "Failed to create the {} group or add user {} to group " - "to manipulate".format(ADD_GROUP, ADD_USER) - ) - - getinfo = self.run_function("group.getent") - self.assertTrue(getinfo) - self.assertIn(ADD_GROUP, str(getinfo)) - self.assertIn(ADD_USER, str(getinfo)) - - def tearDown(self): - """ - Clean up after tests - """ - # Delete ADD_GROUP - add_info = self.run_function("group.info", [ADD_GROUP]) - if add_info: - self.run_function("group.delete", [ADD_GROUP]) - - # Delete DEL_GROUP if something failed - del_info = self.run_function("group.info", [DEL_GROUP]) - if del_info: - self.run_function("group.delete", [DEL_GROUP]) - - # Delete CHANGE_GROUP - change_info = self.run_function("group.info", [CHANGE_GROUP]) - if change_info: - self.run_function("group.delete", [CHANGE_GROUP]) diff --git a/tests/integration/modules/test_mac_keychain.py b/tests/integration/modules/test_mac_keychain.py deleted file mode 100644 index afd195524f36..000000000000 --- a/tests/integration/modules/test_mac_keychain.py +++ /dev/null @@ -1,106 +0,0 @@ -""" -Validate the mac-keychain module -""" - -import os - -import pytest - -from salt.exceptions import CommandExecutionError -from tests.support.case import ModuleCase -from tests.support.runtests import RUNTIME_VARS - - -@pytest.mark.destructive_test -@pytest.mark.skip_if_not_root -@pytest.mark.skip_unless_on_darwin -class MacKeychainModuleTest(ModuleCase): - """ - Integration tests for the mac_keychain module - """ - - @classmethod - def setUpClass(cls): - cls.cert = os.path.join( - RUNTIME_VARS.FILES, "file", "base", "certs", "salttest.p12" - ) - cls.cert_alias = "Salt Test" - cls.passwd = "salttest" - - def tearDown(self): - """ - Clean up after tests - """ - # Remove the salttest cert, if left over. - certs_list = self.run_function("keychain.list_certs") - if self.cert_alias in certs_list: - self.run_function("keychain.uninstall", [self.cert_alias]) - - @pytest.mark.slow_test - def test_mac_keychain_install(self): - """ - Tests that attempts to install a certificate - """ - install_cert = self.run_function("keychain.install", [self.cert, self.passwd]) - self.assertTrue(install_cert) - - # check to ensure the cert was installed - certs_list = self.run_function("keychain.list_certs") - self.assertIn(self.cert_alias, certs_list) - - @pytest.mark.slow_test - def test_mac_keychain_uninstall(self): - """ - Tests that attempts to uninstall a certificate - """ - self.run_function("keychain.install", [self.cert, self.passwd]) - certs_list = self.run_function("keychain.list_certs") - - if self.cert_alias not in certs_list: - self.run_function("keychain.uninstall", [self.cert_alias]) - self.skipTest("Failed to install keychain") - - # uninstall cert - self.run_function("keychain.uninstall", [self.cert_alias]) - certs_list = self.run_function("keychain.list_certs") - - # check to ensure the cert was uninstalled - try: - self.assertNotIn(self.cert_alias, str(certs_list)) - except CommandExecutionError: - self.run_function("keychain.uninstall", [self.cert_alias]) - - @pytest.mark.slow_test - def test_mac_keychain_get_friendly_name(self): - """ - Test that attempts to get friendly name of a cert - """ - self.run_function("keychain.install", [self.cert, self.passwd]) - certs_list = self.run_function("keychain.list_certs") - if self.cert_alias not in certs_list: - self.run_function("keychain.uninstall", [self.cert_alias]) - self.skipTest("Failed to install keychain") - - get_name = self.run_function( - "keychain.get_friendly_name", [self.cert, self.passwd] - ) - self.assertEqual(get_name, self.cert_alias) - - @pytest.mark.slow_test - def test_mac_keychain_get_default_keychain(self): - """ - Test that attempts to get the default keychain - """ - salt_get_keychain = self.run_function("keychain.get_default_keychain") - sys_get_keychain = self.run_function( - "cmd.run", ["security default-keychain -d user"] - ) - self.assertEqual(salt_get_keychain, sys_get_keychain) - - def test_mac_keychain_list_certs(self): - """ - Test that attempts to list certs - """ - cert_default = "com.apple.systemdefault" - certs = self.run_function("keychain.list_certs") - self.assertIn(cert_default, certs) diff --git a/tests/integration/modules/test_mac_portspkg.py b/tests/integration/modules/test_mac_portspkg.py deleted file mode 100644 index 35ebe3735b6e..000000000000 --- a/tests/integration/modules/test_mac_portspkg.py +++ /dev/null @@ -1,104 +0,0 @@ -""" -integration tests for mac_ports -""" - -import pytest - -from tests.support.case import ModuleCase - - -@pytest.mark.skip_if_not_root -@pytest.mark.skip_if_binaries_missing("port") -@pytest.mark.skip_unless_on_darwin -class MacPortsModuleTest(ModuleCase): - """ - Validate the mac_ports module - """ - - AGREE_INSTALLED = False - - def setUp(self): - """ - Get current settings - """ - self.AGREE_INSTALLED = "agree" in self.run_function("pkg.list_pkgs") - self.run_function("pkg.refresh_db") - - def tearDown(self): - """ - Reset to original settings - """ - if not self.AGREE_INSTALLED: - self.run_function("pkg.remove", ["agree"]) - - @pytest.mark.destructive_test - def test_list_pkgs(self): - """ - Test pkg.list_pkgs - """ - self.run_function("pkg.install", ["agree"]) - self.assertIsInstance(self.run_function("pkg.list_pkgs"), dict) - self.assertIn("agree", self.run_function("pkg.list_pkgs")) - - @pytest.mark.destructive_test - def test_latest_version(self): - """ - Test pkg.latest_version - """ - self.run_function("pkg.install", ["agree"]) - result = self.run_function("pkg.latest_version", ["agree"], refresh=False) - self.assertIsInstance(result, dict) - self.assertIn("agree", result) - - @pytest.mark.destructive_test - def test_remove(self): - """ - Test pkg.remove - """ - self.run_function("pkg.install", ["agree"]) - removed = self.run_function("pkg.remove", ["agree"]) - self.assertIsInstance(removed, dict) - self.assertIn("agree", removed) - - @pytest.mark.destructive_test - def test_install(self): - """ - Test pkg.install - """ - self.run_function("pkg.remove", ["agree"]) - installed = self.run_function("pkg.install", ["agree"]) - self.assertIsInstance(installed, dict) - self.assertIn("agree", installed) - - def test_list_upgrades(self): - """ - Test pkg.list_upgrades - """ - self.assertIsInstance( - self.run_function("pkg.list_upgrades", refresh=False), dict - ) - - @pytest.mark.destructive_test - def test_upgrade_available(self): - """ - Test pkg.upgrade_available - """ - self.run_function("pkg.install", ["agree"]) - self.assertFalse( - self.run_function("pkg.upgrade_available", ["agree"], refresh=False) - ) - - def test_refresh_db(self): - """ - Test pkg.refresh_db - """ - self.assertTrue(self.run_function("pkg.refresh_db")) - - @pytest.mark.destructive_test - def test_upgrade(self): - """ - Test pkg.upgrade - """ - results = self.run_function("pkg.upgrade", refresh=False) - self.assertIsInstance(results, dict) - self.assertTrue(results["result"]) diff --git a/tests/integration/modules/test_mac_power.py b/tests/integration/modules/test_mac_power.py deleted file mode 100644 index aa0de2667077..000000000000 --- a/tests/integration/modules/test_mac_power.py +++ /dev/null @@ -1,345 +0,0 @@ -""" -integration tests for mac_power -""" - -import pytest - -from tests.support.case import ModuleCase - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -@pytest.mark.slow_test -class MacPowerModuleTest(ModuleCase): - """ - Validate the mac_power module - """ - - def setUp(self): - """ - Get current settings - """ - # Get current settings - self.COMPUTER_SLEEP = self.run_function("power.get_computer_sleep") - self.DISPLAY_SLEEP = self.run_function("power.get_display_sleep") - self.HARD_DISK_SLEEP = self.run_function("power.get_harddisk_sleep") - - def tearDown(self): - """ - Reset to original settings - """ - self.run_function("power.set_computer_sleep", [self.COMPUTER_SLEEP]) - self.run_function("power.set_display_sleep", [self.DISPLAY_SLEEP]) - self.run_function("power.set_harddisk_sleep", [self.HARD_DISK_SLEEP]) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_computer_sleep(self): - """ - Test power.get_computer_sleep - Test power.set_computer_sleep - """ - - # Normal Functionality - self.assertTrue(self.run_function("power.set_computer_sleep", [90])) - self.assertEqual( - self.run_function("power.get_computer_sleep"), "after 90 minutes" - ) - self.assertTrue(self.run_function("power.set_computer_sleep", ["Off"])) - self.assertEqual(self.run_function("power.get_computer_sleep"), "Never") - - # Test invalid input - self.assertIn( - "Invalid String Value for Minutes", - self.run_function("power.set_computer_sleep", ["spongebob"]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_computer_sleep", [0]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_computer_sleep", [181]), - ) - self.assertIn( - "Invalid Boolean Value for Minutes", - self.run_function("power.set_computer_sleep", [True]), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_display_sleep(self): - """ - Test power.get_display_sleep - Test power.set_display_sleep - """ - - # Normal Functionality - self.assertTrue(self.run_function("power.set_display_sleep", [90])) - self.assertEqual( - self.run_function("power.get_display_sleep"), "after 90 minutes" - ) - self.assertTrue(self.run_function("power.set_display_sleep", ["Off"])) - self.assertEqual(self.run_function("power.get_display_sleep"), "Never") - - # Test invalid input - self.assertIn( - "Invalid String Value for Minutes", - self.run_function("power.set_display_sleep", ["spongebob"]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_display_sleep", [0]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_display_sleep", [181]), - ) - self.assertIn( - "Invalid Boolean Value for Minutes", - self.run_function("power.set_display_sleep", [True]), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_harddisk_sleep(self): - """ - Test power.get_harddisk_sleep - Test power.set_harddisk_sleep - """ - - # Normal Functionality - self.assertTrue(self.run_function("power.set_harddisk_sleep", [90])) - self.assertEqual( - self.run_function("power.get_harddisk_sleep"), "after 90 minutes" - ) - self.assertTrue(self.run_function("power.set_harddisk_sleep", ["Off"])) - self.assertEqual(self.run_function("power.get_harddisk_sleep"), "Never") - - # Test invalid input - self.assertIn( - "Invalid String Value for Minutes", - self.run_function("power.set_harddisk_sleep", ["spongebob"]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_harddisk_sleep", [0]), - ) - self.assertIn( - "Invalid Integer Value for Minutes", - self.run_function("power.set_harddisk_sleep", [181]), - ) - self.assertIn( - "Invalid Boolean Value for Minutes", - self.run_function("power.set_harddisk_sleep", [True]), - ) - - @pytest.mark.slow_test - def test_restart_freeze(self): - """ - Test power.get_restart_freeze - Test power.set_restart_freeze - """ - # Normal Functionality - self.assertTrue(self.run_function("power.set_restart_freeze", ["on"])) - self.assertTrue(self.run_function("power.get_restart_freeze")) - # This will return False because mac fails to actually make the change - self.assertFalse(self.run_function("power.set_restart_freeze", ["off"])) - # Even setting to off returns true, it actually is never set - # This is an apple bug - self.assertTrue(self.run_function("power.get_restart_freeze")) - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -class MacPowerModuleTestSleepOnPowerButton(ModuleCase): - """ - Test power.get_sleep_on_power_button - Test power.set_sleep_on_power_button - """ - - SLEEP_ON_BUTTON = None - - def setUp(self): - """ - Check if function is available - Get existing value - """ - # Is the function available - ret = self.run_function("power.get_sleep_on_power_button") - if isinstance(ret, bool): - self.SLEEP_ON_BUTTON = self.run_function("power.get_sleep_on_power_button") - - def tearDown(self): - """ - Reset to original value - """ - if self.SLEEP_ON_BUTTON is not None: - self.run_function("power.set_sleep_on_power_button", [self.SLEEP_ON_BUTTON]) - - @pytest.mark.slow_test - def test_sleep_on_power_button(self): - """ - Test power.get_sleep_on_power_button - Test power.set_sleep_on_power_button - """ - # If available on this system, test it - if self.SLEEP_ON_BUTTON is None: - # Check for not available - ret = self.run_function("power.get_sleep_on_power_button") - self.assertIn("Error", ret) - else: - self.assertTrue( - self.run_function("power.set_sleep_on_power_button", ["on"]) - ) - self.assertTrue(self.run_function("power.get_sleep_on_power_button")) - self.assertTrue( - self.run_function("power.set_sleep_on_power_button", ["off"]) - ) - self.assertFalse(self.run_function("power.get_sleep_on_power_button")) - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -class MacPowerModuleTestRestartPowerFailure(ModuleCase): - """ - Test power.get_restart_power_failure - Test power.set_restart_power_failure - """ - - RESTART_POWER = None - - def setUp(self): - """ - Check if function is available - Get existing value - """ - # Is the function available - ret = self.run_function("power.get_restart_power_failure") - if isinstance(ret, bool): - self.RESTART_POWER = ret - - def tearDown(self): - """ - Reset to original value - """ - if self.RESTART_POWER is not None: - self.run_function("power.set_sleep_on_power_button", [self.RESTART_POWER]) - - def test_restart_power_failure(self): - """ - Test power.get_restart_power_failure - Test power.set_restart_power_failure - """ - # If available on this system, test it - if self.RESTART_POWER is None: - # Check for not available - ret = self.run_function("power.get_restart_power_failure") - self.assertIn("Error", ret) - else: - self.assertTrue( - self.run_function("power.set_restart_power_failure", ["on"]) - ) - self.assertTrue(self.run_function("power.get_restart_power_failure")) - self.assertTrue( - self.run_function("power.set_restart_power_failure", ["off"]) - ) - self.assertFalse(self.run_function("power.get_restart_power_failure")) - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -class MacPowerModuleTestWakeOnNet(ModuleCase): - """ - Test power.get_wake_on_network - Test power.set_wake_on_network - """ - - WAKE_ON_NET = None - - def setUp(self): - """ - Check if function is available - Get existing value - """ - # Is the function available - ret = self.run_function("power.get_wake_on_network") - if isinstance(ret, bool): - self.WAKE_ON_NET = ret - - def tearDown(self): - """ - Reset to original value - """ - if self.WAKE_ON_NET is not None: - self.run_function("power.set_wake_on_network", [self.WAKE_ON_NET]) - - def test_wake_on_network(self): - """ - Test power.get_wake_on_network - Test power.set_wake_on_network - """ - # If available on this system, test it - if self.WAKE_ON_NET is None: - # Check for not available - ret = self.run_function("power.get_wake_on_network") - self.assertIn("Error", ret) - else: - self.assertTrue(self.run_function("power.set_wake_on_network", ["on"])) - self.assertTrue(self.run_function("power.get_wake_on_network")) - self.assertTrue(self.run_function("power.set_wake_on_network", ["off"])) - self.assertFalse(self.run_function("power.get_wake_on_network")) - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -class MacPowerModuleTestWakeOnModem(ModuleCase): - """ - Test power.get_wake_on_modem - Test power.set_wake_on_modem - """ - - WAKE_ON_MODEM = None - - def setUp(self): - """ - Check if function is available - Get existing value - """ - # Is the function available - ret = self.run_function("power.get_wake_on_modem") - if isinstance(ret, bool): - self.WAKE_ON_MODEM = ret - - def tearDown(self): - """ - Reset to original value - """ - if self.WAKE_ON_MODEM is not None: - self.run_function("power.set_wake_on_modem", [self.WAKE_ON_MODEM]) - - def test_wake_on_modem(self): - """ - Test power.get_wake_on_modem - Test power.set_wake_on_modem - """ - # If available on this system, test it - if self.WAKE_ON_MODEM is None: - # Check for not available - ret = self.run_function("power.get_wake_on_modem") - self.assertIn("Error", ret) - else: - self.assertTrue(self.run_function("power.set_wake_on_modem", ["on"])) - self.assertTrue(self.run_function("power.get_wake_on_modem")) - self.assertTrue(self.run_function("power.set_wake_on_modem", ["off"])) - self.assertFalse(self.run_function("power.get_wake_on_modem")) diff --git a/tests/integration/modules/test_mac_service.py b/tests/integration/modules/test_mac_service.py deleted file mode 100644 index d4022cab535e..000000000000 --- a/tests/integration/modules/test_mac_service.py +++ /dev/null @@ -1,233 +0,0 @@ -""" -integration tests for mac_service -""" - -import plistlib - -import pytest - -import salt.utils.files -from tests.support.case import ModuleCase - - -@pytest.mark.skip_if_not_root -@pytest.mark.skip_if_binaries_missing("launchctl", "plutil") -@pytest.mark.skip_unless_on_darwin -class MacServiceModuleTest(ModuleCase): - """ - Validate the mac_service module - """ - - SERVICE_NAME = "com.salt.integration.test" - SERVICE_PATH = "/Library/LaunchDaemons/com.salt.integration.test.plist" - - def setUp(self): - """ - setup our test launch service. - """ - service_data = { - "KeepAlive": True, - "Label": self.SERVICE_NAME, - "ProgramArguments": ["/bin/sleep", "1000"], - "RunAtLoad": True, - } - with salt.utils.files.fopen(self.SERVICE_PATH, "wb") as fp: - plistlib.dump(service_data, fp) - self.run_function("service.enable", [self.SERVICE_NAME]) - self.run_function("service.start", [self.SERVICE_NAME]) - - def tearDown(self): - """ - stop and remove our test service. - """ - self.run_function("service.stop", [self.SERVICE_NAME]) - salt.utils.files.safe_rm(self.SERVICE_PATH) - - @pytest.mark.slow_test - def test_show(self): - """ - Test service.show - """ - # Existing Service - service_info = self.run_function("service.show", [self.SERVICE_NAME]) - self.assertIsInstance(service_info, dict) - self.assertEqual(service_info["plist"]["Label"], self.SERVICE_NAME) - - # Missing Service - self.assertIn( - "Service not found", self.run_function("service.show", ["spongebob"]) - ) - - @pytest.mark.slow_test - def test_launchctl(self): - """ - Test service.launchctl - """ - # Expected Functionality - self.assertTrue( - self.run_function("service.launchctl", ["error", "bootstrap", 64]) - ) - self.assertEqual( - self.run_function( - "service.launchctl", ["error", "bootstrap", 64], return_stdout=True - ), - "64: unknown error code", - ) - - # Raise an error - self.assertIn( - "Failed to error service", - self.run_function("service.launchctl", ["error", "bootstrap"]), - ) - - @pytest.mark.slow_test - def test_list(self): - """ - Test service.list - """ - # Expected Functionality - self.assertIn("PID", self.run_function("service.list")) - self.assertIn("{", self.run_function("service.list", [self.SERVICE_NAME])) - - # Service not found - self.assertIn( - "Service not found", self.run_function("service.list", ["spongebob"]) - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_enable(self): - """ - Test service.enable - """ - self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME])) - - self.assertIn( - "Service not found", self.run_function("service.enable", ["spongebob"]) - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_disable(self): - """ - Test service.disable - """ - self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME])) - - self.assertIn( - "Service not found", self.run_function("service.disable", ["spongebob"]) - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_start(self): - """ - Test service.start - Test service.stop - Test service.status - """ - self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME])) - - self.assertIn( - "Service not found", self.run_function("service.start", ["spongebob"]) - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_stop(self): - """ - Test service.stop - """ - self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME])) - - self.assertIn( - "Service not found", self.run_function("service.stop", ["spongebob"]) - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_status(self): - """ - Test service.status - """ - # A running service - self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.status", [self.SERVICE_NAME])) - - # A stopped service - self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME])) - self.assertFalse(self.run_function("service.status", [self.SERVICE_NAME])) - - # Service not found - self.assertFalse(self.run_function("service.status", ["spongebob"])) - - @pytest.mark.slow_test - def test_available(self): - """ - Test service.available - """ - self.assertTrue(self.run_function("service.available", [self.SERVICE_NAME])) - self.assertFalse(self.run_function("service.available", ["spongebob"])) - - @pytest.mark.slow_test - def test_missing(self): - """ - Test service.missing - """ - self.assertFalse(self.run_function("service.missing", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.missing", ["spongebob"])) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_enabled(self): - """ - Test service.enabled - """ - self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME])) - - self.assertTrue(self.run_function("service.enabled", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.stop", [self.SERVICE_NAME])) - - self.assertTrue(self.run_function("service.enabled", ["spongebob"])) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_disabled(self): - """ - Test service.disabled - """ - self.assertTrue(self.run_function("service.start", [self.SERVICE_NAME])) - self.assertFalse(self.run_function("service.disabled", [self.SERVICE_NAME])) - - self.assertTrue(self.run_function("service.disable", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.disabled", [self.SERVICE_NAME])) - self.assertTrue(self.run_function("service.enable", [self.SERVICE_NAME])) - self.assertIn( - "Service not found", self.run_function("service.stop", ["spongebob"]) - ) - - @pytest.mark.slow_test - def test_get_all(self): - """ - Test service.get_all - """ - services = self.run_function("service.get_all") - self.assertIsInstance(services, list) - self.assertIn(self.SERVICE_NAME, services) - - @pytest.mark.slow_test - def test_get_enabled(self): - """ - Test service.get_enabled - """ - services = self.run_function("service.get_enabled") - self.assertIsInstance(services, list) - self.assertIn(self.SERVICE_NAME, services) - - @pytest.mark.slow_test - def test_service_laoded(self): - """ - Test service.get_enabled - """ - self.assertTrue(self.run_function("service.loaded", [self.SERVICE_NAME])) diff --git a/tests/integration/modules/test_mac_shadow.py b/tests/integration/modules/test_mac_shadow.py deleted file mode 100644 index bb859ffbf099..000000000000 --- a/tests/integration/modules/test_mac_shadow.py +++ /dev/null @@ -1,226 +0,0 @@ -""" -integration tests for mac_shadow -""" - -import datetime - -import pytest -from saltfactories.utils import random_string - -from tests.support.case import ModuleCase - -TEST_USER = random_string("RS-", lowercase=False) -NO_USER = random_string("RS-", lowercase=False) - - -@pytest.mark.skip_if_binaries_missing("dscl", "pwpolicy") -@pytest.mark.skip_if_not_root -@pytest.mark.skip_unless_on_darwin -class MacShadowModuleTest(ModuleCase): - """ - Validate the mac_shadow module - """ - - def setUp(self): - """ - Get current settings - """ - self.run_function("user.add", [TEST_USER]) - - def tearDown(self): - """ - Reset to original settings - """ - self.run_function("user.delete", [TEST_USER]) - - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_info(self): - """ - Test shadow.info - """ - # Correct Functionality - ret = self.run_function("shadow.info", [TEST_USER]) - self.assertEqual(ret["name"], TEST_USER) - - # User does not exist - ret = self.run_function("shadow.info", [NO_USER]) - self.assertEqual(ret["name"], "") - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_account_created(self): - """ - Test shadow.get_account_created - """ - # Correct Functionality - text_date = self.run_function("shadow.get_account_created", [TEST_USER]) - self.assertNotEqual(text_date, "Invalid Timestamp") - obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S") - self.assertIsInstance(obj_date, datetime.date) - - # User does not exist - self.assertEqual( - self.run_function("shadow.get_account_created", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_get_last_change(self): - """ - Test shadow.get_last_change - """ - # Correct Functionality - text_date = self.run_function("shadow.get_last_change", [TEST_USER]) - self.assertNotEqual(text_date, "Invalid Timestamp") - obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S") - self.assertIsInstance(obj_date, datetime.date) - - # User does not exist - self.assertEqual( - self.run_function("shadow.get_last_change", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_get_login_failed_last(self): - """ - Test shadow.get_login_failed_last - """ - # Correct Functionality - text_date = self.run_function("shadow.get_login_failed_last", [TEST_USER]) - self.assertNotEqual(text_date, "Invalid Timestamp") - obj_date = datetime.datetime.strptime(text_date, "%Y-%m-%d %H:%M:%S") - self.assertIsInstance(obj_date, datetime.date) - - # User does not exist - self.assertEqual( - self.run_function("shadow.get_login_failed_last", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_get_login_failed_count(self): - """ - Test shadow.get_login_failed_count - """ - # Correct Functionality - self.assertEqual( - self.run_function("shadow.get_login_failed_count", [TEST_USER]), "0" - ) - - # User does not exist - self.assertEqual( - self.run_function("shadow.get_login_failed_count", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_maxdays(self): - """ - Test shadow.get_maxdays - Test shadow.set_maxdays - """ - # Correct Functionality - self.assertTrue(self.run_function("shadow.set_maxdays", [TEST_USER, 20])) - self.assertEqual(self.run_function("shadow.get_maxdays", [TEST_USER]), 20) - - # User does not exist - self.assertEqual( - self.run_function("shadow.set_maxdays", [NO_USER, 7]), - "ERROR: User not found: {}".format(NO_USER), - ) - self.assertEqual( - self.run_function("shadow.get_maxdays", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_change(self): - """ - Test shadow.get_change - Test shadow.set_change - """ - # Correct Functionality - self.assertTrue( - self.run_function("shadow.set_change", [TEST_USER, "02/11/2011"]) - ) - self.assertEqual( - self.run_function("shadow.get_change", [TEST_USER]), "02/11/2011" - ) - - # User does not exist - self.assertEqual( - self.run_function("shadow.set_change", [NO_USER, "02/11/2012"]), - "ERROR: User not found: {}".format(NO_USER), - ) - self.assertEqual( - self.run_function("shadow.get_change", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_expire(self): - """ - Test shadow.get_expire - Test shadow.set_expire - """ - # Correct Functionality - self.assertTrue( - self.run_function("shadow.set_expire", [TEST_USER, "02/11/2011"]) - ) - self.assertEqual( - self.run_function("shadow.get_expire", [TEST_USER]), "02/11/2011" - ) - - # User does not exist - self.assertEqual( - self.run_function("shadow.set_expire", [NO_USER, "02/11/2012"]), - "ERROR: User not found: {}".format(NO_USER), - ) - self.assertEqual( - self.run_function("shadow.get_expire", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_del_password(self): - """ - Test shadow.del_password - """ - # Correct Functionality - self.assertTrue(self.run_function("shadow.del_password", [TEST_USER])) - self.assertEqual(self.run_function("shadow.info", [TEST_USER])["passwd"], "*") - - # User does not exist - self.assertEqual( - self.run_function("shadow.del_password", [NO_USER]), - "ERROR: User not found: {}".format(NO_USER), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_set_password(self): - """ - Test shadow.set_password - """ - # Correct Functionality - self.assertTrue( - self.run_function("shadow.set_password", [TEST_USER, "Pa$$W0rd"]) - ) - - # User does not exist - self.assertEqual( - self.run_function("shadow.set_password", [NO_USER, "P@SSw0rd"]), - "ERROR: User not found: {}".format(NO_USER), - ) diff --git a/tests/integration/modules/test_mac_softwareupdate.py b/tests/integration/modules/test_mac_softwareupdate.py deleted file mode 100644 index a8094969c358..000000000000 --- a/tests/integration/modules/test_mac_softwareupdate.py +++ /dev/null @@ -1,170 +0,0 @@ -""" -integration tests for mac_softwareupdate -""" - -import pytest - -from tests.support.case import ModuleCase - - -@pytest.mark.skip_if_not_root -@pytest.mark.skip_if_binaries_missing("softwareupdate") -@pytest.mark.skip_unless_on_darwin -class MacSoftwareUpdateModuleTest(ModuleCase): - """ - Validate the mac_softwareupdate module - """ - - IGNORED_LIST = [] - SCHEDULE = False - CATALOG = "" - - def setUp(self): - """ - Get current settings - """ - self.IGNORED_LIST = self.run_function("softwareupdate.list_ignored") - self.SCHEDULE = self.run_function("softwareupdate.schedule") - self.CATALOG = self.run_function("softwareupdate.get_catalog") - - super().setUp() - - def tearDown(self): - """ - Reset to original settings - """ - if self.IGNORED_LIST: - for item in self.IGNORED_LIST: - self.run_function("softwareupdate.ignore", [item]) - else: - self.run_function("softwareupdate.reset_ignored") - - self.run_function("softwareupdate.schedule", [self.SCHEDULE]) - - if self.CATALOG == "Default": - self.run_function("softwareupdate.reset_catalog") - else: - self.run_function("softwareupdate.set_catalog", [self.CATALOG]) - - super().tearDown() - - @pytest.mark.slow_test - def test_list_available(self): - """ - Test softwareupdate.list_available - """ - # Can't predict what will be returned, so can only test that the return - # is the correct type, dict - self.assertIsInstance(self.run_function("softwareupdate.list_available"), dict) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_ignore(self): - """ - Test softwareupdate.ignore - Test softwareupdate.list_ignored - Test softwareupdate.reset_ignored - """ - # Test reset_ignored - self.assertTrue(self.run_function("softwareupdate.reset_ignored")) - self.assertEqual(self.run_function("softwareupdate.list_ignored"), []) - - # Test ignore - self.assertTrue(self.run_function("softwareupdate.ignore", ["spongebob"])) - self.assertTrue(self.run_function("softwareupdate.ignore", ["squidward"])) - - # Test list_ignored and verify ignore - self.assertIn("spongebob", self.run_function("softwareupdate.list_ignored")) - self.assertIn("squidward", self.run_function("softwareupdate.list_ignored")) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_schedule(self): - """ - Test softwareupdate.schedule_enable - Test softwareupdate.schedule_enabled - """ - # Test enable - self.assertTrue(self.run_function("softwareupdate.schedule_enable", [True])) - self.assertTrue(self.run_function("softwareupdate.schedule_enabled")) - - # Test disable in case it was already enabled - self.assertTrue(self.run_function("softwareupdate.schedule_enable", [False])) - self.assertFalse(self.run_function("softwareupdate.schedule_enabled")) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_update(self): - """ - Test softwareupdate.update_all - Test softwareupdate.update - Test softwareupdate.update_available - - Need to know the names of updates that are available to properly test - the update functions... - """ - # There's no way to know what the dictionary will contain, so all we can - # check is that the return is a dictionary - self.assertIsInstance(self.run_function("softwareupdate.update_all"), dict) - - # Test update_available - self.assertFalse( - self.run_function("softwareupdate.update_available", ["spongebob"]) - ) - - # Test update not available - self.assertIn( - "Update not available", - self.run_function("softwareupdate.update", ["spongebob"]), - ) - - @pytest.mark.slow_test - def test_list_downloads(self): - """ - Test softwareupdate.list_downloads - """ - self.assertIsInstance(self.run_function("softwareupdate.list_downloads"), list) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_download(self): - """ - Test softwareupdate.download - - Need to know the names of updates that are available to properly test - the download function - """ - # Test update not available - self.assertIn( - "Update not available", - self.run_function("softwareupdate.download", ["spongebob"]), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_download_all(self): - """ - Test softwareupdate.download_all - """ - self.assertIsInstance(self.run_function("softwareupdate.download_all"), list) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_get_set_reset_catalog(self): - """ - Test softwareupdate.download_all - """ - # Reset the catalog - self.assertTrue(self.run_function("softwareupdate.reset_catalog")) - self.assertEqual(self.run_function("softwareupdate.get_catalog"), "Default") - - # Test setting and getting the catalog - self.assertTrue(self.run_function("softwareupdate.set_catalog", ["spongebob"])) - self.assertEqual(self.run_function("softwareupdate.get_catalog"), "spongebob") - - # Test reset the catalog - self.assertTrue(self.run_function("softwareupdate.reset_catalog")) - self.assertEqual(self.run_function("softwareupdate.get_catalog"), "Default") diff --git a/tests/integration/modules/test_mac_sysctl.py b/tests/integration/modules/test_mac_sysctl.py deleted file mode 100644 index cdf1b665a537..000000000000 --- a/tests/integration/modules/test_mac_sysctl.py +++ /dev/null @@ -1,174 +0,0 @@ -""" - :codeauthor: Nicole Thomas -""" - -import os -import random - -import pytest - -import salt.utils.files -from salt.exceptions import CommandExecutionError -from tests.support.case import ModuleCase - -# Module Variables -ASSIGN_CMD = "net.inet.icmp.timestamp" -CONFIG = "/etc/sysctl.conf" - - -@pytest.mark.destructive_test -@pytest.mark.skip_if_not_root -@pytest.mark.skip_unless_on_darwin -class DarwinSysctlModuleTest(ModuleCase): - """ - Integration tests for the darwin_sysctl module - """ - - def setUp(self): - """ - Sets up the test requirements - """ - super().setUp() - # Data needed for cleanup - self.has_conf = False - self.val = self.run_function("sysctl.get", [ASSIGN_CMD]) - - # If sysctl file is present, make a copy - # Remove original file so we can replace it with test files - if os.path.isfile(CONFIG): - self.has_conf = True - try: - self.conf = self.__copy_sysctl() - except CommandExecutionError: - msg = "Could not copy file: {0}" - raise CommandExecutionError(msg.format(CONFIG)) - os.remove(CONFIG) - - @pytest.mark.slow_test - def test_assign(self): - """ - Tests assigning a single sysctl parameter - """ - try: - rand = random.randint(0, 500) - while rand == self.val: - rand = random.randint(0, 500) - self.run_function("sysctl.assign", [ASSIGN_CMD, rand]) - info = int(self.run_function("sysctl.get", [ASSIGN_CMD])) - try: - self.assertEqual(rand, info) - except AssertionError: - self.run_function("sysctl.assign", [ASSIGN_CMD, self.val]) - raise - except CommandExecutionError: - self.run_function("sysctl.assign", [ASSIGN_CMD, self.val]) - raise - - @pytest.mark.slow_test - def test_persist_new_file(self): - """ - Tests assigning a sysctl value to a system without a sysctl.conf file - """ - # Always start with a clean/known sysctl.conf state - if os.path.isfile(CONFIG): - os.remove(CONFIG) - try: - self.run_function("sysctl.persist", [ASSIGN_CMD, 10]) - line = f"{ASSIGN_CMD}={10}" - found = self.__check_string(CONFIG, line) - self.assertTrue(found) - except CommandExecutionError: - os.remove(CONFIG) - raise - - @pytest.mark.slow_test - def test_persist_already_set(self): - """ - Tests assigning a sysctl value that is already set in sysctl.conf file - """ - # Always start with a clean/known sysctl.conf state - if os.path.isfile(CONFIG): - os.remove(CONFIG) - try: - self.run_function("sysctl.persist", [ASSIGN_CMD, 50]) - ret = self.run_function("sysctl.persist", [ASSIGN_CMD, 50]) - self.assertEqual(ret, "Already set") - except CommandExecutionError: - os.remove(CONFIG) - raise - - @pytest.mark.slow_test - def test_persist_apply_change(self): - """ - Tests assigning a sysctl value and applying the change to system - """ - # Always start with a clean/known sysctl.conf state - if os.path.isfile(CONFIG): - os.remove(CONFIG) - try: - rand = random.randint(0, 500) - while rand == self.val: - rand = random.randint(0, 500) - self.run_function("sysctl.persist", [ASSIGN_CMD, rand], apply_change=True) - info = int(self.run_function("sysctl.get", [ASSIGN_CMD])) - self.assertEqual(info, rand) - except CommandExecutionError: - os.remove(CONFIG) - raise - - def __copy_sysctl(self): - """ - Copies an existing sysconf file and returns temp file path. Copied - file will be restored in tearDown - """ - # Create new temporary file path and open needed files - temp_path = salt.utils.files.mkstemp() - with salt.utils.files.fopen(CONFIG, "r") as org_conf: - with salt.utils.files.fopen(temp_path, "w") as temp_sysconf: - # write sysctl lines to temp file - for line in org_conf: - temp_sysconf.write(line) - return temp_path - - def __restore_sysctl(self): - """ - Restores the original sysctl.conf file from temporary copy - """ - # If sysctl testing file exists, delete it - if os.path.isfile(CONFIG): - os.remove(CONFIG) - - # write temp lines to sysctl file to restore - with salt.utils.files.fopen(self.conf, "r") as temp_sysctl: - with salt.utils.files.fopen(CONFIG, "w") as sysctl: - for line in temp_sysctl: - sysctl.write(line) - - # delete temporary file - os.remove(self.conf) - - def __check_string(self, conf_file, to_find): - """ - Returns True if given line is present in file - """ - with salt.utils.files.fopen(conf_file, "r") as f_in: - for line in f_in: - if to_find in salt.utils.stringutils.to_unicode(line): - return True - return False - - def tearDown(self): - """ - Clean up after tests - """ - ret = self.run_function("sysctl.get", [ASSIGN_CMD]) - if ret != self.val: - self.run_function("sysctl.assign", [ASSIGN_CMD, self.val]) - - if self.has_conf is True: - # restore original sysctl file - self.__restore_sysctl() - - if self.has_conf is False and os.path.isfile(CONFIG): - # remove sysctl.conf created by tests - os.remove(CONFIG) diff --git a/tests/integration/modules/test_mac_system.py b/tests/integration/modules/test_mac_system.py deleted file mode 100644 index bf590f82c784..000000000000 --- a/tests/integration/modules/test_mac_system.py +++ /dev/null @@ -1,263 +0,0 @@ -""" -integration tests for mac_system -""" - -import logging - -import pytest -from saltfactories.utils import random_string - -from tests.support.case import ModuleCase - -log = logging.getLogger(__name__) - - -SET_COMPUTER_NAME = random_string("RS-", lowercase=False) -SET_SUBNET_NAME = random_string("RS-", lowercase=False) - - -@pytest.mark.flaky(max_runs=10) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.usefixtures("salt_sub_minion") -@pytest.mark.skip_if_not_root -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.slow_test -class MacSystemModuleTest(ModuleCase): - """ - Validate the mac_system module - """ - - ATRUN_ENABLED = False - REMOTE_LOGIN_ENABLED = False - REMOTE_EVENTS_ENABLED = False - SUBNET_NAME = "" - KEYBOARD_DISABLED = False - - def setUp(self): - """ - Get current settings - """ - self.ATRUN_ENABLED = self.run_function("service.enabled", ["com.apple.atrun"]) - self.REMOTE_LOGIN_ENABLED = self.run_function("system.get_remote_login") - self.REMOTE_EVENTS_ENABLED = self.run_function("system.get_remote_events") - self.SUBNET_NAME = self.run_function("system.get_subnet_name") - self.KEYBOARD_DISABLED = self.run_function( - "system.get_disable_keyboard_on_lock" - ) - - def tearDown(self): - """ - Reset to original settings - """ - if not self.ATRUN_ENABLED: - atrun = "/System/Library/LaunchDaemons/com.apple.atrun.plist" - self.run_function("service.stop", [atrun]) - - self.run_function("system.set_remote_login", [self.REMOTE_LOGIN_ENABLED]) - self.run_function("system.set_remote_events", [self.REMOTE_EVENTS_ENABLED]) - self.run_function("system.set_subnet_name", [self.SUBNET_NAME]) - self.run_function( - "system.set_disable_keyboard_on_lock", [self.KEYBOARD_DISABLED] - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_remote_login(self): - """ - Test system.get_remote_login - Test system.set_remote_login - """ - # Normal Functionality - self.assertTrue(self.run_function("system.set_remote_login", [True])) - self.assertTrue(self.run_function("system.get_remote_login")) - self.assertTrue(self.run_function("system.set_remote_login", [False])) - self.assertFalse(self.run_function("system.get_remote_login")) - - # Test valid input - self.assertTrue(self.run_function("system.set_remote_login", [True])) - self.assertTrue(self.run_function("system.set_remote_login", [False])) - self.assertTrue(self.run_function("system.set_remote_login", ["yes"])) - self.assertTrue(self.run_function("system.set_remote_login", ["no"])) - self.assertTrue(self.run_function("system.set_remote_login", ["On"])) - self.assertTrue(self.run_function("system.set_remote_login", ["Off"])) - self.assertTrue(self.run_function("system.set_remote_login", [1])) - self.assertTrue(self.run_function("system.set_remote_login", [0])) - - # Test invalid input - self.assertIn( - "Invalid String Value for Enabled", - self.run_function("system.set_remote_login", ["spongebob"]), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_remote_events(self): - """ - Test system.get_remote_events - Test system.set_remote_events - """ - # Normal Functionality - self.assertTrue(self.run_function("system.set_remote_events", [True])) - self.assertTrue(self.run_function("system.get_remote_events")) - self.assertTrue(self.run_function("system.set_remote_events", [False])) - self.assertFalse(self.run_function("system.get_remote_events")) - - # Test valid input - self.assertTrue(self.run_function("system.set_remote_events", [True])) - self.assertTrue(self.run_function("system.set_remote_events", [False])) - self.assertTrue(self.run_function("system.set_remote_events", ["yes"])) - self.assertTrue(self.run_function("system.set_remote_events", ["no"])) - self.assertTrue(self.run_function("system.set_remote_events", ["On"])) - self.assertTrue(self.run_function("system.set_remote_events", ["Off"])) - self.assertTrue(self.run_function("system.set_remote_events", [1])) - self.assertTrue(self.run_function("system.set_remote_events", [0])) - - # Test invalid input - self.assertIn( - "Invalid String Value for Enabled", - self.run_function("system.set_remote_events", ["spongebob"]), - ) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_get_set_subnet_name(self): - """ - Test system.get_subnet_name - Test system.set_subnet_name - """ - self.assertTrue(self.run_function("system.set_subnet_name", [SET_SUBNET_NAME])) - self.assertEqual(self.run_function("system.get_subnet_name"), SET_SUBNET_NAME) - - @pytest.mark.slow_test - @pytest.mark.skip_initial_gh_actions_failure - def test_get_list_startup_disk(self): - """ - Test system.get_startup_disk - Test system.list_startup_disks - Don't know how to test system.set_startup_disk as there's usually only - one startup disk available on a system - """ - # Test list and get - ret = self.run_function("system.list_startup_disks") - self.assertIsInstance(ret, list) - self.assertIn(self.run_function("system.get_startup_disk"), ret) - - # Test passing set a bad disk - self.assertIn( - "Invalid value passed for path.", - self.run_function("system.set_startup_disk", ["spongebob"]), - ) - - @pytest.mark.skip(reason="Skip this test until mac fixes it.") - def test_get_set_restart_delay(self): - """ - Test system.get_restart_delay - Test system.set_restart_delay - system.set_restart_delay does not work due to an apple bug, see docs - may need to disable this test as we can't control the delay value - """ - # Normal Functionality - self.assertTrue(self.run_function("system.set_restart_delay", [90])) - self.assertEqual(self.run_function("system.get_restart_delay"), "90 seconds") - - # Pass set bad value for seconds - self.assertIn( - "Invalid value passed for seconds.", - self.run_function("system.set_restart_delay", [70]), - ) - - @pytest.mark.slow_test - def test_get_set_disable_keyboard_on_lock(self): - """ - Test system.get_disable_keyboard_on_lock - Test system.set_disable_keyboard_on_lock - """ - # Normal Functionality - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", [True]) - ) - self.assertTrue(self.run_function("system.get_disable_keyboard_on_lock")) - - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", [False]) - ) - self.assertFalse(self.run_function("system.get_disable_keyboard_on_lock")) - - # Test valid input - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", [True]) - ) - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", [False]) - ) - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", ["yes"]) - ) - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", ["no"]) - ) - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", ["On"]) - ) - self.assertTrue( - self.run_function("system.set_disable_keyboard_on_lock", ["Off"]) - ) - self.assertTrue(self.run_function("system.set_disable_keyboard_on_lock", [1])) - self.assertTrue(self.run_function("system.set_disable_keyboard_on_lock", [0])) - - # Test invalid input - self.assertIn( - "Invalid String Value for Enabled", - self.run_function("system.set_disable_keyboard_on_lock", ["spongebob"]), - ) - - @pytest.mark.skip(reason="Skip this test until mac fixes it.") - def test_get_set_boot_arch(self): - """ - Test system.get_boot_arch - Test system.set_boot_arch - system.set_boot_arch does not work due to an apple bug, see docs - may need to disable this test as we can't set the boot architecture - """ - # Normal Functionality - self.assertTrue(self.run_function("system.set_boot_arch", ["i386"])) - self.assertEqual(self.run_function("system.get_boot_arch"), "i386") - self.assertTrue(self.run_function("system.set_boot_arch", ["default"])) - self.assertEqual(self.run_function("system.get_boot_arch"), "default") - - # Test invalid input - self.assertIn( - "Invalid value passed for arch", - self.run_function("system.set_boot_arch", ["spongebob"]), - ) - - -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_not_root -class MacSystemComputerNameTest(ModuleCase): - def setUp(self): - self.COMPUTER_NAME = self.run_function("system.get_computer_name") - self.wait_for_all_jobs() - - def tearDown(self): - self.run_function("system.set_computer_name", [self.COMPUTER_NAME]) - self.wait_for_all_jobs() - - # A similar test used to be skipped on py3 due to 'hanging', if we see - # something similar again we may want to skip this gain until we - # 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.slow_test - def test_get_set_computer_name(self): - """ - Test system.get_computer_name - Test system.set_computer_name - """ - log.debug("Set name is %s", SET_COMPUTER_NAME) - self.assertTrue( - self.run_function("system.set_computer_name", [SET_COMPUTER_NAME]) - ) - self.assertEqual( - self.run_function("system.get_computer_name"), SET_COMPUTER_NAME - ) diff --git a/tests/integration/modules/test_mac_timezone.py b/tests/integration/modules/test_mac_timezone.py deleted file mode 100644 index c424710a410e..000000000000 --- a/tests/integration/modules/test_mac_timezone.py +++ /dev/null @@ -1,206 +0,0 @@ -""" -Integration tests for mac_timezone - -If using parallels, make sure Time sync is turned off. Otherwise, parallels will -keep changing your date/time settings while the tests are running. To turn off -Time sync do the following: - - Go to actions -> configure - - Select options at the top and 'More Options' on the left - - Set time to 'Do not sync' -""" - -import datetime - -import pytest - -from tests.support.case import ModuleCase - - -@pytest.mark.flaky(max_runs=4) -@pytest.mark.skip_unless_on_darwin -@pytest.mark.skip_if_binaries_missing("systemsetup") -@pytest.mark.skip_if_not_root -@pytest.mark.slow_test -class MacTimezoneModuleTest(ModuleCase): - """ - Validate the mac_timezone module - """ - - USE_NETWORK_TIME = False - TIME_SERVER = "time.apple.com" - TIME_ZONE = "" - CURRENT_DATE = "" - CURRENT_TIME = "" - - def setUp(self): - """ - Get current settings - """ - self.USE_NETWORK_TIME = self.run_function("timezone.get_using_network_time") - self.TIME_SERVER = self.run_function("timezone.get_time_server") - self.TIME_ZONE = self.run_function("timezone.get_zone") - self.CURRENT_DATE = self.run_function("timezone.get_date") - self.CURRENT_TIME = self.run_function("timezone.get_time") - - self.run_function("timezone.set_using_network_time", [False]) - self.run_function("timezone.set_zone", ["America/Denver"]) - - def tearDown(self): - """ - Reset to original settings - """ - self.run_function("timezone.set_time_server", [self.TIME_SERVER]) - self.run_function("timezone.set_using_network_time", [self.USE_NETWORK_TIME]) - self.run_function("timezone.set_zone", [self.TIME_ZONE]) - if not self.USE_NETWORK_TIME: - self.run_function("timezone.set_date", [self.CURRENT_DATE]) - self.run_function("timezone.set_time", [self.CURRENT_TIME]) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_set_date(self): - """ - Test timezone.get_date - Test timezone.set_date - """ - # Correct Functionality - self.assertTrue(self.run_function("timezone.set_date", ["2/20/2011"])) - self.assertEqual(self.run_function("timezone.get_date"), "2/20/2011") - - # Test bad date format - self.assertEqual( - self.run_function("timezone.set_date", ["13/12/2014"]), - "ERROR executing 'timezone.set_date': Invalid Date/Time Format: 13/12/2014", - ) - - @pytest.mark.slow_test - def test_get_time(self): - """ - Test timezone.get_time - """ - text_time = self.run_function("timezone.get_time") - self.assertNotEqual(text_time, "Invalid Timestamp") - obj_date = datetime.datetime.strptime(text_time, "%H:%M:%S") - self.assertIsInstance(obj_date, datetime.date) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_set_time(self): - """ - Test timezone.set_time - """ - # Correct Functionality - self.assertTrue(self.run_function("timezone.set_time", ["3:14"])) - - # Test bad time format - self.assertEqual( - self.run_function("timezone.set_time", ["3:71"]), - "ERROR executing 'timezone.set_time': Invalid Date/Time Format: 3:71", - ) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_set_zone(self): - """ - Test timezone.get_zone - Test timezone.set_zone - """ - # Correct Functionality - self.assertTrue(self.run_function("timezone.set_zone", ["Pacific/Wake"])) - self.assertEqual(self.run_function("timezone.get_zone"), "Pacific/Wake") - - # Test bad time zone - self.assertEqual( - self.run_function("timezone.set_zone", ["spongebob"]), - "ERROR executing 'timezone.set_zone': Invalid Timezone: spongebob", - ) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_offset(self): - """ - Test timezone.get_offset - """ - self.assertTrue(self.run_function("timezone.set_zone", ["Pacific/Wake"])) - self.assertIsInstance(self.run_function("timezone.get_offset"), (str,)) - self.assertEqual(self.run_function("timezone.get_offset"), "+1200") - - self.assertTrue(self.run_function("timezone.set_zone", ["America/Los_Angeles"])) - self.assertIsInstance(self.run_function("timezone.get_offset"), (str,)) - self.assertEqual(self.run_function("timezone.get_offset"), "-0700") - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_set_zonecode(self): - """ - Test timezone.get_zonecode - Test timezone.set_zonecode - """ - self.assertTrue(self.run_function("timezone.set_zone", ["America/Los_Angeles"])) - self.assertIsInstance(self.run_function("timezone.get_zonecode"), (str,)) - self.assertEqual(self.run_function("timezone.get_zonecode"), "PDT") - - self.assertTrue(self.run_function("timezone.set_zone", ["Pacific/Wake"])) - self.assertIsInstance(self.run_function("timezone.get_zonecode"), (str,)) - self.assertEqual(self.run_function("timezone.get_zonecode"), "WAKT") - - @pytest.mark.slow_test - def test_list_zones(self): - """ - Test timezone.list_zones - """ - zones = self.run_function("timezone.list_zones") - self.assertIsInstance(self.run_function("timezone.list_zones"), list) - self.assertIn("America/Denver", self.run_function("timezone.list_zones")) - self.assertIn("America/Los_Angeles", self.run_function("timezone.list_zones")) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_zone_compare(self): - """ - Test timezone.zone_compare - """ - self.assertTrue(self.run_function("timezone.set_zone", ["America/Denver"])) - self.assertTrue(self.run_function("timezone.zone_compare", ["America/Denver"])) - self.assertFalse(self.run_function("timezone.zone_compare", ["Pacific/Wake"])) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_set_using_network_time(self): - """ - Test timezone.get_using_network_time - Test timezone.set_using_network_time - """ - self.assertTrue(self.run_function("timezone.set_using_network_time", [True])) - self.assertTrue(self.run_function("timezone.get_using_network_time")) - - self.assertTrue(self.run_function("timezone.set_using_network_time", [False])) - self.assertFalse(self.run_function("timezone.get_using_network_time")) - - @pytest.mark.skip( - reason="Skip until we can figure out why modifying the system clock causes ZMQ errors", - ) - @pytest.mark.destructive_test - def test_get_set_time_server(self): - """ - Test timezone.get_time_server - Test timezone.set_time_server - """ - self.assertTrue( - self.run_function("timezone.set_time_server", ["spongebob.com"]) - ) - self.assertEqual(self.run_function("timezone.get_time_server"), "spongebob.com") diff --git a/tests/integration/modules/test_mac_user.py b/tests/integration/modules/test_mac_user.py deleted file mode 100644 index 144267d72f6b..000000000000 --- a/tests/integration/modules/test_mac_user.py +++ /dev/null @@ -1,235 +0,0 @@ -""" - :codeauthor: Nicole Thomas -""" - -import os - -import pytest -from saltfactories.utils import random_string - -import salt.utils.files -from salt.exceptions import CommandExecutionError -from tests.support.case import ModuleCase - -# Create user strings for tests -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) - - -@pytest.mark.skip_if_not_root -@pytest.mark.destructive_test -@pytest.mark.skip_unless_on_darwin -class MacUserModuleTest(ModuleCase): - """ - Integration tests for the mac_user module - """ - - def setUp(self): - """ - Sets up test requirements - """ - super().setUp() - os_grain = self.run_function("grains.item", ["kernel"]) - if os_grain["kernel"] not in "Darwin": - self.skipTest("Test not applicable to '{kernel}' kernel".format(**os_grain)) - - @pytest.mark.slow_test - def test_mac_user_add(self): - """ - Tests the add function - """ - try: - self.run_function("user.add", [ADD_USER]) - user_info = self.run_function("user.info", [ADD_USER]) - self.assertEqual(ADD_USER, user_info["name"]) - except CommandExecutionError: - self.run_function("user.delete", [ADD_USER]) - raise - - @pytest.mark.slow_test - def test_mac_user_delete(self): - """ - Tests the delete function - """ - - # Create a user to delete - If unsuccessful, skip the test - if self.run_function("user.add", [DEL_USER]) is not True: - self.run_function("user.delete", [DEL_USER]) - self.skipTest("Failed to create a user to delete") - - # Now try to delete the added user - ret = self.run_function("user.delete", [DEL_USER]) - self.assertTrue(ret) - - @pytest.mark.slow_test - def test_mac_user_primary_group(self): - """ - Tests the primary_group function - """ - - # Create a user to test primary group function - if self.run_function("user.add", [PRIMARY_GROUP_USER]) is not True: - self.run_function("user.delete", [PRIMARY_GROUP_USER]) - self.skipTest("Failed to create a user") - - try: - # Test mac_user.primary_group - primary_group = self.run_function( - "user.primary_group", [PRIMARY_GROUP_USER] - ) - uid_info = self.run_function("user.info", [PRIMARY_GROUP_USER]) - self.assertIn(primary_group, uid_info["groups"]) - - except AssertionError: - self.run_function("user.delete", [PRIMARY_GROUP_USER]) - raise - - @pytest.mark.slow_test - def test_mac_user_changes(self): - """ - Tests mac_user functions that change user properties - """ - # Create a user to manipulate - if unsuccessful, skip the test - if self.run_function("user.add", [CHANGE_USER]) is not True: - self.run_function("user.delete", [CHANGE_USER]) - self.skipTest("Failed to create a user") - - try: - # Test mac_user.chuid - self.run_function("user.chuid", [CHANGE_USER, 4376]) - uid_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(uid_info["uid"], 4376) - - # Test mac_user.chgid - self.run_function("user.chgid", [CHANGE_USER, 4376]) - gid_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(gid_info["gid"], 4376) - - # Test mac.user.chshell - self.run_function("user.chshell", [CHANGE_USER, "/bin/zsh"]) - shell_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(shell_info["shell"], "/bin/zsh") - - # Test mac_user.chhome - self.run_function("user.chhome", [CHANGE_USER, "/Users/foo"]) - home_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(home_info["home"], "/Users/foo") - - # Test mac_user.chfullname - self.run_function("user.chfullname", [CHANGE_USER, "Foo Bar"]) - fullname_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(fullname_info["fullname"], "Foo Bar") - - # Test mac_user.chgroups - pre_info = self.run_function("user.info", [CHANGE_USER])["groups"] - expected = pre_info + ["wheel"] - self.run_function("user.chgroups", [CHANGE_USER, "wheel"]) - groups_info = self.run_function("user.info", [CHANGE_USER]) - self.assertEqual(groups_info["groups"], expected) - - except AssertionError: - self.run_function("user.delete", [CHANGE_USER]) - raise - - @pytest.mark.slow_test - def test_mac_user_enable_auto_login(self): - """ - Tests mac_user functions that enable auto login - """ - # Make sure auto login is disabled before we start - if self.run_function("user.get_auto_login"): - self.skipTest("Auto login already enabled") - - try: - # Does enable return True - self.assertTrue( - self.run_function( - "user.enable_auto_login", ["Spongebob", "Squarepants"] - ) - ) - - # Did it set the user entry in the plist file - self.assertEqual(self.run_function("user.get_auto_login"), "Spongebob") - - # Did it generate the `/etc/kcpassword` file - self.assertTrue(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() - self.assertEqual(test_data, file_data) - - # Does disable return True - self.assertTrue(self.run_function("user.disable_auto_login")) - - # Does it remove the user entry in the plist file - self.assertFalse(self.run_function("user.get_auto_login")) - - # Is the `/etc/kcpassword` file removed - self.assertFalse(os.path.exists("/etc/kcpassword")) - - finally: - # Make sure auto_login is disabled - self.assertTrue(self.run_function("user.disable_auto_login")) - - # Make sure autologin is disabled - if self.run_function("user.get_auto_login"): - raise Exception("Failed to disable auto login") - - @pytest.mark.slow_test - def test_mac_user_disable_auto_login(self): - """ - Tests mac_user functions that disable auto login - """ - # Make sure auto login is enabled before we start - # Is there an existing setting - if self.run_function("user.get_auto_login"): - self.skipTest("Auto login already enabled") - - try: - # Enable auto login for the test - self.run_function("user.enable_auto_login", ["Spongebob", "Squarepants"]) - - # Make sure auto login got set up - if not self.run_function("user.get_auto_login") == "Spongebob": - raise Exception("Failed to enable auto login") - - # Does disable return True - self.assertTrue(self.run_function("user.disable_auto_login")) - - # Does it remove the user entry in the plist file - self.assertFalse(self.run_function("user.get_auto_login")) - - # Is the `/etc/kcpassword` file removed - self.assertFalse(os.path.exists("/etc/kcpassword")) - - finally: - # Make sure auto login is disabled - self.assertTrue(self.run_function("user.disable_auto_login")) - - # Make sure auto login is disabled - if self.run_function("user.get_auto_login"): - raise Exception("Failed to disable auto login") - - def tearDown(self): - """ - Clean up after tests - """ - - # Delete ADD_USER - add_info = self.run_function("user.info", [ADD_USER]) - if add_info: - self.run_function("user.delete", [ADD_USER]) - - # Delete DEL_USER if something failed - del_info = self.run_function("user.info", [DEL_USER]) - if del_info: - self.run_function("user.delete", [DEL_USER]) - - # Delete CHANGE_USER - change_info = self.run_function("user.info", [CHANGE_USER]) - if change_info: - self.run_function("user.delete", [CHANGE_USER]) diff --git a/tests/integration/modules/test_mac_xattr.py b/tests/integration/modules/test_mac_xattr.py deleted file mode 100644 index bcc39339b069..000000000000 --- a/tests/integration/modules/test_mac_xattr.py +++ /dev/null @@ -1,193 +0,0 @@ -""" -integration tests for mac_xattr -""" - -import os - -import pytest - -from tests.support.case import ModuleCase -from tests.support.runtests import RUNTIME_VARS - - -@pytest.mark.skip_if_binaries_missing("xattr") -@pytest.mark.skip_unless_on_darwin -class MacXattrModuleTest(ModuleCase): - """ - Validate the mac_xattr module - """ - - @classmethod - def setUpClass(cls): - cls.test_file = os.path.join(RUNTIME_VARS.TMP, "xattr_test_file.txt") - cls.no_file = os.path.join(RUNTIME_VARS.TMP, "xattr_no_file.txt") - - def setUp(self): - """ - Create test file for testing extended attributes - """ - self.run_function("file.touch", [self.test_file]) - - def tearDown(self): - """ - Clean up test file - """ - if os.path.exists(self.test_file): - os.remove(self.test_file) - - @pytest.mark.slow_test - def test_list_no_xattr(self): - """ - Make sure there are no attributes - """ - # Clear existing attributes - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Test no attributes - self.assertEqual(self.run_function("xattr.list", [self.test_file]), {}) - - # Test file not found - self.assertEqual( - self.run_function("xattr.list", [self.no_file]), - "ERROR: File not found: {}".format(self.no_file), - ) - - @pytest.mark.slow_test - def test_write(self): - """ - Write an attribute - """ - # Clear existing attributes - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Write some attributes - self.assertTrue( - self.run_function( - "xattr.write", [self.test_file, "spongebob", "squarepants"] - ) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "squidward", "plankton"]) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "crabby", "patty"]) - ) - - # Test that they were actually added - self.assertEqual( - self.run_function("xattr.list", [self.test_file]), - {"spongebob": "squarepants", "squidward": "plankton", "crabby": "patty"}, - ) - - # Test file not found - self.assertEqual( - self.run_function("xattr.write", [self.no_file, "patrick", "jellyfish"]), - "ERROR: File not found: {}".format(self.no_file), - ) - - @pytest.mark.slow_test - def test_read(self): - """ - Test xattr.read - """ - # Clear existing attributes - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Write an attribute - self.assertTrue( - self.run_function( - "xattr.write", [self.test_file, "spongebob", "squarepants"] - ) - ) - - # Read the attribute - self.assertEqual( - self.run_function("xattr.read", [self.test_file, "spongebob"]), - "squarepants", - ) - - # Test file not found - self.assertEqual( - self.run_function("xattr.read", [self.no_file, "spongebob"]), - "ERROR: File not found: {}".format(self.no_file), - ) - - # Test attribute not found - self.assertEqual( - self.run_function("xattr.read", [self.test_file, "patrick"]), - "ERROR: Attribute not found: patrick", - ) - - @pytest.mark.slow_test - def test_delete(self): - """ - Test xattr.delete - """ - # Clear existing attributes - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Write some attributes - self.assertTrue( - self.run_function( - "xattr.write", [self.test_file, "spongebob", "squarepants"] - ) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "squidward", "plankton"]) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "crabby", "patty"]) - ) - - # Delete an attribute - self.assertTrue( - self.run_function("xattr.delete", [self.test_file, "squidward"]) - ) - - # Make sure it was actually deleted - self.assertEqual( - self.run_function("xattr.list", [self.test_file]), - {"spongebob": "squarepants", "crabby": "patty"}, - ) - - # Test file not found - self.assertEqual( - self.run_function("xattr.delete", [self.no_file, "spongebob"]), - "ERROR: File not found: {}".format(self.no_file), - ) - - # Test attribute not found - self.assertEqual( - self.run_function("xattr.delete", [self.test_file, "patrick"]), - "ERROR: Attribute not found: patrick", - ) - - @pytest.mark.slow_test - def test_clear(self): - """ - Test xattr.clear - """ - # Clear existing attributes - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Write some attributes - self.assertTrue( - self.run_function( - "xattr.write", [self.test_file, "spongebob", "squarepants"] - ) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "squidward", "plankton"]) - ) - self.assertTrue( - self.run_function("xattr.write", [self.test_file, "crabby", "patty"]) - ) - - # Test Clear - self.assertTrue(self.run_function("xattr.clear", [self.test_file])) - - # Test file not found - self.assertEqual( - self.run_function("xattr.clear", [self.no_file]), - "ERROR: File not found: {}".format(self.no_file), - ) diff --git a/tests/pytests/functional/modules/test_mac_assistive.py b/tests/pytests/functional/modules/test_mac_assistive.py new file mode 100644 index 000000000000..1c5206ed04a6 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_assistive.py @@ -0,0 +1,106 @@ +""" + :codeauthor: Nicole Thomas +""" + +import pytest + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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): + try: + ret = assistive.install(osa_script, True) + except CommandExecutionError as exc: + pytest.skip(f"Unable to install {osa_script} - {str(exc.value)}") + + try: + yield + finally: + osa_script_ret = assistive.installed(osa_script) + if osa_script_ret: + assistive.remove(osa_script) + + smile_bundle = "com.smileonmymac.textexpander" + 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): + """ + Tests installing and removing a bundled ID or command to use assistive access. + """ + new_bundle = "com.smileonmymac.textexpander" + ret = assistive.install(new_bundle) + assert ret + ret = assistive.remove(new_bundle) + assert ret + + +@pytest.mark.slow_test +def test_installed(assistive, osa_script): + """ + Tests the True and False return of assistive.installed. + """ + # OSA script should have been installed in _setup_teardown_vars function + ret = assistive.installed(osa_script) + assert ret + # Clean up install + assistive.remove(osa_script) + # Installed should now return False + ret = assistive.installed(osa_script) + assert not ret + + +@pytest.mark.slow_test +def test_enable(assistive, osa_script): + """ + Tests setting the enabled status of a bundled ID or command. + """ + # OSA script should have been installed and enabled in _setup_teardown_vars function + # Now let's disable it, which should return True. + ret = assistive.enable(osa_script, False) + assert ret + # Double check the script was disabled, as intended. + ret = assistive.enabled(osa_script) + assert not ret + # Now re-enable + ret = assistive.enable(osa_script) + assert ret + # Double check the script was enabled, as intended. + ret = assistive.enabled(osa_script) + assert ret + + +@pytest.mark.slow_test +def test_enabled(assistive, osa_script): + """ + Tests if a bundled ID or command is listed in assistive access returns True. + """ + # OSA script should have been installed in _setup_teardown_vars function, which sets + # enabled to True by default. + ret = assistive.enabled(osa_script) + assert ret + # Disable OSA Script + assistive.enable(osa_script, False) + # Assert against new disabled status + ret = assistive.enabled(osa_script) + assert not ret diff --git a/tests/pytests/functional/modules/test_mac_brew_pkg.py b/tests/pytests/functional/modules/test_mac_brew_pkg.py new file mode 100644 index 000000000000..2bcf84b48e04 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_brew_pkg.py @@ -0,0 +1,147 @@ +""" + :codeauthor: Nicole Thomas + :codeauthor: Gareth J. Greenaway +""" + +import pytest + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, + pytest.mark.skip_if_binaries_missing("brew"), +] + + +@pytest.fixture(scope="module") +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): + try: + yield + 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) + + +def test_brew_install(pkg, add_pkg): + """ + Tests the installation of packages + """ + pkg.install(add_pkg) + pkg_list = pkg.list_pkgs() + assert add_pkg in pkg_list + + +def test_remove(pkg, del_pkg): + """ + Tests the removal of packages + """ + # Install a package to delete - If unsuccessful, skip the test + pkg.install(del_pkg) + 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 + + +def test_version(pkg, add_pkg): + """ + 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_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 + ) + # 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] + ) + + +def test_latest_version(pkg, add_pkg): + """ + Test pkg.latest_version: + - get the latest version available + - install the package + - 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.install(add_pkg) + installed_latest = pkg.latest_version(add_pkg) + version = pkg.version(add_pkg) + assert isinstance(uninstalled_latest, str) + assert installed_latest == version + + +def test_refresh_db(pkg): + """ + Integration test to ensure pkg.refresh_db works with brew + """ + refresh_brew = pkg.refresh_db() + assert refresh_brew + + +def test_list_upgrades(pkg, add_pkg): + """ + Test pkg.list_upgrades: data is in the form {'name1': 'version1', + 'name2': 'version2', ... } + """ + upgrades = pkg.list_upgrades() + assert isinstance(upgrades, dict) + if upgrades: + for name in upgrades: + assert isinstance(name, str) + assert isinstance(upgrades[name], str) + + +def test_info_installed(pkg, add_pkg): + """ + 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"] diff --git a/tests/pytests/functional/modules/test_mac_desktop.py b/tests/pytests/functional/modules/test_mac_desktop.py new file mode 100644 index 000000000000..7de6744adc06 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_desktop.py @@ -0,0 +1,74 @@ +""" +Integration tests for the mac_desktop execution module. +""" + +import pytest + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def desktop(modules): + return modules.desktop + + +def test_get_output_volume(desktop): + """ + Tests the return of get_output_volume. + """ + ret = desktop.get_output_volume() + assert ret is not None + + +def test_set_output_volume(desktop): + """ + Tests the return of set_output_volume. + """ + current_vol = desktop.get_output_volume() + try: + to_set = 10 + if current_vol == str(to_set): + to_set += 2 + new_vol = desktop.set_output_volume(str(to_set)) + check_vol = desktop.get_output_volume() + assert new_vol == check_vol + finally: + # Set volume back to what it was before + desktop.set_output_volume(current_vol) + + +def test_screensaver(desktop): + """ + Tests the return of the screensaver function. + """ + try: + ret = desktop.screensaver() + except CommandExecutionError as exc: + pytest.skip("Skipping. Screensaver unavailable.") + assert ret + + +def test_lock(desktop): + """ + Tests the return of the lock function. + """ + try: + ret = desktop.lock() + except CommandExecutionError as exc: + pytest.skip("Skipping. Unable to lock screen.") + assert ret + + +def test_say(desktop): + """ + Tests the return of the say function. + """ + ret = desktop.say("hello", "world") + assert ret diff --git a/tests/pytests/functional/modules/test_mac_group.py b/tests/pytests/functional/modules/test_mac_group.py new file mode 100644 index 000000000000..e5f98f6e63d9 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_group.py @@ -0,0 +1,181 @@ +""" + :codeauthor: Nicole Thomas +""" + +import pytest +from saltfactories.utils import random_string + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def group(modules): + return modules.group + + +# Create group name strings for tests +@pytest.fixture(scope="module") +def add_group(): + yield random_string("RS-", lowercase=False) + + +@pytest.fixture(scope="module") +def del_group(): + yield random_string("RS-", lowercase=False) + + +@pytest.fixture(scope="module") +def change_group(): + yield random_string("RS-", lowercase=False) + + +@pytest.fixture(scope="module") +def add_user(): + yield random_string("RS-", lowercase=False) + + +@pytest.fixture(scope="module") +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): + try: + yield + 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) + + +def test_mac_group_add(group, add_group): + """ + Tests the add group function + """ + group.add(add_group, 3456) + group_info = group.info(add_group) + assert group_info["name"] == add_group + + +def test_mac_group_delete(group, del_group): + """ + 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) + assert ret + + +def test_mac_group_chgid(group, change_group): + """ + 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 + + +def test_mac_adduser(group, add_group, add_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) + if ret is not True: + group.delete(add_group) + 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"]) + + +def test_mac_deluser(group, add_group, add_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) + assert delusr + + group_info = group.info(add_group) + assert add_user not in "".join(group_info["members"]) + + +def test_mac_members(group, add_group, add_user, rep_user_group): + """ + Test replacing members of a group + """ + 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) + ) + + rep_group_mem = group.members(add_group, rep_user_group) + 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"]) + + +def test_mac_getent(group, add_group, add_user): + """ + 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) diff --git a/tests/pytests/functional/modules/test_mac_keychain.py b/tests/pytests/functional/modules/test_mac_keychain.py new file mode 100644 index 000000000000..eb67304487d2 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_keychain.py @@ -0,0 +1,129 @@ +""" +Validate the mac-keychain module +""" + +import os + +import pytest + +import salt.utils.versions +from tests.support.runtests import RUNTIME_VARS + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def cmd(modules): + return modules.cmd + + +@pytest.fixture(scope="module") +def keychain(modules): + return modules.keychain + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown_vars(keychain, base_env_state_tree_root_dir): + cert = os.path.join(RUNTIME_VARS.FILES, "file", "base", "certs", "salttest.p12") + cert_alias = "Salt Test" + passwd = "salttest" + + try: + yield cert, cert_alias, passwd + finally: + certs_list = keychain.list_certs() + if cert_alias in certs_list: + keychain.uninstall(cert_alias) + + +def test_mac_keychain_install(keychain, setup_teardown_vars): + """ + Tests that attempts to install a certificate + """ + + cert = setup_teardown_vars[0] + cert_alias = setup_teardown_vars[1] + passwd = setup_teardown_vars[2] + + install_cert = keychain.install(cert, passwd) + assert install_cert + assert install_cert == "1 identity imported." + + # check to ensure the cert was installed + certs_list = keychain.list_certs() + assert cert_alias in certs_list + + +def test_mac_keychain_uninstall(keychain, setup_teardown_vars): + """ + Tests that attempts to uninstall a certificate + """ + + cert = setup_teardown_vars[0] + cert_alias = setup_teardown_vars[1] + passwd = setup_teardown_vars[2] + + keychain.install(cert, passwd) + certs_list = keychain.list_certs() + + if cert_alias not in certs_list: + keychain.uninstall(cert_alias) + pytest.skip("Failed to install keychain") + + # uninstall cert + keychain.uninstall(cert_alias) + certs_list = keychain.list_certs() + + # check to ensure the cert was uninstalled + assert cert_alias not in str(certs_list) + + +@pytest.mark.skip_if_binaries_missing("openssl") +def test_mac_keychain_get_friendly_name(keychain, shell, setup_teardown_vars): + """ + Test that attempts to get friendly name of a cert + """ + cert = setup_teardown_vars[0] + cert_alias = setup_teardown_vars[1] + passwd = setup_teardown_vars[2] + + keychain.install(cert, passwd) + certs_list = keychain.list_certs() + if cert_alias not in certs_list: + keychain.uninstall(cert_alias) + pytest.skip("Failed to install keychain") + + ret = shell.run("openssl", "version") + assert ret.stdout + openssl_version = ret.stdout.split()[1] + + # openssl versions under 3.0.0 do not include legacy flag + if salt.utils.versions.compare(ver1=openssl_version, oper="<", ver2="3.0.0"): + get_name = keychain.get_friendly_name(cert, passwd, legacy=False) + else: + get_name = keychain.get_friendly_name(cert, passwd, legacy=True) + + assert get_name == cert_alias + + +def test_mac_keychain_get_default_keychain(keychain, cmd, setup_teardown_vars): + """ + Test that attempts to get the default keychain + """ + sys_get_keychain = keychain.get_default_keychain() + salt_get_keychain = cmd.run("security default-keychain -d user") + assert salt_get_keychain == sys_get_keychain + + +def test_mac_keychain_list_certs(keychain, setup_teardown_vars): + """ + Test that attempts to list certs + """ + cert_default = "com.apple.systemdefault" + certs = keychain.list_certs() + assert cert_default in certs diff --git a/tests/pytests/functional/modules/test_mac_portspkg.py b/tests/pytests/functional/modules/test_mac_portspkg.py new file mode 100644 index 000000000000..bfeef1e87fb2 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_portspkg.py @@ -0,0 +1,106 @@ +""" +integration tests for mac_ports +""" + +import pytest + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, + pytest.mark.skip_if_binaries_missing("port"), +] + + +@pytest.fixture(scope="module") +def pkg(modules): + return modules.pkg + + +@pytest.fixture(scope="function", autouse=True) +def _setup_teardown_vars(pkg): + AGREE_INSTALLED = False + try: + ret = pkg.list_pkgs() + AGREE_INSTALLED = "agree" in ret + pkg.refresh_db() + yield + finally: + if AGREE_INSTALLED: + pkg.remove("agree") + + +def test_list_pkgs(pkg): + """ + 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 + + +def test_latest_version(pkg): + """ + Test pkg.latest_version + """ + pkg.install("agree") + result = pkg.latest_version("agree", refresh=False) + assert isinstance(result, dict) + assert "agree" in result.data + + +def test_remove(pkg): + """ + Test pkg.remove + """ + pkg.install("agree") + removed = pkg.remove("agree") + assert isinstance(removed, dict) + assert "agree" in removed + + +@pytest.mark.destructive_test +def test_install(pkg): + """ + Test pkg.install + """ + pkg.remove("agree") + installed = pkg.install("agree") + assert isinstance(installed, dict) + assert "agree" in installed + + +def test_list_upgrades(pkg): + """ + Test pkg.list_upgrades + """ + upgrade = pkg.list_upgrades(refresh=False) + assert isinstance(upgrade, dict) + + +def test_upgrade_available(pkg): + """ + Test pkg.upgrade_available + """ + pkg.install("agree") + upgrade_available = pkg.upgrade_available("agree", refresh=False) + assert not upgrade_available.data + + +def test_refresh_db(pkg): + """ + Test pkg.refresh_db + """ + refresh = pkg.refresh_db() + assert refresh + + +def test_upgrade(pkg): + """ + Test pkg.upgrade + """ + results = pkg.upgrade(refresh=False) + assert isinstance(results, dict) + assert results.data["result"] diff --git a/tests/pytests/functional/modules/test_mac_power.py b/tests/pytests/functional/modules/test_mac_power.py new file mode 100644 index 000000000000..5600b2275692 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_power.py @@ -0,0 +1,166 @@ +""" +integration tests for mac_power +""" + +import pytest + +from salt.exceptions import SaltInvocationError + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("systemsetup"), + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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() + try: + yield + finally: + power.set_computer_sleep(computer_sleep) + power.set_display_sleep(display_sleep) + power.set_harddisk_sleep(hard_disk_sleep) + + +def test_computer_sleep(power): + """ + Test power.get_computer_sleep + Test power.set_computer_sleep + """ + + # Normal Functionality + ret = power.set_computer_sleep(90) + assert ret + + ret = power.get_computer_sleep() + assert ret == "after 90 minutes" + + ret = power.set_computer_sleep("Off") + assert ret + + ret = power.get_computer_sleep() + assert ret == "Never" + + # Test invalid input + with pytest.raises(SaltInvocationError) as exc: + power.set_computer_sleep("spongebob") + assert "Invalid String Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_computer_sleep(0) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_computer_sleep(181) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_computer_sleep(True) + assert "Invalid Boolean Value for Minutes" in str(exc.value) + + +def test_display_sleep(power): + """ + Test power.get_display_sleep + Test power.set_display_sleep + """ + + # Normal Functionality + ret = power.set_display_sleep(90) + assert ret + + ret = power.get_display_sleep() + assert ret == "after 90 minutes" + + ret = power.set_display_sleep("Off") + assert ret + + ret = power.get_display_sleep() + assert ret == "Never" + + # Test invalid input + with pytest.raises(SaltInvocationError) as exc: + power.set_display_sleep("spongebob") + assert "Invalid String Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_display_sleep(0) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_display_sleep(181) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_display_sleep(True) + assert "Invalid Boolean Value for Minutes" in str(exc.value) + + +def test_harddisk_sleep(power): + """ + Test power.get_harddisk_sleep + Test power.set_harddisk_sleep + """ + + # Normal Functionality + ret = power.set_harddisk_sleep(90) + assert ret + + ret = power.get_harddisk_sleep() + assert ret == "after 90 minutes" + + ret = power.set_harddisk_sleep("Off") + assert ret + + ret = power.get_harddisk_sleep() + assert ret == "Never" + + # Test invalid input + with pytest.raises(SaltInvocationError) as exc: + power.set_harddisk_sleep("spongebob") + assert "Invalid String Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_harddisk_sleep(0) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_harddisk_sleep(181) + assert "Invalid Integer Value for Minutes" in str(exc.value) + + with pytest.raises(SaltInvocationError) as exc: + power.set_harddisk_sleep(True) + assert "Invalid Boolean Value for Minutes" in str(exc.value) + + +def test_restart_freeze(power): + """ + Test power.get_restart_freeze + Test power.set_restart_freeze + """ + # Normal Functionality + ret = power.set_restart_freeze("on") + assert ret + + ret = power.get_restart_freeze() + assert ret + + # This will return False because mac fails to actually make the change + ret = power.set_restart_freeze("off") + assert not ret + + # Even setting to off returns true, it actually is never set + # This is an apple bug + ret = power.get_restart_freeze() + assert ret diff --git a/tests/pytests/functional/modules/test_mac_power_restart_power_failure.py b/tests/pytests/functional/modules/test_mac_power_restart_power_failure.py new file mode 100644 index 000000000000..e7196848dc44 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_power_restart_power_failure.py @@ -0,0 +1,49 @@ +""" +integration tests for mac_power +""" + +import pytest + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("systemsetup"), + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def power(modules): + return modules.power + + +def test_restart_power_failure(power): + """ + Test power.get_restart_power_failure + Test power.set_restart_power_failure + """ + RESTART_POWER = None + + ret = power.get_restart_power_failure() + if isinstance(ret, bool): + RESTART_POWER = ret + + # If available on this system, test it + if RESTART_POWER is None: + # Check for not available + ret = power.get_restart_power_failure() + assert "Error" in ret + else: + 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 + + power.set_sleep_on_power_button(RESTART_POWER) diff --git a/tests/pytests/functional/modules/test_mac_power_sleep_on_power_button.py b/tests/pytests/functional/modules/test_mac_power_sleep_on_power_button.py new file mode 100644 index 000000000000..35b99e57804c --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_power_sleep_on_power_button.py @@ -0,0 +1,51 @@ +""" +integration tests for mac_power sleep_on_power_button +""" + +import pytest + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("systemsetup"), + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def power(modules): + return modules.power + + +def test_sleep_on_power_button(power): + """ + Test power.get_sleep_on_power_button + Test power.set_sleep_on_power_button + """ + try: + ret = power.get_sleep_on_power_button() + if isinstance(ret, bool): + SLEEP_ON_BUTTON = ret + except CommandExecutionError as exc: + SLEEP_ON_BUTTON = None + + # If available on this system, test it + if SLEEP_ON_BUTTON is None: + # Check for not available + pytest.skip("Skipping. sleep_on_power_button unavailable.") + else: + 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 + + power.set_sleep_on_power_button(SLEEP_ON_BUTTON) diff --git a/tests/pytests/functional/modules/test_mac_power_wake_on_modem.py b/tests/pytests/functional/modules/test_mac_power_wake_on_modem.py new file mode 100644 index 000000000000..b6f759cb26d3 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_power_wake_on_modem.py @@ -0,0 +1,50 @@ +""" +integration tests for mac_power wake_on_modem +""" + +import pytest + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, + pytest.mark.skip_if_binaries_missing("systemsetup"), +] + + +@pytest.fixture(scope="module") +def power(modules): + return modules.power + + +def test_wake_on_modem(power): + """ + Test power.get_wake_on_modem + Test power.set_wake_on_modem + """ + try: + ret = power.get_wake_on_modem() + if isinstance(ret, bool): + WAKE_ON_MODEM = ret + except CommandExecutionError as exc: + WAKE_ON_MODEM = None + + if WAKE_ON_MODEM is None: + # Check for not available + pytest.skip("Skipping. wake_on_modem unavailable.") + else: + 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 + + power.set_wake_on_modem(WAKE_ON_MODEM) diff --git a/tests/pytests/functional/modules/test_mac_power_wake_on_net.py b/tests/pytests/functional/modules/test_mac_power_wake_on_net.py new file mode 100644 index 000000000000..a36bf695e990 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_power_wake_on_net.py @@ -0,0 +1,49 @@ +""" +integration tests for mac_power wake_on_network +""" + +import pytest + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("systemsetup"), + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def power(modules): + return modules.power + + +def test_wake_on_network(power): + """ + Test power.get_wake_on_network + Test power.set_wake_on_network + """ + + WAKE_ON_NET = None + ret = power.get_wake_on_network() + if isinstance(ret, bool): + WAKE_ON_NET = ret + + # If available on this system, test it + if WAKE_ON_NET is None: + # Check for not available + ret = power.get_wake_on_network() + assert "Error" in ret + else: + 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 + + power.set_wake_on_network(WAKE_ON_NET) diff --git a/tests/pytests/functional/modules/test_mac_service.py b/tests/pytests/functional/modules/test_mac_service.py new file mode 100644 index 000000000000..0aa38a8ac8ea --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_service.py @@ -0,0 +1,252 @@ +""" +integration tests for mac_service +""" + +import plistlib + +import pytest + +import salt.utils.files +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.skip_if_binaries_missing("launchctl", "plutil"), + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def service(modules): + return modules.service + + +@pytest.fixture(scope="function", autouse=True) +def service_name(service): + + service_name = "com.salt.integration.test" + service_path = "/Library/LaunchDaemons/com.salt.integration.test.plist" + + service_data = { + "KeepAlive": True, + "Label": service_name, + "ProgramArguments": ["/bin/sleep", "1000"], + "RunAtLoad": True, + } + with salt.utils.files.fopen(service_path, "wb") as fp: + plistlib.dump(service_data, fp) + service.enable(service_name) + service.start(service_name) + + try: + yield service_name + finally: + # Try to stop the service if it's running + try: + service.stop(service_name) + except CommandExecutionError: + pass + salt.utils.files.safe_rm(service_path) + + +def test_show(service, service_name): + """ + Test service.show + """ + # Existing Service + service_info = service.show(service_name) + assert isinstance(service_info, dict) + assert service_info["plist"]["Label"] == service_name + + # Missing Service + with pytest.raises(CommandExecutionError) as exc: + ret = service.show("spongebob") + assert "Service not found" in str(exc.value) + + +def test_launchctl(service, service_name): + """ + Test service.launchctl + """ + # Expected Functionality + ret = service.launchctl("error", "bootstrap", 64) + assert ret + + ret = service.launchctl("error", "bootstrap", 64, return_stdout=True) + assert ret == "64: unknown error code" + + # Raise an error + with pytest.raises(CommandExecutionError) as exc: + ret = service.launchctl("error", "bootstrap") + assert "Failed to error service" in str(exc.value) + + +def test_list(service, service_name): + """ + Test service.list + """ + # Expected Functionality + ret = service.list() + assert "PID" in ret + ret = service.list(service_name) + assert "{" in ret + + # Service not found + with pytest.raises(CommandExecutionError) as exc: + ret = service.list("spongebob") + assert "Service not found" in str(exc.value) + + +def test_enable(service, service_name): + """ + Test service.enable + """ + ret = service.enable(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.enable("spongebob") + assert "Service not found" in str(exc.value) + + +def test_disable(service, service_name): + """ + Test service.disable + """ + ret = service.disable(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.disable("spongebob") + assert "Service not found" in str(exc.value) + + +def test_start(service, service_name): + """ + Test service.start + Test service.stop + Test service.status + """ + service.stop(service_name) + ret = service.start(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.start("spongebob") + assert "Service not found" in str(exc.value) + + +def test_stop(service, service_name): + """ + Test service.stop + """ + ret = service.stop(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.stop("spongebob") + assert "Service not found" in str(exc.value) + + service.start(service_name) + + +def test_status(service, service_name): + """ + Test service.status + """ + # A running service + ret = service.status(service_name) + assert ret + + # A stopped service + service.stop(service_name) + ret = service.status(service_name) + assert not ret + + # Service not found + ret = service.status("spongebob") + assert not ret + + service.start(service_name) + + +def test_available(service, service_name): + """ + Test service.available + """ + ret = service.available(service_name) + assert ret + + ret = service.available("spongebob") + assert not ret + + +def test_missing(service, service_name): + """ + Test service.missing + """ + ret = service.missing(service_name) + assert not ret + + ret = service.missing("spongebob") + assert ret + + +def test_enabled(service, service_name): + """ + Test service.enabled + """ + service.disabled(service_name) + ret = service.enabled(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.enabled("spongebob") + assert "Service not found: spongebob" in str(exc.value) + + +def test_disabled(service, service_name): + """ + Test service.disabled + """ + ret = service.disabled(service_name) + assert not ret + + ret = service.disable(service_name) + assert ret + + ret = service.disabled(service_name) + assert ret + + ret = service.enable(service_name) + assert ret + + with pytest.raises(CommandExecutionError) as exc: + ret = service.disable("spongebob") + assert "Service not found: spongebob" in str(exc.value) + + +def test_get_all(service, service_name): + """ + Test service.get_all + """ + services = service.get_all() + assert isinstance(services, list) + assert service_name in services + + +def test_get_enabled(service, service_name): + """ + Test service.get_enabled + """ + services = service.get_enabled() + assert isinstance(services, list) + assert service_name in services + + +def test_service_laoded(service, service_name): + """ + Test service.get_enabled + """ + ret = service.loaded(service_name) + assert ret diff --git a/tests/pytests/functional/modules/test_mac_shadow.py b/tests/pytests/functional/modules/test_mac_shadow.py new file mode 100644 index 000000000000..f4fd783756fe --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_shadow.py @@ -0,0 +1,189 @@ +""" +integration tests for mac_shadow +""" + +import datetime +import types + +import pytest +from saltfactories.utils import random_string + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("dscl", "pwpolicy"), + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +def shadow(modules): + return modules.shadow + + +@pytest.fixture(scope="function") +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) + ) + + +def test_info(shadow, accounts): + """ + Test shadow.info + """ + # Correct Functionality + ret = shadow.info(accounts.created) + assert ret["name"] == accounts.created + + # User does not exist + ret = shadow.info(accounts.not_created) + assert ret["name"] == "" + + +def test_get_account_created(shadow, accounts): + """ + Test shadow.get_account_created + """ + # Correct Functionality + text_date = shadow.get_account_created(accounts.created) + 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) + + +def test_get_last_change(shadow, accounts): + """ + Test shadow.get_last_change + """ + # Correct Functionality + text_date = shadow.get_last_change(accounts.created) + 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) + + +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) + 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_login_failed_last(accounts) + assert f"ERROR: User not found: {accounts.not_created}" in str(exc.value) + + +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" + + # 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) + + +def test_get_set_maxdays(shadow, accounts): + """ + Test shadow.get_maxdays + Test shadow.set_maxdays + """ + # Correct Functionality + assert shadow.set_maxdays(accounts.created, 20) + assert shadow.get_maxdays(accounts.created) == 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) + + 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) + + +def test_get_set_change(shadow, accounts): + """ + Test shadow.get_change + Test shadow.set_change + """ + # Correct Functionality + assert shadow.set_change(accounts.created, "02/11/2011") + assert shadow.get_change(accounts.created) == "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) + + 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) + + +def test_get_set_expire(shadow, accounts): + """ + Test shadow.get_expire + Test shadow.set_expire + """ + # Correct Functionality + assert shadow.set_expire(accounts.created, "02/11/2011") + assert shadow.get_expire(accounts.created) == "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) + + 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) + + +def test_del_password(shadow, accounts): + """ + Test shadow.del_password + """ + # Correct Functionality + assert shadow.del_password(accounts.created) + assert shadow.info(accounts.created)["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) + + +def test_set_password(shadow, accounts): + """ + Test shadow.set_password + """ + # Correct Functionality + assert shadow.set_password(accounts.created, "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) diff --git a/tests/pytests/functional/modules/test_mac_softwareupdate.py b/tests/pytests/functional/modules/test_mac_softwareupdate.py new file mode 100644 index 000000000000..b4545252c989 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_softwareupdate.py @@ -0,0 +1,184 @@ +""" +integration tests for mac_softwareupdate +""" + +import pytest + +from salt.exceptions import SaltInvocationError + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.skip_if_binaries_missing("softwareupdate"), + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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() + + try: + yield IGNORED_LIST, SCHEDULE, CATALOG + finally: + if IGNORED_LIST: + for item in IGNORED_LIST: + softwareupdate.ignore(item) + else: + softwareupdate.reset_ignored() + + softwareupdate.schedule_enable(SCHEDULE) + + if CATALOG == "Default": + softwareupdate.reset_catalog() + else: + softwareupdate.set_catalog(CATALOG) + + +def test_list_available(softwareupdate): + """ + Test softwareupdate.list_available + """ + # Can't predict what will be returned, so can only test that the return + # is the correct type, dict + ret = softwareupdate.list_available() + assert isinstance(ret, dict) + + +@pytest.mark.skip(reason="Ignore removed from latest OS X.") +def test_ignore(softwareupdate): + """ + Test softwareupdate.ignore + Test softwareupdate.list_ignored + Test softwareupdate.reset_ignored + """ + # Test reset_ignored + ret = softwareupdate.reset_ignored() + assert ret + + ret = softwareupdate.list_ignored() + assert ret == [] + + # Test ignore + ret = softwareupdate.ignore("spongebob") + assert ret + + ret = softwareupdate.ignore("squidward") + assert ret + + # Test list_ignored and verify ignore + ret = softwareupdate.list_ignored() + assert "spongebob" in ret + + ret = softwareupdate.list_ignored() + assert "squidward" in ret + + +@pytest.mark.skip(reason="Ignore schedule support removed from latest OS X.") +def test_schedule(softwareupdate): + """ + Test softwareupdate.schedule_enable + Test softwareupdate.schedule_enabled + """ + # Test enable + ret = softwareupdate.schedule_enable(True) + assert ret + + ret = softwareupdate.schedule_enabled() + assert ret + + # Test disable in case it was already enabled + ret = softwareupdate.schedule_enable(False) + assert not ret + + ret = softwareupdate.schedule_enabled() + assert not ret + + +def test_update(softwareupdate): + """ + Test softwareupdate.update_all + Test softwareupdate.update + Test softwareupdate.update_available + + Need to know the names of updates that are available to properly test + the update functions... + """ + # There's no way to know what the dictionary will contain, so all we can + # check is that the return is a dictionary + ret = softwareupdate.update_all() + assert isinstance(ret, dict) + + # Test update_available + ret = softwareupdate.update_available("spongebob") + assert not ret + + # Test update not available + with pytest.raises(SaltInvocationError) as exc: + ret = softwareupdate.update("spongebob") + assert "Update not available" in str(exc.value) + + +def test_list_downloads(softwareupdate): + """ + Test softwareupdate.list_downloads + """ + ret = softwareupdate.list_downloads() + assert isinstance(ret, list) + + +def test_download(softwareupdate): + """ + Test softwareupdate.download + + Need to know the names of updates that are available to properly test + the download function + """ + # Test update not available + with pytest.raises(SaltInvocationError) as exc: + softwareupdate.download("spongebob") + assert "Update not available" in str(exc.value) + + +def test_download_all(softwareupdate): + """ + Test softwareupdate.download_all + """ + ret = softwareupdate.download_all() + assert isinstance(ret, list) + + +@pytest.mark.skip(reason="Ignore catalog support removed from latest OS X.") +def test_get_set_reset_catalog(softwareupdate): + """ + Test softwareupdate.download_all + """ + # Reset the catalog + ret = softwareupdate.reset_catalog() + assert ret + + ret = softwareupdate.get_catalog() + assert ret == "Default" + + # Test setting and getting the catalog + ret = softwareupdate.set_catalog("spongebob") + assert ret + + ret = softwareupdate.get_catalog() + assert ret == "spongebob" + + # Test reset the catalog + ret = softwareupdate.reset_catalog() + assert ret + + assert softwareupdate.get_catalog() + assert ret == "Default" diff --git a/tests/pytests/functional/modules/test_mac_system.py b/tests/pytests/functional/modules/test_mac_system.py new file mode 100644 index 000000000000..3858ccf01c91 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_system.py @@ -0,0 +1,354 @@ +""" +integration tests for mac_system +""" + +import logging + +import pytest +from saltfactories.utils import random_string + +from salt.exceptions import CommandExecutionError, SaltInvocationError + +log = logging.getLogger(__name__) + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, + pytest.mark.skip_if_binaries_missing("systemsetup"), +] + + +@pytest.fixture +def service(modules): + return modules.service + + +@pytest.fixture +def system(modules): + return modules.system + + +@pytest.fixture +def _remote_login_cleanup(system, grains): + if grains["osmajorrelease"] >= 13: + pytest.skip("SKipping until we figure out how to have full dist access") + + remote_login_enabled = system.get_remote_login() + try: + yield + finally: + if system.get_remote_login() != remote_login_enabled: + system.set_remote_login(remote_login_enabled) + + +@pytest.fixture +def _remote_events_cleanup(system, grains): + if grains["osmajorrelease"] >= 13: + pytest.skip("SKipping until we figure out how to have full dist access") + + remote_events_enabled = system.get_remote_events() + try: + yield + finally: + if system.get_remote_events() != remote_events_enabled: + system.set_remote_events(remote_events_enabled) + + +@pytest.fixture +def _subnet_cleanup(system): + subnet_name = system.get_subnet_name() + try: + yield + finally: + if system.get_subnet_name() != subnet_name: + system.set_subnet_name(subnet_name) + + +@pytest.fixture +def _keyboard_cleanup(system): + keyboard_disabled = system.get_disable_keyboard_on_lock() + try: + yield + finally: + if system.get_disable_keyboard_on_lock() != keyboard_disabled: + system.set_disable_keyboard_on_lock(keyboard_disabled) + + +@pytest.fixture +def _computer_name_cleanup(system): + computer_name = system.get_computer_name() + try: + yield + finally: + if system.get_computer_name() != computer_name: + system.set_computer_name(computer_name) + + +@pytest.fixture(autouse=True) +def _setup_teardown_vars(service, system): + atrun_enabled = service.enabled("com.apple.atrun") + try: + yield + finally: + if not atrun_enabled: + atrun = "/System/Library/LaunchDaemons/com.apple.atrun.plist" + service.stop(atrun) + + +@pytest.mark.usefixtures("_remote_login_cleanup") +def test_get_set_remote_login(system): + """ + Test system.get_remote_login + Test system.set_remote_login + """ + # Normal Functionality + ret = system.set_remote_login(True) + assert ret + + ret = system.get_remote_login() + assert ret + + ret = system.set_remote_login(False) + assert ret + + ret = system.get_remote_login() + assert not ret + + # Test valid input + ret = system.set_remote_login(True) + assert ret + + ret = system.set_remote_login(False) + assert ret + + ret = system.set_remote_login("yes") + assert ret + + ret = system.set_remote_login("no") + assert ret + + ret = system.set_remote_login("On") + assert ret + + ret = system.set_remote_login("Off") + assert ret + + ret = system.set_remote_login(1) + assert ret + + ret = system.set_remote_login(0) + assert ret + + # Test invalid input + with pytest.raises(CommandExecutionError) as exc: + system.set_remote_login("spongebob") + assert "Invalid String Value for Enabled" in str(exc.value) + + +@pytest.mark.usefixtures("_remote_events_cleanup") +def test_get_set_remote_events(system): + """ + Test system.get_remote_events + Test system.set_remote_events + """ + # Normal Functionality + ret = system.set_remote_events(True) + assert ret + + ret = system.get_remote_events() + assert ret + + ret = system.set_remote_events(False) + assert ret + + ret = not system.get_remote_events() + assert not ret + + # Test valid input + ret = system.set_remote_events(True) + assert ret + + ret = system.set_remote_events(False) + assert ret + + ret = system.set_remote_events("yes") + assert ret + + ret = system.set_remote_events("no") + assert ret + + ret = system.set_remote_events("On") + assert ret + + ret = system.set_remote_events("Off") + assert ret + + ret = system.set_remote_events(1) + assert ret + + ret = system.set_remote_events(0) + assert ret + + # Test invalid input + with pytest.raises(CommandExecutionError) as exc: + system.set_remote_events("spongebob") + assert "Invalid String Value for Enabled" in str(exc.value) + + +@pytest.mark.usefixtures("_subnet_cleanup") +def test_get_set_subnet_name(system): + """ + 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) + assert ret + + ret = system.get_subnet_name() + assert ret == set_subnet_name + + +def test_get_list_startup_disk(system): + """ + Test system.get_startup_disk + Test system.list_startup_disks + Don't know how to test system.set_startup_disk as there's usually only + one startup disk available on a system + """ + # Test list and get + ret = system.list_startup_disks() + assert isinstance(ret, list) + + startup_disk = system.get_startup_disk() + assert startup_disk in ret + + # Test passing set a bad disk + with pytest.raises(SaltInvocationError) as exc: + system.set_startup_disk("spongebob") + assert "Invalid value passed for path." in str(exc.value) + + +@pytest.mark.skip(reason="Skip this test until mac fixes it.") +def test_get_set_restart_delay(system): + """ + Test system.get_restart_delay + Test system.set_restart_delay + system.set_restart_delay does not work due to an apple bug, see docs + may need to disable this test as we can't control the delay value + """ + # Normal Functionality + ret = system.set_restart_delay(90) + assert ret + + ret = system.get_restart_delay() + assert ret == "90 seconds" + + # Pass set bad value for seconds + with pytest.raises(CommandExecutionError) as exc: + system.set_restart_delay(70) + assert "Invalid value passed for seconds." in str(exc.value) + + +@pytest.mark.usefixtures("_keyboard_cleanup") +def test_get_set_disable_keyboard_on_lock(system): + """ + Test system.get_disable_keyboard_on_lock + Test system.set_disable_keyboard_on_lock + """ + # Normal Functionality + ret = system.set_disable_keyboard_on_lock(True) + assert ret + + ret = system.get_disable_keyboard_on_lock() + assert ret + + ret = system.set_disable_keyboard_on_lock(False) + assert ret + + ret = system.get_disable_keyboard_on_lock() + assert not ret + + # Test valid input + ret = system.set_disable_keyboard_on_lock(True) + assert ret + + ret = system.set_disable_keyboard_on_lock(False) + assert ret + + ret = system.set_disable_keyboard_on_lock("yes") + assert ret + + ret = system.set_disable_keyboard_on_lock("no") + assert ret + + ret = system.set_disable_keyboard_on_lock("On") + assert ret + + ret = system.set_disable_keyboard_on_lock("Off") + assert ret + + ret = system.set_disable_keyboard_on_lock(1) + assert ret + + ret = system.set_disable_keyboard_on_lock(0) + assert ret + + # Test invalid input + with pytest.raises(SaltInvocationError) as exc: + system.set_disable_keyboard_on_lock("spongebob") + assert "Invalid String Value for Enabled" in str(exc.value) + + +@pytest.mark.skip(reason="Skip this test until mac fixes it.") +def test_get_set_boot_arch(system): + """ + Test system.get_boot_arch + Test system.set_boot_arch + system.set_boot_arch does not work due to an apple bug, see docs + may need to disable this test as we can't set the boot architecture + """ + # Normal Functionality + ret = system.set_boot_arch("i386") + assert ret + + ret = system.get_boot_arch() + assert ret == "i386" + + ret = system.set_boot_arch("default") + assert ret + + ret = system.get_boot_arch() + assert ret == "default" + + # Test invalid input + with pytest.raises(CommandExecutionError) as exc: + system.set_boot_arch("spongebob") + assert "Invalid value passed for arch" in str(exc.value) + + +# A similar test used to be skipped on py3 due to 'hanging', if we see +# something similar again we may want to skip this gain until we +# 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): + """ + Test system.get_computer_name + Test system.set_computer_name + """ + set_computer_name = random_string("RS-", lowercase=False) + + computer_name = system.get_computer_name() + + log.debug("set name is %s", set_computer_name) + ret = system.set_computer_name(set_computer_name) + assert ret + + ret = system.get_computer_name() + assert ret == set_computer_name + + system.set_computer_name(computer_name) diff --git a/tests/pytests/functional/modules/test_mac_timezone.py b/tests/pytests/functional/modules/test_mac_timezone.py new file mode 100644 index 000000000000..251db5417323 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_timezone.py @@ -0,0 +1,216 @@ +""" +Integration tests for mac_timezone + +If using parallels, make sure Time sync is turned off. Otherwise, parallels will +keep changing your date/time settings while the tests are running. To turn off +Time sync do the following: + - Go to actions -> configure + - Select options at the top and 'More Options' on the left + - Set time to 'Do not sync' +""" + +import datetime + +import pytest + +from salt.exceptions import SaltInvocationError + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("systemsetup"), + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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") + + 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) + + +@pytest.mark.destructive_test +def test_get_set_date(timezone): + """ + Test timezone.get_date + Test timezone.set_date + """ + # Correct Functionality + ret = timezone.set_date("2/20/2011") + assert ret + ret = timezone.get_date() + assert ret == "2/20/2011" + + # Test bad date format + with pytest.raises(SaltInvocationError) as exc: + ret = timezone.set_date("13/12/2014") + assert ( + "ERROR executing 'timezone.set_date': Invalid Date/Time Format: 13/12/2014" + in str(exc.value) + ) + + +@pytest.mark.slow_test +def test_get_time(timezone): + """ + Test timezone.get_time + """ + text_time = timezone.get_time() + assert text_time != "Invalid Timestamp" + obj_date = datetime.datetime.strptime(text_time, "%H:%M:%S") + assert isinstance(obj_date, datetime.date) + + +@pytest.mark.destructive_test +def test_set_time(timezone): + """ + Test timezone.set_time + """ + # Correct Functionality + ret = timezone.set_time("3:14") + assert ret + + # Test bad time format + with pytest.raises(SaltInvocationError) as exc: + ret = timezone.set_time("3:71") + assert ( + "ERROR executing 'timezone.set_time': Invalid Date/Time Format: 3:71" + in str(exc.value) + ) + + +@pytest.mark.destructive_test +def test_get_set_zone(timezone): + """ + Test timezone.get_zone + Test timezone.set_zone + """ + # Correct Functionality + ret = timezone.set_zone("Pacific/Wake") + assert ret + + ret = timezone.get_zone() + assert ret == "Pacific/Wake" + + # Test bad time zone + with pytest.raises(SaltInvocationError) as exc: + ret = timezone.set_zone("spongebob") + assert ( + "ERROR executing 'timezone.set_zone': Invalid Timezone: spongebob" + in str(exc.value) + ) + + +@pytest.mark.destructive_test +def test_get_offset(timezone): + """ + Test timezone.get_offset + """ + ret = timezone.set_zone("Pacific/Wake") + assert ret + ret = timezone.get_offset() + assert isinstance(ret, str) + assert ret == "+1200" + + ret = timezone.set_zone("America/Los_Angeles") + assert ret + ret = timezone.get_offset() + assert isinstance(ret, str) + assert ret == "-0800" + + +@pytest.mark.destructive_test +def test_get_set_zonecode(timezone): + """ + Test timezone.get_zonecode + Test timezone.set_zonecode + """ + ret = timezone.set_zone("America/Los_Angeles") + assert ret + ret = timezone.get_zone() + assert isinstance(ret, str) + assert ret == "America/Los_Angeles" + + ret = timezone.set_zone("Pacific/Wake") + assert ret + ret = timezone.get_zone() + assert isinstance(ret, str) + assert ret == "Pacific/Wake" + + +@pytest.mark.slow_test +def test_list_zones(timezone): + """ + Test timezone.list_zones + """ + zones = timezone.list_zones() + assert isinstance(zones, list) + assert "America/Denver" in zones + assert "America/Los_Angeles" in zones + + +@pytest.mark.destructive_test +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 +def test_get_set_using_network_time(timezone): + """ + Test timezone.get_using_network_time + Test timezone.set_using_network_time + """ + ret = timezone.set_using_network_time(True) + assert ret + + ret = timezone.get_using_network_time() + assert ret + + ret = timezone.set_using_network_time(False) + assert ret + + ret = timezone.get_using_network_time() + assert not ret + + +@pytest.mark.destructive_test +def test_get_set_time_server(timezone): + """ + Test timezone.get_time_server + Test timezone.set_time_server + """ + ret = timezone.set_time_server("spongebob.com") + assert ret + + ret = timezone.get_time_server() + assert ret == "spongebob.com" diff --git a/tests/pytests/functional/modules/test_mac_user.py b/tests/pytests/functional/modules/test_mac_user.py new file mode 100644 index 000000000000..42cc0b4fc8d6 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_user.py @@ -0,0 +1,233 @@ +""" + :codeauthor: Nicole Thomas +""" + +import os + +import pytest +from saltfactories.utils import random_string + +import salt.utils.files + +pytestmark = [ + pytest.mark.slow_test, + pytest.mark.destructive_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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 + """ + # 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") + + finally: + # Make sure auto_login is disabled + ret = user.disable_auto_login() + assert ret + + # Make sure autologin is disabled + ret = user.get_auto_login() + if ret: + raise Exception("Failed to disable auto login") + + +@pytest.mark.slow_test +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") + + 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 Exception("Failed to enable auto login") + + # 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") + + 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") diff --git a/tests/pytests/functional/modules/test_mac_xattr.py b/tests/pytests/functional/modules/test_mac_xattr.py new file mode 100644 index 000000000000..8dd72ac57709 --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_xattr.py @@ -0,0 +1,193 @@ +""" +integration tests for mac_xattr +""" + +import pytest + +from salt.exceptions import CommandExecutionError + +pytestmark = [ + pytest.mark.skip_if_binaries_missing("xattr"), + pytest.mark.slow_test, + pytest.mark.skip_if_not_root, + pytest.mark.skip_unless_on_darwin, +] + + +@pytest.fixture(scope="module") +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() + + +def test_list_no_xattr(xattr, setup_teardown_vars): + """ + 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) + assert ret + + # Test no attributes + ret = xattr.list(test_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) + + +def test_write(xattr, setup_teardown_vars): + """ + Write an attribute + """ + test_file = setup_teardown_vars[0] + no_file = setup_teardown_vars[1] + + # Clear existing attributes + ret = xattr.clear(test_file) + assert ret + + # Write some attributes + ret = xattr.write(test_file, "spongebob", "squarepants") + assert ret + + ret = xattr.write(test_file, "squidward", "plankton") + assert ret + + ret = xattr.write(test_file, "crabby", "patty") + assert ret + + # Test that they were actually added + ret = xattr.list(test_file) + assert ret == { + "spongebob": "squarepants", + "squidward": "plankton", + "crabby": "patty", + } + + # 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) + + +def test_read(xattr, setup_teardown_vars): + """ + Test xattr.read + """ + test_file = setup_teardown_vars[0] + no_file = setup_teardown_vars[1] + + # Clear existing attributes + ret = xattr.clear(test_file) + assert ret + + # Write an attribute + ret = xattr.write(test_file, "spongebob", "squarepants") + assert ret + + # Read the attribute + ret = xattr.read(test_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) + + # Test attribute not found + with pytest.raises(CommandExecutionError) as exc: + ret = xattr.read(test_file, "patrick") + assert "Attribute not found: patrick" in str(exc.value) + + +def test_delete(xattr, setup_teardown_vars): + """ + Test xattr.delete + """ + test_file = setup_teardown_vars[0] + no_file = setup_teardown_vars[1] + + # Clear existing attributes + ret = xattr.clear(test_file) + assert ret + + # Write some attributes + ret = xattr.write(test_file, "spongebob", "squarepants") + assert ret + + ret = xattr.write(test_file, "squidward", "plankton") + assert ret + + ret = xattr.write(test_file, "crabby", "patty") + assert ret + + # Delete an attribute + ret = xattr.delete(test_file, "squidward") + assert ret + + # Make sure it was actually deleted + ret = xattr.list(test_file) + assert ret == { + "spongebob": "squarepants", + "crabby": "patty", + } + + # 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) + + # Test attribute not found + with pytest.raises(CommandExecutionError) as exc: + ret = xattr.delete(test_file, "patrick") + assert "Attribute not found: patrick" in str(exc.value) + + +def test_clear(xattr, setup_teardown_vars): + """ + Test xattr.clear + """ + test_file = setup_teardown_vars[0] + no_file = setup_teardown_vars[1] + + # Clear existing attributes + ret = xattr.clear(test_file) + assert ret + + # Write some attributes + ret = xattr.write(test_file, "spongebob", "squarepants") + assert ret + + ret = xattr.write(test_file, "squidward", "plankton") + assert ret + + ret = xattr.write(test_file, "crabby", "patty") + assert ret + + # Test Clear + ret = xattr.clear(test_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) diff --git a/tests/pytests/unit/modules/test_mac_keychain.py b/tests/pytests/unit/modules/test_mac_keychain.py index eb411e69b57b..bbf9d20aaf41 100644 --- a/tests/pytests/unit/modules/test_mac_keychain.py +++ b/tests/pytests/unit/modules/test_mac_keychain.py @@ -65,7 +65,7 @@ def test_list_certs(): out = keychain.list_certs("/path/to/cert.p12") mock.assert_called_once_with( "security find-certificate -a /path/to/cert.p12 | " - 'grep -o "alis".*\\" | grep -o \'\\"[-A-Za-z0-9.:() ]*\\"\'', + 'grep -o "alis.*" | grep -o \'\\"[-A-Za-z0-9.:() ]*\\"\'', python_shell=True, ) @@ -79,7 +79,18 @@ def test_get_friendly_name(): expected = "ID Installer Salt" mock = MagicMock(return_value="friendlyName: ID Installer Salt") with patch.dict(keychain.__salt__, {"cmd.run": mock}): - out = keychain.get_friendly_name("/path/to/cert.p12", "passw0rd") + out = keychain.get_friendly_name("/path/to/cert.p12", "passw0rd", legacy=True) + mock.assert_called_once_with( + "openssl pkcs12 -legacy -in /path/to/cert.p12 -passin pass:passw0rd -info " + "-nodes -nokeys 2> /dev/null | grep friendlyName:", + python_shell=True, + ) + + assert out == expected + + mock = MagicMock(return_value="friendlyName: ID Installer Salt") + with patch.dict(keychain.__salt__, {"cmd.run": mock}): + out = keychain.get_friendly_name("/path/to/cert.p12", "passw0rd", legacy=False) mock.assert_called_once_with( "openssl pkcs12 -in /path/to/cert.p12 -passin pass:passw0rd -info " "-nodes -nokeys 2> /dev/null | grep friendlyName:",