blob: c0cd140cfb9be60b2ff3c8cd552e71b021c1f452 [file] [log] [blame]
Christopher Wiley6b1d9e72014-12-13 18:07:41 -08001# 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
Cheng-Yi Chiang22612862015-08-20 20:39:57 +080025def is_ip_address(hostname):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080026 """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,
Christopher Wiley003bf822014-12-22 14:38:18 -080042 not_dnsname_msg=DEFAULT_FAILURE_MESSAGE,
43 allow_failure=False):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080044 """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.
Christopher Wiley003bf822014-12-22 14:38:18 -080055 @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.
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080059
60 """
61 if cmdline_override is not None:
62 return cmdline_override
Cheng-Yi Chiang22612862015-08-20 20:39:57 +080063 if is_ip_address(client_hostname):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080064 logging.error('%r looks like an IP address?', client_hostname)
Christopher Wiley003bf822014-12-22 14:38:18 -080065 if allow_failure:
66 return None
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080067 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
Christopher Wiley003bf822014-12-22 14:38:18 -080091def get_attenuator_addr(client_hostname,
92 cmdline_override=None,
93 allow_failure=False):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080094 """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.
Christopher Wiley003bf822014-12-22 14:38:18 -0800101 @param allow_failure: boolean True iff we should return None on failure to
102 infer a DNS name.
Christopher Wiley6b1d9e72014-12-13 18:07:41 -0800103 @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,
Christopher Wiley003bf822014-12-22 14:38:18 -0800110 not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE,
111 allow_failure=allow_failure)
Christopher Wiley6b1d9e72014-12-13 18:07:41 -0800112
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)