| 1 |
|
| 2 |
|
| 3 |
import filecmp |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import urllib.request |
| 7 |
import unittest |
| 8 |
import os.path |
| 9 |
|
| 10 |
TIMEOUT = 300 |
| 11 |
|
| 12 |
|
| 13 |
TEST_HOST = 'jsonplaceholder.typicode.com' |
| 14 |
|
| 15 |
|
| 16 |
elasticurl_cmd_prefix = sys.argv[1:] |
| 17 |
if not elasticurl_cmd_prefix: |
| 18 |
print('You must pass the elasticurl cmd prefix') |
| 19 |
sys.exit(-1) |
| 20 |
|
| 21 |
program_to_run = elasticurl_cmd_prefix[0] |
| 22 |
|
| 23 |
if 'bin' in program_to_run: |
| 24 |
if not os.path.exists(program_to_run): |
| 25 |
print(f'the {program_to_run} is not found, skip integration test') |
| 26 |
sys.exit(0) |
| 27 |
|
| 28 |
|
| 29 |
sys.argv = sys.argv[:1] |
| 30 |
|
| 31 |
|
| 32 |
def run_command(args): |
| 33 |
|
| 34 |
process = subprocess.Popen( |
| 35 |
args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 36 |
timedout = False |
| 37 |
try: |
| 38 |
output = process.communicate(timeout=TIMEOUT)[0] |
| 39 |
except subprocess.TimeoutExpired: |
| 40 |
timedout = True |
| 41 |
process.kill() |
| 42 |
args_str = subprocess.list2cmdline(args) |
| 43 |
output = process.communicate()[0] |
| 44 |
finally: |
| 45 |
if process.returncode != 0 or timedout: |
| 46 |
args_str = subprocess.list2cmdline(args) |
| 47 |
print(args_str) |
| 48 |
for line in output.splitlines(): |
| 49 |
print(line.decode()) |
| 50 |
if timedout: |
| 51 |
raise RuntimeError("Timeout happened after {secs} secs from: {cmd}".format( |
| 52 |
secs=TIMEOUT, cmd=args_str)) |
| 53 |
else: |
| 54 |
raise RuntimeError("Return code {code} from: {cmd}".format( |
| 55 |
code=process.returncode, cmd=args_str)) |
| 56 |
|
| 57 |
|
| 58 |
def compare_files(filename_expected, filename_other): |
| 59 |
if not filecmp.cmp(filename_expected, filename_other, shallow=False): |
| 60 |
|
| 61 |
with open(filename_expected, 'rb') as expected: |
| 62 |
try: |
| 63 |
bytes_expected = bytearray(expected.read()) |
| 64 |
except: |
| 65 |
raise RuntimeError("Failed to open %s" % filename_expected) |
| 66 |
|
| 67 |
with open(filename_other, 'rb') as other: |
| 68 |
try: |
| 69 |
bytes_other = bytearray(other.read()) |
| 70 |
except: |
| 71 |
raise RuntimeError("Failed to open %s" % filename_other) |
| 72 |
|
| 73 |
if len(bytes_expected) != len(bytes_other): |
| 74 |
raise RuntimeError("File lengths differ. Expected %d, got %d" % ( |
| 75 |
len(bytes_expected), len(bytes_other))) |
| 76 |
|
| 77 |
for i in range(len(bytes_expected)): |
| 78 |
if bytes_expected[i] != bytes_other[i]: |
| 79 |
raise RuntimeError("Files differ at byte[%d]. Expected %d, got %d." % ( |
| 80 |
i, bytes_expected[i], bytes_other[i])) |
| 81 |
|
| 82 |
print("filecmp says these files differ, but they are identical. what the heck.") |
| 83 |
|
| 84 |
|
| 85 |
class SimpleTests(unittest.TestCase): |
| 86 |
def test_simple_get_amazon(self): |
| 87 |
"""make a simple GET request via alpn h2;http/1.1 to amazon and make sure it succeeds""" |
| 88 |
simple_get_args = elasticurl_cmd_prefix + \ |
| 89 |
['-v', 'TRACE', 'https://www.amazon.com'] |
| 90 |
run_command(simple_get_args) |
| 91 |
|
| 92 |
def test_simple_get_google(self): |
| 93 |
"""make a simple GET request via alpn h2;http/1.1 to google and make sure it succeeds""" |
| 94 |
simple_get_args = elasticurl_cmd_prefix + \ |
| 95 |
['-v', 'TRACE', 'https://www.google.com'] |
| 96 |
run_command(simple_get_args) |
| 97 |
|
| 98 |
def test_simple_get_h1(self): |
| 99 |
"""make a simple GET request via HTTP/1.1 and make sure it succeeds""" |
| 100 |
simple_get_args = elasticurl_cmd_prefix + \ |
| 101 |
['-v', 'TRACE', '--http1_1', f'http://{TEST_HOST}/posts'] |
| 102 |
run_command(simple_get_args) |
| 103 |
|
| 104 |
def test_simple_post_h1(self): |
| 105 |
"""make a simple POST request via HTTP/1.1 to make sure sending data succeeds""" |
| 106 |
simple_post_args = elasticurl_cmd_prefix + ['-v', 'TRACE', '--http1_1', '-P', '-H', |
| 107 |
'content-type: application/json', '-i', '-d', '\"{\'test\':\'testval\'}\"', f'http://{TEST_HOST}/posts'] |
| 108 |
run_command(simple_post_args) |
| 109 |
|
| 110 |
def test_simple_download_h1(self): |
| 111 |
"""download a large file via HTTP/1.1 and compare the results with something we assume works (e.g. urllib)""" |
| 112 |
elasticurl_download_args = elasticurl_cmd_prefix + \ |
| 113 |
['-v', 'TRACE', '--http1_1', '-o', 'elastigirl.png', |
| 114 |
'https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png'] |
| 115 |
run_command(elasticurl_download_args) |
| 116 |
urllib.request.urlretrieve( |
| 117 |
'https://s3.amazonaws.com/code-sharing-aws-crt/elastigirl.png', 'elastigirl_expected.png') |
| 118 |
|
| 119 |
compare_files('elastigirl_expected.png', 'elastigirl.png') |
| 120 |
|
| 121 |
def test_simple_get_h2(self): |
| 122 |
"""make a simple GET request via HTTP2 and make sure it succeeds""" |
| 123 |
simple_get_args = elasticurl_cmd_prefix + \ |
| 124 |
['-v', 'TRACE', '--http2', f'https://{TEST_HOST}/posts'] |
| 125 |
run_command(simple_get_args) |
| 126 |
|
| 127 |
def test_simple_post_h2(self): |
| 128 |
"""make a simple POST request via HTTP2 to make sure sending data succeeds""" |
| 129 |
simple_post_args = elasticurl_cmd_prefix + ['-v', 'TRACE', '--http2', '-P', '-H', |
| 130 |
'content-type: application/json', '-i', '-d', '\"{\'test\':\'testval\'}\"', f'https://{TEST_HOST}/posts'] |
| 131 |
run_command(simple_post_args) |
| 132 |
|
| 133 |
def test_simple_download_h2(self): |
| 134 |
"""download a large file via HTTP2 and compare the results with something we assume works (e.g. urllib)""" |
| 135 |
elasticurl_download_args = elasticurl_cmd_prefix + \ |
| 136 |
['-v', 'TRACE', '--http2', '-o', 'elastigirl_h2.png', |
| 137 |
'https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png'] |
| 138 |
run_command(elasticurl_download_args) |
| 139 |
urllib.request.urlretrieve( |
| 140 |
'https://d1cz66xoahf9cl.cloudfront.net/elastigirl.png', 'elastigirl_expected.png') |
| 141 |
|
| 142 |
compare_files('elastigirl_expected.png', 'elastigirl_h2.png') |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == '__main__': |
| 146 |
unittest.main(verbosity=2) |