• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright (c) 2018 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import datetime
7import logging
8import random
9
10from autotest_lib.client.bin import test
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.common_lib.cros.network import interface
13from autotest_lib.client.cros.power import sys_power
14
15class network_WiFiResume(test.test):
16    """
17    Ensure wireless interface comes up after suspend-resume.
18    """
19    version = 1
20
21    def _get_wifi_dev(self):
22        wlan_ifs = [nic for nic in interface.get_interfaces()
23                    if nic.is_wifi_device()]
24        if wlan_ifs:
25            return wlan_ifs[0].name
26        else:
27            return None
28
29    # Default suspend values are for smoke test that expects to run in <10s
30    def _suspend_to_ram(self, min_secs_suspend=2, max_secs_suspend=7):
31        secs_to_suspend = (random.randint(min_secs_suspend, max_secs_suspend))
32        logging.info('Scheduling wakeup in %d seconds.\n', secs_to_suspend)
33        sys_power.do_suspend(secs_to_suspend)
34
35    def run_once(self, wifi_timeout=2, dev=None):
36        '''Check that WiFi interface is available after a resume
37
38        @param dev: device (eg 'wlan0') to use for wifi tests.
39                    autodetected if unset.
40        @param wifi_timeout: number of seconds within which the interface must
41                             come up after a suspend/resume cycle.
42        '''
43
44        if dev is None:
45            dev = self._get_wifi_dev()
46        if dev is None:
47            raise error.TestError('No wifi device supplied to check for'
48                                  'or found on system')
49
50        random.seed()
51        self._suspend_to_ram()
52        start_time = datetime.datetime.now()
53        deadline = start_time + datetime.timedelta(seconds=wifi_timeout)
54        found_dev = None
55
56        while (not found_dev) and (deadline > datetime.datetime.now()):
57            found_dev = self._get_wifi_dev()
58            if found_dev == dev:
59                delay = datetime.datetime.now() - start_time
60                logging.info('Found %s about %d ms after resume'%
61                             (dev, int(delay.total_seconds()*1000)))
62                return
63            elif found_dev is not None:
64                logging.error('Found %s on resume, was %s before suspend' %
65                              (found_dev, dev))
66
67        if found_dev != dev:
68            delay = datetime.datetime.now() - start_time
69            raise error.TestFail('Did not find %s after %d ms' %
70                                 (dev, int(delay.total_seconds()*1000)))
71