diff --git a/cloudinit/analyze/show.py b/cloudinit/analyze/show.py index 5fd9cdfd5df..e1e340ce9a7 100644 --- a/cloudinit/analyze/show.py +++ b/cloudinit/analyze/show.py @@ -139,7 +139,7 @@ class SystemctlReader(object): def __init__(self, property, parameter=None): self.epoch = None - self.args = ["/bin/systemctl", "show"] + self.args = [subp.which("systemctl"), "show"] if parameter: self.args.append(parameter) self.args.extend(["-p", property]) diff --git a/cloudinit/config/cc_puppet.py b/cloudinit/config/cc_puppet.py index f51f49bcc21..ad6acafe1e3 100644 --- a/cloudinit/config/cc_puppet.py +++ b/cloudinit/config/cc_puppet.py @@ -142,10 +142,8 @@ def _autostart_puppet(log): ], capture=False, ) - elif os.path.exists("/bin/systemctl"): - subp.subp( - ["/bin/systemctl", "enable", "puppet.service"], capture=False - ) + elif subp.which("systemctl"): + subp.subp(["systemctl", "enable", "puppet.service"], capture=False) elif os.path.exists("/sbin/chkconfig"): subp.subp(["/sbin/chkconfig", "puppet", "on"], capture=False) else: diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py index d07dc3c055c..0a9f1f459e2 100755 --- a/cloudinit/sources/helpers/azure.py +++ b/cloudinit/sources/helpers/azure.py @@ -115,7 +115,7 @@ def get_boot_telemetry(): try: out, _ = subp.subp( - ["/bin/systemctl", "show", "-p", "UserspaceTimestampMonotonic"], + ["systemctl", "show", "-p", "UserspaceTimestampMonotonic"], capture=True, ) tsm = None @@ -140,7 +140,7 @@ def get_boot_telemetry(): try: out, _ = subp.subp( [ - "/bin/systemctl", + "systemctl", "show", "cloud-init-local", "-p", diff --git a/tests/unittests/config/test_cc_puppet.py b/tests/unittests/config/test_cc_puppet.py index 2c4481da7f9..d070ab41146 100644 --- a/tests/unittests/config/test_cc_puppet.py +++ b/tests/unittests/config/test_cc_puppet.py @@ -10,10 +10,13 @@ LOG = logging.getLogger(__name__) +@mock.patch("cloudinit.config.cc_puppet.subp.which") @mock.patch("cloudinit.config.cc_puppet.subp.subp") @mock.patch("cloudinit.config.cc_puppet.os") class TestAutostartPuppet(CiTestCase): - def test_wb_autostart_puppet_updates_puppet_default(self, m_os, m_subp): + def test_wb_autostart_puppet_updates_puppet_default( + self, m_os, m_subp, m_subpw + ): """Update /etc/default/puppet to autostart if it exists.""" def _fake_exists(path): @@ -37,27 +40,28 @@ def _fake_exists(path): m_subp.call_args_list, ) - def test_wb_autostart_pupppet_enables_puppet_systemctl(self, m_os, m_subp): + def test_wb_autostart_pupppet_enables_puppet_systemctl( + self, m_os, m_subp, m_subpw + ): """If systemctl is present, enable puppet via systemctl.""" - def _fake_exists(path): - return path == "/bin/systemctl" - - m_os.path.exists.side_effect = _fake_exists + m_os.path.exists.return_value = False + m_subpw.return_value = "/usr/bin/systemctl" cc_puppet._autostart_puppet(LOG) expected_calls = [ - mock.call( - ["/bin/systemctl", "enable", "puppet.service"], capture=False - ) + mock.call(["systemctl", "enable", "puppet.service"], capture=False) ] self.assertEqual(expected_calls, m_subp.call_args_list) - def test_wb_autostart_pupppet_enables_puppet_chkconfig(self, m_os, m_subp): + def test_wb_autostart_pupppet_enables_puppet_chkconfig( + self, m_os, m_subp, m_subpw + ): """If chkconfig is present, enable puppet via checkcfg.""" def _fake_exists(path): return path == "/sbin/chkconfig" + m_subpw.return_value = None m_os.path.exists.side_effect = _fake_exists cc_puppet._autostart_puppet(LOG) expected_calls = [