Merge branch '8341-crunchstat-job-time-axis'
[arvados.git] / tools / crunchstat-summary / tests / test_examples.py
1 import arvados
2 import collections
3 import crunchstat_summary.command
4 import difflib
5 import glob
6 import gzip
7 import mock
8 import os
9 import unittest
10
11 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
12
13
14 class ReportDiff(unittest.TestCase):
15     def diff_known_report(self, logfile, cmd):
16         expectfile = logfile+'.report'
17         expect = open(expectfile).readlines()
18         self.diff_report(cmd, expect, expectfile=expectfile)
19
20     def diff_report(self, cmd, expect, expectfile=None):
21         got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
22         self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
23             expect, got, fromfile=expectfile, tofile="(generated)")))
24
25
26 class SummarizeFile(ReportDiff):
27     def test_example_files(self):
28         for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
29             logfile = os.path.join(TESTS_DIR, fnm)
30             args = crunchstat_summary.command.ArgumentParser().parse_args(
31                 ['--log-file', logfile])
32             cmd = crunchstat_summary.command.Command(args)
33             cmd.run()
34             self.diff_known_report(logfile, cmd)
35
36
37 class HTMLFromFile(ReportDiff):
38     def test_example_files(self):
39         # Note we don't test the output content at all yet; we're
40         # mainly just verifying the --format=html option isn't ignored
41         # and the HTML code path doesn't crash.
42         for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
43             logfile = os.path.join(TESTS_DIR, fnm)
44             args = crunchstat_summary.command.ArgumentParser().parse_args(
45                 ['--format=html', '--log-file', logfile])
46             cmd = crunchstat_summary.command.Command(args)
47             cmd.run()
48             self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
49
50
51 class SummarizeEdgeCases(unittest.TestCase):
52     def test_error_messages(self):
53         logfile = open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'))
54         s = crunchstat_summary.summarizer.Summarizer(logfile)
55         s.run()
56
57
58 class SummarizeJob(ReportDiff):
59     fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
60     fake_log_id = 'fake-log-collection-id'
61     fake_job = {
62         'uuid': fake_job_uuid,
63         'log': fake_log_id,
64     }
65     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
66
67     @mock.patch('arvados.collection.CollectionReader')
68     @mock.patch('arvados.api')
69     def test_job_report(self, mock_api, mock_cr):
70         mock_api().jobs().get().execute.return_value = self.fake_job
71         mock_cr().__iter__.return_value = ['fake-logfile.txt']
72         mock_cr().open.return_value = gzip.open(self.logfile)
73         args = crunchstat_summary.command.ArgumentParser().parse_args(
74             ['--job', self.fake_job_uuid])
75         cmd = crunchstat_summary.command.Command(args)
76         cmd.run()
77         self.diff_known_report(self.logfile, cmd)
78         mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
79         mock_cr.assert_called_with(self.fake_log_id)
80         mock_cr().open.assert_called_with('fake-logfile.txt')
81
82
83 class SummarizePipeline(ReportDiff):
84     fake_instance = {
85         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
86         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
87         'components': collections.OrderedDict([
88             ['foo', {
89                 'job': {
90                     'uuid': 'zzzzz-8i9sb-000000000000000',
91                     'log': 'fake-log-pdh-0',
92                     'runtime_constraints': {
93                         'min_ram_mb_per_node': 900,
94                         'min_cores_per_node': 1,
95                     },
96                 },
97             }],
98             ['bar', {
99                 'job': {
100                     'uuid': 'zzzzz-8i9sb-000000000000001',
101                     'log': 'fake-log-pdh-1',
102                     'runtime_constraints': {
103                         'min_ram_mb_per_node': 900,
104                         'min_cores_per_node': 1,
105                     },
106                 },
107             }],
108             ['no-job-assigned', {}],
109             ['unfinished-job', {
110                 'job': {
111                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
112                 },
113             }],
114             ['baz', {
115                 'job': {
116                     'uuid': 'zzzzz-8i9sb-000000000000002',
117                     'log': 'fake-log-pdh-2',
118                     'runtime_constraints': {
119                         'min_ram_mb_per_node': 900,
120                         'min_cores_per_node': 1,
121                     },
122                 },
123             }]]),
124     }
125
126     @mock.patch('arvados.collection.CollectionReader')
127     @mock.patch('arvados.api')
128     def test_pipeline(self, mock_api, mock_cr):
129         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
130         mock_api().pipeline_instances().get().execute. \
131             return_value = self.fake_instance
132         mock_cr().__iter__.return_value = ['fake-logfile.txt']
133         mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
134         args = crunchstat_summary.command.ArgumentParser().parse_args(
135             ['--pipeline-instance', self.fake_instance['uuid']])
136         cmd = crunchstat_summary.command.Command(args)
137         cmd.run()
138
139         job_report = [
140             line for line in open(logfile+'.report').readlines()
141             if not line.startswith('#!! ')]
142         expect = (
143             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
144             job_report + ['\n'] +
145             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
146             job_report + ['\n'] +
147             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
148             job_report)
149         self.diff_report(cmd, expect)
150         mock_cr.assert_has_calls(
151             [
152                 mock.call('fake-log-pdh-0'),
153                 mock.call('fake-log-pdh-1'),
154                 mock.call('fake-log-pdh-2'),
155             ], any_order=True)
156         mock_cr().open.assert_called_with('fake-logfile.txt')