3 import crunchstat_summary.command
4 import crunchstat_summary.summarizer
13 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
15 class ReportDiff(unittest.TestCase):
16 def diff_known_report(self, logfile, summarizer):
17 expectfile = logfile+'.report'
18 expect = open(expectfile).readlines()
19 self.diff_report(summarizer, expect, expectfile=expectfile)
21 def diff_report(self, summarizer, expect, expectfile=None):
22 got = [x+"\n" for x in summarizer.report().strip("\n").split("\n")]
23 self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
24 expect, got, fromfile=expectfile, tofile="(generated)")))
27 class SummarizeFile(ReportDiff):
28 def test_example_files(self):
29 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
30 logfile = os.path.join(TESTS_DIR, fnm)
31 args = crunchstat_summary.command.ArgumentParser().parse_args(
32 ['--log-file', logfile])
33 summarizer = crunchstat_summary.command.Command(args).summarizer()
35 self.diff_known_report(logfile, summarizer)
38 class SummarizeJob(ReportDiff):
39 fake_job_uuid = 'zzzzz-8i9sb-jjjjjjjjjjjjjjj'
40 fake_log_id = 'fake-log-collection-id'
42 'uuid': fake_job_uuid,
45 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
47 @mock.patch('arvados.collection.CollectionReader')
48 @mock.patch('arvados.api')
49 def test_job_report(self, mock_api, mock_cr):
50 mock_api().jobs().get().execute.return_value = self.fake_job
51 mock_cr().__iter__.return_value = ['fake-logfile.txt']
52 mock_cr().open.return_value = gzip.open(self.logfile)
53 args = crunchstat_summary.command.ArgumentParser().parse_args(
54 ['--job', self.fake_job_uuid])
55 summarizer = crunchstat_summary.command.Command(args).summarizer()
57 self.diff_known_report(self.logfile, summarizer)
58 mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
59 mock_cr.assert_called_with(self.fake_log_id)
60 mock_cr().open.assert_called_with('fake-logfile.txt')
63 class SummarizePipeline(ReportDiff):
65 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
66 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
67 'components': collections.OrderedDict([
70 'uuid': 'zzzzz-8i9sb-000000000000000',
71 'log': 'fake-log-pdh-0',
72 'runtime_constraints': {
73 'min_ram_mb_per_node': 1024,
74 'min_cores_per_node': 1,
80 'uuid': 'zzzzz-8i9sb-000000000000001',
81 'log': 'fake-log-pdh-1',
82 'runtime_constraints': {
83 'min_ram_mb_per_node': 1024,
84 'min_cores_per_node': 1,
88 ['no-job-assigned', {}],
91 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
96 'uuid': 'zzzzz-8i9sb-000000000000002',
97 'log': 'fake-log-pdh-2',
98 'runtime_constraints': {
99 'min_ram_mb_per_node': 1024,
100 'min_cores_per_node': 1,
106 @mock.patch('arvados.collection.CollectionReader')
107 @mock.patch('arvados.api')
108 def test_pipeline(self, mock_api, mock_cr):
109 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
110 mock_api().pipeline_instances().get().execute. \
111 return_value = self.fake_instance
112 mock_cr().__iter__.return_value = ['fake-logfile.txt']
113 mock_cr().open.side_effect = [gzip.open(logfile) for _ in range(3)]
114 args = crunchstat_summary.command.ArgumentParser().parse_args(
115 ['--pipeline-instance', self.fake_instance['uuid']])
116 summarizer = crunchstat_summary.command.Command(args).summarizer()
120 line for line in open(logfile+'.report').readlines()
121 if not line.startswith('#!! ')]
123 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
124 job_report + ['\n'] +
125 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
126 job_report + ['\n'] +
127 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
129 self.diff_report(summarizer, expect)
130 mock_cr.assert_has_calls(
132 mock.call('fake-log-pdh-0'),
133 mock.call('fake-log-pdh-1'),
134 mock.call('fake-log-pdh-2'),
136 mock_cr().open.assert_called_with('fake-logfile.txt')