1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 import crunchstat_summary.command
18 from crunchstat_summary.command import UTF8Decode
19 from crunchstat_summary import logger
21 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
24 class TestCase(unittest.TestCase):
26 self.logbuf = io.StringIO()
27 self.loghandler = logging.StreamHandler(stream=self.logbuf)
28 logger.addHandler(self.loghandler)
29 logger.setLevel(logging.WARNING)
32 logger.removeHandler(self.loghandler)
34 def diff_known_report(self, logfile, cmd):
35 expectfile = logfile+'.report'
36 with io.open(expectfile, encoding='utf-8') as f:
37 expect = f.readlines()
38 self.diff_report(cmd, expect, expectfile=expectfile)
40 def diff_report(self, cmd, expect, expectfile='(expected)'):
41 got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
42 self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
43 expect, got, fromfile=expectfile, tofile="(generated)")))
46 class SummarizeFile(TestCase):
47 def test_example_files(self):
48 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
49 logfile = os.path.join(TESTS_DIR, fnm)
50 args = crunchstat_summary.command.ArgumentParser().parse_args(
51 ['--log-file', logfile])
52 cmd = crunchstat_summary.command.Command(args)
54 self.diff_known_report(logfile, cmd)
57 class HTMLFromFile(TestCase):
58 def test_example_files(self):
59 # Note we don't test the output content at all yet; we're
60 # mainly just verifying the --format=html option isn't ignored
61 # and the HTML code path doesn't crash.
62 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
63 logfile = os.path.join(TESTS_DIR, fnm)
64 args = crunchstat_summary.command.ArgumentParser().parse_args(
65 ['--format=html', '--log-file', logfile])
66 cmd = crunchstat_summary.command.Command(args)
68 self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
71 class SummarizeEdgeCases(TestCase):
72 def test_error_messages(self):
73 logfile = io.open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'), encoding='utf-8')
74 s = crunchstat_summary.summarizer.Summarizer(logfile)
76 self.assertRegex(self.logbuf.getvalue(), r'CPU stats are missing -- possible cluster configuration issue')
77 self.assertRegex(self.logbuf.getvalue(), r'memory stats are missing -- possible cluster configuration issue')
78 self.assertRegex(self.logbuf.getvalue(), r'network I/O stats are missing -- possible cluster configuration issue')
79 self.assertRegex(self.logbuf.getvalue(), r'storage space stats are missing -- possible cluster configuration issue')
82 class SummarizeContainerCommon(TestCase):
84 'uuid': '9tee4-dz642-lymtndkpy39eibk',
85 'created_at': '2017-08-18T14:27:25.371388141',
86 'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
89 'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
91 'created_at': '2017-08-18T14:27:25.242339223Z',
92 'container_uuid': fake_container['uuid'],
93 'runtime_constraints': {
97 'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
100 logfile = os.path.join(
101 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-crunchstat.txt.gz')
102 arvmountlog = os.path.join(
103 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-arv-mount.txt.gz')
105 @mock.patch('arvados.collection.CollectionReader')
106 @mock.patch('arvados.api')
107 def check_common(self, mock_api, mock_cr):
108 items = [ {'items':[self.fake_request]}] + [{'items':[]}] * 100
109 # Index and list mean the same thing, but are used in different places in the
110 # code. It's fragile, but exploit that fact to distinguish the two uses.
111 mock_api().container_requests().index().execute.return_value = {'items': [] } # child_crs
112 mock_api().container_requests().list().execute.side_effect = items # parent request
113 mock_api().container_requests().get().execute.return_value = self.fake_request
114 mock_api().containers().get().execute.return_value = self.fake_container
115 mock_cr().__iter__.return_value = [
116 'crunch-run.txt', 'stderr.txt', 'node-info.txt',
117 'container.json', 'crunchstat.txt', 'arv-mount.txt']
119 if n == "crunchstat.txt":
120 return UTF8Decode(gzip.open(self.logfile))
121 elif n == "arv-mount.txt":
122 return UTF8Decode(gzip.open(self.arvmountlog))
123 mock_cr().open.side_effect = _open
124 args = crunchstat_summary.command.ArgumentParser().parse_args(
126 cmd = crunchstat_summary.command.Command(args)
128 self.diff_known_report(self.reportfile, cmd)
132 class SummarizeContainer(SummarizeContainerCommon):
133 uuid = '9tee4-dz642-lymtndkpy39eibk'
134 reportfile = os.path.join(TESTS_DIR, 'container_%s.txt.gz' % uuid)
135 arg_strings = ['--container', uuid, '-v', '-v']
137 def test_container(self):
141 class SummarizeContainerRequest(SummarizeContainerCommon):
142 uuid = '9tee4-xvhdp-kk0ja1cl8b2kr1y'
143 reportfile = os.path.join(TESTS_DIR, 'container_request_%s.txt.gz' % uuid)
144 arg_strings = ['--container-request', uuid, '-v', '-v']
146 def test_container_request(self):
148 self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
149 self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')
152 class SummarizeJob(TestCase):
153 fake_job_uuid = '4xphq-8i9sb-jq0ekny1xou3zoh'
154 fake_log_id = 'fake-log-collection-id'
156 'uuid': fake_job_uuid,
159 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
161 @mock.patch('arvados.collection.CollectionReader')
162 @mock.patch('arvados.api')
163 def test_job_report(self, mock_api, mock_cr):
164 mock_api().jobs().get().execute.return_value = self.fake_job
165 mock_cr().__iter__.return_value = ['fake-logfile.txt']
166 mock_cr().open.return_value = UTF8Decode(gzip.open(self.logfile))
167 args = crunchstat_summary.command.ArgumentParser().parse_args(
168 ['--job', self.fake_job_uuid])
169 cmd = crunchstat_summary.command.Command(args)
171 self.diff_known_report(self.logfile, cmd)
172 mock_api().jobs().get.assert_called_with(uuid=self.fake_job_uuid)
173 mock_cr.assert_called_with(self.fake_log_id)
174 mock_cr().open.assert_called_with('fake-logfile.txt')
177 class SummarizePipeline(TestCase):
179 'uuid': 'zzzzz-d1hrv-i3e77t9z5y8j9cc',
180 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
181 'components': collections.OrderedDict([
184 'uuid': 'zzzzz-8i9sb-000000000000000',
185 'log': 'fake-log-pdh-0',
186 'runtime_constraints': {
187 'min_ram_mb_per_node': 900,
188 'min_cores_per_node': 1,
194 'uuid': 'zzzzz-8i9sb-000000000000001',
195 'log': 'fake-log-pdh-1',
196 'runtime_constraints': {
197 'min_ram_mb_per_node': 900,
198 'min_cores_per_node': 1,
202 ['no-job-assigned', {}],
205 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
210 'uuid': 'zzzzz-8i9sb-000000000000002',
211 'log': 'fake-log-pdh-2',
212 'runtime_constraints': {
213 'min_ram_mb_per_node': 900,
214 'min_cores_per_node': 1,
220 @mock.patch('arvados.collection.CollectionReader')
221 @mock.patch('arvados.api')
222 def test_pipeline(self, mock_api, mock_cr):
223 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
224 mock_api().pipeline_instances().get().execute. \
225 return_value = self.fake_instance
226 mock_cr().__iter__.return_value = ['fake-logfile.txt']
227 mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
228 args = crunchstat_summary.command.ArgumentParser().parse_args(
229 ['--pipeline-instance', self.fake_instance['uuid']])
230 cmd = crunchstat_summary.command.Command(args)
233 with io.open(logfile+'.report', encoding='utf-8') as f:
234 job_report = [line for line in f if not line.startswith('#!! ')]
236 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
237 job_report + ['\n'] +
238 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
239 job_report + ['\n'] +
240 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
241 '(no report generated)\n',
243 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
245 self.diff_report(cmd, expect)
246 mock_cr.assert_has_calls(
248 mock.call('fake-log-pdh-0'),
249 mock.call('fake-log-pdh-1'),
250 mock.call('fake-log-pdh-2'),
252 mock_cr().open.assert_called_with('fake-logfile.txt')
255 class SummarizeACRJob(TestCase):
257 'uuid': 'zzzzz-8i9sb-i3e77t9z5y8j9cc',
258 'owner_uuid': 'zzzzz-tpzed-xurymjxw79nv3jz',
260 'foo': 'zzzzz-8i9sb-000000000000000',
261 'bar': 'zzzzz-8i9sb-000000000000001',
262 'unfinished-job': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
263 'baz': 'zzzzz-8i9sb-000000000000002',
266 fake_jobs_index = { 'items': [
268 'uuid': 'zzzzz-8i9sb-000000000000000',
269 'log': 'fake-log-pdh-0',
270 'runtime_constraints': {
271 'min_ram_mb_per_node': 900,
272 'min_cores_per_node': 1,
276 'uuid': 'zzzzz-8i9sb-000000000000001',
277 'log': 'fake-log-pdh-1',
278 'runtime_constraints': {
279 'min_ram_mb_per_node': 900,
280 'min_cores_per_node': 1,
284 'uuid': 'zzzzz-8i9sb-xxxxxxxxxxxxxxx',
287 'uuid': 'zzzzz-8i9sb-000000000000002',
288 'log': 'fake-log-pdh-2',
289 'runtime_constraints': {
290 'min_ram_mb_per_node': 900,
291 'min_cores_per_node': 1,
295 @mock.patch('arvados.collection.CollectionReader')
296 @mock.patch('arvados.api')
297 def test_acr_job(self, mock_api, mock_cr):
298 logfile = os.path.join(TESTS_DIR, 'logfile_20151204190335.txt.gz')
299 mock_api().jobs().index().execute.return_value = self.fake_jobs_index
300 mock_api().jobs().get().execute.return_value = self.fake_job
301 mock_cr().__iter__.return_value = ['fake-logfile.txt']
302 mock_cr().open.side_effect = [UTF8Decode(gzip.open(logfile)) for _ in range(3)]
303 args = crunchstat_summary.command.ArgumentParser().parse_args(
304 ['--job', self.fake_job['uuid']])
305 cmd = crunchstat_summary.command.Command(args)
308 with io.open(logfile+'.report', encoding='utf-8') as f:
309 job_report = [line for line in f if not line.startswith('#!! ')]
311 ['### Summary for zzzzz-8i9sb-i3e77t9z5y8j9cc (partial) (zzzzz-8i9sb-i3e77t9z5y8j9cc)\n',
312 '(no report generated)\n',
314 ['### Summary for bar (zzzzz-8i9sb-000000000000001)\n'] +
315 job_report + ['\n'] +
316 ['### Summary for baz (zzzzz-8i9sb-000000000000002)\n'] +
317 job_report + ['\n'] +
318 ['### Summary for foo (zzzzz-8i9sb-000000000000000)\n'] +
319 job_report + ['\n'] +
320 ['### Summary for unfinished-job (partial) (zzzzz-8i9sb-xxxxxxxxxxxxxxx)\n',
321 '(no report generated)\n']
323 self.diff_report(cmd, expect)
324 mock_cr.assert_has_calls(
326 mock.call('fake-log-pdh-0'),
327 mock.call('fake-log-pdh-1'),
328 mock.call('fake-log-pdh-2'),
330 mock_cr().open.assert_called_with('fake-logfile.txt')