8123: Add option (--format html) to generate canvasjs charts.
[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 SummarizeJob(ReportDiff):
38     fake_job_uuid = 'zzzzz-8i9sb-jjjjjjjjjjjjjjj'
39     fake_log_id = 'fake-log-collection-id'
40     fake_job = {
41         'uuid': fake_job_uuid,
42         'log': fake_log_id,
43     }
44     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
45
46     @mock.patch('arvados.collection.CollectionReader')
47     @mock.patch('arvados.api')
48     def test_job_report(self, mock_api, mock_cr):
49         mock_api().jobs().get().execute.return_value = self.fake_job
50         mock_cr().__iter__.return_value = ['fake-logfile.txt']
51         mock_cr().open.return_value = gzip.open(self.logfile)
52         args = crunchstat_summary.command.ArgumentParser().parse_args(
53             ['--job', self.fake_job_uuid])
54         cmd = crunchstat_summary.command.Command(args)
55         cmd.run()
56         self.diff_known_report(self.logfile, cmd)
57         mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
58         mock_cr.assert_called_with(self.fake_log_id)
59         mock_cr().open.assert_called_with('fake-logfile.txt')
60
61
62 class SummarizePipeline(ReportDiff):
63     fake_instance = {
64         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
65         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
66         'components': collections.OrderedDict([
67             ['foo', {
68                 'job': {
69                     'uuid': 'zzzzz-8i9sb-000000000000000',
70                     'log': 'fake-log-pdh-0',
71                     'runtime_constraints': {
72                         'min_ram_mb_per_node': 900,
73                         'min_cores_per_node': 1,
74                     },
75                 },
76             }],
77             ['bar', {
78                 'job': {
79                     'uuid': 'zzzzz-8i9sb-000000000000001',
80                     'log': 'fake-log-pdh-1',
81                     'runtime_constraints': {
82                         'min_ram_mb_per_node': 900,
83                         'min_cores_per_node': 1,
84                     },
85                 },
86             }],
87             ['no-job-assigned', {}],
88             ['unfinished-job', {
89                 'job': {
90                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
91                 },
92             }],
93             ['baz', {
94                 'job': {
95                     'uuid': 'zzzzz-8i9sb-000000000000002',
96                     'log': 'fake-log-pdh-2',
97                     'runtime_constraints': {
98                         'min_ram_mb_per_node': 900,
99                         'min_cores_per_node': 1,
100                     },
101                 },
102             }]]),
103     }
104
105     @mock.patch('arvados.collection.CollectionReader')
106     @mock.patch('arvados.api')
107     def test_pipeline(self, mock_api, mock_cr):
108         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
109         mock_api().pipeline_instances().get().execute. \
110             return_value = self.fake_instance
111         mock_cr().__iter__.return_value = ['fake-logfile.txt']
112         mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
113         args = crunchstat_summary.command.ArgumentParser().parse_args(
114             ['--pipeline-instance', self.fake_instance['uuid']])
115         cmd = crunchstat_summary.command.Command(args)
116         cmd.run()
117
118         job_report = [
119             line for line in open(logfile+'.report').readlines()
120             if not line.startswith('#!! ')]
121         expect = (
122             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
123             job_report + ['\n'] +
124             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
125             job_report + ['\n'] +
126             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
127             job_report)
128         self.diff_report(cmd, expect)
129         mock_cr.assert_has_calls(
130             [
131                 mock.call('fake-log-pdh-0'),
132                 mock.call('fake-log-pdh-1'),
133                 mock.call('fake-log-pdh-2'),
134             ], any_order=True)
135         mock_cr().open.assert_called_with('fake-logfile.txt')