autotest: Move path utilities out of wifi_test_utils

Refactor the utilities to be usable from a client context.

BUG=None
TEST=network_WiFi_SimpleConnect.wifi_checkWPA_TKIP passes.

Change-Id: If820921d777b0377f529ea40cc34c3377e192150
Reviewed-on: https://chromium-review.googlesource.com/236658
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
Tested-by: Christopher Wiley <wiley@chromium.org>
diff --git a/server/cros/wifi_test_utils.py b/client/common_lib/cros/path_utils.py
similarity index 70%
rename from server/cros/wifi_test_utils.py
rename to client/common_lib/cros/path_utils.py
index 979aa69..81a78f5 100644
--- a/server/cros/wifi_test_utils.py
+++ b/client/common_lib/cros/path_utils.py
@@ -1,21 +1,25 @@
-# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+# Copyright 2014 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
 import os
 
+from autotest_lib.client.bin import utils
 from autotest_lib.client.common_lib import error
 
 
-def get_install_path(host, filename):
+def get_install_path(filename, host=None):
     """
     Checks if a file exists on a remote machine in one of several paths.
 
-    @param host Host object representing the remote machine.
     @param filename String name of the file to check for existence.
+    @param host Host object representing the remote machine.
     @return String full path of installed file, or None if not found.
 
     """
