Moving tests/integration/modules/test_pkg.py to tests/pytests/functional/states/test_pkg.py

This commit is contained in:
Gareth J. Greenaway 2021-10-11 16:32:19 -07:00 committed by Megan Wilhite
parent 93f69d9710
commit 5f0659c510
4 changed files with 480 additions and 494 deletions

View file

@ -10,7 +10,7 @@ setup.py:
salt/modules/(apkpkg|aptpkg|ebuildpkg|dpkg_lowpkg|freebsdpkg|mac_brew_pkg|mac_ports_pkg|openbsdpkg|opkg|pacmanpkg|pkgin|pkgng|pkg_resource|rpm_lowpkg|solarisipspkg|solarispkg|win_pkg|xbpspkg|yumpkg|zypperpkg)\.py:
- pytests.unit.states.test_pkg
- integration.modules.test_pkg
- pytests.functional.modules.test_pkg
- integration.states.test_pkg
- integration.states.test_pkgrepo

View file

@ -1,493 +0,0 @@
import os
import pprint
import pytest
import salt.utils.path
import salt.utils.pkg
import salt.utils.platform
from tests.support.case import ModuleCase
from tests.support.helpers import requires_system_grains
from tests.support.mixins import SaltReturnAssertsMixin
from tests.support.unit import skipIf
@pytest.mark.windows_whitelisted
class PkgModuleTest(ModuleCase, SaltReturnAssertsMixin):
"""
Validate the pkg module
"""
@classmethod
@requires_system_grains
def setUpClass(cls, grains): # pylint: disable=arguments-differ
cls.ctx = {}
cls.pkg = "figlet"
if salt.utils.platform.is_windows():
cls.pkg = "putty"
elif grains["os_family"] == "RedHat":
if grains["os"] == "VMware Photon OS":
cls.pkg = "snoopy"
else:
cls.pkg = "units"
@pytest.mark.skip_if_not_root
@pytest.mark.requires_salt_modules("pkg.refresh_db")
def setUp(self):
if "refresh" not in self.ctx:
self.run_function("pkg.refresh_db")
self.ctx["refresh"] = True
@pytest.mark.requires_salt_modules("pkg.list_pkgs")
@pytest.mark.slow_test
def test_list(self):
"""
verify that packages are installed
"""
ret = self.run_function("pkg.list_pkgs")
self.assertNotEqual(len(ret.keys()), 0)
@pytest.mark.requires_salt_modules("pkg.version_cmp")
@requires_system_grains
@pytest.mark.slow_test
def test_version_cmp(self, grains):
"""
test package version comparison on supported platforms
"""
func = "pkg.version_cmp"
if grains["os_family"] == "Debian":
lt = ["0.2.4-0ubuntu1", "0.2.4.1-0ubuntu1"]
eq = ["0.2.4-0ubuntu1", "0.2.4-0ubuntu1"]
gt = ["0.2.4.1-0ubuntu1", "0.2.4-0ubuntu1"]
elif grains["os_family"] == "Suse":
lt = ["2.3.0-1", "2.3.1-15.1"]
eq = ["2.3.1-15.1", "2.3.1-15.1"]
gt = ["2.3.2-15.1", "2.3.1-15.1"]
else:
lt = ["2.3.0", "2.3.1"]
eq = ["2.3.1", "2.3.1"]
gt = ["2.3.2", "2.3.1"]
self.assertEqual(self.run_function(func, lt), -1)
self.assertEqual(self.run_function(func, eq), 0)
self.assertEqual(self.run_function(func, gt), 1)
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.mod_repo", "pkg.del_repo", "pkg.get_repo")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_mod_del_repo(self, grains):
"""
test modifying and deleting a software repository
"""
repo = None
try:
if grains["os"] == "Ubuntu":
repo = "ppa:otto-kesselgulasch/gimp-edge"
uri = "http://ppa.launchpad.net/otto-kesselgulasch/gimp-edge/ubuntu"
ret = self.run_function("pkg.mod_repo", [repo, "comps=main"])
self.assertNotEqual(ret, {})
ret = self.run_function("pkg.get_repo", [repo])
self.assertIsInstance(
ret,
dict,
"The 'pkg.get_repo' command did not return the excepted dictionary."
" Output:\n{}".format(ret),
)
self.assertEqual(
ret["uri"],
uri,
msg="The URI did not match. Full return:\n{}".format(
pprint.pformat(ret)
),
)
elif grains["os_family"] == "RedHat":
repo = "saltstack"
name = "SaltStack repo for RHEL/CentOS {}".format(
grains["osmajorrelease"]
)
baseurl = (
"http://repo.saltproject.io/py3/redhat/{}/x86_64/latest/".format(
grains["osmajorrelease"]
)
)
gpgkey = "https://repo.saltproject.io/py3/redhat/{}/x86_64/latest/SALTSTACK-GPG-KEY.pub".format(
grains["osmajorrelease"]
)
gpgcheck = 1
enabled = 1
ret = self.run_function(
"pkg.mod_repo",
[repo],
name=name,
baseurl=baseurl,
gpgkey=gpgkey,
gpgcheck=gpgcheck,
enabled=enabled,
)
# return data from pkg.mod_repo contains the file modified at
# the top level, so use next(iter(ret)) to get that key
self.assertNotEqual(ret, {})
repo_info = ret[next(iter(ret))]
self.assertIn(repo, repo_info)
self.assertEqual(repo_info[repo]["baseurl"], baseurl)
ret = self.run_function("pkg.get_repo", [repo])
self.assertEqual(ret["baseurl"], baseurl)
finally:
if repo is not None:
self.run_function("pkg.del_repo", [repo])
@pytest.mark.slow_test
def test_mod_del_repo_multiline_values(self):
"""
test modifying and deleting a software repository defined with multiline values
"""
os_grain = self.run_function("grains.item", ["os"])["os"]
repo = None
try:
if os_grain in ["CentOS", "RedHat", "VMware Photon OS"]:
my_baseurl = (
"http://my.fake.repo/foo/bar/\n http://my.fake.repo.alt/foo/bar/"
)
expected_get_repo_baseurl = (
"http://my.fake.repo/foo/bar/\nhttp://my.fake.repo.alt/foo/bar/"
)
major_release = int(
self.run_function("grains.item", ["osmajorrelease"])[
"osmajorrelease"
]
)
repo = "fakerepo"
name = "Fake repo for RHEL/CentOS/SUSE"
baseurl = my_baseurl
gpgkey = "https://my.fake.repo/foo/bar/MY-GPG-KEY.pub"
failovermethod = "priority"
gpgcheck = 1
enabled = 1
ret = self.run_function(
"pkg.mod_repo",
[repo],
name=name,
baseurl=baseurl,
gpgkey=gpgkey,
gpgcheck=gpgcheck,
enabled=enabled,
failovermethod=failovermethod,
)
# return data from pkg.mod_repo contains the file modified at
# the top level, so use next(iter(ret)) to get that key
self.assertNotEqual(ret, {})
repo_info = ret[next(iter(ret))]
self.assertIn(repo, repo_info)
self.assertEqual(repo_info[repo]["baseurl"], my_baseurl)
ret = self.run_function("pkg.get_repo", [repo])
self.assertEqual(ret["baseurl"], expected_get_repo_baseurl)
self.run_function("pkg.mod_repo", [repo])
ret = self.run_function("pkg.get_repo", [repo])
self.assertEqual(ret["baseurl"], expected_get_repo_baseurl)
finally:
if repo is not None:
self.run_function("pkg.del_repo", [repo])
@pytest.mark.requires_salt_modules("pkg.owner")
def test_owner(self):
"""
test finding the package owning a file
"""
func = "pkg.owner"
ret = self.run_function(func, ["/bin/ls"])
self.assertNotEqual(len(ret), 0)
# Similar to pkg.owner, but for FreeBSD's pkgng
@pytest.mark.requires_salt_modules("pkg.which")
def test_which(self):
"""
test finding the package owning a file
"""
func = "pkg.which"
ret = self.run_function(func, ["/usr/local/bin/salt-call"])
self.assertNotEqual(len(ret), 0)
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.version", "pkg.install", "pkg.remove")
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_install_remove(self):
"""
successfully install and uninstall a package
"""
version = self.run_function("pkg.version", [self.pkg])
def test_install():
install_ret = self.run_function("pkg.install", [self.pkg])
self.assertIn(self.pkg, install_ret)
def test_remove():
remove_ret = self.run_function("pkg.remove", [self.pkg])
self.assertIn(self.pkg, remove_ret)
if version and isinstance(version, dict):
version = version[self.pkg]
if version:
test_remove()
test_install()
else:
test_install()
test_remove()
@pytest.mark.destructive_test
@pytest.mark.skipif(
salt.utils.platform.is_photonos(),
reason="package hold/unhold unsupported on Photon OS",
)
@pytest.mark.requires_salt_modules(
"pkg.hold",
"pkg.unhold",
"pkg.install",
"pkg.version",
"pkg.remove",
"pkg.list_pkgs",
)
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
@pytest.mark.requires_salt_states("pkg.installed")
def test_hold_unhold(self, grains):
"""
test holding and unholding a package
"""
versionlock_pkg = None
if grains["os_family"] == "RedHat":
pkgs = {
p for p in self.run_function("pkg.list_pkgs") if "-versionlock" in p
}
if not pkgs:
self.skipTest("No versionlock package found in repositories")
for versionlock_pkg in pkgs:
ret = self.run_state(
"pkg.installed", name=versionlock_pkg, refresh=False
)
# Exit loop if a versionlock package installed correctly
try:
self.assertSaltTrueReturn(ret)
break
except AssertionError:
pass
else:
self.fail("Could not install versionlock package from {}".format(pkgs))
self.run_function("pkg.install", [self.pkg])
try:
hold_ret = self.run_function("pkg.hold", [self.pkg])
if versionlock_pkg and "-versionlock is not installed" in str(hold_ret):
self.skipTest("{} `{}` is installed".format(hold_ret, versionlock_pkg))
self.assertIn(self.pkg, hold_ret)
self.assertTrue(hold_ret[self.pkg]["result"])
unhold_ret = self.run_function("pkg.unhold", [self.pkg])
self.assertIn(self.pkg, unhold_ret)
self.assertTrue(unhold_ret[self.pkg]["result"])
self.run_function("pkg.remove", [self.pkg])
finally:
if versionlock_pkg:
ret = self.run_state("pkg.removed", name=versionlock_pkg)
self.assertSaltTrueReturn(ret)
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.refresh_db")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_refresh_db(self, grains):
"""
test refreshing the package database
"""
func = "pkg.refresh_db"
rtag = salt.utils.pkg.rtag(self.minion_opts)
salt.utils.pkg.write_rtag(self.minion_opts)
self.assertTrue(os.path.isfile(rtag))
ret = self.run_function(func)
if not isinstance(ret, dict):
self.skipTest(
"Upstream repo did not return coherent results: {}".format(ret)
)
if grains["os_family"] == "RedHat":
self.assertIn(ret, (True, None))
elif grains["os_family"] == "Suse":
if not isinstance(ret, dict):
self.skipTest(
"Upstream repo did not return coherent results. Skipping test."
)
self.assertNotEqual(ret, {})
for source, state in ret.items():
self.assertIn(state, (True, False, None))
self.assertFalse(os.path.isfile(rtag))
@pytest.mark.requires_salt_modules("pkg.info_installed")
@requires_system_grains
@pytest.mark.slow_test
def test_pkg_info(self, grains):
"""
Test returning useful information on Ubuntu systems.
"""
func = "pkg.info_installed"
if grains["os_family"] == "Debian":
ret = self.run_function(func, ["bash", "dpkg"])
keys = ret.keys()
self.assertIn("bash", keys)
self.assertIn("dpkg", keys)
elif grains["os_family"] == "RedHat":
ret = self.run_function(func, ["rpm", "bash"])
keys = ret.keys()
self.assertIn("rpm", keys)
self.assertIn("bash", keys)
elif grains["os_family"] == "Suse":
ret = self.run_function(func, ["less", "zypper"])
keys = ret.keys()
self.assertIn("less", keys)
self.assertIn("zypper", keys)
else:
ret = self.run_function(func, [self.pkg])
keys = ret.keys()
self.assertIn(self.pkg, keys)
@skipIf(True, "Temporary Skip - Causes centos 8 test to fail")
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules(
"pkg.refresh_db",
"pkg.upgrade",
"pkg.install",
"pkg.list_repo_pkgs",
"pkg.list_upgrades",
)
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_pkg_upgrade_has_pending_upgrades(self, grains):
"""
Test running a system upgrade when there are packages that need upgrading
"""
if grains["os"] == "Arch":
self.skipTest("Arch moved to Python 3.8 and we're not ready for it yet")
func = "pkg.upgrade"
# First make sure that an up-to-date copy of the package db is available
self.run_function("pkg.refresh_db")
if grains["os_family"] == "Suse":
# This test assumes that there are multiple possible versions of a
# package available. That makes it brittle if you pick just one
# target, as changes in the available packages will break the test.
# Therefore, we'll choose from several packages to make sure we get
# one that is suitable for this test.
packages = ("hwinfo", "avrdude", "diffoscope", "vim")
available = self.run_function("pkg.list_repo_pkgs", packages)
for package in packages:
try:
new, old = available[package][:2]
except (KeyError, ValueError):
# Package not available, or less than 2 versions
# available. This is not a suitable target.
continue
else:
target = package
break
else:
# None of the packages have more than one version available, so
# we need to find new package(s). pkg.list_repo_pkgs can be
# used to get an overview of the available packages. We should
# try to find packages with few dependencies and small download
# sizes, to keep this test from taking longer than necessary.
self.fail("No suitable package found for this test")
# Make sure we have the 2nd-oldest available version installed
ret = self.run_function("pkg.install", [target], version=old)
if not isinstance(ret, dict):
if ret.startswith("ERROR"):
self.skipTest(
"Could not install older {} to complete test.".format(target)
)
# Run a system upgrade, which should catch the fact that the
# targeted package needs upgrading, and upgrade it.
ret = self.run_function(func)
# The changes dictionary should not be empty.
if "changes" in ret:
self.assertIn(target, ret["changes"])
else:
self.assertIn(target, ret)
else:
ret = self.run_function("pkg.list_upgrades")
if ret == "" or ret == {}:
self.skipTest(
"No updates available for this machine. Skipping pkg.upgrade test."
)
else:
args = []
if grains["os_family"] == "Debian":
args = ["dist_upgrade=True"]
ret = self.run_function(func, args)
self.assertNotEqual(ret, {})
@pytest.mark.destructive_test
@skipIf(
salt.utils.platform.is_darwin(),
"The jenkins user is equivalent to root on mac, causing the test to be"
" unrunnable",
)
@pytest.mark.requires_salt_modules("pkg.remove", "pkg.latest_version")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_salt_states("pkg.removed")
def test_pkg_latest_version(self, grains):
"""
Check that pkg.latest_version returns the latest version of the uninstalled package.
The package is not installed. Only the package version is checked.
"""
self.run_state("pkg.removed", name=self.pkg)
cmd_pkg = []
if grains["os_family"] == "RedHat":
cmd_pkg = self.run_function("cmd.run", ["yum list {}".format(self.pkg)])
elif salt.utils.platform.is_windows():
cmd_pkg = self.run_function("pkg.list_available", [self.pkg])
elif grains["os_family"] == "Debian":
cmd_pkg = self.run_function("cmd.run", ["apt list {}".format(self.pkg)])
elif grains["os_family"] == "Arch":
cmd_pkg = self.run_function("cmd.run", ["pacman -Si {}".format(self.pkg)])
elif grains["os_family"] == "FreeBSD":
cmd_pkg = self.run_function(
"cmd.run", ["pkg search -S name -qQ version -e {}".format(self.pkg)]
)
elif grains["os_family"] == "Suse":
cmd_pkg = self.run_function("cmd.run", ["zypper info {}".format(self.pkg)])
elif grains["os_family"] == "MacOS":
brew_bin = salt.utils.path.which("brew")
mac_user = self.run_function("file.get_user", [brew_bin])
if mac_user == "root":
self.skipTest(
"brew cannot run as root, try a user in {}".format(
os.listdir("/Users/")
)
)
cmd_pkg = self.run_function(
"cmd.run", ["brew info {}".format(self.pkg)], run_as=mac_user
)
else:
self.skipTest(
"TODO: test not configured for {}".format(grains["os_family"])
)
pkg_latest = self.run_function("pkg.latest_version", [self.pkg])
self.assertIn(pkg_latest, cmd_pkg)

