diff options
author | Andy Fiddaman <omnios@citrus-it.co.uk> | 2021-10-20 20:58:27 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-20 15:58:27 -0500 |
commit | 8c89009e75c7cf6c2f87635b82656f07f58095e1 (patch) | |
tree | 31ca588f2196a77c85ad4a7f9c06d9c8ae467551 /tests/unittests/test_distros/test_manage_service.py | |
parent | 3a6bee59eb5e7f363c25a667ef36b9695f0ebe8d (diff) | |
download | vyos-cloud-init-8c89009e75c7cf6c2f87635b82656f07f58095e1.tar.gz vyos-cloud-init-8c89009e75c7cf6c2f87635b82656f07f58095e1.zip |
Leave the details of service management to the distro (#1074)
Various modules restart services and they all have logic to try and
detect if they are running on a system that needs 'systemctl' or
'service', and then have code to decide which order the arguments
need to be etc. On top of that, not all modules do this in the same way.
The duplication and different approaches are not ideal but this also
makes it hard to add support for a new distribution that does not use
either 'systemctl' or 'service'.
This change adds a new manage_service() method to the distro class
and updates several modules to use it.
Diffstat (limited to 'tests/unittests/test_distros/test_manage_service.py')
-rw-r--r-- | tests/unittests/test_distros/test_manage_service.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tests/unittests/test_distros/test_manage_service.py b/tests/unittests/test_distros/test_manage_service.py new file mode 100644 index 00000000..47e7cfb0 --- /dev/null +++ b/tests/unittests/test_distros/test_manage_service.py @@ -0,0 +1,38 @@ +# This file is part of cloud-init. See LICENSE file for license information. + +from cloudinit.tests.helpers import (CiTestCase, mock) +from tests.unittests.util import TestingDistro + + +class TestManageService(CiTestCase): + + with_logs = True + + def setUp(self): + super(TestManageService, self).setUp() + self.dist = TestingDistro() + + @mock.patch.object(TestingDistro, 'uses_systemd', return_value=False) + @mock.patch("cloudinit.distros.subp.subp") + def test_manage_service_systemctl_initcmd(self, m_subp, m_sysd): + self.dist.init_cmd = ['systemctl'] + self.dist.manage_service('start', 'myssh') + m_subp.assert_called_with(['systemctl', 'start', 'myssh'], + capture=True) + + @mock.patch.object(TestingDistro, 'uses_systemd', return_value=False) + @mock.patch("cloudinit.distros.subp.subp") + def test_manage_service_service_initcmd(self, m_subp, m_sysd): + self.dist.init_cmd = ['service'] + self.dist.manage_service('start', 'myssh') + m_subp.assert_called_with(['service', 'myssh', 'start'], capture=True) + + @mock.patch.object(TestingDistro, 'uses_systemd', return_value=True) + @mock.patch("cloudinit.distros.subp.subp") + def test_manage_service_systemctl(self, m_subp, m_sysd): + self.dist.init_cmd = ['ignore'] + self.dist.manage_service('start', 'myssh') + m_subp.assert_called_with(['systemctl', 'start', 'myssh'], + capture=True) + +# vi: ts=4 sw=4 expandtab |