Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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 | |
| 5 | import logging |
| 6 | import socket |
| 7 | |
| 8 | from autotest_lib.client.common_lib import error |
| 9 | |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 10 | # See server/cros/network/wifi_test_context_manager.py for commandline |
| 11 | # flags to control IP addresses in WiFi tests. |
| 12 | DEFAULT_FAILURE_MESSAGE = ( |
| 13 | 'Cannot infer DNS name of companion device from an IP address.') |
| 14 | ATTENUATOR_FAILURE_MESSAGE = ( |
| 15 | 'Cannot infer DNS name of WiFi variable attenuator from a client IP ' |
| 16 | 'address. Use --atten_addr=<ip or dns name>') |
| 17 | BLUETOOTH_TESTER_FAILURE_MESSAGE = ( |
| 18 | 'Remote host cannot be an IP address unless tester specified with ' |
| 19 | '--args tester=IP') |
| 20 | ROUTER_FAILURE_MESSAGE = ( |
| 21 | 'Cannot infer DNS name of WiFi router from a client IP address.') |
Bindu Mahadev | b8812f4 | 2016-04-21 15:59:11 -0700 | [diff] [blame] | 22 | PCAP_FAILURE_MESSAGE = ( |
| 23 | 'Cannot infer DNS name of Packet Capturer from a client IP address.') |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 24 | |
| 25 | |
Cheng-Yi Chiang | 2261286 | 2015-08-20 20:39:57 +0800 | [diff] [blame] | 26 | def is_ip_address(hostname): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 27 | """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 | |
| 40 | def get_companion_device_addr(client_hostname, |
| 41 | suffix, |
| 42 | cmdline_override=None, |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 43 | not_dnsname_msg=DEFAULT_FAILURE_MESSAGE, |
| 44 | allow_failure=False): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 45 | """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 Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 56 | @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 Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 60 | |
| 61 | """ |
| 62 | if cmdline_override is not None: |
| 63 | return cmdline_override |
Cheng-Yi Chiang | 2261286 | 2015-08-20 20:39:57 +0800 | [diff] [blame] | 64 | if is_ip_address(client_hostname): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 65 | logging.error('%r looks like an IP address?', client_hostname) |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 66 | if allow_failure: |
| 67 | return None |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 68 | raise error.TestError(not_dnsname_msg) |
| 69 | parts = client_hostname.split('.', 1) |
| 70 | parts[0] = parts[0] + suffix |
| 71 | return '.'.join(parts) |
| 72 | |
| 73 | |
| 74 | def 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 Mahadev | b8812f4 | 2016-04-21 15:59:11 -0700 | [diff] [blame] | 92 | def 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 Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 110 | def get_attenuator_addr(client_hostname, |
| 111 | cmdline_override=None, |
| 112 | allow_failure=False): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 113 | """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 Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 120 | @param allow_failure: boolean True iff we should return None on failure to |
| 121 | infer a DNS name. |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 122 | @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 Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 129 | not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE, |
| 130 | allow_failure=allow_failure) |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 131 | |
| 132 | |
| 133 | def 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) |