autotest: Migrate network_WiFiRoaming.005* to new framework

TEST=This is a test that passes.
BUG=chromium:263892

Change-Id: Ia7700a39057f24a731d77bc7114b920def4fadb0
Reviewed-on: https://chromium-review.googlesource.com/176477
Tested-by: Christopher Wiley <wiley@chromium.org>
Reviewed-by: Paul Stewart <pstew@chromium.org>
Commit-Queue: Christopher Wiley <wiley@chromium.org>
diff --git a/server/site_linux_router.py b/server/site_linux_router.py
index 0e94f5c..a4d9b8a 100644
--- a/server/site_linux_router.py
+++ b/server/site_linux_router.py
@@ -690,13 +690,26 @@
         @param params dict of parameters from site_wifitest.
 
         """
+        self.deconfig_aps(instance=params.get('instance', None),
+                          silent='silent' in params)
+
+
+    def deconfig_aps(self, instance=None, silent=False):
+        """De-configure an AP (will also bring wlan down).
+
+        @param instance: int or None.  If instance is None, will bring down all
+                instances of hostapd.
+        @param silent: True if instances should be brought without de-authing
+                the DUT.
+
+        """
         if not self.hostapd['configured'] and not self.station['configured']:
             return
 
         if self.hostapd['configured']:
             local_servers = []
-            if 'instance' in params:
-                instances = [ self.hostapd_instances.pop(params['instance']) ]
+            if instance is not None:
+                instances = [ self.hostapd_instances.pop(instance) ]
                 for server in self.local_servers:
                     if server['interface'] == instances[0]['interface']:
                         local_servers = [server]
@@ -709,7 +722,7 @@
                 self.local_servers = []
 
             for instance in instances:
-                if 'silent' in params:
+                if silent:
                     # Deconfigure without notifying DUT.  Remove the interface
                     # hostapd uses to send beacon and DEAUTH packets.
                     self._remove_interface(instance['interface'], True)
diff --git a/server/site_tests/network_WiFiRoaming/005SuspendRoam b/server/site_tests/network_WiFiRoaming/005SuspendRoam
deleted file mode 100644
index 297ba7b..0000000
--- a/server/site_tests/network_WiFiRoaming/005SuspendRoam
+++ /dev/null
@@ -1,66 +0,0 @@
-# Copyright (c) 2010 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.
-
-# This test run is designed to test that the connection manager is able
-# to roam to a previously connected AP when it loses connectivity to its
-# current AP on resume.  To accomplish this, we join one AP (so we are
-# sure that flimflam remembers it), then join a second AP which we
-# shut down while the DUT is suspended.  We sit around and observe
-# if and how long it takes for the connection manager to re-acquire
-# connectivity to the first AP.  We expect the DUT will resume, find
-# the previous AP is gone, and then roam to the other AP.
-{ "name":"SuspendRoam",
-  "steps":[
-    [ "create",         { "type":"hostap" } ],
-
-    # NB: use a fixed SSID so we know what to reference below
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoam_t1'} ],
-
-    # Connect to the first AP.  This just guarantees that this AP has
-    # been placed in the connection manager profile.
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-    [ "deconfig" ],
-
-    # Configure and connect to the second AP.
-    [ "config",         { "channel":"2462", "mode":"11g",
-                          "ssid_suffix": "t2"} ],
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-
-    # Ask the DUT to sleep for 20 seconds while we switch things around,
-    # then see how long it takes for the DUT to return the service to the
-    # "ready" state.  As a result, the arguments to this command are a
-    # combination of suspend and wait_service arguments.  In this case,
-    # except for "suspend_time", all arguments are to wait_change.  Note
-    # that the wait_change occurs AFTER the suspend, 20 seconds later in
-    # this specific example.  Note also that we must specify the SSID of
-    # the previous AP to wait for; otherwise this will wait for the AP
-    # we just deconfig'd to come ready (and fail).
-    [ "wait_service_suspend_bg",
-                        { "suspend_time": "20",  # How long to sleep
-                          "run_timeout":20,      # Maximum time to wait
-                          "debug":True,          # Output all state changes
-                          "states": [             # Wait for "ready" state
-                            ('SuspendRoam_t1', 'ready')
-                          ] } ],
-
-    # Locally, let's wait 10 seconds to make sure the DUT is really asleep
-    # before we proceed.
-    [ "sleep",          { "time":"15" } ],
-
-    # Shut down the second AP and bring the first one back up
-    [ "deconfig" ],
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoam_t1'} ],
-
-    # Wait for the DUT to wake up and return the results of wait_suspend
-    [ "wait_service_suspend_end",   { } ],
-
-    [ "client_ping",    { "count":"10" } ],
-
-    [ "destroy" ],
-  ],
-}
diff --git a/server/site_tests/network_WiFiRoaming/005SuspendRoamRSN b/server/site_tests/network_WiFiRoaming/005SuspendRoamRSN
deleted file mode 100644
index 46c6b12..0000000
--- a/server/site_tests/network_WiFiRoaming/005SuspendRoamRSN
+++ /dev/null
@@ -1,73 +0,0 @@
-# Copyright (c) 2010 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.
-
-# This test run is designed to test that the connection manager is able
-# to roam to a previously connected AP when it loses connectivity to its
-# current AP on resume.  To accomplish this, we join one AP (so we are
-# sure that flimflam remembers it), then join a second AP which we
-# shut down while the DUT is suspended.  We sit around and observe
-# if and how long it takes for the connection manager to re-acquire
-# connectivity to the first AP.  We expect the DUT will resume, find
-# the previous AP is gone, and then roam to the other AP.
-{ "name":"SuspendRoamRSN",
-  "steps":[
-    [ "create",         { "type":"hostap" } ],
-
-    # NB: use a fixed SSID so we know what to reference below
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoamRSN_t1',
-                          "wpa":"2", "wpa_key_mgmt":"WPA-PSK",
-                          "wpa_pairwise":"CCMP",
-                          "wpa_passphrase":"chromeos"} ],
-
-    # Connect to the first AP.  This just guarantees that this AP has
-    # been placed in the connection manager profile.
-    [ "connect",        { "security":"rsn", "psk":"chromeos" } ],
-    [ "client_ping",    { "count":"10" } ],
-    [ "deconfig" ],
-
-    # Configure and connect to the second AP.
-    [ "config",         { "channel":"5240", "mode":"11a",
-                          "ssid_suffix": "t2",
-                          "wpa":"0"} ],
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-
-    # Ask the DUT to sleep for 20 seconds while we switch things around,
-    # then see how long it takes for the DUT to return the service to the
-    # "ready" state.  As a result, the arguments to this command are a
-    # combination of suspend and wait_service arguments.  In this case,
-    # except for "suspend_time", all arguments are to wait_change.  Note
-    # that the wait_change occurs AFTER the suspend, 20 seconds later in
-    # this specific example.  Note also that we must specify the SSID of
-    # the previous AP to wait for; otherwise this will wait for the AP
-    # we just deconfig'd to come ready (and fail).
-    [ "wait_service_suspend_bg",
-                        { "suspend_time": "20",  # How long to sleep
-                          "run_timeout":20,      # Maximum time to wait
-                          "debug":True,          # Output all state changes
-                          "states": [            # Wait for "ready" state
-                            ('SuspendRoamRSN_t1', 'ready')
-                          ] } ],
-
-    # Locally, let's wait 10 seconds to make sure the DUT is really asleep
-    # before we proceed.
-    [ "sleep",          { "time":"15" } ],
-
-    # Shut down the second AP and bring the first one back up
-    [ "deconfig" ],
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoamRSN_t1',
-                          "wpa":"2", "wpa_key_mgmt":"WPA-PSK",
-                          "wpa_pairwise":"CCMP",
-                          "wpa_passphrase":"chromeos"} ],
-
-    # Wait for the DUT to wake up and return the results of wait_suspend
-    [ "wait_service_suspend_end",   { } ],
-
-    [ "client_ping",    { "count":"10" } ],
-
-    [ "destroy" ],
-  ],
-}
diff --git a/server/site_tests/network_WiFiRoaming/005SuspendRoamWPA b/server/site_tests/network_WiFiRoaming/005SuspendRoamWPA
deleted file mode 100644
index 92bec23..0000000
--- a/server/site_tests/network_WiFiRoaming/005SuspendRoamWPA
+++ /dev/null
@@ -1,73 +0,0 @@
-# Copyright (c) 2010 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.
-
-# This test run is designed to test that the connection manager is able
-# to roam to a previously connected AP when it loses connectivity to its
-# current AP on resume.  To accomplish this, we join one AP (so we are
-# sure that flimflam remembers it), then join a second AP which we
-# shut down while the DUT is suspended.  We sit around and observe
-# if and how long it takes for the connection manager to re-acquire
-# connectivity to the first AP.  We expect the DUT will resume, find
-# the previous AP is gone, and then roam to the other AP.
-{ "name":"SuspendRoamWPA",
-  "steps":[
-    [ "create",         { "type":"hostap" } ],
-
-    # NB: use a fixed SSID so we know what to reference below
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoamWPA_t1',
-                          "wpa":"1", "wpa_key_mgmt":"WPA-PSK",
-                          "wpa_pairwise":"TKIP",
-                          "wpa_passphrase":"chromeos"} ],
-
-    # Connect to the first AP.  This just guarantees that this AP has
-    # been placed in the connection manager profile.
-    [ "connect",        { "security":"wpa", "psk":"chromeos" } ],
-    [ "client_ping",    { "count":"10" } ],
-    [ "deconfig" ],
-
-    # Configure and connect to the second AP.
-    [ "config",         { "channel":"5240", "mode":"11a",
-                          "ssid_suffix": "t2",
-                          "wpa":"0"} ],
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-
-    # Ask the DUT to sleep for 20 seconds while we switch things around,
-    # then see how long it takes for the DUT to return the service to the
-    # "ready" state.  As a result, the arguments to this command are a
-    # combination of suspend and wait_service arguments.  In this case,
-    # except for "suspend_time", all arguments are to wait_change.  Note
-    # that the wait_change occurs AFTER the suspend, 20 seconds later in
-    # this specific example.  Note also that we must specify the SSID of
-    # the previous AP to wait for; otherwise this will wait for the AP
-    # we just deconfig'd to come ready (and fail).
-    [ "wait_service_suspend_bg",
-                        { "suspend_time": "20",  # How long to sleep
-                          "run_timeout":20,      # Maximum time to wait
-                          "debug":True,          # Output all state changes
-                          "states": [            # Wait for "ready" state
-                            ('SuspendRoamWPA_t1', 'ready')
-                          ] } ],
-
-    # Locally, let's wait 10 seconds to make sure the DUT is really asleep
-    # before we proceed.
-    [ "sleep",          { "time":"15" } ],
-
-    # Shut down the second AP and bring the first one back up
-    [ "deconfig" ],
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoamWPA_t1',
-                          "wpa":"1", "wpa_key_mgmt":"WPA-PSK",
-                          "wpa_pairwise":"TKIP",
-                          "wpa_passphrase":"chromeos"} ],
-
-    # Wait for the DUT to wake up and return the results of wait_suspend
-    [ "wait_service_suspend_end",   { } ],
-
-    [ "client_ping",    { "count":"10" } ],
-
-    [ "destroy" ],
-  ],
-}
diff --git a/server/site_tests/network_WiFiRoaming/005SuspendRoamXBand b/server/site_tests/network_WiFiRoaming/005SuspendRoamXBand
deleted file mode 100644
index a763df5..0000000
--- a/server/site_tests/network_WiFiRoaming/005SuspendRoamXBand
+++ /dev/null
@@ -1,67 +0,0 @@
-# Copyright (c) 2010 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.
-
-# This test run is designed to test that the connection manager is able
-# to roam to a previously connected AP when it loses connectivity to its
-# current AP on resume.  To accomplish this, we join one AP (so we are
-# sure that flimflam remembers it), then join a second AP which we
-# shut down while the DUT is suspended.  We sit around and observe
-# if and how long it takes for the connection manager to re-acquire
-# connectivity to the first AP.  We expect the DUT will resume, find
-# the previous AP is gone, and then roam to the other AP.
-{ "name":"SuspendRoamXBand",
-  "steps":[
-    [ "create",         { "type":"hostap" } ],
-
-    # NB: use a fixed SSID so we know what to reference below
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoam_t1'} ],
-
-    # Connect to the first AP.  This just guarantees that this AP has
-    # been placed in the connection manager profile.
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-    [ "deconfig" ],
-
-    # Configure and connect to the second AP.
-    [ "config",         { "channel":"5240", "mode":"11a",
-                          "ssid_suffix": "t2",
-                          "wpa":"0"} ],
-    [ "connect",        { "security":"none" } ],
-    [ "client_ping",    { "count":"10" } ],
-
-    # Ask the DUT to sleep for 20 seconds while we switch things around,
-    # then see how long it takes for the DUT to return the service to the
-    # "ready" state.  As a result, the arguments to this command are a
-    # combination of suspend and wait_service arguments.  In this case,
-    # except for "suspend_time", all arguments are to wait_change.  Note
-    # that the wait_change occurs AFTER the suspend, 20 seconds later in
-    # this specific example.  Note also that we must specify the SSID of
-    # the previous AP to wait for; otherwise this will wait for the AP
-    # we just deconfig'd to come ready (and fail).
-    [ "wait_service_suspend_bg",
-                        { "suspend_time": "20",  # How long to sleep
-                          "run_timeout":20,      # Maximum time to wait
-                          "debug":True,          # Output all state changes
-                          "states": [            # Wait for "ready" state
-                            ('SuspendRoam_t1', 'ready')
-                          ] } ],
-
-    # Locally, let's wait 10 seconds to make sure the DUT is really asleep
-    # before we proceed.
-    [ "sleep",          { "time":"15" } ],
-
-    # Shut down the second AP and bring the first one back up
-    [ "deconfig" ],
-    [ "config",         { "channel":"2412", "mode":"11g",
-                          "ssid": 'SuspendRoam_t1'} ],
-
-    # Wait for the DUT to wake up and return the results of wait_suspend
-    [ "wait_service_suspend_end",   { } ],
-
-    [ "client_ping",    { "count":"10" } ],
-
-    [ "destroy" ],
-  ],
-}
diff --git a/server/site_tests/network_WiFi_RoamSuspendSSID/control.none b/server/site_tests/network_WiFi_RoamSuspendSSID/control.none
new file mode 100644
index 0000000..a15d2a6
--- /dev/null
+++ b/server/site_tests/network_WiFi_RoamSuspendSSID/control.none
@@ -0,0 +1,31 @@
+# Copyright (c) 2013 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.
+
+AUTHOR = 'wiley@chromium.org'
+TIME = 'SHORT'
+NAME = 'network_WiFi_RoamSuspendSSID.none'
+TEST_CATEGORY = 'Functional'
+TEST_CLASS = 'network'
+TEST_TYPE = 'Server'
+DOC = """
+This test verifies that the connection manager connects to a
+previously connected network on resume from suspend when an
+SSID disappears during suspend.
+
+"""
+
+from autotest_lib.server.cros.network import hostap_config
+
+
+def run(machine):
+    ap_config0 = hostap_config.HostapConfig(channel=1)
+    ap_config1 = hostap_config.HostapConfig(channel=11)
+    job.run_test('network_WiFi_RoamSuspendSSID',
+                 tag=NAME.split('.')[1],
+                 host=hosts.create_host(machine),
+                 raw_cmdline_args=args,
+                 additional_params=(ap_config0, ap_config1))
+
+
+parallel_simple(run, machines)
diff --git a/server/site_tests/network_WiFi_RoamSuspendSSID/control.rsn b/server/site_tests/network_WiFi_RoamSuspendSSID/control.rsn
new file mode 100644
index 0000000..e59e569
--- /dev/null
+++ b/server/site_tests/network_WiFi_RoamSuspendSSID/control.rsn
@@ -0,0 +1,39 @@
+# Copyright (c) 2013 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.
+
+AUTHOR = 'wiley@chromium.org'
+TIME = 'SHORT'
+NAME = 'network_WiFi_RoamSuspendSSID.rsn'
+TEST_CATEGORY = 'Functional'
+TEST_CLASS = 'network'
+TEST_TYPE = 'Server'
+DOC = """
+This test verifies that the connection manager connects to a
+previously connected network on resume from suspend when an
+SSID disappears during suspend.
+
+"""
+
+from autotest_lib.server.cros.network import hostap_config
+
+
+def run(machine):
+    wpa_config0 = xmlrpc_security_types.WPAConfig(
+            psk='chromeos0',
+            wpa_mode=xmlrpc_security_types.WPAConfig.MODE_PURE_WPA2)
+    wpa_config1 = xmlrpc_security_types.WPAConfig(
+            psk='chromeos1',
+            wpa_mode=xmlrpc_security_types.WPAConfig.MODE_PURE_WPA2)
+    ap_config0 = hostap_config.HostapConfig(channel=1,
+                                            security_config=wpa_config0)
+    ap_config1 = hostap_config.HostapConfig(channel=44,
+                                            security_config=wpa_config1)
+    job.run_test('network_WiFi_RoamSuspendSSID',
+                 tag=NAME.split('.')[1],
+                 host=hosts.create_host(machine),
+                 raw_cmdline_args=args,
+                 additional_params=(ap_config0, ap_config1))
+
+
+parallel_simple(run, machines)
diff --git a/server/site_tests/network_WiFi_RoamSuspendSSID/control.wpa b/server/site_tests/network_WiFi_RoamSuspendSSID/control.wpa
new file mode 100644
index 0000000..d2ee1b8
--- /dev/null
+++ b/server/site_tests/network_WiFi_RoamSuspendSSID/control.wpa
@@ -0,0 +1,39 @@
+# Copyright (c) 2013 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.
+
+AUTHOR = 'wiley@chromium.org'
+TIME = 'SHORT'
+NAME = 'network_WiFi_RoamSuspendSSID.wpa'
+TEST_CATEGORY = 'Functional'
+TEST_CLASS = 'network'
+TEST_TYPE = 'Server'
+DOC = """
+This test verifies that the connection manager connects to a
+previously connected network on resume from suspend when an
+SSID disappears during suspend.
+
+"""
+
+from autotest_lib.server.cros.network import hostap_config
+
+
+def run(machine):
+    wpa_config0 = xmlrpc_security_types.WPAConfig(
+            psk='chromeos0',
+            wpa_mode=xmlrpc_security_types.WPAConfig.MODE_PURE_WPA)
+    wpa_config1 = xmlrpc_security_types.WPAConfig(
+            psk='chromeos1',
+            wpa_mode=xmlrpc_security_types.WPAConfig.MODE_PURE_WPA)
+    ap_config0 = hostap_config.HostapConfig(channel=44,
+                                            security_config=wpa_config0)
+    ap_config1 = hostap_config.HostapConfig(channel=11,
+                                            security_config=wpa_config1)
+    job.run_test('network_WiFi_RoamSuspendSSID',
+                 tag=NAME.split('.')[1],
+                 host=hosts.create_host(machine),
+                 raw_cmdline_args=args,
+                 additional_params=(ap_config0, ap_config1))
+
+
+parallel_simple(run, machines)
diff --git a/server/site_tests/network_WiFi_RoamSuspendSSID/network_WiFi_RoamSuspendSSID.py b/server/site_tests/network_WiFi_RoamSuspendSSID/network_WiFi_RoamSuspendSSID.py
new file mode 100644
index 0000000..6200466
--- /dev/null
+++ b/server/site_tests/network_WiFi_RoamSuspendSSID/network_WiFi_RoamSuspendSSID.py
@@ -0,0 +1,64 @@
+# Copyright (c) 2013 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 logging
+import time
+
+from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
+from autotest_lib.server.cros.network import wifi_cell_test_base
+
+class network_WiFi_RoamSuspendSSID(wifi_cell_test_base.WiFiCellTestBase):
+    """Tests roaming to a new SSID when a previous SSID disappears in suspend.
+
+    This test:
+    1) Connects the DUT to a network A
+    2) Connects the DUT to a network B while keeping network A around.
+    3) Suspend the DUT (while connected to network B).
+    4) Deconfigure (take down) network B.
+    5) Assert that the DUT automatically connects to network A on resume.
+
+    """
+
+    version = 1
+
+    SUSPEND_TIME_SECONDS = 15
+
+
+    def parse_additional_arguments(self, commandline_args, additional_params):
+        """Hook into super class to take control files parameters.
+
+        @param commandline_args: dict of parsed parameters from the autotest.
+        @param additional_params: tuple(HostapConfig, HostapConfig) used as
+                networks A and B from the test description.
+
+        """
+        self._ap_config0 = additional_params[0]
+        self._ap_config1 = additional_params[1]
+
+
+    def run_once(self):
+        """Test body."""
+        get_client_config = lambda ssid, ap_config: \
+                xmlrpc_datatypes.AssociationParameters(
+                        ssid=ssid,
+                        security_config=ap_config.security_config)
+        self.context.configure(self._ap_config0)
+        self.context.configure(self._ap_config1, multi_interface=True)
+        self.context.assert_connect_wifi(
+                get_client_config(self.context.router.get_ssid(instance=0),
+                                  self._ap_config0))
+        self.context.assert_connect_wifi(
+                get_client_config(self.context.router.get_ssid(instance=1),
+                                  self._ap_config1))
+        self.context.client.do_suspend_bg(self.SUSPEND_TIME_SECONDS + 5)
+        logging.info('Waiting %d seconds for DUT to be fully suspended.',
+                     self.SUSPEND_TIME_SECONDS)
+        time.sleep(self.SUSPEND_TIME_SECONDS)
+        logging.info('Tearing down the most recently connected AP.')
+        self.context.router.deconfig_aps(instance=1)
+        logging.info('Expect that we connect to our previous connected AP '
+                     'on resume.')
+        self.context.wait_for_connection(
+                self.context.router.get_ssid(instance=0),
+                self._ap_config0.frequency)