1a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang# Copyright 2016 The Chromium OS Authors. All rights reserved.
2506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish# Use of this source code is governed by a BSD-style license that can be
3506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish# found in the LICENSE file.
4506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
5ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambishimport contextlib
6506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambishimport datetime
7385213165849f3867702f3467ec803957ebe3506Kris Rambishimport logging
826d3d4d699070320ad45c28749518373422f759dbmahadevimport pprint
90ccfc15cd256dde8dfe55428a87b671fb1f49c78Kris Rambishimport time
10506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
11c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Piusimport common
12a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Changfrom autotest_lib.client.common_lib import error, site_utils
1385418ab3321d272e07ec798455d41fe0f354c75fTien Changfrom autotest_lib.client.common_lib import utils as base_utils
1422093e32603a2c850e8a15121f4718f15e435415Roshan Piusfrom autotest_lib.client.common_lib.cros.network import ap_constants
1506977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadevfrom autotest_lib.client.common_lib.cros.network import iw_runner
16506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambishfrom autotest_lib.server import hosts
17506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambishfrom autotest_lib.server import site_linux_system
18506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambishfrom autotest_lib.server.cros import host_lock_manager
1922093e32603a2c850e8a15121f4718f15e435415Roshan Piusfrom autotest_lib.server.cros.ap_configurators import ap_batch_locker
209c577533250ce17448894b625f987d54259dc159Bindu Mahadevfrom autotest_lib.server.cros.ap_configurators \
21f71b696e2fd062dd7ab40e58a6d28bd2e5cc6374Bindu Mahadev        import ap_configurator_factory
22c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Piusfrom autotest_lib.server.cros.network import chaos_clique_utils as utils
23ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambishfrom autotest_lib.server.cros.network import wifi_client
24d59aeeb9b6ab4cc42540d6df69308a48a0e3fc32Bindu Mahadevfrom autotest_lib.server.hosts import adb_host
25ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
26d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang# Webdriver master hostname
27d2ae97483945e7f78e57e41cafcd800553e08598Tien ChangMASTERNAME = 'chromeos3-chaosvmmaster.cros.corp.google.com'
28a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien ChangWEBDRIVER_PORT = 9515
29d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang
30506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
31506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambishclass ChaosRunner(object):
32506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish    """Object to run a network_WiFi_ChaosXXX test."""
33506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
34506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
35093d651517da7a4c760aa3fe07d8466ebb15d6aebmahadev    def __init__(self, test, host, spec, broken_pdus=list()):
36506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        """Initializes and runs test.
37506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
38506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        @param test: a string, test name.
39506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        @param host: an Autotest host object, device under test.
4026d3d4d699070320ad45c28749518373422f759dbmahadev        @param spec: an APSpec object.
4126d3d4d699070320ad45c28749518373422f759dbmahadev        @param broken_pdus: list of offline PDUs.
42506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
43506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        """
44506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        self._test = test
45506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        self._host = host
46299dad5b2a0cd35e27719d730231ccafb0226385Kris Rambish        self._ap_spec = spec
4726d3d4d699070320ad45c28749518373422f759dbmahadev        self._broken_pdus = broken_pdus
48506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        # Log server and DUT times
49506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        dt = datetime.datetime.now()
50506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        logging.info('Server time: %s', dt.strftime('%a %b %d %H:%M:%S %Y'))
51506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        logging.info('DUT time: %s', self._host.run('date').stdout.strip())
52506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
53506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
54f71b696e2fd062dd7ab40e58a6d28bd2e5cc6374Bindu Mahadev    def run(self, job, batch_size=10, tries=10, capturer_hostname=None,
5569ceb35ae6df406df4bf1c6d44a4f4d3b87f8115Jason Abele            conn_worker=None, work_client_hostname=None,
5669ceb35ae6df406df4bf1c6d44a4f4d3b87f8115Jason Abele            disabled_sysinfo=False):
57506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        """Executes Chaos test.
58506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
59506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        @param job: an Autotest job object.
60506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        @param batch_size: an integer, max number of APs to lock in one batch.
61506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        @param tries: an integer, number of iterations to run per AP.
62618f21f85484caf173e42e9b7f0a3bd892482b2eJason Abele        @param capturer_hostname: a string or None, hostname or IP of capturer.
637bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan        @param conn_worker: ConnectionWorkerAbstract or None, to run extra
647bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                            work after successful connection.
657bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan        @param work_client_hostname: a string or None, hostname of work client
6669ceb35ae6df406df4bf1c6d44a4f4d3b87f8115Jason Abele        @param disabled_sysinfo: a bool, disable collection of logs from DUT.
67506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
68d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang
69d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang        @raises TestError: Issues locking VM webdriver instance
70506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        """
71506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
72506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        lock_manager = host_lock_manager.HostLockManager()
73d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang        webdriver_master = hosts.SSHHost(MASTERNAME, user='chaosvmmaster')
74d78ef9c1c093b461a9e8276ba723c1b00eb31fb3Tien Chang        host_prefix = self._host.hostname.split('-')[0]
75506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish        with host_lock_manager.HostsLockedBy(lock_manager):
76c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius            capture_host = utils.allocate_packet_capturer(
77d78ef9c1c093b461a9e8276ba723c1b00eb31fb3Tien Chang                    lock_manager, hostname=capturer_hostname,
78d78ef9c1c093b461a9e8276ba723c1b00eb31fb3Tien Chang                    prefix=host_prefix)
7906977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            # Cleanup and reboot packet capturer before the test.
8006977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            utils.sanitize_client(capture_host)
8105b03e057e1acff341ffcbd4d358e38331dfa9f8Bindu Mahadev            capturer = site_linux_system.LinuxSystem(capture_host, {},
8205b03e057e1acff341ffcbd4d358e38331dfa9f8Bindu Mahadev                                                     'packet_capturer')
8306977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev
8406977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            # Run iw scan and abort if more than allowed number of APs are up.
8506977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            iw_command = iw_runner.IwRunner(capture_host)
8606977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            start_time = time.time()
8706977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            logging.info('Performing a scan with a max timeout of 30 seconds.')
8804d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev            capture_interface = 'wlan0'
8904d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev            capturer_info = capture_host.run('cat /etc/lsb-release',
9004d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev                                             ignore_status=True, timeout=5).stdout
9104d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev            if 'whirlwind' in capturer_info:
9204d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev                # Use the dual band aux radio for scanning networks.
9304d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev                capture_interface = 'wlan2'
9406977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev            while time.time() - start_time <= ap_constants.MAX_SCAN_TIMEOUT:
9504d830a19a8e78435c3e5912291df250812408a3Bindu Mahadev                networks = iw_command.scan(capture_interface)
9606977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                if networks is None:
9706977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                    if (time.time() - start_time ==
9806977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                            ap_constants.MAX_SCAN_TIMEOUT):
9906977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                        raise error.TestError(
10006977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                            'Packet capturer is not responding to scans. Check'
10106977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                            'device and re-run test')
10206977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                    continue
10306977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                elif len(networks) < ap_constants.MAX_SSID_COUNT:
10406977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                    break
10506977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                elif len(networks) >= ap_constants.MAX_SSID_COUNT:
10606977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                    raise error.TestError(
107a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                        'Probably someone is already running a '
10806977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev                        'chaos test?!')
10906977bf3187e127ac369700ff4cb9fd23f61dbf1Bindu Mahadev
1107bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan            if conn_worker is not None:
111c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                work_client_machine = utils.allocate_packet_capturer(
1127bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                        lock_manager, hostname=work_client_hostname)
1137bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                conn_worker.prepare_work_client(work_client_machine)
114d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang
11585418ab3321d272e07ec798455d41fe0f354c75fTien Chang            # Lock VM. If on, power off; always power on. Then create a tunnel.
116d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang            webdriver_instance = utils.allocate_webdriver_instance(lock_manager)
11785418ab3321d272e07ec798455d41fe0f354c75fTien Chang
11885418ab3321d272e07ec798455d41fe0f354c75fTien Chang            if utils.is_VM_running(webdriver_master, webdriver_instance):
11985418ab3321d272e07ec798455d41fe0f354c75fTien Chang                logging.info('VM %s was on; powering off for a clean instance',
12085418ab3321d272e07ec798455d41fe0f354c75fTien Chang                             webdriver_instance)
12185418ab3321d272e07ec798455d41fe0f354c75fTien Chang                utils.power_off_VM(webdriver_master, webdriver_instance)
12285418ab3321d272e07ec798455d41fe0f354c75fTien Chang                logging.info('Allow VM time to gracefully shut down')
12385418ab3321d272e07ec798455d41fe0f354c75fTien Chang                time.sleep(5)
12485418ab3321d272e07ec798455d41fe0f354c75fTien Chang
12585418ab3321d272e07ec798455d41fe0f354c75fTien Chang            logging.info('Starting up VM %s', webdriver_instance)
12685418ab3321d272e07ec798455d41fe0f354c75fTien Chang            utils.power_on_VM(webdriver_master, webdriver_instance)
12785418ab3321d272e07ec798455d41fe0f354c75fTien Chang            logging.info('Allow VM time to power on before creating a tunnel.')
12885418ab3321d272e07ec798455d41fe0f354c75fTien Chang            time.sleep(5)
12985418ab3321d272e07ec798455d41fe0f354c75fTien Chang
130a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang            if not site_utils.host_is_in_lab_zone(webdriver_instance.hostname):
1318407d2439de4dfdcc5bda41417a1dd7da0fe63d3Godofredo Contreras                self._ap_spec._webdriver_hostname = webdriver_instance.hostname
132a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang            else:
133a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                # If in the lab then port forwarding must be done so webdriver
134a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                # connection will be over localhost.
135a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                self._ap_spec._webdriver_hostname = 'localhost'
136a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                webdriver_tunnel = webdriver_instance.create_ssh_tunnel(
137a2581fb871334efdebaf4b2c64fe682c92bd5c27Tien Chang                                                WEBDRIVER_PORT, WEBDRIVER_PORT)
13885418ab3321d272e07ec798455d41fe0f354c75fTien Chang                logging.info('Wait for tunnel to be created.')
13985418ab3321d272e07ec798455d41fe0f354c75fTien Chang                for i in range(3):
14085418ab3321d272e07ec798455d41fe0f354c75fTien Chang                    time.sleep(10)
14185418ab3321d272e07ec798455d41fe0f354c75fTien Chang                    results = base_utils.run('lsof -i:%s' % WEBDRIVER_PORT,
14285418ab3321d272e07ec798455d41fe0f354c75fTien Chang                                             ignore_status=True)
14385418ab3321d272e07ec798455d41fe0f354c75fTien Chang                    if results:
14485418ab3321d272e07ec798455d41fe0f354c75fTien Chang                        break
14585418ab3321d272e07ec798455d41fe0f354c75fTien Chang                if not results:
14685418ab3321d272e07ec798455d41fe0f354c75fTien Chang                    raise error.TestError(
14785418ab3321d272e07ec798455d41fe0f354c75fTien Chang                            'Unable to listen to WEBDRIVER_PORT: %s', results)
148d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang
14922093e32603a2c850e8a15121f4718f15e435415Roshan Pius            batch_locker = ap_batch_locker.ApBatchLocker(
15022093e32603a2c850e8a15121f4718f15e435415Roshan Pius                    lock_manager, self._ap_spec,
15122093e32603a2c850e8a15121f4718f15e435415Roshan Pius                    ap_test_type=ap_constants.AP_TEST_TYPE_CHAOS)
1527bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan
153506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish            while batch_locker.has_more_aps():
154d59aeeb9b6ab4cc42540d6df69308a48a0e3fc32Bindu Mahadev                # Work around for CrOS devices only:crbug.com/358716
155d59aeeb9b6ab4cc42540d6df69308a48a0e3fc32Bindu Mahadev                # Do not reboot Android devices:b/27977927
156d59aeeb9b6ab4cc42540d6df69308a48a0e3fc32Bindu Mahadev                if self._host.get_os_type() != adb_host.OS_TYPE_ANDROID:
157d59aeeb9b6ab4cc42540d6df69308a48a0e3fc32Bindu Mahadev                    utils.sanitize_client(self._host)
1581afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                healthy_dut = True
1599fcf81129c44c7d43edf7a1d6f269803d22233e0Tien Chang
160ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                with contextlib.closing(wifi_client.WiFiClient(
161cc715ecee5c554db4acb3023075c90ce31a20cbaRoshan Pius                    hosts.create_host({'hostname' : self._host.hostname,
16205ae2a45397188dd7d7adbc49a61095ebf85e151Kevin Cheng                            'afe_host' : self._host._afe_host},
16305ae2a45397188dd7d7adbc49a61095ebf85e151Kevin Cheng                            host_class=self._host.__class__),
164cc715ecee5c554db4acb3023075c90ce31a20cbaRoshan Pius                    './debug', False)) as client:
165ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
166ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    aps = batch_locker.get_ap_batch(batch_size=batch_size)
167ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    if not aps:
168ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        logging.info('No more APs to test.')
169ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        break
170ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
171ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    # Power down all of the APs because some can get grumpy
172ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    # if they are configured several times and remain on.
173ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    # User the cartridge to down group power downs and
174ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    # configurations.
175c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                    utils.power_down_aps(aps, self._broken_pdus)
176c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                    utils.configure_aps(aps, self._ap_spec, self._broken_pdus)
177ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
178c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                    aps = utils.filter_quarantined_and_config_failed_aps(aps,
179c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                            batch_locker, job, self._broken_pdus)
1801afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish
181ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                    for ap in aps:
182ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        # http://crbug.com/306687
183ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        if ap.ssid == None:
184ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                            logging.error('The SSID was not set for the AP:%s',
185ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                          ap)
186ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
187c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                        healthy_dut = utils.is_dut_healthy(client, ap)
1881afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish
1891afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                        if not healthy_dut:
19089c93289686b415118e0fa54064ceddefcc58128Kris Rambish                            logging.error('DUT is not healthy, rebooting.')
1911afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            batch_locker.unlock_and_reclaim_aps()
1921afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            break
193ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
194c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                        networks = utils.return_available_networks(
195c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                                ap, capturer, job, self._ap_spec)
196299dad5b2a0cd35e27719d730231ccafb0226385Kris Rambish
197491aed5b5f9b9bb167944e2360e5f046bc1ac18bbmahadev                        if networks is None:
198d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                            # If scan returned no networks, iw scan failed.
1991afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            # Reboot the packet capturer device and
2001afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            # reconfigure the capturer.
2011afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            batch_locker.unlock_and_reclaim_ap(ap.host_name)
20289c93289686b415118e0fa54064ceddefcc58128Kris Rambish                            logging.error('Packet capture is not healthy, '
20389c93289686b415118e0fa54064ceddefcc58128Kris Rambish                                          'rebooting.')
204d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                            capturer.host.reboot()
205d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                            capturer = site_linux_system.LinuxSystem(
206d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                                           capture_host, {},'packet_capturer')
207ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                            continue
208d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                        if networks == list():
209491aed5b5f9b9bb167944e2360e5f046bc1ac18bbmahadev                           # Packet capturer did not find the SSID in scan or
210491aed5b5f9b9bb167944e2360e5f046bc1ac18bbmahadev                           # there was a security mismatch.
211c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                           utils.release_ap(ap, batch_locker, self._broken_pdus)
212d24b69d964c851a0f36562f844a2acbd3bab124abmahadev                           continue
213ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
214ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        assoc_params = ap.get_association_parameters()
2151afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish
216c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                        if not utils.is_conn_worker_healthy(
217c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                                conn_worker, ap, assoc_params, job):
218c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                            utils.release_ap(
219c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                                    ap, batch_locker, self._broken_pdus)
2201afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                            continue
2217bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan
222cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                        name = ap.name
223cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                        kernel_ver = self._host.get_kernel_ver()
224c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                        firmware_ver = utils.get_firmware_ver(self._host)
225cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                        if not firmware_ver:
226cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                            firmware_ver = "Unknown"
227cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang
228a5c9abf9c20e1e6065b0a3860b5d1297bfac11bfKris Rambish                        debug_dict = {'+++PARSE DATA+++': '+++PARSE DATA+++',
229a5c9abf9c20e1e6065b0a3860b5d1297bfac11bfKris Rambish                                      'SSID': ap._ssid,
230a5c9abf9c20e1e6065b0a3860b5d1297bfac11bfKris Rambish                                      'DUT': client.wifi_mac,
231a5c9abf9c20e1e6065b0a3860b5d1297bfac11bfKris Rambish                                      'AP Info': ap.name,
232cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                                      'kernel_version': kernel_ver,
233cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                                      'wifi_firmware_version': firmware_ver}
234cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                        debug_string = pprint.pformat(debug_dict)
235cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang
2364a637b1d020d2eeb294e29102b1cefbed17f1234Kris Rambish                        logging.info('Waiting %d seconds for the AP dhcp '
2374a637b1d020d2eeb294e29102b1cefbed17f1234Kris Rambish                                     'server', ap.dhcp_delay)
2384a637b1d020d2eeb294e29102b1cefbed17f1234Kris Rambish                        time.sleep(ap.dhcp_delay)
2394a637b1d020d2eeb294e29102b1cefbed17f1234Kris Rambish
240ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                        result = job.run_test(self._test,
241ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     capturer=capturer,
242ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     capturer_frequency=networks[0].frequency,
243ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     capturer_ht_type=networks[0].ht,
244ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     host=self._host,
245ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     assoc_params=assoc_params,
246ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     client=client,
247ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     tries=tries,
248cf3da1540523d918aadec06b3a0e4bb74e85185cTien Chang                                     debug_info=debug_string,
249ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish                                     # Copy all logs from the system
25069ceb35ae6df406df4bf1c6d44a4f4d3b87f8115Jason Abele                                     disabled_sysinfo=disabled_sysinfo,
2517bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                                     conn_worker=conn_worker,
2527bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                                     tag=ap.ssid if conn_worker is None else
2537bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                                         '%s.%s' % (conn_worker.name, ap.ssid))
254ebaf2ad2dbd521dde28ac7d8100f3dc375f150dcKris Rambish
255c6db3d062f9166eeccd816427dfc2f3e8b2e0052Roshan Pius                        utils.release_ap(ap, batch_locker, self._broken_pdus)
2561afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish
2577bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                        if conn_worker is not None:
2587bf5d460a10c0ac9c1c22e3e23b898d9792a1af4Ashok Nagarajan                            conn_worker.cleanup()
259506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish
2601afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                    if not healthy_dut:
2611afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                        continue
2621afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish
263506a99d582456eff2f6d5e534cbf00a62990e97dKris Rambish                batch_locker.unlock_aps()
264f71b696e2fd062dd7ab40e58a6d28bd2e5cc6374Bindu Mahadev
2658407d2439de4dfdcc5bda41417a1dd7da0fe63d3Godofredo Contreras            if webdriver_tunnel:
2668407d2439de4dfdcc5bda41417a1dd7da0fe63d3Godofredo Contreras                webdriver_instance.disconnect_ssh_tunnel(webdriver_tunnel,
2678407d2439de4dfdcc5bda41417a1dd7da0fe63d3Godofredo Contreras                                                         WEBDRIVER_PORT)
2688407d2439de4dfdcc5bda41417a1dd7da0fe63d3Godofredo Contreras                webdriver_instance.close()
269b125b27b573ce72946caf7174cac8747099e67e6Kris Rambish            capturer.close()
270d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang            logging.info('Powering off VM %s', webdriver_instance)
271d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang            utils.power_off_VM(webdriver_master, webdriver_instance)
272be547f84c2e337c4dec5393facbb20026cc4c68bTien Chang            lock_manager.unlock(webdriver_instance.hostname)
273d2ae97483945e7f78e57e41cafcd800553e08598Tien Chang
27426d3d4d699070320ad45c28749518373422f759dbmahadev            if self._broken_pdus:
2751afc69706f835eee0cd324e2aaf3f59101f89a69Kris Rambish                logging.info('PDU is down!!!\nThe following PDUs are down:\n')
27626d3d4d699070320ad45c28749518373422f759dbmahadev                pprint.pprint(self._broken_pdus)
277f71b696e2fd062dd7ab40e58a6d28bd2e5cc6374Bindu Mahadev
2788f8c16d7a91ce25b0432178de61c0d3d49f4c22dBindu Mahadev            factory = ap_configurator_factory.APConfiguratorFactory(
2798f8c16d7a91ce25b0432178de61c0d3d49f4c22dBindu Mahadev                    ap_constants.AP_TEST_TYPE_CHAOS)
2808f8c16d7a91ce25b0432178de61c0d3d49f4c22dBindu Mahadev            factory.turn_off_all_routers(self._broken_pdus)
281