From 0f87b2b6093ca0bbc1a7ac6e1c205b8e42ddbc2a Mon Sep 17 00:00:00 2001 From: Mihaela Balutoiu Date: Wed, 26 Nov 2025 15:22:15 +0000 Subject: [PATCH 1/2] Fix `cloud-init` systemd service enablement Ensured `cloud-init` configuration and service enabling only occur if it's in the package list. Signed-off-by: Mihaela Balutoiu --- coriolis/osmorphing/base.py | 6 ++++++ coriolis/osmorphing/debian.py | 8 -------- coriolis/osmorphing/redhat.py | 5 ----- coriolis/osmorphing/suse.py | 5 ----- 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/coriolis/osmorphing/base.py b/coriolis/osmorphing/base.py index 6308153f..77564680 100644 --- a/coriolis/osmorphing/base.py +++ b/coriolis/osmorphing/base.py @@ -443,6 +443,9 @@ def _create_cloudinit_user(self): if not self._check_user_exists(cloud_user): self._exec_cmd_chroot("useradd %s" % cloud_user) + def _has_systemd_chroot(self): + return self._test_path("/lib/systemd/system") + def _configure_cloud_init(self): cloud_cfg_mods = {} if "cloud-init" not in self.get_packages()[0]: @@ -468,6 +471,9 @@ def _configure_cloud_init(self): self._write_cloud_init_mods_config(cloud_cfg_mods) + if self._has_systemd_chroot(): + self._enable_systemd_service("cloud-init") + def _test_path_chroot(self, path): # This method uses _exec_cmd_chroot() instead of SFTP stat() # because in some situations, the SSH user used may not have diff --git a/coriolis/osmorphing/debian.py b/coriolis/osmorphing/debian.py index 0138075b..e500e1a3 100644 --- a/coriolis/osmorphing/debian.py +++ b/coriolis/osmorphing/debian.py @@ -97,9 +97,6 @@ def _compose_netplan_cfg(self, nics_info): } return yaml.dump(cfg, default_flow_style=False) - def _has_systemd_chroot(self): - return self._test_path("/lib/systemd/system") - def set_net_config(self, nics_info, dhcp): if not dhcp: LOG.info("Setting static IP configuration") @@ -127,11 +124,6 @@ def set_net_config(self, nics_info, dhcp): cfg_name = "%s/coriolis_netplan.yaml" % self.netplan_base self._write_file_sudo(cfg_name, new_cfg) - def _configure_cloud_init(self): - super(BaseDebianMorphingTools, self)._configure_cloud_init() - if self._has_systemd_chroot(): - self._enable_systemd_service("cloud-init") - def get_installed_packages(self): cmd = "dpkg-query -f '${binary:Package}\\n' -W" try: diff --git a/coriolis/osmorphing/redhat.py b/coriolis/osmorphing/redhat.py index 8fb28f27..a8369e27 100644 --- a/coriolis/osmorphing/redhat.py +++ b/coriolis/osmorphing/redhat.py @@ -288,11 +288,6 @@ def _set_network_nozeroconf_config(self): network_cfg["NOZEROCONF"] = "yes" self._write_config_file(network_cfg_file, network_cfg) - def _configure_cloud_init(self): - super(BaseRedHatMorphingTools, self)._configure_cloud_init() - if self._has_systemd(): - self._enable_systemd_service("cloud-init") - def _write_config_file(self, chroot_path, config_data): content = self._get_config_file_content(config_data) self._write_file_sudo(chroot_path, content) diff --git a/coriolis/osmorphing/suse.py b/coriolis/osmorphing/suse.py index 79538c5b..3331367a 100644 --- a/coriolis/osmorphing/suse.py +++ b/coriolis/osmorphing/suse.py @@ -127,11 +127,6 @@ def _has_systemd(self): except Exception: return False - def _configure_cloud_init(self): - super(BaseSUSEMorphingTools, self)._configure_cloud_init() - if self._has_systemd(): - self._enable_systemd_service("cloud-init") - def post_packages_install(self, package_names): self._configure_cloud_init() self._run_dracut() From 6fc5be5507651b71677766090b4ccf2529446703 Mon Sep 17 00:00:00 2001 From: Mihaela Balutoiu Date: Wed, 26 Nov 2025 20:06:53 +0200 Subject: [PATCH 2/2] Fix `unit tests` Signed-off-by: Mihaela Balutoiu --- coriolis/tests/osmorphing/test_base.py | 24 +++++++++++++++++++----- coriolis/tests/osmorphing/test_debian.py | 11 ----------- coriolis/tests/osmorphing/test_redhat.py | 11 ----------- coriolis/tests/osmorphing/test_suse.py | 10 ---------- 4 files changed, 19 insertions(+), 37 deletions(-) diff --git a/coriolis/tests/osmorphing/test_base.py b/coriolis/tests/osmorphing/test_base.py index b9a1fa7b..af4dd1f0 100644 --- a/coriolis/tests/osmorphing/test_base.py +++ b/coriolis/tests/osmorphing/test_base.py @@ -713,7 +713,8 @@ def test__create_cloudinit_user_already_exists( ["vim"], {}, False, - None + None, + False ), ( ["cloud-init"], @@ -724,16 +725,21 @@ def test__create_cloudinit_user_already_exists( "ssh_pwauth": True, "users": None, "network": {"config": "disabled"}, - } + }, + True ), ( ["cloud-init", "vim"], {"retain_user_credentials": False, "set_dhcp": True}, True, - {} + {}, + False ), ) @ddt.unpack + @mock.patch.object(base.BaseLinuxOSMorphingTools, + '_enable_systemd_service') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_has_systemd_chroot') @mock.patch.object(base.BaseLinuxOSMorphingTools, '_write_cloud_init_mods_config') @mock.patch.object(base.BaseLinuxOSMorphingTools, '_create_cloudinit_user') @@ -745,12 +751,15 @@ def test__create_cloudinit_user_already_exists( @mock.patch.object(base.BaseLinuxOSMorphingTools, 'get_packages') def test__configure_cloud_init( self, returned_packages, osmorphing_params, creates_cloudinit_user, - expected_result, mock_get_packages, + expected_result, has_systemd_chroot, mock_get_packages, mock__disable_installer_cloud_config, mock__ensure_cloud_init_not_disabled, mock__reset_cloud_init_run, - mock__create_cloudinit_user, mock__write_cloud_init_mods_config): + mock__create_cloudinit_user, mock__write_cloud_init_mods_config, + mock__has_systemd_chroot, mock__enable_systemd_service + ): mock_get_packages.return_value = returned_packages self.os_morphing_tools._osmorphing_parameters = osmorphing_params + mock__has_systemd_chroot.return_value = has_systemd_chroot self.os_morphing_tools._configure_cloud_init() @@ -763,6 +772,11 @@ def test__configure_cloud_init( mock__create_cloudinit_user.assert_called_once() else: mock__create_cloudinit_user.assert_not_called() + if has_systemd_chroot: + mock__enable_systemd_service.assert_called_once_with( + "cloud-init") + else: + mock__enable_systemd_service.assert_not_called() else: mock__disable_installer_cloud_config.assert_not_called() diff --git a/coriolis/tests/osmorphing/test_debian.py b/coriolis/tests/osmorphing/test_debian.py index 26757f38..f18a0498 100644 --- a/coriolis/tests/osmorphing/test_debian.py +++ b/coriolis/tests/osmorphing/test_debian.py @@ -213,17 +213,6 @@ def test_set_net_config_no_dhcp( mock_disable_predictable_nic_names.assert_not_called() mock_write_file_sudo.assert_not_called() - @mock.patch.object(debian.BaseDebianMorphingTools, - '_enable_systemd_service') - @mock.patch.object(debian.BaseDebianMorphingTools, '_has_systemd_chroot') - def test__configure_cloud_init( - self, mock__has_systemd_chroot, mock__enable_systemd_service): - mock__has_systemd_chroot.return_value = True - - self.morpher._configure_cloud_init() - - mock__enable_systemd_service.assert_called_once_with("cloud-init") - @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') def test_get_installed_packages(self, mock_exec_cmd_chroot): mock_exec_cmd_chroot.return_value = \ diff --git a/coriolis/tests/osmorphing/test_redhat.py b/coriolis/tests/osmorphing/test_redhat.py index 1020654c..c92697c7 100644 --- a/coriolis/tests/osmorphing/test_redhat.py +++ b/coriolis/tests/osmorphing/test_redhat.py @@ -483,17 +483,6 @@ def test__set_network_nozeroconf_config( mock_write_config_file.assert_called_once_with( "etc/sysconfig/network", mock_read_config_file.return_value) - @mock.patch.object(redhat.BaseRedHatMorphingTools, - '_enable_systemd_service') - @mock.patch.object(redhat.BaseRedHatMorphingTools, '_has_systemd') - def test__configure_cloud_init( - self, mock__has_systemd, mock__enable_systemd_service): - mock__has_systemd.return_value = True - - self.morphing_tools._configure_cloud_init() - - mock__enable_systemd_service.assert_called_once_with("cloud-init") - @mock.patch.object( redhat.BaseRedHatMorphingTools, '_get_config_file_content' ) diff --git a/coriolis/tests/osmorphing/test_suse.py b/coriolis/tests/osmorphing/test_suse.py index bdaa067d..eef4bb96 100644 --- a/coriolis/tests/osmorphing/test_suse.py +++ b/coriolis/tests/osmorphing/test_suse.py @@ -226,16 +226,6 @@ def test__has_systemd_with_exception(self, mock_exec_cmd_chroot): self.assertFalse(result) - @mock.patch.object(suse.BaseSUSEMorphingTools, '_enable_systemd_service') - @mock.patch.object(suse.BaseSUSEMorphingTools, '_has_systemd') - def test__configure_cloud_init( - self, mock__has_systemd, mock__enable_systemd_service): - mock__has_systemd.return_value = True - - self.morphing_tools._configure_cloud_init() - - mock__enable_systemd_service.assert_called_once_with("cloud-init") - @mock.patch.object(suse.BaseSUSEMorphingTools, '_configure_cloud_init') @mock.patch.object(suse.BaseSUSEMorphingTools, '_run_dracut') @mock.patch.object(base.BaseLinuxOSMorphingTools, 'post_packages_install')