Derek Beckett | 8affba0 | 2020-10-20 14:24:18 -0700 | [diff] [blame] | 1 | # Lint as: python2, python3 |
Alex Khouderchah | 7d763ab | 2019-07-25 12:22:20 -0700 | [diff] [blame] | 2 | # Copyright (c) 2019 The Chromium OS Authors. All rights reserved. |
David Rochberg | 1068c20 | 2011-05-18 15:42:03 -0400 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 6 | import logging |
Derek Beckett | 8affba0 | 2020-10-20 14:24:18 -0700 | [diff] [blame] | 7 | from six.moves import urllib |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 8 | import socket |
| 9 | import time |
David Rochberg | 65a34fc | 2011-12-29 17:39:13 -0500 | [diff] [blame] | 10 | |
Alex Khouderchah | 7d763ab | 2019-07-25 12:22:20 -0700 | [diff] [blame] | 11 | from autotest_lib.client.bin import utils |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import error |
David Rochberg | c1b3240 | 2011-05-24 16:51:48 -0400 | [diff] [blame] | 13 | |
David Rochberg | 6eef6cc | 2011-10-31 10:19:06 -0400 | [diff] [blame] | 14 | |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 15 | def CheckThatInterfaceCanAccessDestination(host, |
| 16 | interface, |
| 17 | families=[socket.AF_UNSPEC]): |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 18 | """ |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 19 | Checks that we can access a host using a specific interface. |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 20 | |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 21 | @param host: Destination host |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 22 | @param interface: Name of the network interface to be used |
| 23 | @raises: error.TestFail if the interface cannot access the specified host. |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 24 | |
| 25 | """ |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 26 | logging.debug('Check connection to %s', host) |
Alex Khouderchah | 7d763ab | 2019-07-25 12:22:20 -0700 | [diff] [blame] | 27 | # addrinfo records: (family, type, proto, canonname, (addr, port)) |
Eric Caruso | cd26f14 | 2021-06-01 11:57:32 -0700 | [diff] [blame] | 28 | server_addresses = [] |
| 29 | for family in families: |
| 30 | try: |
| 31 | records = socket.getaddrinfo(host, 80, family) |
| 32 | except: |
| 33 | # Just ignore this family. |
| 34 | continue |
| 35 | server_addresses.extend(record[4][0] for record in records) |
| 36 | |
Eric Caruso | fbd7081 | 2021-01-20 07:21:39 -0800 | [diff] [blame] | 37 | found_route = False |
| 38 | failing_addresses = [] |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 39 | for address in set(server_addresses): |
Alex Khouderchah | 7d763ab | 2019-07-25 12:22:20 -0700 | [diff] [blame] | 40 | # Routes may not always be up by this point. Note that routes for v4 or |
| 41 | # v6 may come up before the other, so we simply do this poll for all |
| 42 | # addresses. |
Eric Caruso | fbd7081 | 2021-01-20 07:21:39 -0800 | [diff] [blame] | 43 | try: |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 44 | utils.poll_for_condition(condition=lambda: utils.ping( |
| 45 | address, interface=interface, tries=2, timeout=2) == 0, |
| 46 | exception=Exception('No route to %s' % |
| 47 | address), |
| 48 | timeout=2) |
Eric Caruso | fbd7081 | 2021-01-20 07:21:39 -0800 | [diff] [blame] | 49 | except Exception as e: |
| 50 | logging.info(e) |
| 51 | failing_addresses.append(address) |
| 52 | else: |
| 53 | found_route = True |
| 54 | |
| 55 | if not found_route: |
Eric Caruso | 252879e | 2021-06-17 08:07:59 -0700 | [diff] [blame] | 56 | raise error.TestFail('Interface %s cannot connect to %s' % (interface, |
| 57 | failing_addresses)) |
Andrew Lassalle | 56a2a99 | 2021-06-04 16:34:17 -0700 | [diff] [blame] | 58 | |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 59 | |
| 60 | FETCH_URL_PATTERN_FOR_TEST = \ |
| 61 | 'http://testing-chargen.appspot.com/download?size=%d' |
| 62 | |
| 63 | def FetchUrl(url_pattern, bytes_to_fetch=10, fetch_timeout=10): |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 64 | """ |
| 65 | Fetches a specified number of bytes from a URL. |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 66 | |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 67 | @param url_pattern: URL pattern for fetching a specified number of bytes. |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 68 | %d in the pattern is to be filled in with the number of bytes to |
| 69 | fetch. |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 70 | @param bytes_to_fetch: Number of bytes to fetch. |
| 71 | @param fetch_timeout: Number of seconds to wait for the fetch to complete |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 72 | before it times out. |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 73 | @return: The time in seconds spent for fetching the specified number of |
| 74 | bytes. |
| 75 | @raises: error.TestError if one of the following happens: |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 76 | - The fetch takes no time. |
Thieu Le | 0f2a00d | 2013-05-30 13:43:55 -0700 | [diff] [blame] | 77 | - The number of bytes fetched differs from the specified |
| 78 | number. |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 79 | |
| 80 | """ |
Thieu Le | 6369a1d | 2014-04-23 14:31:36 -0700 | [diff] [blame] | 81 | # Limit the amount of bytes to read at a time. |
| 82 | _MAX_FETCH_READ_BYTES = 1024 * 1024 |
| 83 | |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 84 | url = url_pattern % bytes_to_fetch |
| 85 | logging.info('FetchUrl %s', url) |
| 86 | start_time = time.time() |
Derek Beckett | 8affba0 | 2020-10-20 14:24:18 -0700 | [diff] [blame] | 87 | result = urllib.request.urlopen(url, timeout=fetch_timeout) |
Thieu Le | 6369a1d | 2014-04-23 14:31:36 -0700 | [diff] [blame] | 88 | bytes_fetched = 0 |
| 89 | while bytes_fetched < bytes_to_fetch: |
| 90 | bytes_left = bytes_to_fetch - bytes_fetched |
| 91 | bytes_to_read = min(bytes_left, _MAX_FETCH_READ_BYTES) |
| 92 | bytes_read = len(result.read(bytes_to_read)) |
| 93 | bytes_fetched += bytes_read |
| 94 | if bytes_read != bytes_to_read: |
| 95 | raise error.TestError('FetchUrl tried to read %d bytes, but got ' |
| 96 | '%d bytes instead.' % |
| 97 | (bytes_to_read, bytes_read)) |
| 98 | fetch_time = time.time() - start_time |
| 99 | if fetch_time > fetch_timeout: |
| 100 | raise error.TestError('FetchUrl exceeded timeout.') |
Ben Chan | 89216c6 | 2013-05-07 15:55:15 -0700 | [diff] [blame] | 101 | |
| 102 | return fetch_time |