• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 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
5import logging
6import socket
7
8from autotest_lib.client.common_lib import error
9
10
11# See server/cros/network/wifi_test_context_manager.py for commandline
12# flags to control IP addresses in WiFi tests.
13DEFAULT_FAILURE_MESSAGE = (
14        'Cannot infer DNS name of companion device from an IP address.')
15ATTENUATOR_FAILURE_MESSAGE = (
16        'Cannot infer DNS name of WiFi variable attenuator from a client IP '
17        'address.  Use --atten_addr=<ip or dns name>')
18BLUETOOTH_TESTER_FAILURE_MESSAGE = (
19        'Remote host cannot be an IP address unless tester specified with '
20         '--args tester=IP')
21ROUTER_FAILURE_MESSAGE = (
22        'Cannot infer DNS name of WiFi router from a client IP address.')
23
24
25def is_ip_address(hostname):
26    """Infers whether |hostname| could be an IP address.
27
28    @param hostname: string DNS name or IP address.
29    @return True iff hostname is a valid IP address.
30
31    """
32    try:
33        socket.inet_aton(hostname)
34        return True
35    except socket.error:
36        return False
37
38
39def get_companion_device_addr(client_hostname,
40                              suffix,
41                              cmdline_override=None,
42                              not_dnsname_msg=DEFAULT_FAILURE_MESSAGE,
43                              allow_failure=False):
44    """Build a usable hostname for a test companion device from the client name.
45
46    Optionally, override the generated name with a commandline provided version.
47
48    @param client_hostname: string DNS name of device under test (the client).
49    @param suffix: string suffix to append to the client hostname.
50    @param cmdline_override: optional DNS name of companion device.  If this is
51            given, it overrides the generated client based hostname.
52    @param not_dnsname_msg: string message to include in the exception raised
53            if the client hostname is found to be an IP address rather than a
54            DNS name.
55    @param allow_failure: boolean True iff we should return None on failure to
56            infer a DNS name.
57    @return string DNS name of companion device or None if |allow_failure|
58            is True and no DNS name can be inferred.
59
60    """
61    if cmdline_override is not None:
62        return cmdline_override
63    if is_ip_address(client_hostname):
64        logging.error('%r looks like an IP address?', client_hostname)
65        if allow_failure:
66            return None
67        raise error.TestError(not_dnsname_msg)
68    parts = client_hostname.split('.', 1)
69    parts[0] = parts[0] + suffix
70    return '.'.join(parts)
71
72
73def get_router_addr(client_hostname, cmdline_override=None):
74    """Build a hostname for a WiFi router from the client hostname.
75
76    Optionally override that hostname with the provided command line hostname.
77
78    @param client_hostname: string DNS name of the client.
79    @param cmdline_override: string DNS name of the router provided
80            via commandline arguments.
81    @return usable DNS name for router host.
82
83    """
84    return get_companion_device_addr(
85            client_hostname,
86            '-router',
87            cmdline_override=cmdline_override,
88            not_dnsname_msg=ROUTER_FAILURE_MESSAGE)
89
90
91def get_attenuator_addr(client_hostname,
92                        cmdline_override=None,
93                        allow_failure=False):
94    """Build a hostname for a WiFi variable attenuator from the client hostname.
95
96    Optionally override that hostname with the provided command line hostname.
97
98    @param client_hostname: string DNS name of the client.
99    @param cmdline_override: string DNS name of the variable attenuator
100            controller provided via commandline arguments.
101    @param allow_failure: boolean True iff we should return None on failure to
102            infer a DNS name.
103    @return usable DNS name for attenuator controller.
104
105    """
106    return get_companion_device_addr(
107            client_hostname,
108            '-attenuator',
109            cmdline_override=cmdline_override,
110            not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE,
111            allow_failure=allow_failure)
112
113
114def get_tester_addr(client_hostname, cmdline_override=None):
115    """Build a hostname for a Bluetooth test device from the client hostname.
116
117    Optionally override that hostname with the provided command line hostname.
118
119    @param client_hostname: string DNS name of the client.
120    @param cmdline_override: string DNS name of the Bluetooth tester
121            provided via commandline arguments.
122    @return usable DNS name for Bluetooth tester device.
123
124    """
125    return get_companion_device_addr(
126            client_hostname,
127            '-router',
128            cmdline_override=cmdline_override,
129            not_dnsname_msg=BLUETOOTH_TESTER_FAILURE_MESSAGE)
130