summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
authorJames Falcon <james.falcon@canonical.com>2021-11-09 09:28:19 -0600
committerGitHub <noreply@github.com>2021-11-09 08:28:19 -0700
commit3d150688617e2b8a16d715c7fb819c759f91915a (patch)
tree839272a885562107a8a16c15600e4553574a2393 /tests/unittests
parent6421a2026f05267f2112c52417d0c4b036aeaf40 (diff)
downloadvyos-cloud-init-3d150688617e2b8a16d715c7fb819c759f91915a.tar.gz
vyos-cloud-init-3d150688617e2b8a16d715c7fb819c759f91915a.zip
Wait for apt lock (#1034)
Currently any attempt to run an apt command while another process holds an apt lock will fail. We should instead wait to acquire the apt lock. LP: #1944611
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/test_distros/test_debian.py80
-rw-r--r--tests/unittests/test_handler/test_handler_landscape.py4
2 files changed, 81 insertions, 3 deletions
diff --git a/tests/unittests/test_distros/test_debian.py b/tests/unittests/test_distros/test_debian.py
index 7ff8240b..a88c2686 100644
--- a/tests/unittests/test_distros/test_debian.py
+++ b/tests/unittests/test_distros/test_debian.py
@@ -1,8 +1,16 @@
# This file is part of cloud-init. See LICENSE file for license information.
+from itertools import count, cycle
+from unittest import mock
-from cloudinit import distros
-from cloudinit import util
-from cloudinit.tests.helpers import (FilesystemMockingTestCase, mock)
+import pytest
+
+from cloudinit import distros, util
+from cloudinit.distros.debian import (
+ APT_GET_COMMAND,
+ APT_GET_WRAPPER,
+)
+from cloudinit.tests.helpers import FilesystemMockingTestCase
+from cloudinit import subp
@mock.patch("cloudinit.distros.debian.subp.subp")
@@ -98,3 +106,69 @@ class TestDebianApplyLocale(FilesystemMockingTestCase):
m_subp.assert_not_called()
self.assertEqual(
'Failed to provide locale value.', str(ctext_m.exception))
+
+
+@mock.patch.dict('os.environ', {}, clear=True)
+@mock.patch("cloudinit.distros.debian.subp.which", return_value=True)
+@mock.patch("cloudinit.distros.debian.subp.subp")
+class TestPackageCommand:
+ distro = distros.fetch("debian")("debian", {}, None)
+
+ @mock.patch("cloudinit.distros.debian.Distro._apt_lock_available",
+ return_value=True)
+ def test_simple_command(self, m_apt_avail, m_subp, m_which):
+ self.distro.package_command('update')
+ apt_args = [APT_GET_WRAPPER['command']]
+ apt_args.extend(APT_GET_COMMAND)
+ apt_args.append('update')
+ expected_call = {
+ 'args': apt_args,
+ 'capture': False,
+ 'env': {'DEBIAN_FRONTEND': 'noninteractive'},
+ }
+ assert m_subp.call_args == mock.call(**expected_call)
+
+ @mock.patch("cloudinit.distros.debian.Distro._apt_lock_available",
+ side_effect=[False, False, True])
+ @mock.patch("cloudinit.distros.debian.time.sleep")
+ def test_wait_for_lock(self, m_sleep, m_apt_avail, m_subp, m_which):
+ self.distro._wait_for_apt_command("stub", {"args": "stub2"})
+ assert m_sleep.call_args_list == [mock.call(1), mock.call(1)]
+ assert m_subp.call_args_list == [mock.call(args='stub2')]
+
+ @mock.patch("cloudinit.distros.debian.Distro._apt_lock_available",
+ return_value=False)
+ @mock.patch("cloudinit.distros.debian.time.sleep")
+ @mock.patch("cloudinit.distros.debian.time.time", side_effect=count())
+ def test_lock_wait_timeout(
+ self, m_time, m_sleep, m_apt_avail, m_subp, m_which
+ ):
+ with pytest.raises(TimeoutError):
+ self.distro._wait_for_apt_command("stub", "stub2", timeout=5)
+ assert m_subp.call_args_list == []
+
+ @mock.patch("cloudinit.distros.debian.Distro._apt_lock_available",
+ side_effect=cycle([True, False]))
+ @mock.patch("cloudinit.distros.debian.time.sleep")
+ def test_lock_exception_wait(self, m_sleep, m_apt_avail, m_subp, m_which):
+ exception = subp.ProcessExecutionError(
+ exit_code=100, stderr="Could not get apt lock"
+ )
+ m_subp.side_effect = [exception, exception, "return_thing"]
+ ret = self.distro._wait_for_apt_command("stub", {"args": "stub2"})
+ assert ret == "return_thing"
+
+ @mock.patch("cloudinit.distros.debian.Distro._apt_lock_available",
+ side_effect=cycle([True, False]))
+ @mock.patch("cloudinit.distros.debian.time.sleep")
+ @mock.patch("cloudinit.distros.debian.time.time", side_effect=count())
+ def test_lock_exception_timeout(
+ self, m_time, m_sleep, m_apt_avail, m_subp, m_which
+ ):
+ m_subp.side_effect = subp.ProcessExecutionError(
+ exit_code=100, stderr="Could not get apt lock"
+ )
+ with pytest.raises(TimeoutError):
+ self.distro._wait_for_apt_command(
+ "stub", {"args": "stub2"}, timeout=5
+ )
diff --git a/tests/unittests/test_handler/test_handler_landscape.py b/tests/unittests/test_handler/test_handler_landscape.py
index 00333985..1cc73ea2 100644
--- a/tests/unittests/test_handler/test_handler_landscape.py
+++ b/tests/unittests/test_handler/test_handler_landscape.py
@@ -22,6 +22,10 @@ class TestLandscape(FilesystemMockingTestCase):
self.conf = self.tmp_path('client.conf', self.new_root)
self.default_file = self.tmp_path('default_landscape', self.new_root)
self.patchUtils(self.new_root)
+ self.add_patch(
+ 'cloudinit.distros.ubuntu.Distro.install_packages',
+ 'm_install_packages'
+ )
def test_handler_skips_empty_landscape_cloudconfig(self):
"""Empty landscape cloud-config section does no work."""