From 5a2a4f44bf814416efd5f6b708f0e60173fca7c8 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 8 Apr 2022 19:13:05 +0100 Subject: [PATCH] Migrate ``integration.modules.test_mac_pkgutil`` to PyTest and functional tests. Fixes #60819 Signed-off-by: Pedro Algarvio --- changelog/60819.fixed | 1 + tests/integration/modules/test_mac_pkgutil.py | 92 ------------- .../functional/modules/test_mac_pkgutil.py | 121 ++++++++++++++++++ 3 files changed, 122 insertions(+), 92 deletions(-) create mode 100644 changelog/60819.fixed delete mode 100644 tests/integration/modules/test_mac_pkgutil.py create mode 100644 tests/pytests/functional/modules/test_mac_pkgutil.py diff --git a/changelog/60819.fixed b/changelog/60819.fixed new file mode 100644 index 000000000000..337b2452997f --- /dev/null +++ b/changelog/60819.fixed @@ -0,0 +1 @@ +The mac assistive execution module no longer shells out to change the database. diff --git a/tests/integration/modules/test_mac_pkgutil.py b/tests/integration/modules/test_mac_pkgutil.py deleted file mode 100644 index 7f4ca61c6bdc..000000000000 --- a/tests/integration/modules/test_mac_pkgutil.py +++ /dev/null @@ -1,92 +0,0 @@ -""" -integration tests for mac_pkgutil -""" - -import os - -import pytest -from tests.support.case import ModuleCase -from tests.support.helpers import requires_system_grains, runs_on -from tests.support.runtests import RUNTIME_VARS - -TEST_PKG_URL = ( - "https://distfiles.macports.org/MacPorts/MacPorts-2.3.4-10.11-ElCapitan.pkg" -) -TEST_PKG_NAME = "org.macports.MacPorts" - - -@runs_on(kernel="Darwin") -@pytest.mark.skip_if_not_root -@pytest.mark.skip_if_binaries_missing("pkgutil") -class MacPkgutilModuleTest(ModuleCase): - """ - Validate the mac_pkgutil module - """ - - @classmethod - def setUpClass(cls): - cls.test_pkg = os.path.join( - RUNTIME_VARS.TMP, "MacPorts-2.3.4-10.11-ElCapitan.pkg" - ) - - @requires_system_grains - def setUp(self, grains): # pylint: disable=arguments-differ - """ - Get current settings - """ - os_release_info = grains["osrelease_info"] - if os_release_info[0] >= 11: - self.pkg_name = "com.apple.pkg.InstallAssistant.macOSBigSur" - else: - self.pkg_name = "com.apple.pkg.BaseSystemResources" - - def tearDown(self): - """ - Reset to original settings - """ - self.run_function("pkgutil.forget", [TEST_PKG_NAME]) - self.run_function("file.remove", ["/opt/local"]) - - @pytest.mark.slow_test - def test_list(self): - """ - Test pkgutil.list - """ - self.assertIsInstance(self.run_function("pkgutil.list"), list) - self.assertIn(self.pkg_name, self.run_function("pkgutil.list")) - - @pytest.mark.slow_test - def test_is_installed(self): - """ - Test pkgutil.is_installed - """ - # Test Package is installed - self.assertTrue(self.run_function("pkgutil.is_installed", [self.pkg_name])) - - # Test Package is not installed - self.assertFalse(self.run_function("pkgutil.is_installed", ["spongebob"])) - - @pytest.mark.destructive_test - @pytest.mark.slow_test - def test_install_forget(self): - """ - Test pkgutil.install - Test pkgutil.forget - """ - # Test if installed - self.assertFalse(self.run_function("pkgutil.is_installed", [TEST_PKG_NAME])) - - # Download the package - self.run_function("cp.get_url", [TEST_PKG_URL, self.test_pkg]) - - # Test install - self.assertTrue( - self.run_function("pkgutil.install", [self.test_pkg, TEST_PKG_NAME]) - ) - self.assertIn( - "Unsupported scheme", - self.run_function("pkgutil.install", ["ftp://test", "spongebob"]), - ) - - # Test forget - self.assertTrue(self.run_function("pkgutil.forget", [TEST_PKG_NAME])) diff --git a/tests/pytests/functional/modules/test_mac_pkgutil.py b/tests/pytests/functional/modules/test_mac_pkgutil.py new file mode 100644 index 000000000000..98c31b31672e --- /dev/null +++ b/tests/pytests/functional/modules/test_mac_pkgutil.py @@ -0,0 +1,121 @@ +""" +integration tests for mac_pkgutil +""" +import shutil + +import pytest +from salt.exceptions import SaltInvocationError + +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("pkgutil"), +] + + +@pytest.fixture(scope="module") +def pkgutil(modules): + return modules.pkgutil + + +@pytest.fixture +def macports_package_name(pkgutil): + pacakge_name = "org.macports.MacPorts" + try: + yield pacakge_name + finally: + try: + pkgutil.forget(pacakge_name) + except Exception: # pylint: disable=broad-except + pass + shutil.rmtree("/opt/local", ignore_errors=True) + + +@pytest.fixture(scope="module") +def macports_package_filename(grains): + if grains["osrelease_info"][0] == 12: + return "MacPorts-2.7.2-12-Monterey.pkg" + if grains["osrelease_info"][0] == 11: + return "MacPorts-2.7.2-11-BigSur.pkg" + if grains["osrelease_info"][:2] == (10, 15): + return "MacPorts-2.7.2-10.15-Catalina.pkg" + pytest.fail( + "Don't know how to handle '{}'. Please fix the test".format(grains["osfinger"]) + ) + + +@pytest.fixture(scope="module") +def macports_package_url(macports_package_filename): + return "https://distfiles.macports.org/MacPorts/{}".format( + macports_package_filename + ) + + +@pytest.fixture(scope="module") +def pkg_name(grains): + if grains["osrelease_info"][0] >= 11: + return "com.apple.pkg.InstallAssistant.macOSBigSur" + if grains["osrelease_info"][:2] == (10, 15): + return "com.apple.pkg.iTunesX" + pytest.fail( + "Don't know how to handle '{}'. Please fix the test".format(grains["osfinger"]) + ) + + +def test_list(pkgutil, pkg_name): + """ + Test pkgutil.list + """ + packages_list = pkgutil.list() + assert isinstance(packages_list, list) + assert pkg_name in packages_list + + +def test_is_installed(pkgutil, pkg_name): + """ + Test pkgutil.is_installed + """ + # Test Package is installed + assert pkgutil.is_installed(pkg_name) + + # Test Package is not installed + assert not pkgutil.is_installed("spongebob") + + +@pytest.mark.skipif( + True, reason="I don't know how to fix this test. Pedro(s0undt3ch), 2022-04-08" +) +def test_install_forget( + tmp_path, + modules, + pkgutil, + macports_package_name, + macports_package_filename, + macports_package_url, +): + """ + Test pkgutil.install + Test pkgutil.forget + """ + pkg_local_path = str(tmp_path / macports_package_filename) + + # Test if installed + assert not pkgutil.is_installed(macports_package_name) + + # Download the package + modules.cp.get_url(macports_package_url, pkg_local_path) + + # Test install + assert pkgutil.install(pkg_local_path, macports_package_name) + assert pkgutil.is_installed(macports_package_name) + + # Test forget + assert pkgutil.forget(macports_package_name) + + +def test_install_unsupported_scheme(pkgutil): + with pytest.raises(SaltInvocationError) as exc: + pkgutil.install("ftp://test", "spongebob") + assert "Unsupported scheme" in str(exc.value)