blob: e61d4afda1d391354c21986c815c1092a0041920 [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
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 """Build a usable hostname for a test companion device from the client name.
44
45 Optionally, override the generated name with a commandline provided version.
46
47 @param client_hostname: string DNS name of device under test (the client).
48 @param suffix: string suffix to append to the client hostname.
49 @param cmdline_override: optional DNS name of companion device. If this is
50 given, it overrides the generated client based hostname.
51 @param not_dnsname_msg: string message to include in the exception raised
52 if the client hostname is found to be an IP address rather than a
53 DNS name.
54 @return string DNS name of companion device.
55
56 """
57 if cmdline_override is not None:
58 return cmdline_override
59 if _is_ip_address(client_hostname):
60 logging.error('%r looks like an IP address?', client_hostname)
61 raise error.TestError(not_dnsname_msg)
62 parts = client_hostname.split('.', 1)
63 parts[0] = parts[0] + suffix
64 return '.'.join(parts)
65
66
67def get_router_addr(client_hostname, cmdline_override=None):
68 """Build a hostname for a WiFi router from the client hostname.
69
70 Optionally override that hostname with the provided command line hostname.
71
72 @param client_hostname: string DNS name of the client.
73 @param cmdline_override: string DNS name of the router provided
74 via commandline arguments.
75 @return usable DNS name for router host.
76
77 """
78 return get_companion_device_addr(
79 client_hostname,
80 '-router',
81 cmdline_override=cmdline_override,
82 not_dnsname_msg=ROUTER_FAILURE_MESSAGE)
83
84
85def get_attenuator_addr(client_hostname, cmdline_override=None):
86 """Build a hostname for a WiFi variable attenuator from the client hostname.
87
88 Optionally override that hostname with the provided command line hostname.
89
90 @param client_hostname: string DNS name of the client.
91 @param cmdline_override: string DNS name of the variable attenuator
92 controller provided via commandline arguments.
93 @return usable DNS name for attenuator controller.
94
95 """
96 return get_companion_device_addr(
97 client_hostname,
98 '-attenuator',
99 cmdline_override=cmdline_override,
100 not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE)
101
102
103def get_tester_addr(client_hostname, cmdline_override=None):
104 """Build a hostname for a Bluetooth test device from the client hostname.
105
106 Optionally override that hostname with the provided command line hostname.
107
108 @param client_hostname: string DNS name of the client.
109 @param cmdline_override: string DNS name of the Bluetooth tester
110 provided via commandline arguments.
111 @return usable DNS name for Bluetooth tester device.
112
113 """
114 return get_companion_device_addr(
115 client_hostname,
116 '-router',
117 cmdline_override=cmdline_override,
118 not_dnsname_msg=BLUETOOTH_TESTER_FAILURE_MESSAGE)