blob: 17592df2d390628680dac63462d7602be50e63ba [file] [log] [blame]
Alex Khouderchah7d763ab2019-07-25 12:22:20 -07001# Copyright (c) 2019 The Chromium OS Authors. All rights reserved.
David Rochberg1068c202011-05-18 15:42:03 -04002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Ben Chan89216c62013-05-07 15:55:15 -07005import logging
Alex Khouderchah7d763ab2019-07-25 12:22:20 -07006import re
Ben Chan89216c62013-05-07 15:55:15 -07007import socket
8import time
9import urllib2
David Rochberg65a34fc2011-12-29 17:39:13 -050010
Ben Chan89216c62013-05-07 15:55:15 -070011import common
12
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070013from autotest_lib.client.bin import utils
Ben Chan89216c62013-05-07 15:55:15 -070014from autotest_lib.client.common_lib import error
David Rochbergc1b32402011-05-24 16:51:48 -040015
David Rochberg6eef6cc2011-10-31 10:19:06 -040016
Ben Chan89216c62013-05-07 15:55:15 -070017def CheckInterfaceForDestination(host, expected_interface):
Thieu Le0f2a00d2013-05-30 13:43:55 -070018 """
19 Checks that routes for host go through a given interface.
Ben Chan89216c62013-05-07 15:55:15 -070020
21 The concern here is that our network setup may have gone wrong
22 and our test connections may go over some other network than
23 the one we're trying to test. So we take all the IP addresses
24 for the supplied host and make sure they go through the given
25 network interface.
26
Thieu Le0f2a00d2013-05-30 13:43:55 -070027 @param host: Destination host
28 @param expected_interface: Expected interface name
29 @raises: error.TestFail if the routes for the given host go through
Ben Chan89216c62013-05-07 15:55:15 -070030 a different interface than the expected one.
31
32 """
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070033 def _MatchesRoute(address, expected_interface):
34 """
35 Returns whether or not |expected_interface| is used to reach |address|.
Ben Chan89216c62013-05-07 15:55:15 -070036
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070037 @param address: string containing an IP (v4 or v6) address.
38 @param expected_interface: string containing an interface name.
Ben Chanbfd52842017-07-28 14:17:08 -070039
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070040 """
41 output = utils.run('ip route get %s' % address).stdout
Eric Caruso7407ed72017-07-17 11:13:59 -070042
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070043 if re.search(r'unreachable', output):
44 return False
45
46 match = re.search(r'\sdev\s(\S+)', output)
47 if match is None:
48 return False
49 interface = match.group(1)
50
Ben Chan89216c62013-05-07 15:55:15 -070051 logging.info('interface for %s: %s', address, interface)
52 if interface != expected_interface:
53 raise error.TestFail('Target server %s uses interface %s'
54 '(%s expected).' %
55 (address, interface, expected_interface))
56
Alex Khouderchah7d763ab2019-07-25 12:22:20 -070057 # addrinfo records: (family, type, proto, canonname, (addr, port))
58 server_addresses = [record[4][0]
59 for record in socket.getaddrinfo(host, 80)]
60 for address in server_addresses:
61 # Routes may not always be up by this point. Note that routes for v4 or
62 # v6 may come up before the other, so we simply do this poll for all
63 # addresses.
64 utils.poll_for_condition(
65 condition=lambda: _MatchesRoute(address, expected_interface),
66 exception=error.TestFail('No route to %s' % address),
67 timeout=1)
Ben Chan89216c62013-05-07 15:55:15 -070068
69FETCH_URL_PATTERN_FOR_TEST = \
70 'http://testing-chargen.appspot.com/download?size=%d'
71
72def FetchUrl(url_pattern, bytes_to_fetch=10, fetch_timeout=10):
Thieu Le0f2a00d2013-05-30 13:43:55 -070073 """
74 Fetches a specified number of bytes from a URL.
Ben Chan89216c62013-05-07 15:55:15 -070075
Thieu Le0f2a00d2013-05-30 13:43:55 -070076 @param url_pattern: URL pattern for fetching a specified number of bytes.
Ben Chan89216c62013-05-07 15:55:15 -070077 %d in the pattern is to be filled in with the number of bytes to
78 fetch.
Thieu Le0f2a00d2013-05-30 13:43:55 -070079 @param bytes_to_fetch: Number of bytes to fetch.
80 @param fetch_timeout: Number of seconds to wait for the fetch to complete
Ben Chan89216c62013-05-07 15:55:15 -070081 before it times out.
Thieu Le0f2a00d2013-05-30 13:43:55 -070082 @return: The time in seconds spent for fetching the specified number of
83 bytes.
84 @raises: error.TestError if one of the following happens:
Ben Chan89216c62013-05-07 15:55:15 -070085 - The fetch takes no time.
Thieu Le0f2a00d2013-05-30 13:43:55 -070086 - The number of bytes fetched differs from the specified
87 number.
Ben Chan89216c62013-05-07 15:55:15 -070088
89 """
Thieu Le6369a1d2014-04-23 14:31:36 -070090 # Limit the amount of bytes to read at a time.
91 _MAX_FETCH_READ_BYTES = 1024 * 1024
92
Ben Chan89216c62013-05-07 15:55:15 -070093 url = url_pattern % bytes_to_fetch
94 logging.info('FetchUrl %s', url)
95 start_time = time.time()
96 result = urllib2.urlopen(url, timeout=fetch_timeout)
Thieu Le6369a1d2014-04-23 14:31:36 -070097 bytes_fetched = 0
98 while bytes_fetched < bytes_to_fetch:
99 bytes_left = bytes_to_fetch - bytes_fetched
100 bytes_to_read = min(bytes_left, _MAX_FETCH_READ_BYTES)
101 bytes_read = len(result.read(bytes_to_read))
102 bytes_fetched += bytes_read
103 if bytes_read != bytes_to_read:
104 raise error.TestError('FetchUrl tried to read %d bytes, but got '
105 '%d bytes instead.' %
106 (bytes_to_read, bytes_read))
107 fetch_time = time.time() - start_time
108 if fetch_time > fetch_timeout:
109 raise error.TestError('FetchUrl exceeded timeout.')
Ben Chan89216c62013-05-07 15:55:15 -0700110
111 return fetch_time