3 import crunchstat_summary.command
11 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
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)
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)")))
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)
34 self.diff_known_report(logfile, cmd)
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)
48 self.assertRegexpMatches(cmd.report(), r'(?is)<html>.*</html>\s*$')
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)
58 class SummarizeJob(ReportDiff):
59 fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
60 fake_log_id = 'fake-log-collection-id'
62 'uuid': fake_job_uuid,
65 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
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)
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')
83 class SummarizePipeline(ReportDiff):
85 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
86 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
87 'components': collections.OrderedDict([
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,
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,
108 ['no-job-assigned', {}],
111 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
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,
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)
140 line for line in open(logfile+'.report').readlines()
141 if not line.startswith('#!! ')]
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'] +
149 self.diff_report(cmd, expect)
150 mock_cr.assert_has_calls(
152 mock.call('fake-log-pdh-0'),
153 mock.call('fake-log-pdh-1'),
154 mock.call('fake-log-pdh-2'),
156 mock_cr().open.assert_called_with('fake-logfile.txt')