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 SummarizeJob(ReportDiff):
38 fake_job_uuid = 'zzzzz-8i9sb-jjjjjjjjjjjjjjj'
39 fake_log_id = 'fake-log-collection-id'
41 'uuid': fake_job_uuid,
44 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
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)
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')
62 class SummarizePipeline(ReportDiff):
64 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
65 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
66 'components': collections.OrderedDict([
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,
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,
87 ['no-job-assigned', {}],
90 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
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,
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)
119 line for line in open(logfile+'.report').readlines()
120 if not line.startswith('#!! ')]
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'] +
128 self.diff_report(cmd, expect)
129 mock_cr.assert_has_calls(
131 mock.call('fake-log-pdh-0'),
132 mock.call('fake-log-pdh-1'),
133 mock.call('fake-log-pdh-2'),
135 mock_cr().open.assert_called_with('fake-logfile.txt')