annotate toolfactory/galaxy-tool-test @ 0:83f8bb78781e draft

Uploaded
author fubar
date Fri, 11 Dec 2020 02:51:15 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
1 #!/usr/bin/env python
83f8bb78781e Uploaded
fubar
parents:
diff changeset
2
83f8bb78781e Uploaded
fubar
parents:
diff changeset
3 import argparse
83f8bb78781e Uploaded
fubar
parents:
diff changeset
4 import datetime as dt
83f8bb78781e Uploaded
fubar
parents:
diff changeset
5 import json
83f8bb78781e Uploaded
fubar
parents:
diff changeset
6 import logging
83f8bb78781e Uploaded
fubar
parents:
diff changeset
7 import os
83f8bb78781e Uploaded
fubar
parents:
diff changeset
8 import sys
83f8bb78781e Uploaded
fubar
parents:
diff changeset
9 import tempfile
83f8bb78781e Uploaded
fubar
parents:
diff changeset
10 from collections import namedtuple
83f8bb78781e Uploaded
fubar
parents:
diff changeset
11 from concurrent.futures import thread, ThreadPoolExecutor
83f8bb78781e Uploaded
fubar
parents:
diff changeset
12
83f8bb78781e Uploaded
fubar
parents:
diff changeset
13 import yaml
83f8bb78781e Uploaded
fubar
parents:
diff changeset
14
83f8bb78781e Uploaded
fubar
parents:
diff changeset
15 from galaxy.tool_util.verify.interactor import (
83f8bb78781e Uploaded
fubar
parents:
diff changeset
16 DictClientTestConfig,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
17 GalaxyInteractorApi,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
18 verify_tool,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
19 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
20
83f8bb78781e Uploaded
fubar
parents:
diff changeset
21 DESCRIPTION = """Script to quickly run a tool test against a running Galaxy instance."""
83f8bb78781e Uploaded
fubar
parents:
diff changeset
22 DEFAULT_SUITE_NAME = "Galaxy Tool Tests"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
23 ALL_TESTS = -1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
24 ALL_TOOLS = "*"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
25 ALL_VERSION = "*"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
26 LATEST_VERSION = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
27
83f8bb78781e Uploaded
fubar
parents:
diff changeset
28
83f8bb78781e Uploaded
fubar
parents:
diff changeset
29 TestReference = namedtuple("TestReference", ["tool_id", "tool_version", "test_index"])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
30 TestException = namedtuple("TestException", ["tool_id", "exception", "was_recorded"])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
31
83f8bb78781e Uploaded
fubar
parents:
diff changeset
32
83f8bb78781e Uploaded
fubar
parents:
diff changeset
33 class Results:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
34
83f8bb78781e Uploaded
fubar
parents:
diff changeset
35 def __init__(self, default_suitename, test_json, append=False):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
36 self.test_json = test_json or "-"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
37 test_results = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
38 test_exceptions = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
39 suitename = default_suitename
83f8bb78781e Uploaded
fubar
parents:
diff changeset
40 if append:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
41 assert test_json != "-"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
42 with open(test_json) as f:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
43 previous_results = json.load(f)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
44 test_results = previous_results["tests"]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
45 if "suitename" in previous_results:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
46 suitename = previous_results["suitename"]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
47 self.test_results = test_results
83f8bb78781e Uploaded
fubar
parents:
diff changeset
48 self.test_exceptions = test_exceptions
83f8bb78781e Uploaded
fubar
parents:
diff changeset
49 self.suitename = suitename
83f8bb78781e Uploaded
fubar
parents:
diff changeset
50
83f8bb78781e Uploaded
fubar
parents:
diff changeset
51 def register_result(self, result):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
52 self.test_results.append(result)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
53
83f8bb78781e Uploaded
fubar
parents:
diff changeset
54 def register_exception(self, test_exception):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
55 self.test_exceptions.append(test_exception)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
56
83f8bb78781e Uploaded
fubar
parents:
diff changeset
57 def already_successful(self, test_reference):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
58 test_id = _test_id_for_reference(test_reference)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
59 for test_result in self.test_results:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
60 if test_result.get('id') != test_id:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
61 continue
83f8bb78781e Uploaded
fubar
parents:
diff changeset
62
83f8bb78781e Uploaded
fubar
parents:
diff changeset
63 has_data = test_result.get('has_data', False)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
64 if has_data:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
65 test_data = test_result.get("data", {})
83f8bb78781e Uploaded
fubar
parents:
diff changeset
66 if 'status' in test_data and test_data['status'] == 'success':
83f8bb78781e Uploaded
fubar
parents:
diff changeset
67 return True
83f8bb78781e Uploaded
fubar
parents:
diff changeset
68
83f8bb78781e Uploaded
fubar
parents:
diff changeset
69 return False
83f8bb78781e Uploaded
fubar
parents:
diff changeset
70
83f8bb78781e Uploaded
fubar
parents:
diff changeset
71 def write(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
72 tests = sorted(self.test_results, key=lambda el: el['id'])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
73 n_passed, n_failures, n_skips = 0, 0, 0
83f8bb78781e Uploaded
fubar
parents:
diff changeset
74 n_errors = len([e for e in self.test_exceptions if not e.was_recorded])
83f8bb78781e Uploaded
fubar
parents:
diff changeset
75 for test in tests:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
76 has_data = test.get('has_data', False)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
77 if has_data:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
78 test_data = test.get("data", {})
83f8bb78781e Uploaded
fubar
parents:
diff changeset
79 if 'status' not in test_data:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
80 raise Exception(f"Test result data {test_data} doesn't contain a status key.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
81 status = test_data['status']
83f8bb78781e Uploaded
fubar
parents:
diff changeset
82 if status == "success":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
83 n_passed += 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
84 elif status == "error":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
85 n_errors += 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
86 elif status == "skip":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
87 n_skips += 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
88 elif status == "failure":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
89 n_failures += 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
90 report_obj = {
83f8bb78781e Uploaded
fubar
parents:
diff changeset
91 'version': '0.1',
83f8bb78781e Uploaded
fubar
parents:
diff changeset
92 'suitename': self.suitename,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
93 'results': {
83f8bb78781e Uploaded
fubar
parents:
diff changeset
94 'total': n_passed + n_failures + n_skips + n_errors,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
95 'errors': n_errors,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
96 'failures': n_failures,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
97 'skips': n_skips,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
98 },
83f8bb78781e Uploaded
fubar
parents:
diff changeset
99 'tests': tests,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
100 }
83f8bb78781e Uploaded
fubar
parents:
diff changeset
101 if self.test_json == "-":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
102 print(json.dumps(report_obj))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
103 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
104 with open(self.test_json, "w") as f:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
105 json.dump(report_obj, f)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
106
83f8bb78781e Uploaded
fubar
parents:
diff changeset
107 def info_message(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
108 messages = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
109 passed_tests = self._tests_with_status('success')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
110 messages.append("Passed tool tests ({}): {}".format(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
111 len(passed_tests),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
112 [t["id"] for t in passed_tests]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
113 ))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
114 failed_tests = self._tests_with_status('failure')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
115 messages.append("Failed tool tests ({}): {}".format(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
116 len(failed_tests),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
117 [t["id"] for t in failed_tests]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
118 ))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
119 skiped_tests = self._tests_with_status('skip')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
120 messages.append("Skipped tool tests ({}): {}".format(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
121 len(skiped_tests),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
122 [t["id"] for t in skiped_tests]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
123 ))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
124 errored_tests = self._tests_with_status('error')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
125 messages.append("Errored tool tests ({}): {}".format(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
126 len(errored_tests),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
127 [t["id"] for t in errored_tests]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
128 ))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
129 return "\n".join(messages)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
130
83f8bb78781e Uploaded
fubar
parents:
diff changeset
131 @property
83f8bb78781e Uploaded
fubar
parents:
diff changeset
132 def success_count(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
133 self._tests_with_status('success')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
134
83f8bb78781e Uploaded
fubar
parents:
diff changeset
135 @property
83f8bb78781e Uploaded
fubar
parents:
diff changeset
136 def skip_count(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
137 self._tests_with_status('skip')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
138
83f8bb78781e Uploaded
fubar
parents:
diff changeset
139 @property
83f8bb78781e Uploaded
fubar
parents:
diff changeset
140 def error_count(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
141 return self._tests_with_status('error') + len(self.test_exceptions)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
142
83f8bb78781e Uploaded
fubar
parents:
diff changeset
143 @property
83f8bb78781e Uploaded
fubar
parents:
diff changeset
144 def failure_count(self):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
145 return self._tests_with_status('failure')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
146
83f8bb78781e Uploaded
fubar
parents:
diff changeset
147 def _tests_with_status(self, status):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
148 return [t for t in self.test_results if t.get("data", {}).get("status") == status]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
149
83f8bb78781e Uploaded
fubar
parents:
diff changeset
150
83f8bb78781e Uploaded
fubar
parents:
diff changeset
151 def test_tools(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
152 galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
153 test_references,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
154 results,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
155 log=None,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
156 parallel_tests=1,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
157 history_per_test_case=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
158 no_history_cleanup=False,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
159 retries=0,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
160 verify_kwds=None,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
161 ):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
162 """Run through tool tests and write report.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
163
83f8bb78781e Uploaded
fubar
parents:
diff changeset
164 Refactor this into Galaxy in 21.01.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
165 """
83f8bb78781e Uploaded
fubar
parents:
diff changeset
166 verify_kwds = (verify_kwds or {}).copy()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
167 tool_test_start = dt.datetime.now()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
168 history_created = False
83f8bb78781e Uploaded
fubar
parents:
diff changeset
169 if history_per_test_case:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
170 test_history = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
171 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
172 history_created = True
83f8bb78781e Uploaded
fubar
parents:
diff changeset
173 test_history = galaxy_interactor.new_history(history_name=f"History for {results.suitename}")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
174 verify_kwds.update({
83f8bb78781e Uploaded
fubar
parents:
diff changeset
175 "no_history_cleanup": no_history_cleanup,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
176 "test_history": test_history,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
177 })
83f8bb78781e Uploaded
fubar
parents:
diff changeset
178 with ThreadPoolExecutor(max_workers=parallel_tests) as executor:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
179 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
180 for test_reference in test_references:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
181 _test_tool(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
182 executor=executor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
183 test_reference=test_reference,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
184 results=results,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
185 galaxy_interactor=galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
186 log=log,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
187 retries=retries,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
188 verify_kwds=verify_kwds,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
189 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
190 finally:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
191 # Always write report, even if test was cancelled.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
192 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
193 executor.shutdown(wait=True)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
194 except KeyboardInterrupt:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
195 executor._threads.clear()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
196 thread._threads_queues.clear()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
197 results.write()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
198 if log:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
199 log.info("Report written to '%s'", os.path.abspath(results.test_json))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
200 log.info(results.info_message())
83f8bb78781e Uploaded
fubar
parents:
diff changeset
201 log.info("Total tool test time: {}".format(dt.datetime.now() - tool_test_start))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
202 if history_created and not no_history_cleanup:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
203 galaxy_interactor.delete_history(test_history)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
204
83f8bb78781e Uploaded
fubar
parents:
diff changeset
205
83f8bb78781e Uploaded
fubar
parents:
diff changeset
206 def _test_id_for_reference(test_reference):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
207 tool_id = test_reference.tool_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
208 tool_version = test_reference.tool_version
83f8bb78781e Uploaded
fubar
parents:
diff changeset
209 test_index = test_reference.test_index
83f8bb78781e Uploaded
fubar
parents:
diff changeset
210
83f8bb78781e Uploaded
fubar
parents:
diff changeset
211 if tool_version and tool_id.endswith("/" + tool_version):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
212 tool_id = tool_id[:-len("/" + tool_version)]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
213
83f8bb78781e Uploaded
fubar
parents:
diff changeset
214 label_base = tool_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
215 if tool_version:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
216 label_base += "/" + str(tool_version)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
217
83f8bb78781e Uploaded
fubar
parents:
diff changeset
218 test_id = label_base + "-" + str(test_index)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
219 return test_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
220
83f8bb78781e Uploaded
fubar
parents:
diff changeset
221
83f8bb78781e Uploaded
fubar
parents:
diff changeset
222 def _test_tool(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
223 executor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
224 test_reference,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
225 results,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
226 galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
227 log,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
228 retries,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
229 verify_kwds,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
230 ):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
231 tool_id = test_reference.tool_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
232 tool_version = test_reference.tool_version
83f8bb78781e Uploaded
fubar
parents:
diff changeset
233 test_index = test_reference.test_index
83f8bb78781e Uploaded
fubar
parents:
diff changeset
234 # If given a tool_id with a version suffix, strip it off so we can treat tool_version
83f8bb78781e Uploaded
fubar
parents:
diff changeset
235 # correctly at least in client_test_config.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
236 if tool_version and tool_id.endswith("/" + tool_version):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
237 tool_id = tool_id[:-len("/" + tool_version)]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
238
83f8bb78781e Uploaded
fubar
parents:
diff changeset
239 test_id = _test_id_for_reference(test_reference)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
240
83f8bb78781e Uploaded
fubar
parents:
diff changeset
241 def run_test():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
242 run_retries = retries
83f8bb78781e Uploaded
fubar
parents:
diff changeset
243 job_data = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
244 job_exception = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
245
83f8bb78781e Uploaded
fubar
parents:
diff changeset
246 def register(job_data_):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
247 nonlocal job_data
83f8bb78781e Uploaded
fubar
parents:
diff changeset
248 job_data = job_data_
83f8bb78781e Uploaded
fubar
parents:
diff changeset
249
83f8bb78781e Uploaded
fubar
parents:
diff changeset
250 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
251 while run_retries >= 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
252 job_exception = None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
253 try:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
254 if log:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
255 log.info("Executing test '%s'", test_id)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
256 verify_tool(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
257 tool_id, galaxy_interactor, test_index=test_index, tool_version=tool_version,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
258 register_job_data=register, **verify_kwds
83f8bb78781e Uploaded
fubar
parents:
diff changeset
259 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
260 if log:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
261 log.info("Test '%s' passed", test_id)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
262 break
83f8bb78781e Uploaded
fubar
parents:
diff changeset
263 except Exception as e:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
264 if log:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
265 log.warning("Test '%s' failed", test_id, exc_info=True)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
266
83f8bb78781e Uploaded
fubar
parents:
diff changeset
267 job_exception = e
83f8bb78781e Uploaded
fubar
parents:
diff changeset
268 run_retries -= 1
83f8bb78781e Uploaded
fubar
parents:
diff changeset
269 finally:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
270 if job_data is not None:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
271 results.register_result({
83f8bb78781e Uploaded
fubar
parents:
diff changeset
272 "id": test_id,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
273 "has_data": True,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
274 "data": job_data,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
275 })
83f8bb78781e Uploaded
fubar
parents:
diff changeset
276 if job_exception is not None:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
277 was_recorded = job_data is not None
83f8bb78781e Uploaded
fubar
parents:
diff changeset
278 test_exception = TestException(tool_id, job_exception, was_recorded)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
279 results.register_exception(test_exception)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
280
83f8bb78781e Uploaded
fubar
parents:
diff changeset
281 executor.submit(run_test)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
282
83f8bb78781e Uploaded
fubar
parents:
diff changeset
283
83f8bb78781e Uploaded
fubar
parents:
diff changeset
284 def build_case_references(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
285 galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
286 tool_id=ALL_TOOLS,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
287 tool_version=LATEST_VERSION,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
288 test_index=ALL_TESTS,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
289 page_size=0,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
290 page_number=0,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
291 check_against=None,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
292 log=None,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
293 ):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
294 test_references = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
295 if tool_id == ALL_TOOLS:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
296 tests_summary = galaxy_interactor.get_tests_summary()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
297 for tool_id, tool_versions_dict in tests_summary.items():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
298 for tool_version, summary in tool_versions_dict.items():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
299 for test_index in range(summary["count"]):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
300 test_reference = TestReference(tool_id, tool_version, test_index)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
301 test_references.append(test_reference)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
302 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
303 assert tool_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
304 tool_test_dicts = galaxy_interactor.get_tool_tests(tool_id, tool_version=tool_version) or {}
83f8bb78781e Uploaded
fubar
parents:
diff changeset
305 for i, tool_test_dict in enumerate(tool_test_dicts):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
306 this_tool_version = tool_test_dict.get("tool_version", tool_version)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
307 this_test_index = i
83f8bb78781e Uploaded
fubar
parents:
diff changeset
308 if test_index == ALL_TESTS or i == test_index:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
309 test_reference = TestReference(tool_id, this_tool_version, this_test_index)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
310 test_references.append(test_reference)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
311
83f8bb78781e Uploaded
fubar
parents:
diff changeset
312 if check_against:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
313 filtered_test_references = []
83f8bb78781e Uploaded
fubar
parents:
diff changeset
314 for test_reference in test_references:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
315 if check_against.already_successful(test_reference):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
316 if log is not None:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
317 log.debug(f"Found successful test for {test_reference}, skipping")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
318 continue
83f8bb78781e Uploaded
fubar
parents:
diff changeset
319 filtered_test_references.append(test_reference)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
320 log.info(f"Skipping {len(test_references)-len(filtered_test_references)} out of {len(test_references)} tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
321 test_references = filtered_test_references
83f8bb78781e Uploaded
fubar
parents:
diff changeset
322
83f8bb78781e Uploaded
fubar
parents:
diff changeset
323 if page_size > 0:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
324 slice_start = page_size * page_number
83f8bb78781e Uploaded
fubar
parents:
diff changeset
325 slice_end = page_size * (page_number + 1)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
326 test_references = test_references[slice_start:slice_end]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
327
83f8bb78781e Uploaded
fubar
parents:
diff changeset
328 return test_references
83f8bb78781e Uploaded
fubar
parents:
diff changeset
329
83f8bb78781e Uploaded
fubar
parents:
diff changeset
330
83f8bb78781e Uploaded
fubar
parents:
diff changeset
331 def main(argv=None):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
332 if argv is None:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
333 argv = sys.argv[1:]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
334
83f8bb78781e Uploaded
fubar
parents:
diff changeset
335 args = _arg_parser().parse_args(argv)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
336 log = setup_global_logger(__name__, verbose=args.verbose)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
337 client_test_config_path = args.client_test_config
83f8bb78781e Uploaded
fubar
parents:
diff changeset
338 if client_test_config_path is not None:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
339 log.debug(f"Reading client config path {client_test_config_path}")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
340 with open(client_test_config_path) as f:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
341 client_test_config = yaml.full_load(f)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
342 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
343 client_test_config = {}
83f8bb78781e Uploaded
fubar
parents:
diff changeset
344
83f8bb78781e Uploaded
fubar
parents:
diff changeset
345 def get_option(key):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
346 arg_val = getattr(args, key, None)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
347 if arg_val is None and key in client_test_config:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
348 val = client_test_config.get(key)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
349 else:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
350 val = arg_val
83f8bb78781e Uploaded
fubar
parents:
diff changeset
351 return val
83f8bb78781e Uploaded
fubar
parents:
diff changeset
352
83f8bb78781e Uploaded
fubar
parents:
diff changeset
353 output_json_path = get_option("output_json")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
354 galaxy_interactor_kwds = {
83f8bb78781e Uploaded
fubar
parents:
diff changeset
355 "galaxy_url": get_option("galaxy_url"),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
356 "master_api_key": get_option("admin_key"),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
357 "api_key": get_option("key"),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
358 "keep_outputs_dir": args.output,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
359 "download_attempts": get_option("download_attempts"),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
360 "download_sleep": get_option("download_sleep"),
83f8bb78781e Uploaded
fubar
parents:
diff changeset
361 }
83f8bb78781e Uploaded
fubar
parents:
diff changeset
362 tool_id = args.tool_id
83f8bb78781e Uploaded
fubar
parents:
diff changeset
363 tool_version = args.tool_version
83f8bb78781e Uploaded
fubar
parents:
diff changeset
364 tools_client_test_config = DictClientTestConfig(client_test_config.get("tools"))
83f8bb78781e Uploaded
fubar
parents:
diff changeset
365 verbose = args.verbose
83f8bb78781e Uploaded
fubar
parents:
diff changeset
366
83f8bb78781e Uploaded
fubar
parents:
diff changeset
367 galaxy_interactor = GalaxyInteractorApi(**galaxy_interactor_kwds)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
368 results = Results(args.suite_name, output_json_path, append=args.append)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
369 check_against = None if not args.skip_successful else results
83f8bb78781e Uploaded
fubar
parents:
diff changeset
370 test_references = build_case_references(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
371 galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
372 tool_id=tool_id,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
373 tool_version=tool_version,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
374 test_index=args.test_index,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
375 page_size=args.page_size,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
376 page_number=args.page_number,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
377 check_against=check_against,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
378 log=log,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
379 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
380 log.debug(f"Built {len(test_references)} test references to executed.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
381 verify_kwds = dict(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
382 client_test_config=tools_client_test_config,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
383 force_path_paste=args.force_path_paste,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
384 skip_with_reference_data=not args.with_reference_data,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
385 quiet=not verbose,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
386 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
387 test_tools(
83f8bb78781e Uploaded
fubar
parents:
diff changeset
388 galaxy_interactor,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
389 test_references,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
390 results,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
391 log=log,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
392 parallel_tests=args.parallel_tests,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
393 history_per_test_case=args.history_per_test_case,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
394 no_history_cleanup=args.no_history_cleanup,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
395 verify_kwds=verify_kwds,
83f8bb78781e Uploaded
fubar
parents:
diff changeset
396 )
83f8bb78781e Uploaded
fubar
parents:
diff changeset
397 exceptions = results.test_exceptions
83f8bb78781e Uploaded
fubar
parents:
diff changeset
398 if exceptions:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
399 exception = exceptions[0]
83f8bb78781e Uploaded
fubar
parents:
diff changeset
400 if hasattr(exception, "exception"):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
401 exception = exception.exception
83f8bb78781e Uploaded
fubar
parents:
diff changeset
402 raise exception
83f8bb78781e Uploaded
fubar
parents:
diff changeset
403
83f8bb78781e Uploaded
fubar
parents:
diff changeset
404
83f8bb78781e Uploaded
fubar
parents:
diff changeset
405 def setup_global_logger(name, log_file=None, verbose=False):
83f8bb78781e Uploaded
fubar
parents:
diff changeset
406 formatter = logging.Formatter('%(asctime)s %(levelname)-5s - %(message)s')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
407 console = logging.StreamHandler()
83f8bb78781e Uploaded
fubar
parents:
diff changeset
408 console.setFormatter(formatter)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
409
83f8bb78781e Uploaded
fubar
parents:
diff changeset
410 logger = logging.getLogger(name)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
411 logger.setLevel(logging.DEBUG if verbose else logging.INFO)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
412 logger.addHandler(console)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
413
83f8bb78781e Uploaded
fubar
parents:
diff changeset
414 if not log_file:
83f8bb78781e Uploaded
fubar
parents:
diff changeset
415 # delete = false is chosen here because it is always nice to have a log file
83f8bb78781e Uploaded
fubar
parents:
diff changeset
416 # ready if you need to debug. Not having the "if only I had set a log file"
83f8bb78781e Uploaded
fubar
parents:
diff changeset
417 # moment after the fact.
83f8bb78781e Uploaded
fubar
parents:
diff changeset
418 temp = tempfile.NamedTemporaryFile(prefix="ephemeris_", delete=False)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
419 log_file = temp.name
83f8bb78781e Uploaded
fubar
parents:
diff changeset
420 file_handler = logging.FileHandler(log_file)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
421 logger.addHandler(file_handler)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
422 logger.info(f"Storing log file in: {log_file}")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
423 return logger
83f8bb78781e Uploaded
fubar
parents:
diff changeset
424
83f8bb78781e Uploaded
fubar
parents:
diff changeset
425
83f8bb78781e Uploaded
fubar
parents:
diff changeset
426 def _arg_parser():
83f8bb78781e Uploaded
fubar
parents:
diff changeset
427 parser = argparse.ArgumentParser(description=DESCRIPTION)
83f8bb78781e Uploaded
fubar
parents:
diff changeset
428 parser.add_argument('-u', '--galaxy-url', default="http://localhost:8080", help='Galaxy URL')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
429 parser.add_argument('-k', '--key', default=None, help='Galaxy User API Key')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
430 parser.add_argument('-a', '--admin-key', default=None, help='Galaxy Admin API Key')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
431 parser.add_argument('--force_path_paste', default=False, action="store_true", help='This requires Galaxy-side config option "allow_path_paste" enabled. Allows for fetching test data locally. Only for admins.')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
432 parser.add_argument('-t', '--tool-id', default=ALL_TOOLS, help='Tool ID')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
433 parser.add_argument('--tool-version', default=None, help='Tool Version (if tool id supplied). Defaults to just latest version, use * to test all versions')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
434 parser.add_argument('-i', '--test-index', default=ALL_TESTS, type=int, help='Tool Test Index (starting at 0) - by default all tests will run.')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
435 parser.add_argument('-o', '--output', default=None, help='directory to dump outputs to')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
436 parser.add_argument('--append', default=False, action="store_true", help="Extend a test record json (created with --output-json) with additional tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
437 parser.add_argument('--skip-successful', default=False, action="store_true", help="When used with --append, skip previously run successful tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
438 parser.add_argument('-j', '--output-json', default=None, help='output metadata json')
83f8bb78781e Uploaded
fubar
parents:
diff changeset
439 parser.add_argument('--verbose', default=False, action="store_true", help="Verbose logging.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
440 parser.add_argument('-c', '--client-test-config', default=None, help="Test config YAML to help with client testing")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
441 parser.add_argument('--suite-name', default=DEFAULT_SUITE_NAME, help="Suite name for tool test output")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
442 parser.add_argument('--with-reference-data', dest="with_reference_data", default=False, action="store_true")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
443 parser.add_argument('--skip-with-reference-data', dest="with_reference_data", action="store_false", help="Skip tests the Galaxy server believes use data tables or loc files.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
444 parser.add_argument('--history-per-suite', dest="history_per_test_case", default=False, action="store_false", help="Create new history per test suite (all tests in same history).")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
445 parser.add_argument('--history-per-test-case', dest="history_per_test_case", action="store_true", help="Create new history per test case.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
446 parser.add_argument('--no-history-cleanup', default=False, action="store_true", help="Perserve histories created for testing.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
447 parser.add_argument('--parallel-tests', default=1, type=int, help="Parallel tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
448 parser.add_argument('--retries', default=0, type=int, help="Retry failed tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
449 parser.add_argument('--page-size', default=0, type=int, help="If positive, use pagination and just run one 'page' to tool tests.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
450 parser.add_argument('--page-number', default=0, type=int, help="If page size is used, run this 'page' of tests - starts with 0.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
451 parser.add_argument('--download-attempts', default=1, type=int, help="Galaxy may return a transient 500 status code for download if test results are written but not yet accessible.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
452 parser.add_argument('--download-sleep', default=1, type=int, help="If download attempts is greater than 1, the amount to sleep between download attempts.")
83f8bb78781e Uploaded
fubar
parents:
diff changeset
453 return parser
83f8bb78781e Uploaded
fubar
parents:
diff changeset
454
83f8bb78781e Uploaded
fubar
parents:
diff changeset
455
83f8bb78781e Uploaded
fubar
parents:
diff changeset
456 if __name__ == "__main__":
83f8bb78781e Uploaded
fubar
parents:
diff changeset
457 main()