-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
208 lines (181 loc) · 6.6 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import requests
import jwt
import base64
import pickle
from pathlib import Path
from requests.exceptions import ConnectionError
test_results = {}
#
# CHECK ENVIRONMENT VARIALBES
#
print('--> Reading the environment variables')
print(f'ENVIRONMENT: {os.environ}')
test_results['environment'] = os.environ
#
# CHECK INPUT_FILE
#
try:
with open(os.environ['INPUT_FILE'], 'rb') as f:
print('--> Reading input file')
input_ = f.read()
print(f'INPUT FILE: {input_}')
test_results['READ_INPUT_FILE'] = {'Success': True}
except Exception as e:
print('x-> Reading input file failed')
test_results['READ_INPUT_FILE'] = {'Success': False, 'Exception': e}
killmsg = 'stop'
if pickle.loads(input_) == killmsg:
print('--> This is a subtask from the feature tester. Exiting.')
exit(0)
#
# CHECK OUTPUT FILE
#
try:
with open(os.environ['OUTPUT_FILE'], 'w') as f:
print('--> Writing to output file (contents: test)')
f.write('test')
with open(os.environ['OUTPUT_FILE'], 'r') as f:
print('--> Reading output file back and check')
print(f.read())
test_results['WRITE_READ_OUTPUT_FILE'] = {'Success': True}
except Exception as e:
print('x-> Reading or Writing output file failed')
test_results['WRITE_READ_OUTPUT_FILE'] = {'Success': False, 'Exception': e}
#
# CHECK TOKEN FILE
#
try:
with open(os.environ['TOKEN_FILE'], 'r') as f:
print('--> Reading token file')
token = f.read()
print(f'TOKEN: {token}')
test_results['READ_TOKEN_FILE'] = {'Success': True}
except Exception as e:
print('x-> Reading token file failed')
test_results['READ_TOKEN_FILE'] = {'Success': False, 'Exception': e}
#
# CHECK TEMPORARY VOLUME
#
print('--> Test temporary volume')
try:
temp_file = f'{os.environ["TEMPORARY_FOLDER"]}/test.txt'
with open(temp_file, 'w') as f:
print(f'--> Writing to temporary file: {temp_file}')
f.write('test')
test_results['TEMPORARY_VOLUME'] = {'Success': True}
except Exception as e:
print('x-> Writing to temporary folder failed')
test_results['TEMPORARY_VOLUME'] = {'Success': False, 'Exception': e}
print('--> Test that the temporary file is created')
try:
file_exists = Path(temp_file).exists()
print(f'FILE CREATED: {file_exists}')
test_results['TEMPORARY_VOLUME_FILE_EXISTS'] = {'Success': file_exists}
except Exception as e:
print('x-> Test temporary volume failed')
test_results['TEMPORARY_VOLUME_FILE_EXISTS'] = {
'Success': False, 'Exception': e
}
# --> Check that we can reach the local proxy
print('--> Test that we can reach the local proxy (and thereby the server)')
try:
host = os.environ['HOST']
port = os.environ['PORT']
response = requests.get(f'{host}:{port}/version')
ok = response.status_code == 200
test_results['LOCAL_PROXY_CENTRAL_SERVER'] = {'Success': ok}
except Exception as e:
print('x-> Using the local proxy failed')
test_results['LOCAL_PROXY_CENTRAL_SERVER'] = {
'Success': False, 'Exception': e
}
print('--> Test that we can create a subtask')
print(' Depending on the collaboration this test the encryption module')
try:
host = os.environ['HOST']
port = os.environ['PORT']
# obtain collaboration id which is stored in the token
identity = (jwt.decode(token, options={"verify_signature": False})['sub'])
response = requests.post(
f'{host}:{port}/task',
json={
'name': 'feature-tester-subtask',
'description': 'This task is initiated from the feature tester',
'image': identity.get('image'),
'collaboration_id': identity.get('collaboration_id'),
'organizations': [{
'id': identity.get('organization_id'),
'input':
# do not run any tests
base64.b64encode(pickle.dumps(killmsg)).decode('utf-8')
}],
'database': 'default'
},
headers={'Authorization': f'Bearer {token}'}
)
test_results['CREATE_SUB_TASK'] = response.json()
except Exception as e:
print('x-> Using the local proxy to create a task failed')
test_results['CREATE_SUB_TASK'] = {'Success': False, 'Exception': e}
# --> check that we cannot reach another address
print('--> Verify that the container has no internet connection')
try:
try:
response = requests.get('https://google.nl')
except ConnectionError:
print('--> Connection error caught')
# print(e)
test_results['ISOLATION_TEST'] = {'Success': ok}
except Exception as e:
print('x-> Testing an external connection failed...')
test_results['ISOLATION_TEST'] = {'Success': False, 'Exception': e}
#
# External port test
#
print('--> Check that two ports have been published')
if test_results['READ_TOKEN_FILE']['Success']:
try:
# obtain own task id
id_ = (jwt.decode(token, options={"verify_signature": False})['sub'])\
.get('task_id')
# port should be published as we are running this code.. So no
# need for polling
response = requests.get(
f'{host}:{port}/vpn/algorithm/addresses',
headers={'Authorization': 'Bearer ' + token},
params={'include_parent': True, 'include_children': True}
)
# we also assume that only a single task has been posted as we
# are not testing the connectivity between nodes yet
p5 = p8 = False
pU = True
result = response.json()
print('debug')
print(result)
print(f'--> Found {len(result["addresses"])} port(s)')
for addr in result['addresses']:
if addr['label'] == 'port5':
print(f'--> found "port5": {addr["port"]}')
p5 = True
elif addr['label'] == 'port8':
print(f'--> found "port8":{addr["port"]}')
p8 = True
else:
print('--> Found an unexpected port!')
pU = False
test_results['EXTERNAL_PORT_TEST'] = {'Success': all([p5, p8, pU])}
except Exception as e:
print('--> external port check failed')
test_results['EXTERNAL_PORT_TEST'] = {'Success': False, 'Exception': e}
# Only works for file based databases
print('--> Check attached databases exists')
for key in os.environ:
if key.endswith('_DATABASE_URI'):
print(f'--> Found database-path: {key}')
path_ = os.environ[key]
if Path(path_).exists():
print(f'--> database \'{key}\' is reachable: {path_}')
else:
print(f'--> database \'{key}\' is *not* reachable: {path_}')
print(test_results)