blob: db7b02f94d94aacafc6bb47dddb815ecce6bc894 [file] [log] [blame]
Gilad Arnoldb5746a12015-12-08 10:41:51 -08001# 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 Nathan8b16d9d2016-03-22 17:56:46 -07007import contextlib
8import logging
9import time
10
11import common
12from autotest_lib.client.bin import site_utils as client_site_utils
13
14
Gilad Arnoldb5746a12015-12-08 10:41:51 -080015_RUN_BACKGROUND_TEMPLATE = '( %(cmd)s ) </dev/null >/dev/null 2>&1 & echo -n $!'
16
17_WAIT_CMD_TEMPLATE = """\
18to=%(timeout)d; \
19while test ${to} -ne 0; do \
20 test $(ps %(pid)d | wc -l) -gt 1 || break; \
21 sleep 1; \
22 to=$((to - 1)); \
23done; \
24test ${to} -ne 0 -o $(ps %(pid)d | wc -l) -eq 1 \
25"""
26
27
28def 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
40def 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 Nathan8b16d9d2016-03-22 17:56:46 -070051
52
53@contextlib.contextmanager
54def connect_to_ssid(host, ssid, passphrase):
55 """Connects to a given ssid.
56
57 @param host: A host object representing the DUT.
Ralph Nathand3be2132016-04-05 16:50:27 -070058 @param ssid: A string representing the ssid to connect to. If ssid is None,
59 assume that host is already connected to wifi.
Ralph Nathan8b16d9d2016-03-22 17:56:46 -070060 @param passphrase: A string representing the passphrase to the ssid.
61 Defaults to None.
62 """
63 try:
Ralph Nathand3be2132016-04-05 16:50:27 -070064 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 Nathan88b56ad2016-04-06 13:25:36 -070085 wifi_cmd = 'shill_setup_wifi --ssid=%s' % ssid
Ralph Nathand3be2132016-04-05 16:50:27 -070086 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 Nathan8b16d9d2016-03-22 17:56:46 -070094 finally:
Ralph Nathand3be2132016-04-05 16:50:27 -070095 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 Nathan88b56ad2016-04-06 13:25:36 -0700101 host.run('stop weaved')
102 host.run('start weaved')