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