blob: c7b04b4e852065ab6809a531ae7b4ab0c2b2af9a [file] [log] [blame]
# Copyright 2016 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Utilities used with Brillo hosts."""
import contextlib
import logging
import common
from autotest_lib.client.bin import utils
_RUN_BACKGROUND_TEMPLATE = '( %(cmd)s ) </dev/null >/dev/null 2>&1 & echo -n $!'
to=%(timeout)d; \
while test ${to} -ne 0; do \
test $(ps %(pid)d | wc -l) -gt 1 || break; \
sleep 1; \
to=$((to - 1)); \
done; \
test ${to} -ne 0 -o $(ps %(pid)d | wc -l) -eq 1 \
def run_in_background(host, cmd):
"""Runs a command in the background on the DUT.
@param host: A host object representing the DUT.
@param cmd: The command to run.
@return The background process ID (integer).
background_cmd = _RUN_BACKGROUND_TEMPLATE % {'cmd': cmd}
return int(host.run_output(background_cmd).strip())
def wait_for_process(host, pid, timeout=-1):
"""Waits for a process on the DUT to terminate.
@param host: A host object representing the DUT.
@param pid: The process ID (integer).
@param timeout: Number of seconds to wait; default is wait forever.
@return True if process terminated within the alotted time, False otherwise.
wait_cmd = _WAIT_CMD_TEMPLATE % {'pid': pid, 'timeout': timeout}
return, ignore_status=True).exit_status == 0
def connect_to_ssid(host, ssid, passphrase):
"""Connects to a given ssid.
@param host: A host object representing the DUT.
@param ssid: A string representing the ssid to connect to. If ssid is None,
assume that host is already connected to wifi.
@param passphrase: A string representing the passphrase to the ssid.
Defaults to None.
if ssid is None:
# No ssid is passed. It is assumed that the host is already
# connected to wifi.
logging.warning('This test assumes that the device is connected to '
'wifi. If it is not, this test will fail.')
else:'Connecting to ssid %s', ssid)
# Update the weaved init.rc to stop privet. This sets up shill in
# client mode allowing it to connect to wifi.
host.remount()'sed \'s/service weaved \/system\/bin\/weaved/'
'service weaved \/system\/bin\/weaved --disable_privet/\' '
'-i /system/etc/init/weaved.rc')
lambda: 'running' in'getprop init.svc.shill'
sleep_interval=1, timeout=300,
desc='shill was not started by init')'Connecting to wifi')
wifi_cmd = ('shill_setup_wifi --ssid=%s '
'--wait-for-online-seconds=%i' % (ssid, 300))
if passphrase:
wifi_cmd += ' --passphrase=%s' % passphrase
if ssid:
# If we connected to a ssid, disconnect.
host.remount()'sed \'s/service weaved \/system\/bin\/weaved '
'--disable_privet/service weaved \/system\/bin\/weaved/\' '
'-i /system/etc/init/weaved.rc')'stop weaved')'start weaved')