Arvados-DCO-1.1-Signed-off-by: Radhika Chippada <radhika@curoverse.com>
[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 mock
12 import os
13 import unittest
14
15 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
16
17
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)
23
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)")))
28
29
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)
37             cmd.run()
38             self.diff_known_report(logfile, cmd)
39
40
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)
51             cmd.run()
52             self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
53
54
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)
59         s.run()
60
61
62 class SummarizeJob(ReportDiff):
63     fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
64     fake_log_id = 'fake-log-collection-id'
65     fake_job = {
66         'uuid': fake_job_uuid,
67         'log': fake_log_id,
68     }
69     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
70
71     @mock.patch('arvados.collection.CollectionReader')
72     @mock.patch('arvados.api')
73     def test_job_report(self, mock_api, mock_cr):
74         mock_api().jobs().get().execute.return_value = self.fake_job
75         mock_cr().__iter__.return_value = ['fake-logfile.txt']
76         mock_cr().open.return_value = gzip.open(self.logfile)
77         args = crunchstat_summary.command.ArgumentParser().parse_args(
78             ['--job', self.fake_job_uuid])
79         cmd = crunchstat_summary.command.Command(args)
80         cmd.run()
81         self.diff_known_report(self.logfile, cmd)
82         mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
83         mock_cr.assert_called_with(self.fake_log_id)
84         mock_cr().open.assert_called_with('fake-logfile.txt')
85
86
87 class SummarizePipeline(ReportDiff):
88     fake_instance = {
89         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
90         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
91         'components': collections.OrderedDict([
92             ['foo', {
93                 'job': {
94                     'uuid': 'zzzzz-8i9sb-000000000000000',
95                     'log': 'fake-log-pdh-0',
96                     'runtime_constraints': {
97                         'min_ram_mb_per_node': 900,
98                         'min_cores_per_node': 1,
99                     },
100                 },
101             }],
102             ['bar', {
103                 'job': {
104                     'uuid': 'zzzzz-8i9sb-000000000000001',
105                     'log': 'fake-log-pdh-1',
106                     'runtime_constraints': {
107                         'min_ram_mb_per_node': 900,
108                         'min_cores_per_node': 1,
109                     },
110                 },
111             }],
112             ['no-job-assigned', {}],
113             ['unfinished-job', {
114                 'job': {
115                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
116                 },
117             }],
118             ['baz', {
119                 'job': {
120                     'uuid': 'zzzzz-8i9sb-000000000000002',
121                     'log': 'fake-log-pdh-2',
122                     'runtime_constraints': {
123                         'min_ram_mb_per_node': 900,
124                         'min_cores_per_node': 1,
125                     },
126                 },
127             }]]),
128     }
129
130     @mock.patch('arvados.collection.CollectionReader')
131     @mock.patch('arvados.api')
132     def test_pipeline(self, mock_api, mock_cr):
133         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
134         mock_api().pipeline_instances().get().execute. \
135             return_value = self.fake_instance
136         mock_cr().__iter__.return_value = ['fake-logfile.txt']
137         mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
138         args = crunchstat_summary.command.ArgumentParser().parse_args(
139             ['--pipeline-instance', self.fake_instance['uuid']])
140         cmd = crunchstat_summary.command.Command(args)
141         cmd.run()
142
143         job_report = [
144             line for line in open(logfile+'.report').readlines()
145             if not line.startswith('#!! ')]
146         expect = (
147             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
148             job_report + ['\n'] +
149             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
150             job_report + ['\n'] +
151             ['### Summary for unfinished-job (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
152              '(no report generated)\n',
153              '\n'] +
154             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
155             job_report)
156         self.diff_report(cmd, expect)
157         mock_cr.assert_has_calls(
158             [
159                 mock.call('fake-log-pdh-0'),
160                 mock.call('fake-log-pdh-1'),
161                 mock.call('fake-log-pdh-2'),
162             ], any_order=True)
163         mock_cr().open.assert_called_with('fake-logfile.txt')