Add additional aptpkg and state tests

This commit is contained in:
Megan Wilhite 2023-10-16 12:42:19 -06:00
parent 79b43ccc9d
commit 862ca146a1
3 changed files with 359 additions and 0 deletions

View file

@ -1656,6 +1656,130 @@ def test_latest_version_names_empty():
assert ret == ""
def test_latest_version_fromrepo():
"""
test latest_version when `fromrepo` is passed in as a kwarg
"""
version = "5.15.0.86.83"
fromrepo = "jammy-updates"
list_ret = {"linux-cloud-tools-virtual": [version]}
apt_ret = {
"pid": 4361,
"retcode": 0,
"stdout": "linux-cloud-tools-virtual:\n"
f"Installed: 5.15.0.69.67\n Candidate: {version}\n Version"
f"table:\n {version} 990\n 990"
f"https://mirrors.edge.kernel.org/ubuntu {fromrepo}/main amd64"
"Packages\n 500 https://mirrors.edge.kernel.org/ubuntu"
"jammy-security/main amd64 Packages\n ***5.15.0.69.67 100\n"
"100 /var/lib/dpkg/status\n 5.15.0.25.27 500\n 500"
"https://mirrors.edge.kernel.org/ubuntu jammy/main amd64 Packages",
"stderr": "",
}
mock_apt = MagicMock(return_value=apt_ret)
patch_apt = patch("salt.modules.aptpkg._call_apt", mock_apt)
mock_list_pkgs = MagicMock(return_value=list_ret)
patch_list_pkgs = patch("salt.modules.aptpkg.list_pkgs", mock_list_pkgs)
with patch_apt, patch_list_pkgs:
ret = aptpkg.latest_version(
"linux-cloud-tools-virtual",
fromrepo=fromrepo,
refresh=False,
show_installed=True,
)
assert ret == version
assert mock_apt.call_args == call(
[
"apt-cache",
"-q",
"policy",
"linux-cloud-tools-virtual",
"-o",
f"APT::Default-Release={fromrepo}",
],
scope=False,
)
def test_latest_version_fromrepo_multiple_names():
"""
test latest_version when multiple names of pkgs are pased
"""
version = "5.15.0.86.83"
fromrepo = "jammy-updates"
list_ret = {
"linux-cloud-tools-virtual": ["5.15.0.69.67"],
"linux-generic": ["5.15.0.69.67"],
}
apt_ret_cloud = {
"pid": 4361,
"retcode": 0,
"stdout": "linux-cloud-tools-virtual:\n"
f"Installed: 5.15.0.69.67\n Candidate: {version}\n Version"
f"table:\n {version} 990\n 990"
f"https://mirrors.edge.kernel.org/ubuntu {fromrepo}/main amd64"
"Packages\n 500 https://mirrors.edge.kernel.org/ubuntu"
"jammy-security/main amd64 Packages\n ***5.15.0.69.67 100\n"
"100 /var/lib/dpkg/status\n 5.15.0.25.27 500\n 500"
"https://mirrors.edge.kernel.org/ubuntu jammy/main amd64 Packages",
"stderr": "",
}
apt_ret_generic = {
"pid": 4821,
"retcode": 0,
"stdout": "linux-generic:\n"
f"Installed: 5.15.0.69.67\n Candidate: {version}\n"
f"Version table:\n {version} 990\n 990"
"https://mirrors.edge.kernel.org/ubuntu"
"jammy-updates/main amd64 Packages\n 500"
"https://mirrors.edge.kernel.org/ubuntu"
"jammy-security/main amd64 Packages\n *** 5.15.0.69.67"
"100\n 100 /var/lib/dpkg/status\n 5.15.0.25.27"
"500\n 500 https://mirrors.edge.kernel.org/ubuntu"
"jammy/main amd64 Packages",
"stderr": "",
}
mock_apt = MagicMock()
mock_apt.side_effect = [apt_ret_cloud, apt_ret_generic]
patch_apt = patch("salt.modules.aptpkg._call_apt", mock_apt)
mock_list_pkgs = MagicMock(return_value=list_ret)
patch_list_pkgs = patch("salt.modules.aptpkg.list_pkgs", mock_list_pkgs)
with patch_apt, patch_list_pkgs:
ret = aptpkg.latest_version(
"linux-cloud-tools-virtual",
"linux-generic",
fromrepo=fromrepo,
refresh=False,
show_installed=True,
)
assert ret == {"linux-cloud-tools-virtual": version, "linux-generic": version}
assert mock_apt.call_args_list == [
call(
[
"apt-cache",
"-q",
"policy",
"linux-cloud-tools-virtual",
"-o",
"APT::Default-Release=jammy-updates",
],
scope=False,
),
call(
[
"apt-cache",
"-q",
"policy",
"linux-generic",
"-o",
"APT::Default-Release=jammy-updates",
],
scope=False,
),
]
def test_hold():
"""
test aptpkg.hold() when passing in the name of a package

View file

@ -0,0 +1,201 @@
"""
Test functions in state.py that are not a part of a class
"""
import pytest
import salt.state
from salt.utils.odict import OrderedDict
pytestmark = [
pytest.mark.core_test,
]
def test_state_args():
"""
Testing state.state_args when this state is being used:
/etc/foo.conf:
file.managed:
- contents: "blah"
- mkdirs: True
- user: ch3ll
- group: ch3ll
- mode: 755
/etc/bar.conf:
file.managed:
- use:
- file: /etc/foo.conf
"""
id_ = "/etc/bar.conf"
state = "file"
high = OrderedDict(
[
(
"/etc/foo.conf",
OrderedDict(
[
(
"file",
[
OrderedDict([("contents", "blah")]),
OrderedDict([("mkdirs", True)]),
OrderedDict([("user", "ch3ll")]),
OrderedDict([("group", "ch3ll")]),
OrderedDict([("mode", 755)]),
"managed",
{"order": 10000},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
(
"/etc/bar.conf",
OrderedDict(
[
(
"file",
[
OrderedDict(
[
(
"use",
[OrderedDict([("file", "/etc/foo.conf")])],
)
]
),
"managed",
{"order": 10001},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
]
)
ret = salt.state.state_args(id_, state, high)
assert ret == {"order", "use"}
def test_state_args_id_not_high():
"""
Testing state.state_args when id_ is not in high
"""
id_ = "/etc/bar.conf2"
state = "file"
high = OrderedDict(
[
(
"/etc/foo.conf",
OrderedDict(
[
(
"file",
[
OrderedDict([("contents", "blah")]),
OrderedDict([("mkdirs", True)]),
OrderedDict([("user", "ch3ll")]),
OrderedDict([("group", "ch3ll")]),
OrderedDict([("mode", 755)]),
"managed",
{"order": 10000},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
(
"/etc/bar.conf",
OrderedDict(
[
(
"file",
[
OrderedDict(
[
(
"use",
[OrderedDict([("file", "/etc/foo.conf")])],
)
]
),
"managed",
{"order": 10001},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
]
)
ret = salt.state.state_args(id_, state, high)
assert ret == set()
def test_state_args_state_not_high():
"""
Testing state.state_args when state is not in high date
"""
id_ = "/etc/bar.conf"
state = "file2"
high = OrderedDict(
[
(
"/etc/foo.conf",
OrderedDict(
[
(
"file",
[
OrderedDict([("contents", "blah")]),
OrderedDict([("mkdirs", True)]),
OrderedDict([("user", "ch3ll")]),
OrderedDict([("group", "ch3ll")]),
OrderedDict([("mode", 755)]),
"managed",
{"order": 10000},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
(
"/etc/bar.conf",
OrderedDict(
[
(
"file",
[
OrderedDict(
[
(
"use",
[OrderedDict([("file", "/etc/foo.conf")])],
)
]
),
"managed",
{"order": 10001},
],
),
("__sls__", "test"),
("__env__", "base"),
]
),
),
]
)
ret = salt.state.state_args(id_, state, high)
assert ret == set()

View file

@ -1302,3 +1302,37 @@ def test_check_refresh_pillar(minion_opts, caplog):
state_obj.check_refresh(data, ret)
mock_refresh.assert_called_once()
assert "Refreshing pillar..." in caplog.text
def test_module_refresh_runtimeerror(minion_opts, caplog):
"""
test module_refresh when runtimerror occurs
"""
mock_importlib = MagicMock()
mock_importlib.side_effect = RuntimeError("Error")
patch_importlib = patch("importlib.reload", mock_importlib)
patch_pillar = patch("salt.state.State._gather_pillar", return_value="")
with patch_importlib, patch_pillar:
state_obj = salt.state.State(minion_opts)
state_obj.module_refresh()
assert (
"Error encountered during module reload. Modules were not reloaded."
in caplog.text
)
def test_module_refresh_typeerror(minion_opts, caplog):
"""
test module_refresh when typeerror occurs
"""
mock_importlib = MagicMock()
mock_importlib.side_effect = TypeError("Error")
patch_importlib = patch("importlib.reload", mock_importlib)
patch_pillar = patch("salt.state.State._gather_pillar", return_value="")
with patch_importlib, patch_pillar:
state_obj = salt.state.State(minion_opts)
state_obj.module_refresh()
assert (
"Error encountered during module reload. Modules were not reloaded."
in caplog.text
)