+    run = utils.run
+    if host is not None:
+        run = host.run
     PATHS = ['/bin',
              '/sbin',
              '/system/bin',
@@ -32,24 +36,30 @@
     return found_path or None
 
 
-def must_be_installed(host, cmd):
+def must_be_installed(cmd, host=None):
     """
     Asserts that cmd is installed on a remote machine at some path and raises
     an exception if this is not the case.
 
-    @param host Host object representing the remote machine.
     @param cmd String name of the command to check for existence.
+    @param host Host object representing the remote machine.
     @return String full path of cmd on success.  Error raised on failure.
 
     """
-    if host.run('ls %s >/dev/null 2>&1' % cmd,
-                ignore_status=True).exit_status == 0:
+    run = utils.run
+    if host is not None:
+        run = host.run
+    if run('ls %s >/dev/null 2>&1' % cmd,
+           ignore_status=True).exit_status == 0:
         return cmd
 
     # Hunt for the equivalent file in a bunch of places.
     cmd_base = os.path.basename(cmd)
-    alternate_path = get_install_path(host, cmd_base)
+    alternate_path = get_install_path(cmd_base, host=host)
     if alternate_path:
         return alternate_path
 
-    raise error.TestFail('Unable to find %s on %s' % (cmd, host.ip))
+    error_msg = 'Unable to find %s' % cmd
+    if host is not None:
+        error_msg += 'on %s' % host.hostname
+    raise error.TestError(error_msg)
diff --git a/server/cros/network/arping_runner.py b/server/cros/network/arping_runner.py
index 316529c..bf82c80 100644
--- a/server/cros/network/arping_runner.py
+++ b/server/cros/network/arping_runner.py
@@ -5,7 +5,7 @@
 import re
 
 from autotest_lib.client.common_lib import error
-from autotest_lib.server.cros import wifi_test_utils
+from autotest_lib.client.common_lib.cros import path_utils
 
 
 class ArpingRunner(object):
@@ -17,8 +17,8 @@
 
     def __init__(self, host, ping_interface):
         self._host = host
-        self._arping_command = wifi_test_utils.must_be_installed(
-                host, '/usr/bin/arping')
+        self._arping_command = path_utils.must_be_installed(
+                '/usr/bin/arping', host=host)
         self._ping_interface = ping_interface
 
 
diff --git a/server/cros/network/netperf_runner.py b/server/cros/network/netperf_runner.py
index 0a176d5..4498a78 100644
--- a/server/cros/network/netperf_runner.py
+++ b/server/cros/network/netperf_runner.py
@@ -10,7 +10,7 @@
 import os.path
 
 from autotest_lib.client.common_lib import error
-from autotest_lib.server.cros import wifi_test_utils
+from autotest_lib.client.common_lib.cros import path_utils
 
 
 class NetperfResult(object):
@@ -490,10 +490,10 @@
             self._server_host = client_proxy.host
             self._client_host = server_proxy.host
             self._target_ip = client_proxy.wifi_ip
-        self._command_netserv = wifi_test_utils.must_be_installed(
-                self._server_host, 'netserver')
-        self._command_netperf = wifi_test_utils.must_be_installed(
-                self._client_host, 'netperf')
+        self._command_netserv = path_utils.must_be_installed(
+                'netserver', host=self._server_host)
+        self._command_netperf = path_utils.must_be_installed(
+                'netperf', host=self._client_host)
         self._config = config
 
 
diff --git a/server/cros/network/packet_capturer.py b/server/cros/network/packet_capturer.py
index 491357d..d6022e0 100644
--- a/server/cros/network/packet_capturer.py
+++ b/server/cros/network/packet_capturer.py
@@ -8,7 +8,7 @@
 import uuid
 
 from autotest_lib.client.common_lib import error
-from autotest_lib.server.cros import wifi_test_utils
+from autotest_lib.client.common_lib.cros import path_utils
 
 
 class PacketCapturesDisabledError(Exception):
@@ -31,14 +31,12 @@
 def get_packet_capturer(host, host_description=None, cmd_ifconfig=None,
                         cmd_ip=None, cmd_iw=None, cmd_netdump=None,
                         ignore_failures=False):
-    cmd_ifconfig = cmd_ifconfig or wifi_test_utils.get_install_path(
-            host, 'ifconfig')
-    cmd_iw = cmd_iw or wifi_test_utils.get_install_path(
-            host, 'iw')
-    cmd_ip = cmd_ip or wifi_test_utils.get_install_path(
-            host, 'ip')
-    cmd_netdump = cmd_netdump or wifi_test_utils.get_install_path(
-            host, 'tcpdump')
+    cmd_ifconfig = (cmd_ifconfig or
+                    path_utils.get_install_path('ifconfig', host=host))
+    cmd_iw = cmd_iw or path_utils.get_install_path('iw', host=host)
+    cmd_ip = cmd_ip or path_utils.get_install_path('ip', host=host)
+    cmd_netdump = (cmd_netdump or
+                   path_utils.get_install_path('tcpdump', host=host))
     host_description = host_description or 'cap_%s' % uuid.uuid4().hex
     if None in [cmd_ifconfig, cmd_iw, cmd_ip, cmd_netdump, host_description]:
         if ignore_failures:
diff --git a/server/site_linux_router.py b/server/site_linux_router.py
index 873ebec..3453078 100644
--- a/server/site_linux_router.py
+++ b/server/site_linux_router.py
@@ -9,13 +9,13 @@
 import time
 
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros import path_utils
 from autotest_lib.client.common_lib.cros.network import interface
 from autotest_lib.client.common_lib.cros.network import netblock
 from autotest_lib.client.common_lib.cros.network import ping_runner
 from autotest_lib.server import hosts
 from autotest_lib.server import site_linux_system
 from autotest_lib.server.cros import dnsname_mangler
-from autotest_lib.server.cros import wifi_test_utils
 from autotest_lib.server.cros.network import hostap_config
 
 
@@ -104,8 +104,8 @@
         """@return iterable object of AP capabilities for this system."""
         caps = set([self.CAPABILITY_IBSS])
         try:
-            self.cmd_send_management_frame = wifi_test_utils.must_be_installed(
-                    self.host, '/usr/bin/send_management_frame')
+            self.cmd_send_management_frame = path_utils.must_be_installed(
+                    '/usr/bin/send_management_frame', host=self.host)
             caps.add(self.CAPABILITY_SEND_MANAGEMENT_FRAME)
         except error.TestFail:
             pass
@@ -145,12 +145,12 @@
         super(LinuxRouter, self).__init__(host, 'router')
 
         self.cmd_dhcpd = '/usr/sbin/dhcpd'
-        self.cmd_hostapd = wifi_test_utils.must_be_installed(
-                host, '/usr/sbin/hostapd')
-        self.cmd_hostapd_cli = wifi_test_utils.must_be_installed(
-                host, '/usr/sbin/hostapd_cli')
-        self.cmd_wpa_supplicant = wifi_test_utils.must_be_installed(
-                host, '/usr/sbin/wpa_supplicant')
+        self.cmd_hostapd = path_utils.must_be_installed(
+                '/usr/sbin/hostapd', host=host)
+        self.cmd_hostapd_cli = path_utils.must_be_installed(
+                '/usr/sbin/hostapd_cli', host=host)
+        self.cmd_wpa_supplicant = path_utils.must_be_installed(
+                '/usr/sbin/wpa_supplicant', host=host)
         self.dhcpd_conf = '/tmp/dhcpd.%s.conf'
         self.dhcpd_leases = '/tmp/dhcpd.leases'
 
diff --git a/server/site_linux_system.py b/server/site_linux_system.py
index 4e519fe..92b06a2 100644
--- a/server/site_linux_system.py
+++ b/server/site_linux_system.py
@@ -8,9 +8,9 @@
 import time
 
 from autotest_lib.client.common_lib import error
+from autotest_lib.client.common_lib.cros import path_utils
 from autotest_lib.client.common_lib.cros.network import iw_runner
 from autotest_lib.client.common_lib.cros.network import ping_runner
-from autotest_lib.server.cros import wifi_test_utils
 from autotest_lib.server.cros.network import packet_capturer
 
 NetDev = collections.namedtuple('NetDev',
@@ -48,12 +48,10 @@
 
     def __init__(self, host, role, inherit_interfaces=False):
         # Command locations.
-        cmd_iw = wifi_test_utils.must_be_installed(
-                host, '/usr/sbin/iw')
-        self.cmd_ip = wifi_test_utils.must_be_installed(
-                host, '/usr/sbin/ip')
-        self.cmd_readlink = '%s -l' % wifi_test_utils.must_be_installed(
-                host, '/bin/ls')
+        cmd_iw = path_utils.must_be_installed('/usr/sbin/iw', host=host)
+        self.cmd_ip = path_utils.must_be_installed('/usr/sbin/ip', host=host)
+        self.cmd_readlink = '%s -l' % path_utils.must_be_installed(
+                '/bin/ls', host=host)
 
         self.host = host
         self.role = role