diff --git a/coriolis/osmorphing/netpreserver/ifcfg.py b/coriolis/osmorphing/netpreserver/ifcfg.py index ad2f01b8..9884cf61 100644 --- a/coriolis/osmorphing/netpreserver/ifcfg.py +++ b/coriolis/osmorphing/netpreserver/ifcfg.py @@ -43,6 +43,17 @@ def parse_network(self): "ip_addresses": [ip_address] if ip_address else [] } + def backup_ifcfg_configs(self, device_names, backup_file_suffix=".bak"): + """Back up ifcfg profiles for the given device names.""" + for dev_name in device_names: + cfg_path = "%s/ifcfg-%s" % ( + self.network_scripts_path, dev_name) + if self.osmorphing_tool._test_path(cfg_path): + self.osmorphing_tool._exec_cmd_chroot( + 'mv "%s" "%s%s"' % ( + cfg_path, cfg_path, backup_file_suffix)) + LOG.debug("Backed up ifcfg profile '%s'", cfg_path) + def _get_net_config_files(self, network_scripts_path): dir_content = self.osmorphing_tool._list_dir(network_scripts_path) return [os.path.join(network_scripts_path, f) for f in diff --git a/coriolis/osmorphing/netpreserver/nmconnection.py b/coriolis/osmorphing/netpreserver/nmconnection.py index b371a319..a41b264a 100644 --- a/coriolis/osmorphing/netpreserver/nmconnection.py +++ b/coriolis/osmorphing/netpreserver/nmconnection.py @@ -18,19 +18,42 @@ def __init__(self, osmorphing_tool): self.nmconnection_file = "etc/NetworkManager/system-connections" def check_net_preserver(self): - if self.osmorphing_tool._test_path(self.nmconnection_file): - nmconnection_files = self._get_nmconnection_files( - self.nmconnection_file) - if nmconnection_files: - nmconnection_ethernet = self._get_keyfiles_by_type( - "ethernet", self.nmconnection_file) - if nmconnection_ethernet: - return True - return False + if not self.get_nmconnection_files(): + return False + return bool(self._get_keyfiles_by_type( + "ethernet", self.nmconnection_file)) - def parse_network(self): - nmconnection_ethernet = self._get_keyfiles_by_type( + def get_nmconnection_files(self): + if not self.osmorphing_tool._test_path(self.nmconnection_file): + return [] + return self._get_nmconnection_files(self.nmconnection_file) + + def get_ethernet_keyfiles(self): + if not self.osmorphing_tool._test_path(self.nmconnection_file): + return [] + return self._get_keyfiles_by_type( "ethernet", self.nmconnection_file) + + def backup_ethernet_keyfiles(self, backup_file_suffix=".bak"): + """Back up existing ethernet nmconnection profiles.""" + for cfg_path, _ in self.get_ethernet_keyfiles(): + self.osmorphing_tool._exec_cmd_chroot( + 'mv "%s" "%s%s"' % ( + cfg_path, cfg_path, backup_file_suffix)) + LOG.debug( + "Backed up ethernet nmconnection profile '%s'", cfg_path) + + def backup_nmconnection_files(self, backup_file_suffix=".bak"): + """Back up all existing nmconnection profiles.""" + for cfg_path in self.get_nmconnection_files(): + self.osmorphing_tool._exec_cmd_chroot( + 'mv "%s" "%s%s"' % ( + cfg_path, cfg_path, backup_file_suffix)) + LOG.debug( + "Backed up nmconnection profile '%s'", cfg_path) + + def parse_network(self): + nmconnection_ethernet = self.get_ethernet_keyfiles() if nmconnection_ethernet: for nmconn_file, nmconn in nmconnection_ethernet: name = nmconn.get("interface-name", nmconn.get("id")) diff --git a/coriolis/osmorphing/redhat.py b/coriolis/osmorphing/redhat.py index b7ac1449..e3be2fdc 100644 --- a/coriolis/osmorphing/redhat.py +++ b/coriolis/osmorphing/redhat.py @@ -9,6 +9,8 @@ from coriolis import exception from coriolis.osmorphing import base +from coriolis.osmorphing.netpreserver import ifcfg +from coriolis.osmorphing.netpreserver import nmconnection from coriolis.osmorphing.osdetect import centos as centos_detect from coriolis.osmorphing.osdetect import redhat as redhat_detect from coriolis import utils @@ -35,12 +37,31 @@ NAME=%(device_name)s DEVICE=%(device_name)s ONBOOT=yes -NM_CONTROLLED=no +NM_CONTROLLED=%(nm_controlled)s +""" + +NMCONNECTION_TEMPLATE = """[connection] +id=%(device_name)s +uuid=%(connection_uuid)s +type=ethernet +interface-name=%(device_name)s +autoconnect=true + +[ethernet] + +[ipv4] +method=auto +may-fail=false + +[ipv6] +method=auto +addr-gen-mode=default """ class BaseRedHatMorphingTools(base.BaseLinuxOSMorphingTools): _NETWORK_SCRIPTS_PATH = "etc/sysconfig/network-scripts" + _NM_CONNECTIONS_PATH = "etc/NetworkManager/system-connections" BIOS_GRUB_LOCATION = "/boot/grub2" UEFI_GRUB_LOCATION = "/boot/efi/EFI/redhat" @@ -97,22 +118,33 @@ def _has_systemd(self): except Exception: return False + def _get_nmconnection_net_preserver(self): + return nmconnection.NmconnectionNetPreserver(self) + + def _get_ifcfg_net_preserver(self): + return ifcfg.IfcfgNetPreserver(self) + + def _get_ifcfg_nm_controlled(self): + if self._version_supported_util(self._version, minimum=8): + return "yes" + return "no" + def _set_dhcp_net_config(self, ifcfgs_ethernet): - for ifcfg_file, ifcfg in ifcfgs_ethernet: - if ifcfg.get("BOOTPROTO") == "none": - ifcfg["BOOTPROTO"] = "dhcp" - ifcfg["UUID"] = str(uuid.uuid4()) - - if 'IPADDR' in ifcfg: - del ifcfg['IPADDR'] - if 'GATEWAY' in ifcfg: - del ifcfg['GATEWAY'] - if 'NETMASK' in ifcfg: - del ifcfg['NETMASK'] - if 'NETWORK' in ifcfg: - del ifcfg['NETWORK'] - - self._write_config_file(ifcfg_file, ifcfg) + for ifcfg_file, iface_cfg in ifcfgs_ethernet: + if iface_cfg.get("BOOTPROTO") == "none": + iface_cfg["BOOTPROTO"] = "dhcp" + iface_cfg["UUID"] = str(uuid.uuid4()) + + if 'IPADDR' in iface_cfg: + del iface_cfg['IPADDR'] + if 'GATEWAY' in iface_cfg: + del iface_cfg['GATEWAY'] + if 'NETMASK' in iface_cfg: + del iface_cfg['NETMASK'] + if 'NETWORK' in iface_cfg: + del iface_cfg['NETWORK'] + + self._write_config_file(ifcfg_file, iface_cfg) network_cfg_file = "etc/sysconfig/network" network_cfg = self._read_config_file(network_cfg_file, @@ -121,10 +153,14 @@ def _set_dhcp_net_config(self, ifcfgs_ethernet): del network_cfg["GATEWAY"] self._write_config_file(network_cfg_file, network_cfg) + def _uses_nmconnection_net_config(self): + return bool( + self._get_nmconnection_net_preserver().get_nmconnection_files()) + def _write_nic_configs(self, nics_info): for idx, _ in enumerate(nics_info): dev_name = "eth%d" % idx - cfg_path = "etc/sysconfig/network-scripts/ifcfg-%s" % dev_name + cfg_path = "%s/ifcfg-%s" % (self._NETWORK_SCRIPTS_PATH, dev_name) if self._test_path(cfg_path): self._exec_cmd_chroot( "cp %s %s.bak" % (cfg_path, cfg_path) @@ -133,8 +169,27 @@ def _write_nic_configs(self, nics_info): cfg_path, IFCFG_TEMPLATE % { "device_name": dev_name, + "nm_controlled": self._get_ifcfg_nm_controlled(), }) + def _write_nmconnection_configs(self, nics_info): + nm_net_preserver = self._get_nmconnection_net_preserver() + nm_net_preserver.backup_nmconnection_files() + device_names = ["eth%d" % idx for idx, _ in enumerate(nics_info)] + self._get_ifcfg_net_preserver().backup_ifcfg_configs(device_names) + + for idx, _ in enumerate(nics_info): + dev_name = "eth%d" % idx + cfg_path = "%s/%s.nmconnection" % ( + self._NM_CONNECTIONS_PATH, dev_name) + self._write_file_sudo( + cfg_path, + NMCONNECTION_TEMPLATE % { + "device_name": dev_name, + "connection_uuid": str(uuid.uuid4()), + }) + self._exec_cmd_chroot("chmod 600 /%s" % cfg_path) + def _comment_keys_from_ifcfg_files( self, keys, interfaces=None, backup_file_suffix=".bak"): """ Comments the provided list of keys from all 'ifcfg-*' files. @@ -172,7 +227,10 @@ def _comment_keys_from_ifcfg_files( def set_net_config(self, nics_info, dhcp): if dhcp: self.disable_predictable_nic_names() - self._write_nic_configs(nics_info) + if self._uses_nmconnection_net_config(): + self._write_nmconnection_configs(nics_info) + else: + self._write_nic_configs(nics_info) return LOG.info("Setting static IP configuration") diff --git a/coriolis/tests/osmorphing/netpreserver/test_ifcfg.py b/coriolis/tests/osmorphing/netpreserver/test_ifcfg.py index 0c7cc476..33a7eb0f 100644 --- a/coriolis/tests/osmorphing/netpreserver/test_ifcfg.py +++ b/coriolis/tests/osmorphing/netpreserver/test_ifcfg.py @@ -160,3 +160,21 @@ def test_parse_network(self, mock_get_ifcfgs_by_type): } self.assertEqual(self.netpreserver.interface_info, expected_info) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') + def test_backup_ifcfg_configs(self, mock_test_path, mock_exec_cmd_chroot): + mock_test_path.return_value = True + + self.netpreserver.backup_ifcfg_configs(['eth0', 'eth1']) + + mock_exec_cmd_chroot.assert_has_calls([ + mock.call( + 'mv "etc/sysconfig/network-scripts/ifcfg-eth0" ' + '"etc/sysconfig/network-scripts/ifcfg-eth0.bak"' + ), + mock.call( + 'mv "etc/sysconfig/network-scripts/ifcfg-eth1" ' + '"etc/sysconfig/network-scripts/ifcfg-eth1.bak"' + ), + ]) diff --git a/coriolis/tests/osmorphing/netpreserver/test_nmconnection.py b/coriolis/tests/osmorphing/netpreserver/test_nmconnection.py index 0948a785..371a03a8 100644 --- a/coriolis/tests/osmorphing/netpreserver/test_nmconnection.py +++ b/coriolis/tests/osmorphing/netpreserver/test_nmconnection.py @@ -31,7 +31,7 @@ def setUp(self): ) @mock.patch.object(base.BaseLinuxOSMorphingTools, '_list_dir') - def test_get_nmconnection_files(self, mock_list_dir): + def test__get_nmconnection_files(self, mock_list_dir): mock_list_dir.return_value = [ 'eth0.nmconnection', 'eth1.nmconnection', 'other-file'] result = self.netpreserver._get_nmconnection_files( @@ -62,41 +62,150 @@ def test_get_keyfiles_by_type(self, mock_get_nmconnection_files, self.assertEqual(result, [(mock.sentinel.nmconn_file, {"type": "ethernet"})]) - @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') - @mock.patch.object(nmconnection.NmconnectionNetPreserver, - '_get_nmconnection_files') @mock.patch.object(nmconnection.NmconnectionNetPreserver, '_get_keyfiles_by_type') - def test_check_net_preserver_True(self, mock_get_keyfiles_by_type, - mock_get_nmconnection_files, - mock_test_path): - mock_test_path.return_value = True - mock_get_nmconnection_files.return_value = ["eth0.nmconnection", - "eth1.nmconnection"] + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_nmconnection_files') + def test_check_net_preserver_True(self, mock_get_nmconnection_files, + mock_get_keyfiles_by_type): + mock_get_nmconnection_files.return_value = [ + 'etc/NetworkManager/system-connections/eth0.nmconnection'] mock_get_keyfiles_by_type.return_value = [ - (mock.sentinel.nmconn_file, {"type": "ethernet", - "connection": {"id": "eth0"}}) - ] + (mock.sentinel.nmconn_file, {"type": "ethernet"})] result = self.netpreserver.check_net_preserver() self.assertTrue(result) - @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') @mock.patch.object(nmconnection.NmconnectionNetPreserver, - '_get_nmconnection_files') - def test_check_net_preserver_no_files(self, mock_get_nmconnection_files, - mock_test_path): - mock_test_path.return_value = True + '_get_keyfiles_by_type') + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_nmconnection_files') + def test_check_net_preserver_no_ethernet_files( + self, mock_get_nmconnection_files, + mock_get_keyfiles_by_type): + mock_get_nmconnection_files.return_value = [ + 'etc/NetworkManager/system-connections/vpn.nmconnection'] + mock_get_keyfiles_by_type.return_value = [] + + result = self.netpreserver.check_net_preserver() + + self.assertFalse(result) + + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_nmconnection_files') + def test_check_net_preserver_no_files(self, mock_get_nmconnection_files): mock_get_nmconnection_files.return_value = [] result = self.netpreserver.check_net_preserver() self.assertFalse(result) + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + '_get_nmconnection_files') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') + def test_get_nmconnection_files(self, mock_test_path, + mock_get_nmconnection_files): + mock_test_path.return_value = True + mock_get_nmconnection_files.return_value = [ + 'etc/NetworkManager/system-connections/eth0.nmconnection'] + + result = self.netpreserver.get_nmconnection_files() + + mock_get_nmconnection_files.assert_called_once_with( + self.netpreserver.nmconnection_file) + self.assertEqual(result, mock_get_nmconnection_files.return_value) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') + def test_get_nmconnection_files_no_dir(self, mock_test_path): + mock_test_path.return_value = False + + result = self.netpreserver.get_nmconnection_files() + + self.assertEqual(result, []) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') @mock.patch.object(nmconnection.NmconnectionNetPreserver, '_get_keyfiles_by_type') - def test_parse_network(self, mock_get_keyfiles_by_type): + def test_get_ethernet_keyfiles(self, mock_get_keyfiles_by_type, + mock_test_path): + mock_test_path.return_value = True + mock_get_keyfiles_by_type.return_value = [ + (mock.sentinel.nmconn_file, {"type": "ethernet"})] + + result = self.netpreserver.get_ethernet_keyfiles() + + mock_get_keyfiles_by_type.assert_called_once_with( + "ethernet", self.netpreserver.nmconnection_file) + self.assertEqual(result, mock_get_keyfiles_by_type.return_value) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') + def test_get_ethernet_keyfiles_no_dir(self, mock_test_path): + mock_test_path.return_value = False + + result = self.netpreserver.get_ethernet_keyfiles() + + self.assertEqual(result, []) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_nmconnection_files') + def test_backup_nmconnection_files(self, mock_get_nmconnection_files, + mock_exec_cmd_chroot): + mock_get_nmconnection_files.return_value = [ + 'etc/NetworkManager/system-connections/ens192.nmconnection', + 'etc/NetworkManager/system-connections/vpn.nmconnection', + ] + + self.netpreserver.backup_nmconnection_files() + + mock_exec_cmd_chroot.assert_has_calls([ + mock.call( + 'mv "etc/NetworkManager/system-connections/' + 'ens192.nmconnection" ' + '"etc/NetworkManager/system-connections/' + 'ens192.nmconnection.bak"' + ), + mock.call( + 'mv "etc/NetworkManager/system-connections/' + 'vpn.nmconnection" ' + '"etc/NetworkManager/system-connections/' + 'vpn.nmconnection.bak"' + ), + ]) + + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_ethernet_keyfiles') + def test_backup_ethernet_keyfiles(self, mock_get_ethernet_keyfiles, + mock_exec_cmd_chroot): + mock_get_ethernet_keyfiles.return_value = [ + ('etc/NetworkManager/system-connections/ens192.nmconnection', + {'type': 'ethernet'}), + ('etc/NetworkManager/system-connections/eth0.nmconnection', + {'type': 'ethernet'}), + ] + + self.netpreserver.backup_ethernet_keyfiles() + + mock_exec_cmd_chroot.assert_has_calls([ + mock.call( + 'mv "etc/NetworkManager/system-connections/' + 'ens192.nmconnection" ' + '"etc/NetworkManager/system-connections/' + 'ens192.nmconnection.bak"' + ), + mock.call( + 'mv "etc/NetworkManager/system-connections/' + 'eth0.nmconnection" ' + '"etc/NetworkManager/system-connections/' + 'eth0.nmconnection.bak"' + ), + ]) + + @mock.patch.object(nmconnection.NmconnectionNetPreserver, + 'get_ethernet_keyfiles') + def test_parse_network(self, mock_get_ethernet_keyfiles): nmconn_file_with_id = ( self.netpreserver.nmconnection_file + "/eth0.nmconnection" ) @@ -137,7 +246,7 @@ def test_parse_network(self, mock_get_keyfiles_by_type): "id": "id_eth4", "address": "192.168.1.60/24", } - mock_get_keyfiles_by_type.return_value = [ + mock_get_ethernet_keyfiles.return_value = [ (nmconn_file_with_id, nmconn_with_id), (nmconn_file_without_id, nmconn_without_id), (nmconn_file_without_mac_address, nmconn_without_mac_address), diff --git a/coriolis/tests/osmorphing/test_redhat.py b/coriolis/tests/osmorphing/test_redhat.py index f3c1e459..9009563f 100644 --- a/coriolis/tests/osmorphing/test_redhat.py +++ b/coriolis/tests/osmorphing/test_redhat.py @@ -8,6 +8,8 @@ from coriolis import exception from coriolis.osmorphing import base +from coriolis.osmorphing.netpreserver import ifcfg +from coriolis.osmorphing.netpreserver import nmconnection from coriolis.osmorphing import redhat from coriolis.tests import test_base @@ -180,11 +182,17 @@ def test_write_nic_configs( mock_write_file_sudo.assert_has_calls([ mock.call( "etc/sysconfig/network-scripts/ifcfg-eth0", - redhat.IFCFG_TEMPLATE % {"device_name": "eth0"}, + redhat.IFCFG_TEMPLATE % { + "device_name": "eth0", + "nm_controlled": "no", + }, ), mock.call( "etc/sysconfig/network-scripts/ifcfg-eth1", - redhat.IFCFG_TEMPLATE % {"device_name": "eth1"}, + redhat.IFCFG_TEMPLATE % { + "device_name": "eth1", + "nm_controlled": "no", + }, ) ]) mock_exec_cmd_chroot.assert_has_calls([ @@ -194,6 +202,151 @@ def test_write_nic_configs( "etc/sysconfig/network-scripts/ifcfg-eth1.bak") ]) + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_write_file_sudo') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_test_path') + def test_write_nic_configs_rhel8( + self, mock_test_path, mock_exec_cmd_chroot, mock_write_file_sudo): + self.morphing_tools._version = '8.10' + nics_info = [{'name': 'eth0'}] + mock_test_path.return_value = False + + self.morphing_tools._write_nic_configs(nics_info) + + mock_write_file_sudo.assert_called_once_with( + "etc/sysconfig/network-scripts/ifcfg-eth0", + redhat.IFCFG_TEMPLATE % { + "device_name": "eth0", + "nm_controlled": "yes", + }, + ) + + @ddt.data(('6', 'no'), ('7.9', 'no'), ('8.10', 'yes')) + @ddt.unpack + def test__get_ifcfg_nm_controlled(self, release_version, expected): + self.morphing_tools._version = release_version + + result = self.morphing_tools._get_ifcfg_nm_controlled() + + self.assertEqual(expected, result) + + @mock.patch.object( + ifcfg.IfcfgNetPreserver, + 'backup_ifcfg_configs' + ) + @mock.patch.object( + nmconnection.NmconnectionNetPreserver, + 'backup_nmconnection_files' + ) + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_get_ifcfg_net_preserver' + ) + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_get_nmconnection_net_preserver' + ) + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_write_file_sudo') + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot') + def test_write_nmconnection_configs( + self, mock_exec_cmd_chroot, mock_write_file_sudo, + mock_get_nmconnection_net_preserver, + mock_get_ifcfg_net_preserver, + mock_backup_nmconnection_files, + mock_backup_ifcfg_configs): + mock_nm_preserver = mock_get_nmconnection_net_preserver.return_value + mock_ifcfg_preserver = mock_get_ifcfg_net_preserver.return_value + nics_info = [{'name': 'eth0'}] + + self.morphing_tools._write_nmconnection_configs(nics_info) + + mock_nm_preserver.backup_nmconnection_files.assert_called_once_with() + mock_ifcfg_preserver.backup_ifcfg_configs.assert_called_once_with( + ['eth0']) + mock_write_file_sudo.assert_called_once() + args, _ = mock_write_file_sudo.call_args + self.assertEqual( + args[0], + "etc/NetworkManager/system-connections/eth0.nmconnection") + self.assertIn("[connection]", args[1]) + self.assertIn("interface-name=eth0", args[1]) + self.assertIn("method=auto", args[1]) + self.assertIn("may-fail=false", args[1]) + mock_exec_cmd_chroot.assert_called_once_with( + "chmod 600 /etc/NetworkManager/system-connections/" + "eth0.nmconnection" + ) + + @ddt.data( + (True, True), + (False, False), + ) + @ddt.unpack + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_get_nmconnection_net_preserver' + ) + def test__uses_nmconnection_net_config( + self, has_nmconnection_files, expected, + mock_get_nmconnection_net_preserver): + mock_nm_preserver = ( + mock_get_nmconnection_net_preserver.return_value) + if has_nmconnection_files: + mock_nm_preserver.get_nmconnection_files.return_value = [ + 'etc/NetworkManager/system-connections/eth0.nmconnection'] + else: + mock_nm_preserver.get_nmconnection_files.return_value = [] + + result = self.morphing_tools._uses_nmconnection_net_config() + + self.assertEqual(expected, result) + + @mock.patch.object( + redhat.BaseRedHatMorphingTools, 'disable_predictable_nic_names' + ) + @mock.patch.object(redhat.BaseRedHatMorphingTools, '_write_nic_configs') + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_uses_nmconnection_net_config' + ) + def test_set_net_config_dhcp( + self, mock_uses_nmconnection_net_config, + mock_write_nic_configs, + mock_disable_predictable_nic_names): + mock_uses_nmconnection_net_config.return_value = False + nics_info = [{ + 'mac_address': mock.sentinel.mac_address, + }] + dhcp = True + + self.morphing_tools.set_net_config(nics_info, dhcp) + + mock_disable_predictable_nic_names.assert_called_once() + mock_write_nic_configs.assert_called_once_with(nics_info) + + @mock.patch.object( + redhat.BaseRedHatMorphingTools, 'disable_predictable_nic_names' + ) + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_write_nmconnection_configs' + ) + @mock.patch.object(redhat.BaseRedHatMorphingTools, '_write_nic_configs') + @mock.patch.object( + redhat.BaseRedHatMorphingTools, '_uses_nmconnection_net_config' + ) + def test_set_net_config_dhcp_nmconnection( + self, mock_uses_nmconnection_net_config, + mock_write_nic_configs, + mock_write_nmconnection_configs, + mock_disable_predictable_nic_names): + mock_uses_nmconnection_net_config.return_value = True + nics_info = [{ + 'mac_address': mock.sentinel.mac_address, + }] + dhcp = True + + self.morphing_tools.set_net_config(nics_info, dhcp) + + mock_disable_predictable_nic_names.assert_called_once() + mock_write_nmconnection_configs.assert_called_once_with(nics_info) + mock_write_nic_configs.assert_not_called() + @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd') @mock.patch.object(redhat.utils, 'list_ssh_dir') def test__comment_keys_from_ifcfg_files( @@ -258,22 +411,6 @@ def test__comment_keys_from_ifcfg_files_no_interfaces( ), ]) - @mock.patch.object( - redhat.BaseRedHatMorphingTools, 'disable_predictable_nic_names' - ) - @mock.patch.object(redhat.BaseRedHatMorphingTools, '_write_nic_configs') - def test_set_net_config_dhcp(self, mock_write_nic_configs, - mock_disable_predictable_nic_names): - nics_info = [{ - 'mac_address': mock.sentinel.mac_address, - }] - dhcp = True - - self.morphing_tools.set_net_config(nics_info, dhcp) - - mock_disable_predictable_nic_names.assert_called_once() - mock_write_nic_configs.assert_called_once_with(nics_info) - @mock.patch.object( redhat.BaseRedHatMorphingTools, 'disable_predictable_nic_names' )