1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 import crunchstat_summary.command
17 from crunchstat_summary.command import UTF8Decode
19 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
22 class ReportDiff(unittest.TestCase):
23 def diff_known_report(self, logfile, cmd):
24 expectfile = logfile+'.report'
25 with open(expectfile, encoding='utf-8') as f:
26 expect = f.readlines()
27 self.diff_report(cmd, expect, expectfile=expectfile)
29 def diff_report(self, cmd, expect, expectfile='(expected)'):
30 got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
31 self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
32 expect, got, fromfile=expectfile, tofile="(generated)")))
35 class SummarizeFile(ReportDiff):
36 def test_example_files(self):
37 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
38 logfile = os.path.join(TESTS_DIR, fnm)
39 args = crunchstat_summary.command.ArgumentParser().parse_args(
40 ['--log-file', logfile])
41 cmd = crunchstat_summary.command.Command(args)
43 self.diff_known_report(logfile, cmd)
46 class HTMLFromFile(ReportDiff):
47 def test_example_files(self):
48 # Note we don't test the output content at all yet; we're
49 # mainly just verifying the --format=html option isn't ignored
50 # and the HTML code path doesn't crash.
51 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
52 logfile = os.path.join(TESTS_DIR, fnm)
53 args = crunchstat_summary.command.ArgumentParser().parse_args(
54 ['--format=html', '--log-file', logfile])
55 cmd = crunchstat_summary.command.Command(args)
57 if sys.version_info >= (3,2):
58 self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
60 self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
63 class SummarizeEdgeCases(unittest.TestCase):
64 def test_error_messages(self):
65 logfile = open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'), encoding='utf-8')
66 s = crunchstat_summary.summarizer.Summarizer(logfile)
70 class SummarizeContainerCommon(ReportDiff):
72 'uuid': '9tee4-dz642-lymtndkpy39eibk',
73 'created_at': '2017-08-18T14:27:25.371388141',
74 'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
77 'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
79 'created_at': '2017-08-18T14:27:25.242339223Z',
80 'container_uuid': fake_container['uuid'],
81 'runtime_constraints': {
85 'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
88 logfile = os.path.join(
89 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-crunchstat.txt.gz')
90 arvmountlog = os.path.join(
91 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-arv-mount.txt.gz')
93 @mock.patch('arvados.collection.CollectionReader')
94 @mock.patch('arvados.api')
95 def check_common(self, mock_api, mock_cr):
96 items = [ {'items':[self.fake_request]}] + [{'items':[]}] * 100
97 # Index and list mean the same thing, but are used in different places in the
98 # code. It's fragile, but exploit that fact to distinguish the two uses.
99 mock_api().container_requests().index().execute.return_value = {'items': [] } # child_crs
100 mock_api().container_requests().list().execute.side_effect = items # parent request
101 mock_api().container_requests().get().execute.return_value = self.fake_request
102 mock_api().containers().get().execute.return_value = self.fake_container
103 mock_cr().__iter__.return_value = [
104 'crunch-run.txt', 'stderr.txt', 'node-info.txt',
105 'container.json', 'crunchstat.txt', 'arv-mount.txt']
107 if n == "crunchstat.txt":
108 return UTF8Decode(gzip.open(self.logfile))
109 elif n == "arv-mount.txt":
110 return UTF8Decode(gzip.open(self.arvmountlog))
111 mock_cr().open.side_effect = _open
112 args = crunchstat_summary.command.ArgumentParser().parse_args(
114 cmd = crunchstat_summary.command.Command(args)
116 self.diff_known_report(self.reportfile, cmd)
120 class SummarizeContainer(SummarizeContainerCommon):
121 uuid = '9tee4-dz642-lymtndkpy39eibk'
122 reportfile = os.path.join(TESTS_DIR, 'container_%s.txt.gz' % uuid)
123 arg_strings = ['--container', uuid, '-v', '-v']
125 def test_container(self):
129 class SummarizeContainerRequest(SummarizeContainerCommon):
130 uuid = '9tee4-xvhdp-kk0ja1cl8b2kr1y'
131 reportfile = os.path.join(TESTS_DIR, 'container_request_%s.txt.gz' % uuid)
132 arg_strings = ['--container-request', uuid, '-v', '-v']
134 def test_container_request(self):
138 class SummarizeJob(ReportDiff):
139 fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
140 fake_log_id = 'fake-log-collection-id'
142 'uuid': fake_job_uuid,
145 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
147 @mock.patch('arvados.collection.CollectionReader')
148 @mock.patch('arvados.api')
149 def test_job_report(self, mock_api, mock_cr):
150 mock_api().jobs().get().execute.return_value = self.fake_job
151 mock_cr().__iter__.return_value = ['fake-logfile.txt']
152 mock_cr().open.return_value = UTF8Decode(gzip.open(self.logfile))
153 args = crunchstat_summary.command.ArgumentParser().parse_args(
154 ['--job', self.fake_job_uuid])
155 cmd = crunchstat_summary.command.Command(args)
157 self.diff_known_report(self.logfile, cmd)
158 mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
159 mock_cr.assert_called_with(self.fake_log_id)
160 mock_cr().open.assert_called_with('fake-logfile.txt')
163 class SummarizePipeline(ReportDiff):
165 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
166 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
167 'components': collections.OrderedDict([
170 'uuid': 'zzzzz-8i9sb-000000000000000',
171 'log': 'fake-log-pdh-0',
172 'runtime_constraints': {
173 'min_ram_mb_per_node': 900,
174 'min_cores_per_node': 1,
180 'uuid': 'zzzzz-8i9sb-000000000000001',
181 'log': 'fake-log-pdh-1',
182 'runtime_constraints': {
183 'min_ram_mb_per_node': 900,
184 'min_cores_per_node': 1,
188 ['no-job-assigned', {}],
191 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
196 'uuid': 'zzzzz-8i9sb-000000000000002',
197 'log': 'fake-log-pdh-2',
198 'runtime_constraints': {
199 'min_ram_mb_per_node': 900,
200 'min_cores_per_node': 1,
206 @mock.patch('arvados.collection.CollectionReader')
207 @mock.patch('arvados.api')
208 def test_pipeline(self, mock_api, mock_cr):
209 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
210 mock_api().pipeline_instances().get().execute. \
211 return_value = self.fake_instance
212 mock_cr().__iter__.return_value = ['fake-logfile.txt']
213 mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
214 args = crunchstat_summary.command.ArgumentParser().parse_args(
215 ['--pipeline-instance', self.fake_instance['uuid']])
216 cmd = crunchstat_summary.command.Command(args)
219 with open(logfile+'.report', encoding='utf-8') as f:
220 job_report = [line for line in f if not line.startswith('#!! ')]
222 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
223 job_report + ['\n'] +
224 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
225 job_report + ['\n'] +
226 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
227 '(no report generated)\n',
229 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
231 self.diff_report(cmd, expect)
232 mock_cr.assert_has_calls(
234 mock.call('fake-log-pdh-0'),
235 mock.call('fake-log-pdh-1'),
236 mock.call('fake-log-pdh-2'),
238 mock_cr().open.assert_called_with('fake-logfile.txt')
241 class SummarizeACRJob(ReportDiff):
243 'uuid': 'zzzzz-8i9sb-i3e77t9z5y8j9cc',
244 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
246 'foo': 'zzzzz-8i9sb-000000000000000',
247 'bar': 'zzzzz-8i9sb-000000000000001',
248 'unfinished-job': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
249 'baz': 'zzzzz-8i9sb-000000000000002',
252 fake_jobs_index = { 'items': [
254 'uuid': 'zzzzz-8i9sb-000000000000000',
255 'log': 'fake-log-pdh-0',
256 'runtime_constraints': {
257 'min_ram_mb_per_node': 900,
258 'min_cores_per_node': 1,
262 'uuid': 'zzzzz-8i9sb-000000000000001',
263 'log': 'fake-log-pdh-1',
264 'runtime_constraints': {
265 'min_ram_mb_per_node': 900,
266 'min_cores_per_node': 1,
270 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
273 'uuid': 'zzzzz-8i9sb-000000000000002',
274 'log': 'fake-log-pdh-2',
275 'runtime_constraints': {
276 'min_ram_mb_per_node': 900,
277 'min_cores_per_node': 1,
281 @mock.patch('arvados.collection.CollectionReader')
282 @mock.patch('arvados.api')
283 def test_acr_job(self, mock_api, mock_cr):
284 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
285 mock_api().jobs().index().execute.return_value = self.fake_jobs_index
286 mock_api().jobs().get().execute.return_value = self.fake_job
287 mock_cr().__iter__.return_value = ['fake-logfile.txt']
288 mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
289 args = crunchstat_summary.command.ArgumentParser().parse_args(
290 ['--job', self.fake_job['uuid']])
291 cmd = crunchstat_summary.command.Command(args)
294 with open(logfile+'.report', encoding='utf-8') as f:
295 job_report = [line for line in f if not line.startswith('#!! ')]
297 ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n',
298 '(no report generated)\n',
300 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
301 job_report + ['\n'] +
302 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
303 job_report + ['\n'] +
304 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
305 job_report + ['\n'] +
306 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
307 '(no report generated)\n']
309 self.diff_report(cmd, expect)
310 mock_cr.assert_has_calls(
312 mock.call('fake-log-pdh-0'),
313 mock.call('fake-log-pdh-1'),
314 mock.call('fake-log-pdh-2'),
316 mock_cr().open.assert_called_with('fake-logfile.txt')