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