13 MY_DIRNAME = os.path.dirname(os.path.realpath(__file__))
14 if __name__ == '__main__' and os.path.exists(
15 os.path.join(MY_DIRNAME, '..', 'arvados', '__init__.py')):
16 # We're being launched to support another test suite.
17 # Add the Python SDK source to the library path.
18 sys.path.insert(1, os.path.dirname(MY_DIRNAME))
23 SERVICES_SRC_DIR = os.path.join(MY_DIRNAME, '../../../services')
24 SERVER_PID_PATH = 'tmp/pids/webrick-test.pid'
25 WEBSOCKETS_SERVER_PID_PATH = 'tmp/pids/passenger-test.pid'
26 os.environ['PATH'] = os.environ['GOPATH'] + '/bin:' + os.environ['PATH']
28 def find_server_pid(PID_PATH, wait=10):
32 while (not good_pid) and (now <= timeout):
35 with open(PID_PATH, 'r') as f:
36 server_pid = int(f.read())
37 good_pid = (os.kill(server_pid, 0) == None)
49 def kill_server_pid(PID_PATH, wait=10):
53 with open(PID_PATH, 'r') as f:
54 server_pid = int(f.read())
56 os.kill(server_pid, signal.SIGTERM) == None
57 os.getpgid(server_pid) # throw OSError if no such pid
65 def run(websockets=False, reuse_server=False):
67 os.chdir(os.path.join(SERVICES_SRC_DIR, 'api'))
70 pid_file = WEBSOCKETS_SERVER_PID_PATH
72 pid_file = SERVER_PID_PATH
74 test_pid = find_server_pid(pid_file, 0)
76 if test_pid == None or not reuse_server:
77 # do not try to run both server variants at once
80 # delete cached discovery document
81 shutil.rmtree(arvados.http_cache('discovery'))
84 os.environ["RAILS_ENV"] = "test"
85 subprocess.call(['bundle', 'exec', 'rake', 'tmp:cache:clear'])
86 subprocess.call(['bundle', 'exec', 'rake', 'db:test:load'])
87 subprocess.call(['bundle', 'exec', 'rake', 'db:fixtures:load'])
90 os.environ["ARVADOS_WEBSOCKETS"] = "true"
91 subprocess.call(['openssl', 'req', '-new', '-x509', '-nodes',
92 '-out', './self-signed.pem',
93 '-keyout', './self-signed.key',
95 '-subj', '/CN=localhost'])
96 subprocess.call(['bundle', 'exec',
97 'passenger', 'start', '-d', '-p3333',
99 os.path.join(os.getcwd(), WEBSOCKETS_SERVER_PID_PATH),
101 '--ssl-certificate', 'self-signed.pem',
102 '--ssl-certificate-key', 'self-signed.key'])
103 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3333"
105 subprocess.call(['bundle', 'exec', 'rails', 'server', '-d',
107 os.path.join(os.getcwd(), SERVER_PID_PATH),
109 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
111 pid = find_server_pid(SERVER_PID_PATH)
113 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
114 os.environ["ARVADOS_API_TOKEN"] = ""
119 os.chdir(os.path.join(SERVICES_SRC_DIR, 'api'))
121 kill_server_pid(WEBSOCKETS_SERVER_PID_PATH, 0)
122 kill_server_pid(SERVER_PID_PATH, 0)
125 os.unlink('self-signed.pem')
130 os.unlink('self-signed.key')
136 def _start_keep(n, keep_args):
137 keep0 = tempfile.mkdtemp()
138 keep_cmd = ["keepstore",
139 "-volumes={}".format(keep0),
140 "-listen=:{}".format(25107+n),
141 "-pid={}".format("tmp/keep{}.pid".format(n))]
143 for arg, val in keep_args.iteritems():
144 keep_cmd.append("{}={}".format(arg, val))
146 kp0 = subprocess.Popen(keep_cmd)
147 with open("tmp/keep{}.pid".format(n), 'w') as f:
148 f.write(str(kp0.pid))
150 with open("tmp/keep{}.volume".format(n), 'w') as f:
153 def run_keep(blob_signing_key=None, enforce_permissions=False):
156 if not os.path.exists("tmp"):
161 with open("tmp/keep.blob_signing_key", "w") as f:
162 f.write(blob_signing_key)
163 keep_args['--permission-key-file'] = 'tmp/keep.blob_signing_key'
164 if enforce_permissions:
165 keep_args['--enforce-permissions'] = 'true'
167 _start_keep(0, keep_args)
168 _start_keep(1, keep_args)
170 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
171 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
173 authorize_with("admin")
174 api = arvados.api('v1', cache=False)
175 for d in api.keep_services().list().execute()['items']:
176 api.keep_services().delete(uuid=d['uuid']).execute()
177 for d in api.keep_disks().list().execute()['items']:
178 api.keep_disks().delete(uuid=d['uuid']).execute()
180 s1 = api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25107, "service_type": "disk"} }).execute()
181 s2 = api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25108, "service_type": "disk"} }).execute()
182 api.keep_disks().create(body={"keep_disk": {"keep_service_uuid": s1["uuid"] } }).execute()
183 api.keep_disks().create(body={"keep_disk": {"keep_service_uuid": s2["uuid"] } }).execute()
186 kill_server_pid("tmp/keep{}.pid".format(n), 0)
187 if os.path.exists("tmp/keep{}.volume".format(n)):
188 with open("tmp/keep{}.volume".format(n), 'r') as r:
189 shutil.rmtree(r.read(), True)
190 os.unlink("tmp/keep{}.volume".format(n))
191 if os.path.exists("tmp/keep.blob_signing_key"):
192 os.remove("tmp/keep.blob_signing_key")
198 def run_keep_proxy(auth):
201 if not os.path.exists("tmp"):
204 os.environ["ARVADOS_API_HOST"] = "127.0.0.1:3001"
205 os.environ["ARVADOS_API_HOST_INSECURE"] = "true"
206 os.environ["ARVADOS_API_TOKEN"] = fixture("api_client_authorizations")[auth]["api_token"]
208 kp0 = subprocess.Popen(["keepproxy",
209 "-pid=tmp/keepproxy.pid", "-listen=:{}".format(25101)])
211 authorize_with("admin")
212 api = arvados.api('v1', cache=False)
213 api.keep_services().create(body={"keep_service": {"service_host": "localhost", "service_port": 25101, "service_type": "proxy"} }).execute()
215 arvados.config.settings()["ARVADOS_KEEP_PROXY"] = "http://localhost:25101"
217 def stop_keep_proxy():
218 kill_server_pid("tmp/keepproxy.pid", 0)
221 '''load a fixture yaml file'''
222 with open(os.path.join(SERVICES_SRC_DIR, 'api', "test", "fixtures",
224 return yaml.load(f.read())
226 def authorize_with(token):
227 '''token is the symbolic name of the token from the api_client_authorizations fixture'''
228 arvados.config.settings()["ARVADOS_API_TOKEN"] = fixture("api_client_authorizations")[token]["api_token"]
229 arvados.config.settings()["ARVADOS_API_HOST"] = os.environ.get("ARVADOS_API_HOST")
230 arvados.config.settings()["ARVADOS_API_HOST_INSECURE"] = "true"
232 if __name__ == "__main__":
233 parser = argparse.ArgumentParser()
234 parser.add_argument('action', type=str, help='''one of "start", "stop", "start_keep", "stop_keep"''')
235 parser.add_argument('--websockets', action='store_true', default=False)
236 parser.add_argument('--reuse', action='store_true', default=False)
237 parser.add_argument('--auth', type=str, help='Print authorization info for given api_client_authorizations fixture')
238 args = parser.parse_args()
240 if args.action == 'start':
241 run(websockets=args.websockets, reuse_server=args.reuse)
242 if args.auth != None:
243 authorize_with(args.auth)
244 print("export ARVADOS_API_HOST={}".format(arvados.config.settings()["ARVADOS_API_HOST"]))
245 print("export ARVADOS_API_TOKEN={}".format(arvados.config.settings()["ARVADOS_API_TOKEN"]))
246 print("export ARVADOS_API_HOST_INSECURE={}".format(arvados.config.settings()["ARVADOS_API_HOST_INSECURE"]))
247 elif args.action == 'stop':
249 elif args.action == 'start_keep':
251 elif args.action == 'stop_keep':
253 elif args.action == 'start_keep_proxy':
254 run_keep_proxy("admin")
255 elif args.action == 'stop_keep_proxy':
258 print('Unrecognized action "{}", actions are "start", "stop", "start_keep", "stop_keep"'.format(args.action))