Sometimes, /sys/class/net may contain files (such as bonding_masters, if the bonding module is loaded). So, let's handle the ShellCmdErrors that might arise when trying to list the mac address of the interfaces.
Also, if no suitable interfaces were found, just return None. Signed-off-by: Lucas Meneghel Rodrigues <[email protected]> --- virttest/utils_test.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/virttest/utils_test.py b/virttest/utils_test.py index b23634d..11b014d 100644 --- a/virttest/utils_test.py +++ b/virttest/utils_test.py @@ -1872,17 +1872,25 @@ def get_linux_ifname(session, mac_address): def sys_method(): try: interfaces = session.cmd('ls --color=never /sys/class/net') - interfaces = interfaces.strip() - for interface in interfaces.split(" "): - if interface: - mac_address_interface = session.cmd("cat " - "/sys/class/net/%s/address" % interface) + except error.CmdError, e: + logging.debug("Error listing /sys/class/net: %s" % e) + return None + + interfaces = interfaces.strip() + for interface in interfaces.split(" "): + if interface: + try: + i_cmd = "cat /sys/class/net/%s/address" % interface + mac_address_interface = session.cmd(i_cmd) mac_address_interface = mac_address_interface.strip() if mac_address_interface == mac_address: return interface - except error.CmdError, e: - logging.debug(e) - return None + except aexpect.ShellCmdError: + pass + + # If after iterating through interfaces (or no interfaces found) + # no interface name was found, just return None + return None # Try ifconfig first i = ifconfig_method() -- 1.8.1.4 _______________________________________________ Virt-test-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/virt-test-devel
