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