Gilad Arnold | b5746a1 | 2015-12-08 10:41:51 -0800 | [diff] [blame] | 1 | # Copyright 2016 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 | """Utilities used with Brillo hosts.""" |
| 6 | |
Ralph Nathan | 8b16d9d | 2016-03-22 17:56:46 -0700 | [diff] [blame] | 7 | import contextlib |
| 8 | import logging |
| 9 | import time |
| 10 | |
| 11 | import common |
| 12 | from autotest_lib.client.bin import site_utils as client_site_utils |
| 13 | |
| 14 | |
Gilad Arnold | b5746a1 | 2015-12-08 10:41:51 -0800 | [diff] [blame] | 15 | _RUN_BACKGROUND_TEMPLATE = '( %(cmd)s ) </dev/null >/dev/null 2>&1 & echo -n $!' |
| 16 | |
| 17 | _WAIT_CMD_TEMPLATE = """\ |
| 18 | to=%(timeout)d; \ |
| 19 | while test ${to} -ne 0; do \ |
| 20 | test $(ps %(pid)d | wc -l) -gt 1 || break; \ |
| 21 | sleep 1; \ |
| 22 | to=$((to - 1)); \ |
| 23 | done; \ |
| 24 | test ${to} -ne 0 -o $(ps %(pid)d | wc -l) -eq 1 \ |
| 25 | """ |
| 26 | |
| 27 | |
| 28 | def run_in_background(host, cmd): |
| 29 | """Runs a command in the background on the DUT. |
| 30 | |
| 31 | @param host: A host object representing the DUT. |
| 32 | @param cmd: The command to run. |
| 33 | |
| 34 | @return The background process ID (integer). |
| 35 | """ |
| 36 | background_cmd = _RUN_BACKGROUND_TEMPLATE % {'cmd': cmd} |
| 37 | return int(host.run_output(background_cmd).strip()) |
| 38 | |
| 39 | |
| 40 | def wait_for_process(host, pid, timeout=-1): |
| 41 | """Waits for a process on the DUT to terminate. |
| 42 | |
| 43 | @param host: A host object representing the DUT. |
| 44 | @param pid: The process ID (integer). |
| 45 | @param timeout: Number of seconds to wait; default is wait forever. |
| 46 | |
| 47 | @return True if process terminated within the alotted time, False otherwise. |
| 48 | """ |
| 49 | wait_cmd = _WAIT_CMD_TEMPLATE % {'pid': pid, 'timeout': timeout} |
| 50 | return host.run(wait_cmd, ignore_status=True).exit_status == 0 |
Ralph Nathan | 8b16d9d | 2016-03-22 17:56:46 -0700 | [diff] [blame] | 51 | |
| 52 | |
| 53 | @contextlib.contextmanager |
| 54 | def connect_to_ssid(host, ssid, passphrase): |
| 55 | """Connects to a given ssid. |
| 56 | |
| 57 | @param host: A host object representing the DUT. |
Ralph Nathan | d3be213 | 2016-04-05 16:50:27 -0700 | [diff] [blame] | 58 | @param ssid: A string representing the ssid to connect to. If ssid is None, |
| 59 | assume that host is already connected to wifi. |
Ralph Nathan | 8b16d9d | 2016-03-22 17:56:46 -0700 | [diff] [blame] | 60 | @param passphrase: A string representing the passphrase to the ssid. |
| 61 | Defaults to None. |
| 62 | """ |
| 63 | try: |
Ralph Nathan | d3be213 | 2016-04-05 16:50:27 -0700 | [diff] [blame] | 64 | if ssid is None: |
| 65 | # No ssid is passed. It is assumed that the host is already |
| 66 | # connected to wifi. |
| 67 | logging.warning('This test assumes that the device is connected to ' |
| 68 | 'wifi. If it is not, this test will fail.') |
| 69 | yield |
| 70 | else: |
| 71 | logging.info('Connecting to ssid %s', ssid) |
| 72 | # Update the weaved init.rc to stop privet. This sets up shill in |
| 73 | # client mode allowing it to connect to wifi. |
| 74 | host.remount() |
| 75 | host.run('sed \'s/service weaved \/system\/bin\/weaved/' |
| 76 | 'service weaved \/system\/bin\/weaved --disable_privet/\' ' |
| 77 | '-i /system/etc/init/weaved.rc') |
| 78 | host.reboot() |
| 79 | client_site_utils.poll_for_condition( |
| 80 | lambda: 'running' in host.run('getprop init.svc.shill' |
| 81 | ).stdout, |
| 82 | sleep_interval=1, timeout=300, |
| 83 | desc='shill was not started by init') |
| 84 | logging.info('Connecting to wifi') |
Ralph Nathan | 88b56ad | 2016-04-06 13:25:36 -0700 | [diff] [blame^] | 85 | wifi_cmd = 'shill_setup_wifi --ssid=%s' % ssid |
Ralph Nathan | d3be213 | 2016-04-05 16:50:27 -0700 | [diff] [blame] | 86 | if passphrase: |
| 87 | wifi_cmd += ' --passphrase=%s' % passphrase |
| 88 | host.run(wifi_cmd) |
| 89 | # TODO(ralphnathan): Once shill_setup_wifi can monitor the service as it |
| 90 | # connects to wifi, remove this timeout. |
| 91 | # Wait for wifi connection to occur. |
| 92 | time.sleep(10) |
| 93 | yield |
Ralph Nathan | 8b16d9d | 2016-03-22 17:56:46 -0700 | [diff] [blame] | 94 | finally: |
Ralph Nathan | d3be213 | 2016-04-05 16:50:27 -0700 | [diff] [blame] | 95 | if ssid: |
| 96 | # If we connected to a ssid, disconnect. |
| 97 | host.remount() |
| 98 | host.run('sed \'s/service weaved \/system\/bin\/weaved ' |
| 99 | '--disable_privet/service weaved \/system\/bin\/weaved/\' ' |
| 100 | '-i /system/etc/init/weaved.rc') |
Ralph Nathan | 88b56ad | 2016-04-06 13:25:36 -0700 | [diff] [blame^] | 101 | host.run('stop weaved') |
| 102 | host.run('start weaved') |