Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / tools / crunchstat-summary / tests / test_examples.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import arvados
6 import collections
7 import crunchstat_summary.command
8 import difflib
9 import glob
10 import gzip
11 from io import open
12 import mock
13 import os
14 import sys
15 import unittest
16
17 from crunchstat_summary.command import UTF8Decode
18
19 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
20
21
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)
28
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)")))
33
34
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)
42             cmd.run()
43             self.diff_known_report(logfile, cmd)
44
45
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)
56             cmd.run()
57             if sys.version_info >= (3,2):
58                 self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
59             else:
60                 self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
61
62
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)
67         s.run()
68
69
70 class SummarizeContainerCommon(ReportDiff):
71     fake_container = {
72         'uuid': '9tee4-dz642-lymtndkpy39eibk',
73         'created_at': '2017-08-18T14:27:25.371388141',
74         'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
75     }
76     fake_request = {
77         'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
78         'name': 'container',
79         'created_at': '2017-08-18T14:27:25.242339223Z',
80         'container_uuid': fake_container['uuid'],
81         'runtime_constraints': {
82             'vcpus': 1,
83             'ram': 2621440000
84             },
85         'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
86         }
87
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')
92
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']
106         def _open(n):
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(
113             self.arg_strings)
114         cmd = crunchstat_summary.command.Command(args)
115         cmd.run()
116         self.diff_known_report(self.reportfile, cmd)
117
118
119
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']
124
125     def test_container(self):
126         self.check_common()
127
128
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']
133
134     def test_container_request(self):
135         self.check_common()
136
137
138 class SummarizeJob(ReportDiff):
139     fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
140     fake_log_id = 'fake-log-collection-id'
141     fake_job = {
142         'uuid': fake_job_uuid,
143         'log': fake_log_id,
144     }
145     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
146
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)
156         cmd.run()
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')
161
162
163 class SummarizePipeline(ReportDiff):
164     fake_instance = {
165         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
166         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
167         'components': collections.OrderedDict([
168             ['foo', {
169                 'job': {
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,
175                     },
176                 },
177             }],
178             ['bar', {
179                 'job': {
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,
185                     },
186                 },
187             }],
188             ['no-job-assigned', {}],
189             ['unfinished-job', {
190                 'job': {
191                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
192                 },
193             }],
194             ['baz', {
195                 'job': {
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,
201                     },
202                 },
203             }]]),
204     }
205
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)
217         cmd.run()
218
219         with open(logfile+'.report', encoding='utf-8') as f:
220             job_report = [line for line in f if not line.startswith('#!! ')]
221         expect = (
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',
228              '\n'] +
229             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
230             job_report)
231         self.diff_report(cmd, expect)
232         mock_cr.assert_has_calls(
233             [
234                 mock.call('fake-log-pdh-0'),
235                 mock.call('fake-log-pdh-1'),
236                 mock.call('fake-log-pdh-2'),
237             ], any_order=True)
238         mock_cr().open.assert_called_with('fake-logfile.txt')
239
240
241 class SummarizeACRJob(ReportDiff):
242     fake_job = {
243         'uuid': 'zzzzz-8i9sb-i3e77t9z5y8j9cc',
244         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
245         'components': {
246             'foo': 'zzzzz-8i9sb-000000000000000',
247             'bar': 'zzzzz-8i9sb-000000000000001',
248             'unfinished-job': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
249             'baz': 'zzzzz-8i9sb-000000000000002',
250         }
251     }
252     fake_jobs_index = { 'items': [
253         {
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,
259             },
260         },
261         {
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,
267             },
268         },
269         {
270             'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
271         },
272         {
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,
278             },
279         },
280     ]}
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)
292         cmd.run()
293
294         with open(logfile+'.report', encoding='utf-8') as f:
295             job_report = [line for line in f if not line.startswith('#!! ')]
296         expect = (
297             ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n',
298              '(no report generated)\n',
299              '\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']
308         )
309         self.diff_report(cmd, expect)
310         mock_cr.assert_has_calls(
311             [
312                 mock.call('fake-log-pdh-0'),
313                 mock.call('fake-log-pdh-1'),
314                 mock.call('fake-log-pdh-2'),
315             ], any_order=True)
316         mock_cr().open.assert_called_with('fake-logfile.txt')