xref: /aosp_15_r20/external/autotest/server/site_tests/power_LW/power_LW.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2020 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5import logging
6
7
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.common_lib import utils
10from autotest_lib.server import autotest
11from autotest_lib.server import test
12from autotest_lib.server.hosts import factory
13
14
15class power_LW(test.test):
16    """Wrapper test around a client test for power lab."""
17    version = 1
18
19    SERVO_V4_ETH_VENDOR = '0bda'
20    SERVO_V4_ETH_PRODUCT = '8153'
21    WIFI_SSID = 'powertest_ap'
22    WIFI_PASSWORD = 'chromeos'
23
24    def _get_wlan_ip(self, host):
25        """Connect to wifi and return wlan ip address."""
26        wlan_ip = host.get_wlan_ip()
27        logging.info('wlan_ip=%s', wlan_ip)
28        if wlan_ip:
29            return wlan_ip
30
31        if not host.connect_to_wifi(self.WIFI_SSID, self.WIFI_PASSWORD):
32            logging.info('Script to connect to wifi is probably missing.'
33                         'Run stub_Pass as a workaround to install it.')
34            autotest_client = autotest.Autotest(host)
35            autotest_client.run_test('stub_Pass')
36            if not host.connect_to_wifi(self.WIFI_SSID, self.WIFI_PASSWORD):
37                raise error.TestError('Can not connect to wifi.')
38
39        wlan_ip = host.get_wlan_ip()
40        logging.info('After connected to wifi wlan_ip=%s', wlan_ip)
41        if not wlan_ip:
42            raise error.TestError('Can not find wlan ip.')
43        return wlan_ip
44
45    def _get_wlan_host(self, host, machine):
46        """Return CrosHost object that use wifi."""
47        wlan_ip = self._get_wlan_ip(host)
48        if machine['hostname'] == wlan_ip:
49            return host
50
51        hostname = wlan_ip
52        if utils.host_is_in_power_lab(machine['hostname']):
53            hostname = utils.get_power_lab_wlan_hostname(machine['hostname'])
54
55        machine['hostname'] = hostname
56        return factory.create_host(machine)
57
58    def _stop_ethernet(self, host):
59        """Find and unbind servo v4 usb ethernet."""
60        # Stop check_ethernet.hook to reconnect the usb device
61        try:
62            host.run('stop recover_duts')
63        except:
64            logging.warning("Continue if stop recover_duts failed.")
65
66        eth_usb = host.find_usb_devices(
67            self.SERVO_V4_ETH_VENDOR, self.SERVO_V4_ETH_PRODUCT)
68        if len(eth_usb) == 1 and eth_usb[0] and host.get_wlan_ip():
69            host.unbind_usb_device(eth_usb[0])
70
71    def run_once(self, host, test, args, machine):
72        """Prepare DUT for power test then run the client test.
73
74        The DUT will
75        - Switch from ethernet connection to wifi.
76        - Unbind Servo v4 USB ethernet device.
77        - Set EC to force discharge during the client test.
78
79        @param host: CrosHost object representing the DUT.
80        @param test: testname
81        @param args: arguments of the test in a dict.
82        @param machine: machine dict of the host.
83        """
84        wlan_host = self._get_wlan_host(host, machine)
85        if wlan_host != host:
86            self._stop_ethernet(host)
87
88        args['force_discharge'] = True
89        args['tag'] = args.get('tag', 'PLW')
90
91        autotest_client = autotest.Autotest(wlan_host)
92        autotest_client.run_test(test, **args)
93
94        # Restore USB ethernet device.
95        wlan_host.reboot()
96