def ifdown(self, interface, iftype='interface'): msg = 'running ifdown on %s: %s' % (iftype, interface) self.execute(msg, '/sbin/ifdown', interface, check_exit_code=False) if utils.is_active_nic(interface): msg = '%s %s is up, trying with ip command' % (iftype, interface) self.execute(msg, '/sbin/ip', 'link', 'set', 'dev', interface, 'down')
def ifrename(self, oldname, newname): msg = 'renaming %s to %s: ' % (oldname, newname) # ifdown isn't enough when renaming, we need the link down for name in (oldname, newname): if utils.is_active_nic(name): self.execute(msg, '/sbin/ip', 'link', 'set', 'dev', name, 'down') self.execute(msg, '/sbin/ip', 'link', 'set', 'dev', name, 'link', 'down') self.execute(msg, '/sbin/ip', 'link', 'set', 'dev', oldname, 'name', newname) self.execute(msg, '/sbin/ip', 'link', 'set', 'dev', newname, 'up')
def test_is_active_nic_for_sriov_vf(self): tmpdir = tempfile.mkdtemp() self.stub_out('os_net_config.utils._SYS_CLASS_NET', tmpdir) # SR-IOV PF = ens802f0 # SR-IOV VF = enp129s2 for nic in ['ens802f0', 'enp129s2']: nic_path = os.path.join(tmpdir, nic) os.makedirs(nic_path) os.makedirs(os.path.join(nic_path, 'device')) with open(os.path.join(nic_path, 'operstate'), 'w') as f: f.write('up') with open(os.path.join(nic_path, 'address'), 'w') as f: f.write('1.2.3.4') nic_path = os.path.join(tmpdir, 'enp129s2', 'device', 'physfn') os.makedirs(nic_path) self.assertEqual(utils.is_active_nic('ens802f0'), True) self.assertEqual(utils.is_active_nic('enp129s2'), False) shutil.rmtree(tmpdir)