Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 1 | # Copyright 2015 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 | # Expects to be run in an environment with sudo and no interactive password |
| 6 | # prompt, such as within the Chromium OS development chroot. |
| 7 | |
| 8 | |
| 9 | """This file provides core logic for pdtester verify/repair process.""" |
| 10 | |
Wai-Hong Tam | 3a8a255 | 2019-11-19 14:28:04 +0800 | [diff] [blame] | 11 | import logging |
| 12 | |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 13 | from autotest_lib.server.hosts import servo_host |
| 14 | |
| 15 | |
| 16 | # Names of the host attributes in the database that represent the values for |
| 17 | # the pdtester_host and pdtester_port for a PD tester connected to the DUT. |
| 18 | PDTESTER_HOST_ATTR = 'pdtester_host' |
| 19 | PDTESTER_PORT_ATTR = 'pdtester_port' |
| 20 | |
| 21 | |
| 22 | def make_pdtester_hostname(dut_hostname): |
| 23 | """Given a DUT's hostname, return the hostname of its PD tester. |
| 24 | |
| 25 | @param dut_hostname: hostname of a DUT. |
| 26 | |
| 27 | @return hostname of the DUT's PD tester. |
| 28 | |
| 29 | """ |
| 30 | host_parts = dut_hostname.split('.') |
| 31 | host_parts[0] = host_parts[0] + '-pdtester' |
| 32 | return '.'.join(host_parts) |
| 33 | |
| 34 | |
| 35 | class PDTesterHost(servo_host.ServoHost): |
| 36 | """Host class for a host that controls a PDTester object.""" |
| 37 | |
| 38 | |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 39 | def _initialize(self, pdtester_host='localhost', pdtester_port=9999, |
| 40 | *args, **dargs): |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 41 | """Initialize a PDTesterHost instance. |
| 42 | |
| 43 | A PDTesterHost instance represents a host that controls a PD tester. |
| 44 | |
| 45 | @param pdtester_host: Name of the host where the servod process |
| 46 | is running. |
| 47 | @param pdtester_port: Port the servod process is listening on. |
| 48 | |
| 49 | """ |
| 50 | super(PDTesterHost, self)._initialize(pdtester_host, pdtester_port, |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 51 | *args, **dargs) |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 52 | self.connect_servo() |
| 53 | |
| 54 | |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 55 | def create_pdtester_host(pdtester_args, servo_host): |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 56 | """Create a PDTesterHost object used to access pdtester servo |
| 57 | |
| 58 | The `pdtester_args` parameter is a dictionary specifying optional |
| 59 | PDTester client parameter overrides (i.e. a specific host or port). |
| 60 | When specified, the caller requires that an exception be raised |
| 61 | unless both the PDTesterHost and the PDTester are successfully |
| 62 | created. |
| 63 | |
| 64 | @param pdtester_args: A dictionary that contains args for creating |
| 65 | a PDTesterHost object, |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 66 | e.g. {'pdtester_host': '172.11.11.111', |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 67 | 'pdtester_port': 9999}. |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 68 | @param servo_host: If PDTester and Servo are the same, this |
| 69 | servo_host object will be returned. |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 70 | @returns: A PDTesterHost object or None. |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 71 | |
| 72 | """ |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 73 | # None means PDTester is not required to run a test. |
Wai-Hong Tam | 0ed9fe1 | 2019-05-23 11:00:58 -0700 | [diff] [blame] | 74 | if pdtester_args is None: |
| 75 | return None |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 76 | |
| 77 | # If an user doesn't pass the PDTester info, fall back to use the servo |
| 78 | # info. Usually we use Servo v4 as PDTester, so make it default. |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 79 | if PDTESTER_HOST_ATTR not in pdtester_args: |
Wai-Hong Tam | 3a8a255 | 2019-11-19 14:28:04 +0800 | [diff] [blame] | 80 | logging.debug('%s not specified, reuse the same hostname as servo: %s', |
| 81 | PDTESTER_HOST_ATTR, servo_host.hostname) |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 82 | pdtester_args[PDTESTER_HOST_ATTR] = servo_host.hostname |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 83 | |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 84 | if PDTESTER_PORT_ATTR not in pdtester_args: |
Wai-Hong Tam | 3a8a255 | 2019-11-19 14:28:04 +0800 | [diff] [blame] | 85 | logging.debug('%s not specified, reuse the same port as servo: %s', |
| 86 | PDTESTER_PORT_ATTR, servo_host.servo_port) |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 87 | pdtester_args[PDTESTER_PORT_ATTR] = servo_host.servo_port |
| 88 | |
| 89 | # Just return the servo_host object, if the hostname and the port are the |
| 90 | # same as servo_host. |
| 91 | if (pdtester_args[PDTESTER_HOST_ATTR] == servo_host.hostname and |
| 92 | pdtester_args[PDTESTER_PORT_ATTR] == servo_host.servo_port): |
Wai-Hong Tam | 3a8a255 | 2019-11-19 14:28:04 +0800 | [diff] [blame] | 93 | logging.debug('Return the servo_host directly as PDTester and Servo ' |
| 94 | 'are the same.') |
Wai-Hong Tam | 90b164d | 2019-10-25 13:15:39 -0700 | [diff] [blame] | 95 | return servo_host |
Wai-Hong Tam | 16e5edb | 2019-09-17 16:10:07 -0700 | [diff] [blame] | 96 | |
| 97 | return PDTesterHost(**pdtester_args) |