Browse Source

Regression test for running Ambassador in Docker

Flynn 3 months ago
parent
commit
54db565de7
No account linked to committer's email address
2 changed files with 118 additions and 0 deletions
  1. 117
    0
      ambassador/tests/test_docker.py
  2. 1
    0
      pytest.ini

+ 117
- 0
ambassador/tests/test_docker.py View File

@@ -0,0 +1,117 @@
1
+import asyncio
2
+import os
3
+
4
+DockerImage = os.environ["AMBASSADOR_DOCKER_IMAGE"]
5
+
6
+@asyncio.coroutine
7
+def docker_ready():
8
+    cmd = [ 'docker', 'run', '--rm', '--name', 'ambassador', '-p8888:8080',
9
+            DockerImage, '--demo']
10
+
11
+    subproc_future = asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
12
+
13
+    subproc = yield from subproc_future
14
+    status = 0
15
+
16
+    while True:
17
+        data = yield from subproc.stdout.readline()
18
+        text = data.decode('utf-8').rstrip()
19
+
20
+        if not text:
21
+            print("Ambassador died?")
22
+            status = subproc.returncode
23
+
24
+            stdout, stderr = yield from subproc.communicate()
25
+
26
+            if stderr:
27
+                print("stderr:")
28
+                print(stderr.decode('utf-8').rstrip())
29
+
30
+            break
31
+
32
+        if text.startswith('AMBASSADOR'):
33
+            print(f'<-- {text}')
34
+
35
+        if text == 'AMBASSADOR DEMO RUNNING':
36
+            print("Done!!")
37
+            break
38
+
39
+    return status
40
+
41
+@asyncio.coroutine
42
+def docker_kill():
43
+    cmd = [ 'docker', 'kill', 'ambassador' ]
44
+
45
+    subproc_future = asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE)
46
+
47
+    subproc = yield from subproc_future
48
+
49
+    yield from subproc.communicate()
50
+
51
+async def do_http():
52
+    reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
53
+
54
+    query = (
55
+        "GET /qotm/?json=true HTTP/1.1\r\n"
56
+        "Host: localhost\r\n"
57
+        "Connection: close\r\n"
58
+        "\r\n"
59
+    )
60
+
61
+    writer.write(query.encode('utf-8'))
62
+
63
+    in_headers = True
64
+
65
+    while True:
66
+        line = await reader.readline()
67
+
68
+        if line:
69
+            line = line.decode('utf-8').rstrip()
70
+
71
+        if line:
72
+            if in_headers:
73
+                print(f'HTTP header> {line}')
74
+            else:
75
+                print(f'HTTP body> {line}')
76
+        else:
77
+            if not in_headers:
78
+                break
79
+            else:
80
+                in_headers = False
81
+
82
+    writer.close()
83
+
84
+async def asynchronicity():
85
+    ready = False
86
+    succeeded = False
87
+
88
+    try:
89
+        returncode = await asyncio.wait_for(docker_ready(), timeout=10.0)
90
+
91
+        if returncode == 0:
92
+            ready = True
93
+    except asyncio.TimeoutError:
94
+        print('timeout')
95
+
96
+    if ready:
97
+        try:
98
+            await do_http()
99
+            succeeded = True
100
+        except Exception as e:
101
+            print(f'Could not do HTTP: {e}')
102
+    else:
103
+        print("Not ready in time")
104
+
105
+    await asyncio.wait_for(docker_kill(), timeout=5.0)
106
+
107
+    assert succeeded
108
+
109
+def test_docker():
110
+    if not DockerImage:
111
+        assert False, f'You must set $AMBASSADOR_DOCKER_IMAGE'
112
+    else:
113
+        asyncio.run(asynchronicity())
114
+
115
+if __name__ == '__main__':
116
+    test_docker()
117
+

+ 1
- 0
pytest.ini View File

@@ -1,2 +1,3 @@
1 1
 [pytest]
2
+testpaths = ambassador
2 3
 norecursedirs = docs

Loading…
Cancel
Save