-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
78 lines (66 loc) · 2.62 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import unittest
import subprocess
import requests
import re
import time
import argparse
def get_port():
# Looks up port number for container name given in argv
port_mapping = subprocess.check_output(
['docker', 'port', get_args().container_name]
).decode('utf-8').strip()
return re.search(':(\d+)', port_mapping).group(1)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--container_name', action='store')
parser.add_argument('--skip_mounted', action='store_true')
parser.add_argument('--skip_envvar_value', action='store_true')
parser.add_argument('--skip_envvar_url', action='store_true')
return parser.parse_args()
class ContainerTest(unittest.TestCase):
def setUp(self):
self.base = 'http://localhost:' + get_port()
# TODO: This didn't work on Travis... should it?
# session = requests.Session()
# adapter = requests.adapters.HTTPAdapter(max_retries=10) # default 0
# session.mount('http://', adapter)
for i in range(5):
try:
requests.get(self.base)
break
except:
print('Still waiting for server...')
time.sleep(1)
else:
self.fail('Server never came up')
def test_home_page(self):
response = requests.get(self.base)
self.assertEqual(response.status_code, 200)
self.assertIn('Tool Launch Data', response.text)
@unittest.skipIf(get_args().skip_mounted, 'CLI param skip')
def test_mounted_json(self):
response = requests.get(self.base + '/data/input.json')
self.assertEqual(response.status_code, 200)
self.assertIn('"Nils"', response.text)
@unittest.skipIf(get_args().skip_envvar_value, 'CLI param skip')
def test_envvar_value_json(self):
response = requests.get(self.base + '/envvar_value.json')
self.assertEqual(response.status_code, 200)
self.assertIn('"Chuck"', response.text)
@unittest.skipIf(get_args().skip_envvar_url, 'CLI param skip')
def test_envvar_url_json(self):
response = requests.get(self.base + '/envvar_url.json')
self.assertEqual(response.status_code, 200)
self.assertIn('"Scott"', response.text)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(ContainerTest)
result = unittest.TextTestRunner(verbosity=2).run(suite)
print('''
browse: http://localhost:{}/
clean up: docker ps -qa | xargs docker stop | xargs docker rm
'''.format(get_port()))
if result.wasSuccessful():
print('PASS!')
else:
print('FAIL!')
exit(1)