Skip to content

Commit

Permalink
fix(test): Remove temporary directory side effect (#5416)
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Jun 28, 2024
1 parent 0d51e78 commit 8a4730b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
12 changes: 8 additions & 4 deletions tests/unittests/distros/package_management/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from cloudinit import helpers, subp
from cloudinit.distros.package_management import apt
from cloudinit.distros.package_management.apt import APT_GET_COMMAND, Apt
from tests.unittests.helpers import MockPaths
from tests.unittests.helpers import get_mock_paths
from tests.unittests.util import FakeDataSource

M_PATH = "cloudinit.distros.package_management.apt.Apt."
Expand Down Expand Up @@ -125,11 +125,15 @@ def test_search_stem(self, m_subp, m_which, mocker):
[f"{TMP_DIR}/{FILE}" for FILE in apt.APT_LOCK_FILES],
)
class TestUpdatePackageSources:
def __init__(self):
MockPaths = get_mock_paths(TMP_DIR)
self.MockPaths = MockPaths({}, FakeDataSource())

@mock.patch.object(apt.subp, "which", return_value=True)
@mock.patch.object(apt.subp, "subp")
def test_force_update_calls_twice(self, m_subp, m_which):
"""Ensure that force=true calls apt update again"""
instance = apt.Apt(helpers.Runners(MockPaths({}, FakeDataSource())))
instance = apt.Apt(helpers.Runners(self.MockPaths))
instance.update_package_sources()
instance.update_package_sources(force=True)
assert 2 == len(m_subp.call_args_list)
Expand All @@ -139,7 +143,7 @@ def test_force_update_calls_twice(self, m_subp, m_which):
@mock.patch.object(apt.subp, "subp")
def test_force_update_twice_calls_twice(self, m_subp, m_which):
"""Ensure that force=true calls apt update again when called twice"""
instance = apt.Apt(helpers.Runners(MockPaths({}, FakeDataSource())))
instance = apt.Apt(helpers.Runners(self.MockPaths))
instance.update_package_sources(force=True)
instance.update_package_sources(force=True)
assert 2 == len(m_subp.call_args_list)
Expand All @@ -149,7 +153,7 @@ def test_force_update_twice_calls_twice(self, m_subp, m_which):
@mock.patch.object(apt.subp, "subp")
def test_no_force_update_calls_once(self, m_subp, m_which):
"""Ensure that apt-get update calls are deduped unless expected"""
instance = apt.Apt(helpers.Runners(MockPaths({}, FakeDataSource())))
instance = apt.Apt(helpers.Runners(self.MockPaths))
instance.update_package_sources()
instance.update_package_sources()
assert 1 == len(m_subp.call_args_list)
Expand Down
27 changes: 16 additions & 11 deletions tests/unittests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ def tmp_cloud(self, distro, sys_cfg=None, metadata=None):
self.new_root = self.tmp_dir()
if not sys_cfg:
sys_cfg = {}
MockPaths = get_mock_paths(self.new_root)
self.paths = MockPaths({})
cls = distros.fetch(distro)
mydist = cls(distro, sys_cfg, self.paths)
Expand Down Expand Up @@ -460,18 +461,22 @@ def _ensure_url_default_path(url):
)


class MockPaths(ch.Paths):
def __init__(self, path_cfgs: dict, ds=None):
super().__init__(path_cfgs=path_cfgs, ds=ds)
self.temp = tempfile.TemporaryDirectory()
def get_mock_paths(temp_dir):
class MockPaths(ch.Paths):
def __init__(self, path_cfgs: dict, ds=None):
super().__init__(path_cfgs=path_cfgs, ds=ds)

self.cloud_dir: str = path_cfgs.get(
"cloud_dir", f"{self.temp}/var/lib/cloud"
)
self.run_dir: str = path_cfgs.get("run_dir", f"{self.temp}/run/cloud/")
self.template_dir: str = path_cfgs.get(
"templates_dir", f"{self.temp}/etc/cloud/templates/"
)
self.cloud_dir: str = path_cfgs.get(
"cloud_dir", f"{temp_dir}/var/lib/cloud"
)
self.run_dir: str = path_cfgs.get(
"run_dir", f"{temp_dir}/run/cloud/"
)
self.template_dir: str = path_cfgs.get(
"templates_dir", f"{temp_dir}/etc/cloud/templates/"
)

return MockPaths


class ResponsesTestCase(CiTestCase):
Expand Down

0 comments on commit 8a4730b

Please sign in to comment.