• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6
7from autotest_lib.client.bin import utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10from autotest_lib.client.common_lib.cros.network import xmlrpc_datatypes
11from autotest_lib.client.cros import constants
12from autotest_lib.server import test
13from autotest_lib.site_utils import lxc
14from autotest_lib.server.cros.network import wifi_test_context_manager
15
16class WiFiCellTestBase(test.test):
17    """An abstract base class for autotests in WiFi cells.
18
19    WiFiCell tests refer to participants in the test as client, router, and
20    server.  The client is just the DUT and the router is a nearby AP which we
21    configure in various ways to test the ability of the client to connect.
22    There is a third entity called a server which is distinct from the autotest
23    server.  In WiFiTests, the server is a host which the client can only talk
24    to over the WiFi network.
25
26    WiFiTests have a notion of the control network vs the WiFi network.  The
27    control network refers to the network between the machine running the
28    autotest server and the various machines involved in the test.  The WiFi
29    network is the subnet(s) formed by WiFi routers between the server and the
30    client.
31
32    """
33
34    def _install_pyshark(self):
35        """Installs pyshark and its dependencies for packet capture analysis.
36
37        Uses SSP to install the required pyshark python module and its
38        dependencies including the tshark binary.
39        """
40        logging.info('Installing Pyshark')
41        try:
42            lxc.install_packages(['tshark', 'python-dev', 'libxml2-dev',
43                                  'libxslt-dev', 'zlib1g-dev'],
44                                 ['pyshark'])
45        except error.ContainerError as e:
46            logging.info('Not installing pyshark: %s', e)
47        except error.CmdError as e:
48            raise error.TestError('Error installing pyshark: %s', e)
49
50
51    def initialize(self, host):
52        self._install_pyshark()
53        if utils.host_could_be_in_afe(host.hostname):
54            # There are some DUTs that have different types of wifi modules.
55            # In order to generate separate performance graphs, a variant
56            # name is needed.  Writing this key will generate results with
57            # the name of <board>-<variant>.
58            info = host.host_info_store.get()
59            variant_name = info.get_label_value('variant')
60            if variant_name:
61                self.write_test_keyval({constants.VARIANT_KEY: variant_name})
62
63    @property
64    def context(self):
65        """@return the WiFi context for this test."""
66        return self._wifi_context
67
68
69    def parse_additional_arguments(self, commandline_args, additional_params):
70        """Parse additional arguments for use in test.
71
72        Subclasses should override this method do any other commandline parsing
73        and setting grabbing that they need to do.  For test clarity, do not
74        parse additional settings in the body of run_once.
75
76        @param commandline_args dict of argument key, value pairs.
77        @param additional_params object defined by test control file.
78
79        """
80        pass
81
82
83    def warmup(self, host, raw_cmdline_args, additional_params=None):
84        """
85        Use the additional_params argument to pass in custom test data from
86        control file to reuse test logic.  This object will be passed down via
87        parse_additional_arguments.
88
89        @param host host object representing the client DUT.
90        @param raw_cmdline_args raw input from autotest.
91        @param additional_params object passed in from control file.
92
93        """
94        cmdline_args = utils.args_to_dict(raw_cmdline_args)
95        logging.info('Running wifi test with commandline arguments: %r',
96                     cmdline_args)
97        self._wifi_context = wifi_test_context_manager.WiFiTestContextManager(
98                self.__class__.__name__,
99                host,
100                cmdline_args,
101                self.debugdir)
102
103        self._wifi_context.setup()
104        self.parse_additional_arguments(cmdline_args, additional_params)
105
106        msg = '======= WiFi autotest setup complete. Starting test... ======='
107        self._wifi_context.client.shill_debug_log(msg)
108
109
110    def cleanup(self):
111        msg = '======= WiFi autotest complete. Cleaning up... ======='
112        self._wifi_context.client.shill_debug_log(msg)
113        # If we fail during initialization, we might not have a context.
114        if hasattr(self, '_wifi_context'):
115            self._wifi_context.teardown()
116
117
118    def configure_and_connect_to_ap(self, configuration_parameters):
119        """
120        Configure the router as an AP with the given parameters and connect
121        the DUT to it.
122
123        @param configuration_parameters HostapConfig object.
124
125        @return name of the configured AP
126        """
127        self.context.configure(configuration_parameters)
128        ap_ssid = self.context.router.get_ssid()
129        assoc_params = xmlrpc_datatypes.AssociationParameters(ssid=ap_ssid)
130        self.context.assert_connect_wifi(assoc_params)
131        return ap_ssid
132