blob: ab8dcd587ec1d58aa42c13001c984acc5dedac7c [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
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080010# See server/cros/network/wifi_test_context_manager.py for commandline
11# flags to control IP addresses in WiFi tests.
12DEFAULT_FAILURE_MESSAGE = (
13 'Cannot infer DNS name of companion device from an IP address.')
14ATTENUATOR_FAILURE_MESSAGE = (
15 'Cannot infer DNS name of WiFi variable attenuator from a client IP '
16 'address. Use --atten_addr=<ip or dns name>')
17BLUETOOTH_TESTER_FAILURE_MESSAGE = (
18 'Remote host cannot be an IP address unless tester specified with '
19 '--args tester=IP')
20ROUTER_FAILURE_MESSAGE = (
21 'Cannot infer DNS name of WiFi router from a client IP address.')
Bindu Mahadevb8812f42016-04-21 15:59:11 -070022PCAP_FAILURE_MESSAGE = (
23 'Cannot infer DNS name of Packet Capturer from a client IP address.')
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080024
25
Cheng-Yi Chiang22612862015-08-20 20:39:57 +080026def is_ip_address(hostname):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080027 """Infers whether |hostname| could be an IP address.
28
29 @param hostname: string DNS name or IP address.
30 @return True iff hostname is a valid IP address.
31
32 """
33 try:
34 socket.inet_aton(hostname)
35 return True
36 except socket.error:
37 return False
38
39
40def get_companion_device_addr(client_hostname,
41 suffix,
42 cmdline_override=None,
Christopher Wiley003bf822014-12-22 14:38:18 -080043 not_dnsname_msg=DEFAULT_FAILURE_MESSAGE,
44 allow_failure=False):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080045 """Build a usable hostname for a test companion device from the client name.
46
47 Optionally, override the generated name with a commandline provided version.
48
49 @param client_hostname: string DNS name of device under test (the client).
50 @param suffix: string suffix to append to the client hostname.
51 @param cmdline_override: optional DNS name of companion device. If this is
52 given, it overrides the generated client based hostname.
53 @param not_dnsname_msg: string message to include in the exception raised
54 if the client hostname is found to be an IP address rather than a
55 DNS name.
Christopher Wiley003bf822014-12-22 14:38:18 -080056 @param allow_failure: boolean True iff we should return None on failure to
57 infer a DNS name.
58 @return string DNS name of companion device or None if |allow_failure|
59 is True and no DNS name can be inferred.
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080060
61 """
62 if cmdline_override is not None:
63 return cmdline_override
Cheng-Yi Chiang22612862015-08-20 20:39:57 +080064 if is_ip_address(client_hostname):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080065 logging.error('%r looks like an IP address?', client_hostname)
Christopher Wiley003bf822014-12-22 14:38:18 -080066 if allow_failure:
67 return None
Christopher Wiley6b1d9e72014-12-13 18:07:41 -080068 raise error.TestError(not_dnsname_msg)
69 parts = client_hostname.split('.', 1)
70 parts[0] = parts[0] + suffix
71 return '.'.join(parts)
72
73
74def get_router_addr(client_hostname, cmdline_override=None):
75 """Build a hostname for a WiFi router from the client hostname.
76
77 Optionally override that hostname with the provided command line hostname.
78
79 @param client_hostname: string DNS name of the client.
80 @param cmdline_override: string DNS name of the router provided
81 via commandline arguments.
82 @return usable DNS name for router host.
83
84 """
85 return get_companion_device_addr(
86 client_hostname,
87 '-router',
88 cmdline_override=cmdline_override,
89 not_dnsname_msg=ROUTER_FAILURE_MESSAGE)
90
91
Bindu Mahadevb8812f42016-04-21 15:59:11 -070092def get_pcap_addr(client_hostname,
93 cmdline_override=None,
94 allow_failure=False):
95 """Build a hostname for a packet capturer from the client hostname.
96
97 @param client_hostname: string DNS name of the client.
98 @param cmdline_override: string DNS name of the packet capturer provided
99 via commandline arguments.
100 @return usable DNS name for capturer host or None.
101
102 """
103 return get_companion_device_addr(
104 client_hostname,
105 '-pcap',
106 not_dnsname_msg=PCAP_FAILURE_MESSAGE,
107 allow_failure=allow_failure)
108
109
Christopher Wiley003bf822014-12-22 14:38:18 -0800110def get_attenuator_addr(client_hostname,
111 cmdline_override=None,
112 allow_failure=False):
Christopher Wiley6b1d9e72014-12-13 18:07:41 -0800113 """Build a hostname for a WiFi variable attenuator from the client hostname.
114
115 Optionally override that hostname with the provided command line hostname.
116
117 @param client_hostname: string DNS name of the client.
118 @param cmdline_override: string DNS name of the variable attenuator
119 controller provided via commandline arguments.
Christopher Wiley003bf822014-12-22 14:38:18 -0800120 @param allow_failure: boolean True iff we should return None on failure to
121 infer a DNS name.
Christopher Wiley6b1d9e72014-12-13 18:07:41 -0800122 @return usable DNS name for attenuator controller.
123
124 """
125 return get_companion_device_addr(
126 client_hostname,
127 '-attenuator',
128 cmdline_override=cmdline_override,
Christopher Wiley003bf822014-12-22 14:38:18 -0800129 not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE,
130 allow_failure=allow_failure)
Christopher Wiley6b1d9e72014-12-13 18:07:41 -0800131
132
133def get_tester_addr(client_hostname, cmdline_override=None):
134 """Build a hostname for a Bluetooth test device from the client hostname.
135
136 Optionally override that hostname with the provided command line hostname.
137
138 @param client_hostname: string DNS name of the client.
139 @param cmdline_override: string DNS name of the Bluetooth tester
140 provided via commandline arguments.
141 @return usable DNS name for Bluetooth tester device.
142
143 """
144 return get_companion_device_addr(
145 client_hostname,
146 '-router',
147 cmdline_override=cmdline_override,
148 not_dnsname_msg=BLUETOOTH_TESTER_FAILURE_MESSAGE)