11309: Fix runtime_constraints suggestions for containers.
[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 SummarizeContainer(ReportDiff):
63     fake_container = {
64         'uuid': '9tee4-dz642-mjfb0i5hzojp16a',
65         'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
66     }
67     fake_request = {
68         'uuid': '9tee4-xvhdp-uper95jktm10d3w',
69         'name': 'container',
70         'container_uuid': fake_container['uuid'],
71     }
72     logfile = os.path.join(
73         TESTS_DIR, 'container_9tee4-dz642-mjfb0i5hzojp16a-crunchstat.txt.gz')
74
75     @mock.patch('arvados.collection.CollectionReader')
76     @mock.patch('arvados.api')
77     def test_container(self, mock_api, mock_cr):
78         mock_api().container_requests().index().execute.return_value = {'items':[]}
79         mock_api().container_requests().get().execute.return_value = self.fake_request
80         mock_api().containers().get().execute.return_value = self.fake_container
81         mock_cr().__iter__.return_value = [
82             'crunch-run.txt', 'stderr.txt', 'node-info.txt',
83             'container.json', 'crunchstat.txt']
84         mock_cr().open.return_value = gzip.open(self.logfile)
85         args = crunchstat_summary.command.ArgumentParser().parse_args(
86             ['--job', self.fake_request['uuid']])
87         cmd = crunchstat_summary.command.Command(args)
88         cmd.run()
89         self.diff_known_report(self.logfile, cmd)
90
91
92 class SummarizeJob(ReportDiff):
93     fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
94     fake_log_id = 'fake-log-collection-id'
95     fake_job = {
96         'uuid': fake_job_uuid,
97         'log': fake_log_id,
98     }
99     logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
100
101     @mock.patch('arvados.collection.CollectionReader')
102     @mock.patch('arvados.api')
103     def test_job_report(self, mock_api, mock_cr):
104         mock_api().jobs().get().execute.return_value = self.fake_job
105         mock_cr().__iter__.return_value = ['fake-logfile.txt']
106         mock_cr().open.return_value = gzip.open(self.logfile)
107         args = crunchstat_summary.command.ArgumentParser().parse_args(
108             ['--job', self.fake_job_uuid])
109         cmd = crunchstat_summary.command.Command(args)
110         cmd.run()
111         self.diff_known_report(self.logfile, cmd)
112         mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
113         mock_cr.assert_called_with(self.fake_log_id)
114         mock_cr().open.assert_called_with('fake-logfile.txt')
115
116
117 class SummarizePipeline(ReportDiff):
118     fake_instance = {
119         'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
120         'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
121         'components': collections.OrderedDict([
122             ['foo', {
123                 'job': {
124                     'uuid': 'zzzzz-8i9sb-000000000000000',
125                     'log': 'fake-log-pdh-0',
126                     'runtime_constraints': {
127                         'min_ram_mb_per_node': 900,
128                         'min_cores_per_node': 1,
129                     },
130                 },
131             }],
132             ['bar', {
133                 'job': {
134                     'uuid': 'zzzzz-8i9sb-000000000000001',
135                     'log': 'fake-log-pdh-1',
136                     'runtime_constraints': {
137                         'min_ram_mb_per_node': 900,
138                         'min_cores_per_node': 1,
139                     },
140                 },
141             }],
142             ['no-job-assigned', {}],
143             ['unfinished-job', {
144                 'job': {
145                     'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
146                 },
147             }],
148             ['baz', {
149                 'job': {
150                     'uuid': 'zzzzz-8i9sb-000000000000002',
151                     'log': 'fake-log-pdh-2',
152                     'runtime_constraints': {
153                         'min_ram_mb_per_node': 900,
154                         'min_cores_per_node': 1,
155                     },
156                 },
157             }]]),
158     }
159
160     @mock.patch('arvados.collection.CollectionReader')
161     @mock.patch('arvados.api')
162     def test_pipeline(self, mock_api, mock_cr):
163         logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
164         mock_api().pipeline_instances().get().execute. \
165             return_value = self.fake_instance
166         mock_cr().__iter__.return_value = ['fake-logfile.txt']
167         mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
168         args = crunchstat_summary.command.ArgumentParser().parse_args(
169             ['--pipeline-instance', self.fake_instance['uuid']])
170         cmd = crunchstat_summary.command.Command(args)
171         cmd.run()
172
173         job_report = [
174             line for line in open(logfile+'.report').readlines()
175             if not line.startswith('#!! ')]
176         expect = (
177             ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
178             job_report + ['\n'] +
179             ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
180             job_report + ['\n'] +
181             ['### Summary for unfinished-job (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
182              '(no report generated)\n',
183              '\n'] +
184             ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
185             job_report)
186         self.diff_report(cmd, expect)
187         mock_cr.assert_has_calls(
188             [
189                 mock.call('fake-log-pdh-0'),
190                 mock.call('fake-log-pdh-1'),
191                 mock.call('fake-log-pdh-2'),
192             ], any_order=True)
193         mock_cr().open.assert_called_with('fake-logfile.txt')