summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordermotbradley <dermot_bradley@yahoo.com>2022-10-28 19:26:00 +0100
committerGitHub <noreply@github.com>2022-10-28 13:26:00 -0500
commit49e01b4298252c1405cbbf316f5433f371c2cc75 (patch)
tree7387a9ac37aa78c29fd54b1f305c715a3a243903
parent53a3526c4a79962fc05602a0bcf7f44e5dbba1c2 (diff)
downloadcloud-init-git-49e01b4298252c1405cbbf316f5433f371c2cc75.tar.gz
alpine.py: Add Alpine-specific manage_service function and update tests (#1804)
Alpine uses OpenRC rather than Systemd. Add a distro-specific manage_service in alpine.py and update some affected testcases appropriately.
-rw-r--r--cloudinit/distros/alpine.py31
-rw-r--r--tests/unittests/config/test_cc_ntp.py7
-rw-r--r--tests/unittests/distros/test_manage_service.py19
3 files changed, 54 insertions, 3 deletions
diff --git a/cloudinit/distros/alpine.py b/cloudinit/distros/alpine.py
index be6956d2..4a23fe07 100644
--- a/cloudinit/distros/alpine.py
+++ b/cloudinit/distros/alpine.py
@@ -22,7 +22,6 @@ NETWORK_FILE_HEADER = """\
class Distro(distros.Distro):
pip_package_name = "py3-pip"
- init_cmd = ["rc-service"] # init scripts
locale_conf_fn = "/etc/profile.d/locale.sh"
network_conf_fn = "/etc/network/interfaces"
renderer_configs = {
@@ -174,5 +173,33 @@ class Distro(distros.Distro):
return command
+ def uses_systemd(self):
+ """
+ Alpine uses OpenRC, not systemd
+ """
+ return False
-# vi: ts=4 expandtab
+ def manage_service(self, action: str, service: str):
+ """
+ Perform the requested action on a service. This handles OpenRC
+ specific implementation details.
+
+ OpenRC has two distinct commands relating to services,
+ 'rc-service' and 'rc-update' and the order of their argument
+ lists differ.
+ May raise ProcessExecutionError
+ """
+ init_cmd = ["rc-service", "--nocolor"]
+ update_cmd = ["rc-update", "--nocolor"]
+ cmds = {
+ "stop": list(init_cmd) + [service, "stop"],
+ "start": list(init_cmd) + [service, "start"],
+ "disable": list(update_cmd) + ["del", service],
+ "enable": list(update_cmd) + ["add", service],
+ "restart": list(init_cmd) + [service, "restart"],
+ "reload": list(init_cmd) + [service, "restart"],
+ "try-reload": list(init_cmd) + [service, "restart"],
+ "status": list(init_cmd) + [service, "status"],
+ }
+ cmd = list(cmds[action])
+ return subp.subp(cmd, capture=True)
diff --git a/tests/unittests/config/test_cc_ntp.py b/tests/unittests/config/test_cc_ntp.py
index a8964144..365945f8 100644
--- a/tests/unittests/config/test_cc_ntp.py
+++ b/tests/unittests/config/test_cc_ntp.py
@@ -463,7 +463,12 @@ class TestNtp(FilesystemMockingTestCase):
if distro == "alpine":
uses_systemd = False
- expected_service_call = ["rc-service", service_name, "restart"]
+ expected_service_call = [
+ "rc-service",
+ "--nocolor",
+ service_name,
+ "restart",
+ ]
# _mock_ntp_client_config call above did not specify a client
# value and so it defaults to "ntp" which on Alpine Linux only
# supports servers and not pools.
diff --git a/tests/unittests/distros/test_manage_service.py b/tests/unittests/distros/test_manage_service.py
index a6b8a3dc..98823770 100644
--- a/tests/unittests/distros/test_manage_service.py
+++ b/tests/unittests/distros/test_manage_service.py
@@ -29,6 +29,25 @@ class TestManageService(CiTestCase):
self.dist.manage_service("start", "myssh")
m_subp.assert_called_with(["service", "myssh", "start"], capture=True)
+ @mock.patch.object(MockDistro, "uses_systemd", return_value=False)
+ @mock.patch("cloudinit.distros.subp.subp")
+ def test_manage_service_rcservice_initcmd(self, m_subp, m_sysd):
+ dist = _get_distro("alpine")
+ dist.init_cmd = ["rc-service", "--nocolor"]
+ dist.manage_service("start", "myssh")
+ m_subp.assert_called_with(
+ ["rc-service", "--nocolor", "myssh", "start"], capture=True
+ )
+
+ @mock.patch("cloudinit.distros.subp.subp")
+ def test_manage_service_alpine_rcupdate_cmd(self, m_subp):
+ dist = _get_distro("alpine")
+ dist.update_cmd = ["rc-update", "--nocolor"]
+ dist.manage_service("enable", "myssh")
+ m_subp.assert_called_with(
+ ["rc-update", "--nocolor", "add", "myssh"], capture=True
+ )
+
@mock.patch("cloudinit.distros.subp.subp")
def test_manage_service_rcctl_initcmd(self, m_subp):
dist = _get_distro("openbsd")