1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 import crunchstat_summary.command
15 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
18 class ReportDiff(unittest.TestCase):
19 def diff_known_report(self, logfile, cmd):
20 expectfile = logfile+'.report'
21 expect = open(expectfile).readlines()
22 self.diff_report(cmd, expect, expectfile=expectfile)
24 def diff_report(self, cmd, expect, expectfile=None):
25 got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
26 self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
27 expect, got, fromfile=expectfile, tofile="(generated)")))
30 class SummarizeFile(ReportDiff):
31 def test_example_files(self):
32 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
33 logfile = os.path.join(TESTS_DIR, fnm)
34 args = crunchstat_summary.command.ArgumentParser().parse_args(
35 ['--log-file', logfile])
36 cmd = crunchstat_summary.command.Command(args)
38 self.diff_known_report(logfile, cmd)
41 class HTMLFromFile(ReportDiff):
42 def test_example_files(self):
43 # Note we don't test the output content at all yet; we're
44 # mainly just verifying the --format=html option isn't ignored
45 # and the HTML code path doesn't crash.
46 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
47 logfile = os.path.join(TESTS_DIR, fnm)
48 args = crunchstat_summary.command.ArgumentParser().parse_args(
49 ['--format=html', '--log-file', logfile])
50 cmd = crunchstat_summary.command.Command(args)
52 self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
55 class SummarizeEdgeCases(unittest.TestCase):
56 def test_error_messages(self):
57 logfile = open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'))
58 s = crunchstat_summary.summarizer.Summarizer(logfile)
62 class SummarizeContainer(ReportDiff):
64 'uuid': '9tee4-dz642-lymtndkpy39eibk',
65 'created_at': '2017-08-18T14:27:25.371388141',
66 'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
69 'uuid': '9tee4-xvhdp-uper95jktm10d3w',
71 'created_at': '2017-08-18T14:27:25.242339223Z',
72 'container_uuid': fake_container['uuid'],
74 reportfile = os.path.join(
75 TESTS_DIR, 'container_9tee4-dz642-lymtndkpy39eibk.txt.gz')
76 logfile = os.path.join(
77 TESTS_DIR, 'container_9tee4-dz642-lymtndkpy39eibk-crunchstat.txt.gz')
78 arvmountlog = os.path.join(
79 TESTS_DIR, 'container_9tee4-dz642-lymtndkpy39eibk-arv-mount.txt.gz')
81 @mock.patch('arvados.collection.CollectionReader')
82 @mock.patch('arvados.api')
83 def test_container(self, mock_api, mock_cr):
84 mock_api().container_requests().index().execute.return_value = {'items':[]}
85 mock_api().container_requests().get().execute.return_value = self.fake_request
86 mock_api().containers().get().execute.return_value = self.fake_container
87 mock_cr().__iter__.return_value = [
88 'crunch-run.txt', 'stderr.txt', 'node-info.txt',
89 'container.json', 'crunchstat.txt', 'arv-mount.txt']
91 if n == "crunchstat.txt":
92 return gzip.open(self.logfile)
93 elif n == "arv-mount.txt":
94 return gzip.open(self.arvmountlog)
95 mock_cr().open.side_effect = _open
96 args = crunchstat_summary.command.ArgumentParser().parse_args(
97 ['--job', self.fake_request['uuid']])
98 cmd = crunchstat_summary.command.Command(args)
100 self.diff_known_report(self.reportfile, cmd)
103 class SummarizeJob(ReportDiff):
104 fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
105 fake_log_id = 'fake-log-collection-id'
107 'uuid': fake_job_uuid,
110 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
112 @mock.patch('arvados.collection.CollectionReader')
113 @mock.patch('arvados.api')
114 def test_job_report(self, mock_api, mock_cr):
115 mock_api().jobs().get().execute.return_value = self.fake_job
116 mock_cr().__iter__.return_value = ['fake-logfile.txt']
117 mock_cr().open.return_value = gzip.open(self.logfile)
118 args = crunchstat_summary.command.ArgumentParser().parse_args(
119 ['--job', self.fake_job_uuid])
120 cmd = crunchstat_summary.command.Command(args)
122 self.diff_known_report(self.logfile, cmd)
123 mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
124 mock_cr.assert_called_with(self.fake_log_id)
125 mock_cr().open.assert_called_with('fake-logfile.txt')
128 class SummarizePipeline(ReportDiff):
130 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
131 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
132 'components': collections.OrderedDict([
135 'uuid': 'zzzzz-8i9sb-000000000000000',
136 'log': 'fake-log-pdh-0',
137 'runtime_constraints': {
138 'min_ram_mb_per_node': 900,
139 'min_cores_per_node': 1,
145 'uuid': 'zzzzz-8i9sb-000000000000001',
146 'log': 'fake-log-pdh-1',
147 'runtime_constraints': {
148 'min_ram_mb_per_node': 900,
149 'min_cores_per_node': 1,
153 ['no-job-assigned', {}],
156 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
161 'uuid': 'zzzzz-8i9sb-000000000000002',
162 'log': 'fake-log-pdh-2',
163 'runtime_constraints': {
164 'min_ram_mb_per_node': 900,
165 'min_cores_per_node': 1,
171 @mock.patch('arvados.collection.CollectionReader')
172 @mock.patch('arvados.api')
173 def test_pipeline(self, mock_api, mock_cr):
174 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
175 mock_api().pipeline_instances().get().execute. \
176 return_value = self.fake_instance
177 mock_cr().__iter__.return_value = ['fake-logfile.txt']
178 mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
179 args = crunchstat_summary.command.ArgumentParser().parse_args(
180 ['--pipeline-instance', self.fake_instance['uuid']])
181 cmd = crunchstat_summary.command.Command(args)
185 line for line in open(logfile+'.report').readlines()
186 if not line.startswith('#!! ')]
188 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
189 job_report + ['\n'] +
190 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
191 job_report + ['\n'] +
192 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
193 '(no report generated)\n',
195 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
197 self.diff_report(cmd, expect)
198 mock_cr.assert_has_calls(
200 mock.call('fake-log-pdh-0'),
201 mock.call('fake-log-pdh-1'),
202 mock.call('fake-log-pdh-2'),
204 mock_cr().open.assert_called_with('fake-logfile.txt')
207 class SummarizeACRJob(ReportDiff):
209 'uuid': 'zzzzz-8i9sb-i3e77t9z5y8j9cc',
210 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
212 'foo': 'zzzzz-8i9sb-000000000000000',
213 'bar': 'zzzzz-8i9sb-000000000000001',
214 'unfinished-job': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
215 'baz': 'zzzzz-8i9sb-000000000000002',
218 fake_jobs_index = { 'items': [
220 'uuid': 'zzzzz-8i9sb-000000000000000',
221 'log': 'fake-log-pdh-0',
222 'runtime_constraints': {
223 'min_ram_mb_per_node': 900,
224 'min_cores_per_node': 1,
228 'uuid': 'zzzzz-8i9sb-000000000000001',
229 'log': 'fake-log-pdh-1',
230 'runtime_constraints': {
231 'min_ram_mb_per_node': 900,
232 'min_cores_per_node': 1,
236 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
239 'uuid': 'zzzzz-8i9sb-000000000000002',
240 'log': 'fake-log-pdh-2',
241 'runtime_constraints': {
242 'min_ram_mb_per_node': 900,
243 'min_cores_per_node': 1,
247 @mock.patch('arvados.collection.CollectionReader')
248 @mock.patch('arvados.api')
249 def test_acr_job(self, mock_api, mock_cr):
250 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
251 mock_api().jobs().index().execute.return_value = self.fake_jobs_index
252 mock_api().jobs().get().execute.return_value = self.fake_job
253 mock_cr().__iter__.return_value = ['fake-logfile.txt']
254 mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
255 args = crunchstat_summary.command.ArgumentParser().parse_args(
256 ['--job', self.fake_job['uuid']])
257 cmd = crunchstat_summary.command.Command(args)
261 line for line in open(logfile+'.report').readlines()
262 if not line.startswith('#!! ')]
264 ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n',
265 '(no report generated)\n',
267 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
268 job_report + ['\n'] +
269 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
270 job_report + ['\n'] +
271 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
272 job_report + ['\n'] +
273 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
274 '(no report generated)\n']
276 self.diff_report(cmd, expect)
277 mock_cr.assert_has_calls(
279 mock.call('fake-log-pdh-0'),
280 mock.call('fake-log-pdh-1'),
281 mock.call('fake-log-pdh-2'),
283 mock_cr().open.assert_called_with('fake-logfile.txt')