Restore IP addresses configuration after spoofing MAC address

In the test_port_security_macspoofing_port test, NIC on one of the vms
is set to DOWN, MAC of this NIC is changed to the spoofed one and NIC is
then bring back to UP.
For some reason it works fine in Cirros 0.5.x but not in 0.6.0 and newer
as after bringinig interface back to be UP there is also need to restore
configured previously IP addresses.
This patch adds check of IPs configured on that NIC before it is switch
to DOWN and later restores the same IPs configuration when NIC is UP
again.

Related-Bug: #2003063
Change-Id: I05d2118125195a387163ad1f0177fd9dfc916238
This commit is contained in:
Slawek Kaplonski
2023-01-20 12:30:05 +01:00
parent 49163f9156
commit 4e4a43ee33
2 changed files with 18 additions and 2 deletions

View File

@@ -109,6 +109,15 @@ class RemoteClient(remote_client.RemoteClient):
LOG.debug('(get_nic_name_by_ip) Command result: %s', nic)
return nic.strip().strip(":").split('@')[0].lower()
def get_nic_ip_addresses(self, nic_name, ip_version=None):
cmd = "ip "
if ip_version:
cmd += "-%s " % ip_version
cmd += "-o addr | awk '/%s/ {print $4}'" % nic_name
ip_addresses = self.exec_command(cmd)
LOG.debug('(get_nic_ip_address): Command result: %s', ip_addresses)
return ip_addresses.strip().split()
def _get_dns_servers(self):
cmd = 'cat /etc/resolv.conf'
resolve_file = self.exec_command(cmd).strip().split('\n')

View File

@@ -897,10 +897,17 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
self.check_remote_connectivity(ssh_client, dest=peer_address,
nic=spoof_nic, should_succeed=True)
# Set a mac address by making nic down temporary
spoof_ip_addresses = ssh_client.get_nic_ip_addresses(spoof_nic)
cmd = ("sudo ip link set {nic} down;"
"sudo ip link set dev {nic} address {mac};"
"sudo ip link set {nic} up").format(nic=spoof_nic,
mac=spoof_mac)
"sudo ip link set {nic} up;"
"sudo ip address flush dev {nic};").format(nic=spoof_nic,
mac=spoof_mac)
for ip_address in spoof_ip_addresses:
cmd += (
"sudo ip addr add {ip_address} dev {nic};"
).format(ip_address=ip_address, nic=spoof_nic)
ssh_client.exec_command(cmd)
new_mac = ssh_client.get_mac_address(nic=spoof_nic)