View file

@ -4,3 +4,8 @@ import pytest
@pytest.fixture(scope="module")
def modules(loaders):
return loaders.modules
@pytest.fixture(scope="module")
def states(loaders):
return loaders.states

View file

@ -0,0 +1,474 @@
import os
import time
import pytest
import salt.utils.path
import salt.utils.pkg
import salt.utils.platform
from tests.support.helpers import requires_system_grains
@pytest.fixture
def ctx():
return {}
@pytest.fixture(autouse=True)
@requires_system_grains
def refresh_db(ctx, grains, modules):
if "refresh" not in ctx:
# modules.pkg.refresh_db()
ctx["refresh"] = True
# If this is Arch Linux, check if pacman is in use by another process
if grains["os_family"] == "Arch":
for _ in range(12):
if not os.path.isfile("/var/lib/pacman/db.lck"):
break
else:
time.sleep(5)
else:
raise Exception("Package database locked after 60 seconds, bailing out")
@pytest.fixture(autouse=True)
@requires_system_grains
def test_pkg(grains):
_pkg = "figlet"
if salt.utils.platform.is_windows():
_pkg = "putty"
elif grains["os_family"] == "RedHat":
if grains["os"] == "VMware Photon OS":
_pkg = "snoopy"
else:
_pkg = "units"
return _pkg
@pytest.mark.requires_salt_modules("pkg.list_pkgs")
@pytest.mark.slow_test
def test_list(modules):
"""
verify that packages are installed
"""
ret = modules.pkg.list_pkgs()
assert len(ret.keys()) != 0
@pytest.mark.requires_salt_modules("pkg.version_cmp")
@requires_system_grains
@pytest.mark.slow_test
def test_version_cmp(grains, modules):
"""
test package version comparison on supported platforms
"""
if grains["os_family"] == "Debian":
lt = ["0.2.4-0ubuntu1", "0.2.4.1-0ubuntu1"]
eq = ["0.2.4-0ubuntu1", "0.2.4-0ubuntu1"]
gt = ["0.2.4.1-0ubuntu1", "0.2.4-0ubuntu1"]
elif grains["os_family"] == "Suse":
lt = ["2.3.0-1", "2.3.1-15.1"]
eq = ["2.3.1-15.1", "2.3.1-15.1"]
gt = ["2.3.2-15.1", "2.3.1-15.1"]
else:
lt = ["2.3.0", "2.3.1"]
eq = ["2.3.1", "2.3.1"]
gt = ["2.3.2", "2.3.1"]
assert modules.pkg.version_cmp(lt) == -1
assert modules.pkg.version_cmp(eq) == 0
assert modules.pkg.version_cmp(gt) == 1
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.mod_repo", "pkg.del_repo", "pkg.get_repo")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_mod_del_repo(grains, modules):
"""
test modifying and deleting a software repository
"""
repo = None
try:
if grains["os"] == "Ubuntu":
repo = "ppa:otto-kesselgulasch/gimp-edge"
uri = "http://ppa.launchpad.net/otto-kesselgulasch/gimp-edge/ubuntu"
ret = modules.pkg.mod_repo(repo, "comps=main")
assert ret != []
ret = modules.pkg.get_repo(repo)
assert isinstance(ret, dict) is True
assert ret["uri"] == uri
elif grains["os_family"] == "RedHat":
repo = "saltstack"
name = "SaltStack repo for RHEL/CentOS {}".format(grains["osmajorrelease"])
baseurl = "http://repo.saltproject.io/py3/redhat/{}/x86_64/latest/".format(
grains["osmajorrelease"]
)
gpgkey = "https://repo.saltproject.io/py3/redhat/{}/x86_64/latest/SALTSTACK-GPG-KEY.pub".format(
grains["osmajorrelease"]
)
gpgcheck = 1
enabled = 1
ret = modules.pkg.mod_repo(
[repo],
name=name,
baseurl=baseurl,
gpgkey=gpgkey,
gpgcheck=gpgcheck,
enabled=enabled,
)
# return data from pkg.mod_repo contains the file modified at
# the top level, so use next(iter(ret)) to get that key
assert ret != {}
repo_info = ret[next(iter(ret))]
assert repo in repo_info
assert repo_info[repo]["baseurl"] == baseurl
ret = modules.pkg.get_repo(repo)
assert ret["baseurl"] == baseurl
finally:
if repo is not None:
modules.pkg.del_repo(repo)
@pytest.mark.slow_test
def test_mod_del_repo_multiline_values(modules):
"""
test modifying and deleting a software repository defined with multiline values
"""
os_grain = modules.grains.item("os")["os"]
repo = None
try:
if os_grain in ["CentOS", "RedHat", "VMware Photon OS"]:
my_baseurl = (
"http://my.fake.repo/foo/bar/\n http://my.fake.repo.alt/foo/bar/"
)
expected_get_repo_baseurl = (
"http://my.fake.repo/foo/bar/\nhttp://my.fake.repo.alt/foo/bar/"
)
major_release = int(modules.grains.item("osmajorrelease")["osmajorrelease"])
repo = "fakerepo"
name = "Fake repo for RHEL/CentOS/SUSE"
baseurl = my_baseurl
gpgkey = "https://my.fake.repo/foo/bar/MY-GPG-KEY.pub"
failovermethod = "priority"
gpgcheck = 1
enabled = 1
ret = modules.pkg.mod_repo(
[repo],
name=name,
baseurl=baseurl,
gpgkey=gpgkey,
gpgcheck=gpgcheck,
enabled=enabled,
failovermethod=failovermethod,
)
# return data from pkg.mod_repo contains the file modified at
# the top level, so use next(iter(ret)) to get that key
assert ret != {}
repo_info = ret[next(iter(ret))]
assert repo in repo_info
assert repo_info[repo]["baseurl"] == my_baseurl
ret = modules.pkg.get_repo(repo)
assert ret["baseurl"] == expected_get_repo_baseurl
modules.pkg.mod_repo(repo)
ret = modules.pkg.get_repo(repo)
assert ret["baseurl"] == expected_get_repo_baseurl
finally:
if repo is not None:
modules.pkg.del_repo(repo)
@pytest.mark.requires_salt_modules("pkg.owner")
def test_owner(modules):
"""
test finding the package owning a file
"""
func = "pkg.owner"
ret = modules.pkg.owner("/bin/ls")
assert len(ret) != 0
# Similar to pkg.owner, but for FreeBSD's pkgng
@pytest.mark.requires_salt_modules("pkg.which")
def test_which(modules):
"""
test finding the package owning a file
"""
func = "pkg.which"
ret = modules.pkg.which("/usr/local/bin/salt-call")
assert len(ret) != 0
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.version", "pkg.install", "pkg.remove")
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_install_remove(modules, test_pkg):
"""
successfully install and uninstall a package
"""
version = modules.pkg.version(test_pkg)
def test_install():
install_ret = modules.pkg.install(test_pkg)
assert test_pkg in install_ret
def test_remove():
remove_ret = modules.pkg.remove(test_pkg)
assert test_pkg in remove_ret
if version and isinstance(version, dict):
version = version[test_pkg]
if version:
test_remove()
test_install()
else:
test_install()
test_remove()
@pytest.mark.destructive_test
@pytest.mark.skipif(
salt.utils.platform.is_photonos(),
reason="package hold/unhold unsupported on Photon OS",
)
@pytest.mark.requires_salt_modules(
"pkg.hold",
"pkg.unhold",
"pkg.install",
"pkg.version",
"pkg.remove",
"pkg.list_pkgs",
)
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
@pytest.mark.requires_salt_states("pkg.installed")
def test_hold_unhold(grains, modules, states, test_pkg):
"""
test holding and unholding a package
"""
versionlock_pkg = None
if grains["os_family"] == "RedHat":
pkgs = {p for p in modules.pkg.list_pkgs() if "-versionlock" in p}
if not pkgs:
pytest.skip("No versionlock package found in repositories")
for versionlock_pkg in pkgs:
ret = states.pkg.installed(name=versionlock_pkg, refresh=False)
# Exit loop if a versionlock package installed correctly
try:
assert ret.result is True
break
except AssertionError:
pass
else:
pytest.fail("Could not install versionlock package from {}".format(pkgs))
modules.pkg.install(test_pkg)
try:
hold_ret = modules.pkg.hold(test_pkg)
if versionlock_pkg and "-versionlock is not installed" in str(hold_ret):
pytest.skip("{} `{}` is installed".format(hold_ret, versionlock_pkg))
assert test_pkg in hold_ret
assert hold_ret[test_pkg]["result"] is True
unhold_ret = modules.pkg.unhold(test_pkg)
assert test_pkg in unhold_ret
assert unhold_ret[test_pkg]["result"] is True
modules.pkg.remove(test_pkg)
finally:
if versionlock_pkg:
ret = states.pkg.removed(name=versionlock_pkg)
assert ret.result is True
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules("pkg.refresh_db")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_refresh_db(grains, modules, tmp_path, minion_opts):
"""
test refreshing the package database
"""
rtag = salt.utils.pkg.rtag(minion_opts)
salt.utils.pkg.write_rtag(minion_opts)
assert os.path.isfile(rtag) is True
ret = modules.pkg.refresh_db()
if not isinstance(ret, dict):
pytest.skipTest("Upstream repo did not return coherent results: {}".format(ret))
if grains["os_family"] == "RedHat":
assert ret in (True, None)
elif grains["os_family"] == "Suse":
if not isinstance(ret, dict):
pytest.skip("Upstream repo did not return coherent results. Skipping test.")
assert ret != {}
for source, state in ret.items():
assert state in (True, False, None)
assert os.path.isfile(rtag) is False
@pytest.mark.requires_salt_modules("pkg.info_installed")
@requires_system_grains
@pytest.mark.slow_test
def test_pkg_info(grains, modules, test_pkg):
"""
Test returning useful information on Ubuntu systems.
"""
if grains["os_family"] == "Debian":
ret = modules.pkg.info_installed(["bash", "dpkg"])
keys = ret.keys()
assert "bash" in keys
assert "dpkg" in keys
elif grains["os_family"] == "RedHat":
ret = modules.pkg.info_installed(["rpm", "bash"])
keys = ret.keys()
assert "rpm" in keys
assert "bash" in keys
elif grains["os_family"] == "Suse":
ret = modules.pkg.info_installed(["less", "zypper"])
keys = ret.keys()
assert "less" in keys
assert "zypper" in keys
else:
ret = modules.pkg.info_installed([test_pkg])
keys = ret.keys()
assert test_pkg in keys
@pytest.mark.skipif(True, reason="Temporary Skip - Causes centos 8 test to fail")
@pytest.mark.destructive_test
@pytest.mark.requires_salt_modules(
"pkg.refresh_db",
"pkg.upgrade",
"pkg.install",
"pkg.list_repo_pkgs",
"pkg.list_upgrades",
)
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_network
def test_pkg_upgrade_has_pending_upgrades(grains, modules, test_pkg):
"""
Test running a system upgrade when there are packages that need upgrading
"""
if grains["os"] == "Arch":
pytest.skipTest("Arch moved to Python 3.8 and we're not ready for it yet")
modules.pkg.upgrade()
# First make sure that an up-to-date copy of the package db is available
modules.pkg.refresh_db()
if grains["os_family"] == "Suse":
# This test assumes that there are multiple possible versions of a
# package available. That makes it brittle if you pick just one
# target, as changes in the available packages will break the test.
# Therefore, we'll choose from several packages to make sure we get
# one that is suitable for this test.
packages = ("hwinfo", "avrdude", "diffoscope", "vim")
available = modules.pkg.list_repo_pkgs(packages)
for package in packages:
try:
new, old = available[package][:2]
except (KeyError, ValueError):
# Package not available, or less than 2 versions
# available. This is not a suitable target.
continue
else:
target = package
break
else:
# None of the packages have more than one version available, so
# we need to find new package(s). pkg.list_repo_pkgs can be
# used to get an overview of the available packages. We should
# try to find packages with few dependencies and small download
# sizes, to keep this test from taking longer than necessary.
pytest.fail("No suitable package found for this test")
# Make sure we have the 2nd-oldest available version installed
ret = modules.pkg.install(target, version=old)
if not isinstance(ret, dict):
if ret.startswith("ERROR"):
pytest.skipTest(
"Could not install older {} to complete test.".format(target)
)
# Run a system upgrade, which should catch the fact that the
# targeted package needs upgrading, and upgrade it.
ret = modules.pkg.upgrade()
# The changes dictionary should not be empty.
if "changes" in ret:
assert target in ret["changes"]
else:
assert target in ret
else:
ret = modules.pkg.list_upgrades()
if ret == "" or ret == {}:
pytest.skipTest(
"No updates available for this machine. Skipping pkg.upgrade test."
)
else:
args = []
if grains["os_family"] == "Debian":
args = ["dist_upgrade=True"]
ret = modules.pkg.upgrade(args)
assert ret != {}
@pytest.mark.destructive_test
@pytest.mark.skipif(
salt.utils.platform.is_darwin() is True,
reason="The jenkins user is equivalent to root on mac, causing the test to be"
" unrunnable",
)
@pytest.mark.requires_salt_modules("pkg.remove", "pkg.latest_version")
@requires_system_grains
@pytest.mark.slow_test
@pytest.mark.requires_salt_states("pkg.removed")
def test_pkg_latest_version(grains, modules, states, test_pkg):
"""
Check that pkg.latest_version returns the latest version of the uninstalled package.
The package is not installed. Only the package version is checked.
"""
states.pkg.removed(test_pkg)
cmd_pkg = []
if grains["os_family"] == "RedHat":
cmd_pkg = modules.cmd.run("yum list {}".format(test_pkg))
elif salt.utils.platform.is_windows():
cmd_pkg = modules.pkg.list_available(test_pkg)
elif grains["os_family"] == "Debian":
cmd_pkg = modules.cmd.run("apt list {}".format(test_pkg))
elif grains["os_family"] == "Arch":
cmd_pkg = modules.cmd.run("pacman -Si {}".format(test_pkg))
elif grains["os_family"] == "FreeBSD":
cmd_pkg = modules.cmd.run(
"pkg search -S name -qQ version -e {}".format(test_pkg)
)
elif grains["os_family"] == "Suse":
cmd_pkg = modules.cmd.run("zypper info {}".format(test_pkg))
elif grains["os_family"] == "MacOS":
brew_bin = salt.utils.path.which("brew")
mac_user = modules.file.get_user(brew_bin)
if mac_user == "root":
pytest.skip(
"brew cannot run as root, try a user in {}".format(
os.listdir("/Users/")
)
)
cmd_pkg = modules.cmd.run("brew info {}".format(test_pkg), run_as=mac_user)
else:
pytest.skip("TODO: test not configured for {}".format(grains["os_family"]))
pkg_latest = modules.pkg.latest_version(test_pkg)
assert pkg_latest in cmd_pkg