Bug 1003417 - Use 'ip addr show' instead of 'ifconfig' for moznetwork tests, r?ted draft
authorAndrew Halberstadt <ahalberstadt@mozilla.com>
Fri, 06 Jan 2017 11:19:19 -0500
changeset 480652 6a12339f7dbfed0fb5f8f7ba433de6c202c0134d
parent 480651 5dd159fa43700688d41e4959bafdedef83885165
child 480653 59a4340b7a031bc859c740e17ec0f4d1a0cb94e3
push id44617
push userahalberstadt@mozilla.com
push dateWed, 08 Feb 2017 19:29:51 +0000
Bug 1003417 - Use 'ip addr show' instead of 'ifconfig' for moznetwork tests, r?ted The taskcluster docker image for source-check tasks does not have 'ifconfig' installed. We could add this package, but ifconfig is more or less deprecated in favour of 'ip addr show'. Although the formats of both commands are different, because the test pulls ip addresses out of the output with regexes, the only change that is needed to make sure the tests still pass is to change the command. MozReview-Commit-ID: 758Qb6KSHzS
--- a/testing/mozbase/moznetwork/tests/test.py
+++ b/testing/mozbase/moznetwork/tests/test.py
@@ -1,20 +1,20 @@
 #!/usr/bin/env python
 Unit-Tests for moznetwork
-import os
 import mock
 import mozinfo
 import moznetwork
 import re
 import subprocess
 import unittest
+from distutils.spawn import find_executable
 import mozunit
 def verify_ip_in_list(ip):
     Helper Method to check if `ip` is listed in Network Adresses
     returned by ipconfig/ifconfig, depending on the platform in use
@@ -26,29 +26,33 @@ def verify_ip_in_list(ip):
     returns True if the `ip` is in the list of IPs in ipconfig/ifconfig
     # Regex to match IPv4 addresses.
     # 0-255.0-255.0-255.0-255, note order is important here.
     regexip = re.compile("((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}"
-    if mozinfo.isLinux or mozinfo.isMac or mozinfo.isBsd:
-        # if "/sbin/ifconfig" exist, use it because it may not be in the
-        # PATH (at least on some linux platforms)
-        if os.path.isfile('/sbin/ifconfig') and os.access('/sbin/ifconfig',
-                                                          os.X_OK):
-            args = ['/sbin/ifconfig']
-        else:
-            args = ["ifconfig"]
+    commands = (
+        ['ip', 'addr', 'show'],
+        ['ifconfig'],
+        ['ipconfig'],
+        # Explicitly search '/sbin' because it doesn't always appear
+        # to be on the $PATH of all systems
+        ['/sbin/ip', 'addr', 'show'],
+        ['/sbin/ifconfig'],
+    )
-    if mozinfo.isWin:
-        args = ["ipconfig"]
+    cmd = None
+    for command in commands:
+        if find_executable(command[0]):
+            cmd = command
+            break
-    ps = subprocess.Popen(args, stdout=subprocess.PIPE)
+    ps = subprocess.Popen(cmd, stdout=subprocess.PIPE)
     standardoutput, standarderror = ps.communicate()
     # Generate a list of IPs by parsing the output of ip/ifconfig
     ip_list = [x.group() for x in re.finditer(regexip, standardoutput)]
     # Check if ip is in list
     if ip in ip_list:
         return True