-
Notifications
You must be signed in to change notification settings - Fork 3
/
ct_run.py
executable file
·219 lines (179 loc) · 7 KB
/
ct_run.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
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
"""Runs integration tests."""
import argparse
import os
import platform
import sys
import subprocess
import time
import json
script_dir = os.path.dirname(os.path.realpath(__file__))
helpers_dir = os.path.join(script_dir, 'helpers')
docker_dir = os.path.join(helpers_dir, 'bamboos', 'docker')
sys.path.insert(0, docker_dir)
from environment import docker, dockers_config
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Run Common Tests.')
parser.add_argument(
'--gdb',
action='store_true',
default=False,
help='run tests in GDB')
parser.add_argument(
'--onenv-config',
action='store',
help='run tests in one-env environment',
dest='onenv_config')
parser.add_argument(
'--no-clean',
action='store_true',
default=False,
help='do not clean the one-env deployment after test',
dest='no_clean')
parser.add_argument(
'--image', '-i',
action='store',
default=None,
help='docker image to use as a test master',
dest='image')
parser.add_argument(
'--release',
action='store',
default='release',
help='release directory to run tests from',
dest='release')
parser.add_argument(
'--suite',
action='append',
default=[],
help='name of the test suite',
dest='suites')
parser.add_argument(
'--no-shed-privileges',
action='store_true',
default=False,
help='Run tests as root in container',
dest='no_shed_privileges')
[args, pass_args] = parser.parse_known_args()
dockers_config.ensure_image(args, 'image', 'builder')
script_dir = os.path.dirname(os.path.realpath(__file__))
if args.onenv_config is not None:
base_test_dir = os.path.join(os.path.realpath(args.release), 'test',
'onenv_tests')
else:
base_test_dir = os.path.join(os.path.realpath(args.release), 'test',
'integration')
test_dirs = map(lambda suite: os.path.join(base_test_dir, suite), args.suites)
if not test_dirs:
test_dirs = [base_test_dir]
envs={'BASE_TEST_DIR': base_test_dir,
'PYTHONWARNINGS': 'ignore:Unverified HTTPS request',
'BACKWARD_CXX_SOURCE_PREFIXES': os.path.join(script_dir, args.release)}
# Setup oneenv environment
if args.onenv_config is not None:
if not os.path.exists(args.onenv_config):
print(f'Error: No such one-env file {args.onenv_config}')
sys.exit(1)
try:
up_output = subprocess.check_output(['./one-env/onenv', 'up', args.onenv_config])
except subprocess.CalledProcessError as e:
print(f'Failed to start onenv up due to: {e.output}')
sys.exit(1)
environment_ready = False
retries = 3
while (not environment_ready) and retries > 0:
try:
subprocess.check_call(['./one-env/onenv', 'wait'])
environment_ready = True
except subprocess.CalledProcessError as e:
retries =- 1
time.sleep(5)
print(f'Waiting for one-env environment setup...')
if retries == 0:
print(f'Failed to start K8S environment from {args.onenv_config}')
sys.exit(1)
print(f'One-env environment ready')
# Get Onezone IP
get_onezoneip_cli = "kubectl get pod dev-onezone-0 --template {{.status.podIP}}"
onezone_ip = subprocess.check_output(get_onezoneip_cli.split(' ')).strip()
envs['ONEZONE_IP'] = onezone_ip.decode('utf-8')
# Get Oneprovider IP
get_oneproviderip_cli = "kubectl get pod dev-oneprovider-krakow-0 --template {{.status.podIP}}"
oneprovider_ip = subprocess.check_output(get_oneproviderip_cli.split(' ')).strip()
envs['ONEPROVIDER_IP'] = oneprovider_ip.decode('utf-8')
get_oneprovider2ip_cli = "kubectl get pod dev-oneprovider-paris-0 --template {{.status.podIP}}"
oneprovider_2_ip = subprocess.check_output(get_oneprovider2ip_cli.split(' ')).strip()
envs['ONEPROVIDER_2_IP'] = oneprovider_2_ip.decode('utf-8')
# Get Ceph storage IP
get_endpoints_cli = f'kubectl get endpoints -lcomponent=volume-ceph -o json'
endpoints = subprocess.check_output(get_endpoints_cli.split(' ')).strip()
for item in json.loads(endpoints)['items']:
if item['metadata']['name'] == 'dev-volume-ceph-krakow':
envs['CEPH_MONITOR_IP'] = item['subsets'][0]['addresses'][0]['ip']
break
if 'CEPH_MONITOR_IP' not in envs:
print(f'Error: Cannot find ceph storage volume pod in:\n {endpoints}')
sys.exit(1)
get_endpoints_cli = f'kubectl get endpoints -lcomponent=volume-s3 -o json'
endpoints = subprocess.check_output(get_endpoints_cli.split(' ')).strip()
for item in json.loads(endpoints)['items']:
if item['metadata']['name'] == 'dev-volume-s3-krakow':
envs['S3_SERVER_IP'] = item['subsets'][0]['addresses'][0]['ip']
break
if 'S3_SERVER_IP' not in envs:
print(f'Error: Cannot find s3 storage volume pod in:\n {endpoints}')
sys.exit(1)
print(f'Environment passed to pytest container: {str(envs)}')
command = '''
import os, subprocess, sys, stat, shutil
if {shed_privileges}:
os.environ['HOME'] = '/tmp'
docker_gid = os.stat('/var/run/docker.sock').st_gid
os.chmod('/etc/resolv.conf', 0o666)
os.setgroups([docker_gid])
os.setregid({gid}, {gid})
os.setreuid({uid}, {uid})
if {gdb}:
command = ['gdb', 'python3', '-silent', '-statistics', '-ex', """run -c "
import pytest
pytest.main({args} + ['{test_dirs}'])" """]
else:
command = ['python3'] + ['-m'] + ['pytest'] + {args} + ['{test_dirs}']
shutil.copyfile('{script_dir}/{release}/onedatafs_py3.so',
'{script_dir}/{release}/test/onenv_tests/onedatafs_test/onedatafs.so')
ret = subprocess.call(command)
sys.exit(ret)
'''
command = command.format(
args=pass_args,
uid=os.geteuid(),
gid=os.getegid(),
test_dirs="', '".join(test_dirs),
shed_privileges=(platform.system() == 'Linux') and not args.no_shed_privileges,
gdb=args.gdb,
script_dir=script_dir,
release=args.release)
add_hosts = {}
if args.onenv_config is not None:
add_hosts = {'dev-onezone.default.svc.cluster.local': onezone_ip.decode('utf-8'),
'dev-oneprovider-krakow.default.svc.cluster.local': oneprovider_ip.decode('utf-8'),
'dev-oneprovider-paris.default.svc.cluster.local': oneprovider_2_ip.decode('utf-8')}
ret = docker.run(tty=True,
rm=True,
interactive=True,
workdir=script_dir,
reflect=[(script_dir, 'rw'),
('/var/run/docker.sock', 'rw')],
image=args.image,
envs=envs,
add_host=add_hosts,
run_params=['--privileged'] if args.gdb or args.no_shed_privileges else [],
command=['python', '-c', command])
if not args.no_clean:
try:
up_output = subprocess.check_output(['./one-env/onenv', 'clean'])
except subprocess.CalledProcessError as e:
print(f'Failed to clean onenv due to: {e.output}')
sys.exit(1)
sys.exit(ret)