xref: /aosp_15_r20/external/autotest/server/cros/tradefed/tradefed_prerequisite.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1# Lint as: python2, python3
2# Copyright 2019 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.
5
6import logging
7
8_ERROR_PREFIX = 'CTS Test Precondition Failed'
9
10def bluetooth(hosts):
11    """Check for missing bluetooth hardware.
12    """
13    # TODO(ianrlee): Reenable, once a nice check is found in b/148621587.
14    # for host in hosts:
15    #    output = host.run('hcitool dev').stdout
16    #    lines = output.splitlines()
17    #    if len(lines) < 2 or not lines[0].startswith('Devices:'):
18    #        return False, '%s: Bluetooth device is missing.'\
19    #                      'Stdout of the command "hcitool dev"'\
20    #                      'on host %s was %s' % (_ERROR_PREFIX, host, output)
21    return True, ''
22
23
24def region_us(hosts):
25    """Check that region is set to "us".
26    """
27    for host in hosts:
28        output = host.run('vpd -g region', ignore_status=True).stdout
29        if output not in ['us', '']:
30            return False, '%s: Region is not "us" or empty. '\
31                          'STDOUT of the command "vpd -l '\
32                          'region" on host %s was %s'\
33                          % (_ERROR_PREFIX, host, output)
34    return True, ''
35
36prerequisite_map = {
37    'bluetooth': bluetooth,
38    'region_us': region_us,
39}
40
41def check(prereq, hosts):
42    """Execute the prerequisite check.
43
44    @return boolean indicating if check passes.
45    @return string error message if check fails.
46    """
47    if prereq not in prerequisite_map:
48        logging.info('%s is not a valid prerequisite.', prereq)
49        return True, ''
50    return prerequisite_map[prereq](hosts)
51