brillo: Fix wifi tests to work in lab and if run as a suite.

Check if the host is in the lab before getting wireless ssid. Also,
allow test to be run when the ssid arg is None.

BUG:b/27297360
TEST=tests pass with or without ssid arg

Change-Id: I1b7fea416b24377d7548a4cb576a8f3c82410bec
Reviewed-on: https://chromium-review.googlesource.com/337322
Commit-Ready: Ralph Nathan <ralphnathan@chromium.org>
Tested-by: Ralph Nathan <ralphnathan@chromium.org>
Reviewed-by: Dan Shi <dshi@chromium.org>
diff --git a/server/brillo/host_utils.py b/server/brillo/host_utils.py
index 05a3b02..e1bf1c1 100644
--- a/server/brillo/host_utils.py
+++ b/server/brillo/host_utils.py
@@ -55,36 +55,48 @@
     """Connects to a given ssid.
 
     @param host: A host object representing the DUT.
-    @param ssid: A string representing the ssid to connect to.
+    @param ssid: A string representing the ssid to connect to. If ssid is None,
+                 assume that host is already connected to wifi.
     @param passphrase: A string representing the passphrase to the ssid.
                        Defaults to None.
     """
     try:
-        # Update the weaved init.rc to stop privet. This sets up shill in client
-        # mode allowing it to connect to wifi.
-        host.remount()
-        host.run('sed \'s/service weaved \/system\/bin\/weaved/'
-                 'service weaved \/system\/bin\/weaved --disable_privet/\' -i '
-                 '/system/etc/init/weaved.rc')
-        host.reboot()
-        client_site_utils.poll_for_condition(
-                lambda: 'running' in host.run('getprop init.svc.shill').stdout,
-                sleep_interval=1, timeout=300,
-                desc='shill was not started by init')
-        logging.info('Connecting to wifi')
-        wifi_cmd = 'su root shill_setup_wifi --ssid=%s' % ssid
-        if passphrase:
-            wifi_cmd += ' --passphrase=%s' % passphrase
-        host.run(wifi_cmd)
-        # TODO(ralphnathan): Once shill_setup_wifi can monitor the service as it
-        # connects to wifi, remove this timeout.
-        # Wait for wifi connection to occur.
-        time.sleep(10)
-        yield
+        if ssid is None:
+            # No ssid is passed. It is assumed that the host is already
+            # connected to wifi.
+            logging.warning('This test assumes that the device is connected to '
+                            'wifi. If it is not, this test will fail.')
+            yield
+        else:
+            logging.info('Connecting to ssid %s', ssid)
+            # Update the weaved init.rc to stop privet. This sets up shill in
+            # client mode allowing it to connect to wifi.
+            host.remount()
+            host.run('sed \'s/service weaved \/system\/bin\/weaved/'
+                     'service weaved \/system\/bin\/weaved --disable_privet/\' '
+                     '-i /system/etc/init/weaved.rc')
+            host.reboot()
+            client_site_utils.poll_for_condition(
+                    lambda: 'running' in host.run('getprop init.svc.shill'
+                                                  ).stdout,
+                    sleep_interval=1, timeout=300,
+                    desc='shill was not started by init')
+            logging.info('Connecting to wifi')
+            wifi_cmd = 'su root shill_setup_wifi --ssid=%s' % ssid
+            if passphrase:
+                wifi_cmd += ' --passphrase=%s' % passphrase
+            host.run(wifi_cmd)
+            # TODO(ralphnathan): Once shill_setup_wifi can monitor the service as it
+            # connects to wifi, remove this timeout.
+            # Wait for wifi connection to occur.
+            time.sleep(10)
+            yield
     finally:
-        host.remount()
-        host.run('sed \'s/service weaved \/system\/bin\/weaved '
-                 '--disable_privet/service weaved \/system\/bin\/weaved/\' -i '
-                 '/system/etc/init/weaved.rc')
-        host.run('su root stop weaved')
-        host.run('su root start weaved')
+        if ssid:
+            # If we connected to a ssid, disconnect.
+            host.remount()
+            host.run('sed \'s/service weaved \/system\/bin\/weaved '
+                     '--disable_privet/service weaved \/system\/bin\/weaved/\' '
+                     '-i /system/etc/init/weaved.rc')
+            host.run('su root stop weaved')
+            host.run('su root start weaved')
diff --git a/server/site_tests/brillo_PingTest/brillo_PingTest.py b/server/site_tests/brillo_PingTest/brillo_PingTest.py
index d7f2d34..34a58b8 100644
--- a/server/site_tests/brillo_PingTest/brillo_PingTest.py
+++ b/server/site_tests/brillo_PingTest/brillo_PingTest.py
@@ -7,6 +7,7 @@
 import common
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib import site_utils
+from autotest_lib.server import afe_utils
 from autotest_lib.server import test
 from autotest_lib.server.brillo import host_utils
 
@@ -36,9 +37,8 @@
 
         @raise TestFail: The test failed.
         """
-        if ssid is None:
+        if afe_utils.host_in_lab(host):
             ssid = site_utils.get_wireless_ssid(host.hostname)
-        logging.info('Connecting to ssid %s', ssid)
         with host_utils.connect_to_ssid(host, ssid, passphrase):
             cmd = 'ping -q -c %s -W %s %s' % (ping_count, ping_timeout,
                                               ping_host)
diff --git a/server/site_tests/brillo_WifiInterfaceTest/brillo_WifiInterfaceTest.py b/server/site_tests/brillo_WifiInterfaceTest/brillo_WifiInterfaceTest.py
index 1327d75..d39bf43 100644
--- a/server/site_tests/brillo_WifiInterfaceTest/brillo_WifiInterfaceTest.py
+++ b/server/site_tests/brillo_WifiInterfaceTest/brillo_WifiInterfaceTest.py
@@ -8,6 +8,7 @@
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib import site_utils
 from autotest_lib.client.common_lib.cros.network import iw_runner
+from autotest_lib.server import afe_utils
 from autotest_lib.server import test
 from autotest_lib.server.brillo import host_utils
 
@@ -55,9 +56,8 @@
         @raise TestFail: The test failed.
         """
         self.host = host
-        if ssid is None:
+        if afe_utils.host_in_lab(host):
             ssid = site_utils.get_wireless_ssid(host.hostname)
-        logging.info('Connecting to ssid %s', ssid)
         with host_utils.connect_to_ssid(host, ssid, passphrase):
             err_iface = ('No interface is' if wifi_iface is None
                           else 'Interface %s is not' % wifi_iface)