Merge branch '20680-rolling-deploy' refs #20680
[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 import io
12 import logging
13 import mock
14 import os
15 import sys
16 import unittest
17
18 from crunchstat_summary.command import UTF8Decode
19 from crunchstat_summary import logger
20
21 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
22
23
24 class TestCase(unittest.TestCase):
25     def setUp(self):
26         self.logbuf = io.StringIO()
27         self.loghandler = logging.StreamHandler(stream=self.logbuf)
28         logger.addHandler(self.loghandler)
29         logger.setLevel(logging.WARNING)
30
31     def tearDown(self):
32         logger.removeHandler(self.loghandler)
33
34     def diff_known_report(self, logfile, cmd):
35         expectfile = logfile+'.report'
36         with io.open(expectfile, encoding='utf-8') as f:
37             expect = f.readlines()
38         self.diff_report(cmd, expect, expectfile=expectfile)
39
40     def diff_report(self, cmd, expect, expectfile='(expected)'):
41         got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
42         self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
43             expect, got, fromfile=expectfile, tofile="(generated)")))
44
45
46 class SummarizeFile(TestCase):
47     def test_example_files(self):
48         for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
49             logfile = os.path.join(TESTS_DIR, fnm)
50             args = crunchstat_summary.command.ArgumentParser().parse_args(
51                 ['--log-file', logfile])
52             cmd = crunchstat_summary.command.Command(args)
53             cmd.run()
54             self.diff_known_report(logfile, cmd)
55
56
57 class HTMLFromFile(TestCase):
58     def test_example_files(self):
59         # Note we don't test the output content at all yet; we're
60         # mainly just verifying the --format=html option isn't ignored
61         # and the HTML code path doesn't crash.
62         for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
63             logfile = os.path.join(TESTS_DIR, fnm)
64             args = crunchstat_summary.command.ArgumentParser().parse_args(
65                 ['--format=html', '--log-file', logfile])
66             cmd = crunchstat_summary.command.Command(args)
67             cmd.run()
68             self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
69
70
71 class SummarizeEdgeCases(TestCase):
72     def test_error_messages(self):
73         logfile = io.open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'), encoding='utf-8')
74         s = crunchstat_summary.summarizer.Summarizer(logfile)
75         s.run()
76         self.assertRegex(self.logbuf.getvalue(), r'CPU stats are missing -- possible cluster configuration issue')
77         self.assertRegex(self.logbuf.getvalue(), r'memory stats are missing -- possible cluster configuration issue')
78         self.assertRegex(self.logbuf.getvalue(), r'network I/O stats are missing -- possible cluster configuration issue')
79         self.assertRegex(self.logbuf.getvalue(), r'storage space stats are missing -- possible cluster configuration issue')
80
81
82 class SummarizeContainerCommon(TestCase):
83     fake_container = {
84         'uuid': '9tee4-dz642-lymtndkpy39eibk',
85         'created_at': '2017-08-18T14:27:25.371388141',
86         'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
87     }
88     fake_request = {
89         'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
90         'name': 'container',
91         'created_at': '2017-08-18T14:27:25.242339223Z',
92         'container_uuid': fake_container['uuid'],
93         'runtime_constraints': {
94             'vcpus': 1,
95             'ram': 2621440000
96             },
97         'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
98         }
99
100     logfile = os.path.join(
101         TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-crunchstat.txt.gz')
102     arvmountlog = os.path.join(
103         TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-arv-mount.txt.gz')
104
105     @mock.patch('arvados.collection.CollectionReader')
106     @mock.patch('arvados.api')
107     def check_common(self, mock_api, mock_cr):
108         items = [ {'items':[self.fake_request]}] + [{'items':[]}] * 100
109         # Index and list mean the same thing, but are used in different places in the
110         # code. It's fragile, but exploit that fact to distinguish the two uses.
111         mock_api().container_requests().index().execute.return_value = {'items': [] }  # child_crs
112         mock_api().container_requests().list().execute.side_effect = items # parent request
113         mock_api().container_requests().get().execute.return_value = self.fake_request
114         mock_api().containers().get().execute.return_value = self.fake_container
115         mock_cr().__iter__.return_value = [
116             'crunch-run.txt', 'stderr.txt', 'node-info.txt',
117             'container.json', 'crunchstat.txt', 'arv-mount.txt']
118         def _open(n):
119             if n == "crunchstat.txt":
120                 return UTF8Decode(gzip.open(self.logfile))
121             elif n == "arv-mount.txt":
122                 return UTF8Decode(gzip.open(self.arvmountlog))
123         mock_cr().open.side_effect = _open
124         args = crunchstat_summary.command.ArgumentParser().parse_args(
125             self.arg_strings)
126         cmd = crunchstat_summary.command.Command(args)
127         cmd.run()
128         self.diff_known_report(self.reportfile, cmd)
129
130
131
132 class SummarizeContainer(SummarizeContainerCommon):
133     uuid = '9tee4-dz642-lymtndkpy39eibk'
134     reportfile = os.path.join(TESTS_DIR, 'container_%s.txt.gz' % uuid)
135     arg_strings = ['--container', uuid, '-v', '-v']
136
137     def test_container(self):
138         self.check_common()
139
140
141 class SummarizeContainerRequest(SummarizeContainerCommon):
142     uuid = '9tee4-xvhdp-kk0ja1cl8b2kr1y'
143     reportfile = os.path.join(TESTS_DIR, 'container_request_%s.txt.gz' % uuid)
144     arg_strings = ['--container-request', uuid, '-v', '-v']
145
146     def test_container_request(self):
147         self.check_common()
148         self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
149         self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')
150
151
152 class SummarizeJob(TestCase):
153     fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
154     fake_log_id = 'fake-log-collection-id'
155     fake_job = {
156         'uuid': fake_job_uuid,
157         'log': fake_log_id,
158     }
159     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
160
161     @mock.patch('arvados.collection.CollectionReader')
162     @mock.patch('arvados.api')
163     def test_job_report(self, mock_api, mock_cr):
164         mock_api().jobs().get().execute.return_value = self.fake_job
165         mock_cr().__iter__.return_value = ['fake-logfile.txt']
166         mock_cr().open.return_value = UTF8Decode(gzip.open(self.logfile))
167         args = crunchstat_summary.command.ArgumentParser().parse_args(
168             ['--job', self.fake_job_uuid])
169         cmd = crunchstat_summary.command.Command(args)
170         cmd.run()
171         self.diff_known_report(self.logfile, cmd)
172         mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
173         mock_cr.assert_called_with(self.fake_log_id)
174         mock_cr().open.assert_called_with('fake-logfile.txt')
175
176
177 class SummarizePipeline(TestCase):
178     fake_instance = {
179         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
180         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
181         'components': collections.OrderedDict([
182             ['foo', {
183                 'job': {
184                     'uuid': 'zzzzz-8i9sb-000000000000000',
185                     'log': 'fake-log-pdh-0',
186                     'runtime_constraints': {
187                         'min_ram_mb_per_node': 900,
188                         'min_cores_per_node': 1,
189                     },
190                 },
191             }],
192             ['bar', {
193                 'job': {
194                     'uuid': 'zzzzz-8i9sb-000000000000001',
195                     'log': 'fake-log-pdh-1',
196                     'runtime_constraints': {
197                         'min_ram_mb_per_node': 900,
198                         'min_cores_per_node': 1,
199                     },
200                 },
201             }],
202             ['no-job-assigned', {}],
203             ['unfinished-job', {
204                 'job': {
205                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
206                 },
207             }],
208             ['baz', {
209                 'job': {
210                     'uuid': 'zzzzz-8i9sb-000000000000002',
211                     'log': 'fake-log-pdh-2',
212                     'runtime_constraints': {
213                         'min_ram_mb_per_node': 900,
214                         'min_cores_per_node': 1,
215                     },
216                 },
217             }]]),
218     }
219
220     @mock.patch('arvados.collection.CollectionReader')
221     @mock.patch('arvados.api')
222     def test_pipeline(self, mock_api, mock_cr):
223         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
224         mock_api().pipeline_instances().get().execute. \
225             return_value = self.fake_instance
226         mock_cr().__iter__.return_value = ['fake-logfile.txt']
227         mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
228         args = crunchstat_summary.command.ArgumentParser().parse_args(
229             ['--pipeline-instance', self.fake_instance['uuid']])
230         cmd = crunchstat_summary.command.Command(args)
231         cmd.run()
232
233         with io.open(logfile+'.report', encoding='utf-8') as f:
234             job_report = [line for line in f if not line.startswith('#!! ')]
235         expect = (
236             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
237             job_report + ['\n'] +
238             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
239             job_report + ['\n'] +
240             ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
241              '(no report generated)\n',
242              '\n'] +
243             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
244             job_report)
245         self.diff_report(cmd, expect)
246         mock_cr.assert_has_calls(
247             [
248                 mock.call('fake-log-pdh-0'),
249                 mock.call('fake-log-pdh-1'),
250                 mock.call('fake-log-pdh-2'),
251             ], any_order=True)
252         mock_cr().open.assert_called_with('fake-logfile.txt')
253
254
255 class SummarizeACRJob(TestCase):
256     fake_job = {
257         'uuid': 'zzzzz-8i9sb-i3e77t9z5y8j9cc',
258         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
259         'components': {
260             'foo': 'zzzzz-8i9sb-000000000000000',
261             'bar': 'zzzzz-8i9sb-000000000000001',
262             'unfinished-job': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
263             'baz': 'zzzzz-8i9sb-000000000000002',
264         }
265     }
266     fake_jobs_index = { 'items': [
267         {
268             'uuid': 'zzzzz-8i9sb-000000000000000',
269             'log': 'fake-log-pdh-0',
270             'runtime_constraints': {
271                 'min_ram_mb_per_node': 900,
272                 'min_cores_per_node': 1,
273             },
274         },
275         {
276             'uuid': 'zzzzz-8i9sb-000000000000001',
277             'log': 'fake-log-pdh-1',
278             'runtime_constraints': {
279                 'min_ram_mb_per_node': 900,
280                 'min_cores_per_node': 1,
281             },
282         },
283         {
284             'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
285         },
286         {
287             'uuid': 'zzzzz-8i9sb-000000000000002',
288             'log': 'fake-log-pdh-2',
289             'runtime_constraints': {
290                 'min_ram_mb_per_node': 900,
291                 'min_cores_per_node': 1,
292             },
293         },
294     ]}
295     @mock.patch('arvados.collection.CollectionReader')
296     @mock.patch('arvados.api')
297     def test_acr_job(self, mock_api, mock_cr):
298         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
299         mock_api().jobs().index().execute.return_value = self.fake_jobs_index
300         mock_api().jobs().get().execute.return_value = self.fake_job
301         mock_cr().__iter__.return_value = ['fake-logfile.txt']
302         mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
303         args = crunchstat_summary.command.ArgumentParser().parse_args(
304             ['--job', self.fake_job['uuid']])
305         cmd = crunchstat_summary.command.Command(args)
306         cmd.run()
307
308         with io.open(logfile+'.report', encoding='utf-8') as f:
309             job_report = [line for line in f if not line.startswith('#!! ')]
310         expect = (
311             ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n',
312              '(no report generated)\n',
313              '\n'] +
314             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
315             job_report + ['\n'] +
316             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
317             job_report + ['\n'] +
318             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
319             job_report + ['\n'] +
320             ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
321              '(no report generated)\n']
322         )
323         self.diff_report(cmd, expect)
324         mock_cr.assert_has_calls(
325             [
326                 mock.call('fake-log-pdh-0'),
327                 mock.call('fake-log-pdh-1'),
328                 mock.call('fake-log-pdh-2'),
329             ], any_order=True)
330         mock_cr().open.assert_called_with('fake-logfile.txt')