Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtaining the ip address of the guest and update address cache #4002

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions virttest/libvirt_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
test_setup,
utils_logfile,
utils_misc,
utils_net,
utils_package,
utils_selinux,
virsh,
Expand Down Expand Up @@ -378,6 +379,15 @@ def backup_xml(self, active=False):
LOG.error("Failed to backup xml file:\n%s", detail)
return ""

def _get_address(self, index=0, ip_version="ipv4", session=None, timeout=60.0):
try:
return super()._get_address(index, ip_version, session, timeout)
except virt_vm.VMIPAddressMissingError:
mac = self.get_mac_address(index).lower()
ipaddr = utils_net.obtain_guest_ip_from_dhcp_leases(mac)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have another concern: we have this scenario:
modifying interface type from network to bridge, but keep the mac address. Seems the dhcp-lease will not be deleted for a while, but the ip address of interface must change. There's a chance we're getting the 'old' ip address, right?

self.address_cache[mac] = ipaddr
return ipaddr

def clone(
self,
name=None,
Expand Down
19 changes: 19 additions & 0 deletions virttest/utils_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
utils_misc,
utils_package,
utils_selinux,
virsh,
)
from virttest.remote import RemoteRunner
from virttest.staging import service, utils_memory
Expand Down Expand Up @@ -4889,3 +4890,21 @@ def check_class_rules(ifname, rule_id, bandwidth, expect_none=False):
stacktrace.log_exc_info(sys.exc_info())
return False
return True


def obtain_guest_ip_from_dhcp_leases(mac):
"""
Obtaining the guest ip address from virsh-net-dhcp-leases command
:param: Mac address of the guest
:return: return ip-address if found for given mac in the
virsh-net-dhcp-leases default table, else return None
"""
output = virsh.net_dhcp_leases("default")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will get dhcp lease from network "default" only, which might not be the default setting for test vms, right?

lines = output.stdout.splitlines()
for line in lines:
if mac in line:
parts = line.split()
for part in parts:
if "/" in part:
return part.split("/")[0]
return None
Loading