Home | History | Annotate | Download | only in brillo_PingTest
      1 # Copyright 2015 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 
      5 import logging
      6 
      7 import common
      8 from autotest_lib.client.common_lib import error
      9 from autotest_lib.client.common_lib import global_config
     10 from autotest_lib.client.common_lib import site_utils
     11 from autotest_lib.client.common_lib.cros import retry
     12 from autotest_lib.server import afe_utils
     13 from autotest_lib.server import test
     14 from autotest_lib.server.brillo import host_utils
     15 
     16 
     17 _DEFAULT_PING_HOST = 'www.google.com'
     18 _DEFAULT_PING_COUNT = 4
     19 _DEFAULT_PING_TIMEOUT = 4
     20 
     21 
     22 class brillo_PingTest(test.test):
     23     """Ping an Internet host."""
     24     version = 1
     25 
     26     def run_once(self, host=None, ssid=None, passphrase=None,
     27                  ping_host=_DEFAULT_PING_HOST,
     28                  ping_count=_DEFAULT_PING_COUNT,
     29                  ping_timeout=_DEFAULT_PING_TIMEOUT):
     30         """Pings an Internet host with given timeout and count values.
     31 
     32         @param host: A host object representing the DUT.
     33         @param ssid: Ssid to connect to.
     34         @param passphrase: A string representing the passphrase to the ssid.
     35         @param ping_host: The Internet host to ping.
     36         @param ping_count: The number of pings to attempt. The test passes if
     37                            we get at least one reply.
     38         @param ping_timeout: The number of seconds to wait for a reply.
     39 
     40         @raise TestFail: The test failed.
     41         """
     42         if afe_utils.host_in_lab(host):
     43             ssid = site_utils.get_wireless_ssid(host.hostname)
     44             passphrase = global_config.global_config.get_config_value(
     45                     'CLIENT', 'wireless_password', default=None)
     46         host.run('am startservice -n com.google.wifisetup/.WifiSetupService '
     47                  '-a WifiSetupService.Connect -e ssid %s -e passphrase %s' %
     48                  (ssid, passphrase))
     49 
     50         @retry.retry(error.GenericHostRunError, timeout_min=1.5, delay_sec=3)
     51         def ping():
     52             cmd = 'ping -q -c %s -W %s %s' % (ping_count, ping_timeout,
     53                                               ping_host)
     54             host.run(cmd)
     55 
     56         try:
     57             ping()
     58         except error.GenericHostRunError:
     59             raise error.TestFail(
     60                     'Failed to ping %s in %d seconds on all %d attempts' %
     61                     (ping_host, ping_timeout, ping_count))
     62