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 | |
| 10 | |
| 11 | # See server/cros/network/wifi_test_context_manager.py for commandline |
| 12 | # flags to control IP addresses in WiFi tests. |
| 13 | DEFAULT_FAILURE_MESSAGE = ( |
| 14 | 'Cannot infer DNS name of companion device from an IP address.') |
| 15 | ATTENUATOR_FAILURE_MESSAGE = ( |
| 16 | 'Cannot infer DNS name of WiFi variable attenuator from a client IP ' |
| 17 | 'address. Use --atten_addr=<ip or dns name>') |
| 18 | BLUETOOTH_TESTER_FAILURE_MESSAGE = ( |
| 19 | 'Remote host cannot be an IP address unless tester specified with ' |
| 20 | '--args tester=IP') |
| 21 | ROUTER_FAILURE_MESSAGE = ( |
| 22 | 'Cannot infer DNS name of WiFi router from a client IP address.') |
| 23 | |
| 24 | |
Cheng-Yi Chiang | 2261286 | 2015-08-20 20:39:57 +0800 | [diff] [blame] | 25 | def is_ip_address(hostname): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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 | |
| 39 | def get_companion_device_addr(client_hostname, |
| 40 | suffix, |
| 41 | cmdline_override=None, |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 42 | not_dnsname_msg=DEFAULT_FAILURE_MESSAGE, |
| 43 | allow_failure=False): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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. |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 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. |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 59 | |
| 60 | """ |
| 61 | if cmdline_override is not None: |
| 62 | return cmdline_override |
Cheng-Yi Chiang | 2261286 | 2015-08-20 20:39:57 +0800 | [diff] [blame] | 63 | if is_ip_address(client_hostname): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 64 | logging.error('%r looks like an IP address?', client_hostname) |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 65 | if allow_failure: |
| 66 | return None |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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 | |
| 73 | def 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 Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 91 | def get_attenuator_addr(client_hostname, |
| 92 | cmdline_override=None, |
| 93 | allow_failure=False): |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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. |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 101 | @param allow_failure: boolean True iff we should return None on failure to |
| 102 | infer a DNS name. |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 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, |
Christopher Wiley | 003bf82 | 2014-12-22 14:38:18 -0800 | [diff] [blame] | 110 | not_dnsname_msg=ATTENUATOR_FAILURE_MESSAGE, |
| 111 | allow_failure=allow_failure) |
Christopher Wiley | 6b1d9e7 | 2014-12-13 18:07:41 -0800 | [diff] [blame] | 112 | |
| 113 | |
| 114 | def 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) |