14 MY_DIRNAME = os.path.dirname(os.path.realpath(__file__))
15 if __name__ == '__main__' and os.path.exists(
16 os.path.join(MY_DIRNAME, '..', 'arvados', '__init__.py')):
17 # We're being launched to support another test suite.
18 # Add the Python SDK source to the library path.
19 sys.path.insert(1, os.path.dirname(MY_DIRNAME))
24 SERVICES_SRC_DIR = os.path.join(MY_DIRNAME, '../../../services')
25 SERVER_PID_PATH = 'tmp/pids/webrick-test.pid'
26 WEBSOCKETS_SERVER_PID_PATH = 'tmp/pids/passenger-test.pid'
27 if 'GOPATH' in os.environ:
28 gopaths = os.environ['GOPATH'].split(':')
29 gobins = [os.path.join(path, 'bin') for path in gopaths]
30 os.environ['PATH'] = ':'.join(gobins) + ':' + os.environ['PATH']
32 def find_server_pid(PID_PATH, wait=10):
36 while (not good_pid) and (now <= timeout):
39 with open(PID_PATH, 'r') as f:
40 server_pid = int(f.read())
41 good_pid = (os.kill(server_pid, 0) == None)
53 def kill_server_pid(PID_PATH, wait=10):
57 with open(PID_PATH, 'r') as f:
58 server_pid = int(f.read())
60 os.kill(server_pid, signal.SIGTERM) == None
61 os.getpgid(server_pid) # throw OSError if no such pid
69 def run(websockets=False, reuse_server=False):
71 os.chdir(os.path.join(SERVICES_SRC_DIR, 'api'))
74 pid_file = WEBSOCKETS_SERVER_PID_PATH
76 pid_file = SERVER_PID_PATH
78 test_pid = find_server_pid(pid_file, 0)
80 if test_pid == None or not reuse_server:
81 # do not try to run both server variants at once
84 # delete cached discovery document
85 shutil.rmtree(arvados.http_cache('discovery'))
88 os.environ["RAILS_ENV"] = "test"
89 subprocess.call(['bundle', 'exec', 'rake', 'tmp:cache:clear'])
90 subprocess.call(['bundle', 'exec', 'rake', 'db:test:load'])
91 subprocess.call(['bundle', 'exec', 'rake', 'db:fixtures:load'])
94 os.environ["ARVADOS_WEBSOCKETS"] = "true"
95 subprocess.call(['openssl', 'req', '-new', '-x509', '-nodes',
96 '-out', './self-signed.pem',
97 '-keyout', './self-signed.key',
99 '-subj', '/CN=localhost'])
100 subprocess.call(['bundle', 'exec',
101 'passenger', 'start', '-d', '-p3333',
103 os.path.join(os.getcwd(), WEBSOCKETS_SERVER_PID_PATH),
105 '--ssl-certificate', 'self-signed.pem',
106 '--ssl-certificate-key', 'self-signed.key'])
107 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3333"
109 subprocess.call(['bundle', 'exec', 'rails', 'server', '-d',
111 os.path.join(os.getcwd(), SERVER_PID_PATH),
113 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
115 pid = find_server_pid(SERVER_PID_PATH)
117 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
118 os.environ["ARVADOS_API_TOKEN"] = ""
123 os.chdir(os.path.join(SERVICES_SRC_DIR, 'api'))
125 kill_server_pid(WEBSOCKETS_SERVER_PID_PATH, 0)
126 kill_server_pid(SERVER_PID_PATH, 0)
129 os.unlink('self-signed.pem')
134 os.unlink('self-signed.key')
140 def _start_keep(n, keep_args):
141 keep0 = tempfile.mkdtemp()
142 keep_cmd = ["keepstore",
143 "-volumes={}".format(keep0),
144 "-listen=:{}".format(25107+n),
145 "-pid={}".format("tests/tmp/keep{}.pid".format(n))]
147 for arg, val in keep_args.iteritems():
148 keep_cmd.append("{}={}".format(arg, val))
150 kp0 = subprocess.Popen(keep_cmd)
151 with open("tests/tmp/keep{}.pid".format(n), 'w') as f:
152 f.write(str(kp0.pid))
154 with open("tests/tmp/keep{}.volume".format(n), 'w') as f:
157 def run_keep(blob_signing_key=None, enforce_permissions=False):
160 if not os.path.exists("tests/tmp"):
161 os.mkdir("tests/tmp")
165 with open("tests/tmp/keep.blob_signing_key", "w") as f:
166 f.write(blob_signing_key)
167 keep_args['--permission-key-file'] = 'tests/tmp/keep.blob_signing_key'
168 if enforce_permissions:
169 keep_args['--enforce-permissions'] = 'true'
171 _start_keep(0, keep_args)
172 _start_keep(1, keep_args)
174 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
175 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
177 authorize_with("admin")
178 api = arvados.api('v1', cache=False)
179 for d in api.keep_services().list().execute()['items']:
180 api.keep_services().delete(uuid=d['uuid']).execute()
181 for d in api.keep_disks().list().execute()['items']:
182 api.keep_disks().delete(uuid=d['uuid']).execute()
184 s1 = api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25107, "service_type": "disk"} }).execute()
185 s2 = api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25108, "service_type": "disk"} }).execute()
186 api.keep_disks().create(body={"keep_disk": {"keep_service_uuid": s1["uuid"] } }).execute()
187 api.keep_disks().create(body={"keep_disk": {"keep_service_uuid": s2["uuid"] } }).execute()
190 kill_server_pid("tests/tmp/keep{}.pid".format(n), 0)
191 if os.path.exists("tests/tmp/keep{}.volume".format(n)):
192 with open("tests/tmp/keep{}.volume".format(n), 'r') as r:
193 shutil.rmtree(r.read(), True)
194 os.unlink("tests/tmp/keep{}.volume".format(n))
195 if os.path.exists("tests/tmp/keep.blob_signing_key"):
196 os.remove("tests/tmp/keep.blob_signing_key")
202 def run_keep_proxy(auth):
205 if not os.path.exists("tests/tmp"):
206 os.mkdir("tests/tmp")
208 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
209 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
210 os.environ["ARVADOS_API_TOKEN"] = fixture("api_client_authorizations")[auth]["api_token"]
212 kp0 = subprocess.Popen(["keepproxy",
213 "-pid=tests/tmp/keepproxy.pid",
214 "-listen=:{}".format(25101)])
216 authorize_with("admin")
217 api = arvados.api('v1', cache=False)
218 api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25101, "service_type": "proxy"} }).execute()
220 os.environ["ARVADOS_KEEP_PROXY"] = "http://localhost:25101"
222 def stop_keep_proxy():
223 kill_server_pid("tests/tmp/keepproxy.pid", 0)
226 '''load a fixture yaml file'''
227 with open(os.path.join(SERVICES_SRC_DIR, 'api', "test", "fixtures",
229 return yaml.load(f.read())
231 def authorize_with(token):
232 '''token is the symbolic name of the token from the api_client_authorizations fixture'''
233 arvados.config.settings()["ARVADOS_API_TOKEN"] = fixture("api_client_authorizations")[token]["api_token"]
234 arvados.config.settings()["ARVADOS_API_HOST"] = os.environ.get("ARVADOS_API_HOST")
235 arvados.config.settings()["ARVADOS_API_HOST_INSECURE"] = "true"
237 class TestCaseWithServers(unittest.TestCase):
238 """TestCase to start and stop supporting Arvados servers.
240 Define any of MAIN_SERVER, KEEP_SERVER, and/or KEEP_PROXY_SERVER
241 class variables as a dictionary of keyword arguments. If you do,
242 setUpClass will start the corresponding servers by passing these
243 keyword arguments to the run, run_keep, and/or run_keep_server
244 functions, respectively. It will also set Arvados environment
245 variables to point to these servers appropriately. If you don't
246 run a Keep or Keep proxy server, setUpClass will set up a
247 temporary directory for Keep local storage, and set it as
250 tearDownClass will stop any servers started, and restore the
251 original environment.
255 KEEP_PROXY_SERVER = None
258 def _restore_dict(src, dest):
259 for key in dest.keys():
266 cls._orig_environ = os.environ.copy()
267 cls._orig_config = arvados.config.settings().copy()
268 cls._cleanup_funcs = []
269 for server_kwargs, start_func, stop_func in (
270 (cls.MAIN_SERVER, run, stop),
271 (cls.KEEP_SERVER, run_keep, stop_keep),
272 (cls.KEEP_PROXY_SERVER, run_keep_proxy, stop_keep_proxy)):
273 if server_kwargs is not None:
274 start_func(**server_kwargs)
275 cls._cleanup_funcs.append(stop_func)
276 os.environ.pop('ARVADOS_EXTERNAL_CLIENT', None)
277 if cls.KEEP_PROXY_SERVER is None:
278 os.environ.pop('ARVADOS_KEEP_PROXY', None)
279 if (cls.KEEP_SERVER is None) and (cls.KEEP_PROXY_SERVER is None):
280 cls.local_store = tempfile.mkdtemp()
281 os.environ['KEEP_LOCAL_STORE'] = cls.local_store
282 cls._cleanup_funcs.append(
283 lambda: shutil.rmtree(cls.local_store, ignore_errors=True))
285 os.environ.pop('KEEP_LOCAL_STORE', None)
286 arvados.config.initialize()
289 def tearDownClass(cls):
290 for clean_func in cls._cleanup_funcs:
292 cls._restore_dict(cls._orig_environ, os.environ)
293 cls._restore_dict(cls._orig_config, arvados.config.settings())
296 if __name__ == "__main__":
297 parser = argparse.ArgumentParser()
298 parser.add_argument('action', type=str, help='''one of "start", "stop", "start_keep", "stop_keep"''')
299 parser.add_argument('--websockets', action='store_true', default=False)
300 parser.add_argument('--reuse', action='store_true', default=False)
301 parser.add_argument('--auth', type=str, help='Print authorization info for given api_client_authorizations fixture')
302 args = parser.parse_args()
304 if args.action == 'start':
305 run(websockets=args.websockets, reuse_server=args.reuse)
306 if args.auth != None:
307 authorize_with(args.auth)
308 print("export ARVADOS_API_HOST={}".format(arvados.config.settings()["ARVADOS_API_HOST"]))
309 print("export ARVADOS_API_TOKEN={}".format(arvados.config.settings()["ARVADOS_API_TOKEN"]))
310 print("export ARVADOS_API_HOST_INSECURE={}".format(arvados.config.settings()["ARVADOS_API_HOST_INSECURE"]))
311 elif args.action == 'stop':
313 elif args.action == 'start_keep':
315 elif args.action == 'stop_keep':
317 elif args.action == 'start_keep_proxy':
318 run_keep_proxy("admin")
319 elif args.action == 'stop_keep_proxy':
322 print('Unrecognized action "{}", actions are "start", "stop", "start_keep", "stop_keep"'.format(args.action))