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, reader
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(reader.StubReader(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')
81 class SummarizeContainerCommon(TestCase):
83 'uuid': '9tee4-dz642-lymtndkpy39eibk',
84 'created_at': '2017-08-18T14:27:25.371388141',
85 'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
88 'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
90 'created_at': '2017-08-18T14:27:25.242339223Z',
91 'container_uuid': fake_container['uuid'],
92 'runtime_constraints': {
96 'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
99 logfile = os.path.join(
100 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-crunchstat.txt.gz')
101 arvmountlog = os.path.join(
102 TESTS_DIR, 'container_request_9tee4-xvhdp-kk0ja1cl8b2kr1y-arv-mount.txt.gz')
104 @mock.patch('arvados.collection.CollectionReader')
105 @mock.patch('arvados.api')
106 def check_common(self, mock_api, mock_cr):
107 items = [ {'items':[self.fake_request]}] + [{'items':[]}] * 100
108 mock_api().container_requests().list().execute.side_effect = items # parent request
109 mock_api().container_requests().get().execute.return_value = self.fake_request
110 mock_api().containers().get().execute.return_value = self.fake_container
111 mock_cr().__iter__.return_value = [
112 'crunch-run.txt', 'stderr.txt', 'node-info.txt',
113 'container.json', 'crunchstat.txt', 'arv-mount.txt']
115 if n == "crunchstat.txt":
116 return UTF8Decode(gzip.open(self.logfile))
117 elif n == "arv-mount.txt":
118 return UTF8Decode(gzip.open(self.arvmountlog))
119 elif n == "node.json":
120 return io.StringIO("{}")
121 mock_cr().open.side_effect = _open
122 args = crunchstat_summary.command.ArgumentParser().parse_args(
124 cmd = crunchstat_summary.command.Command(args)
126 self.diff_known_report(self.reportfile, cmd)
130 class SummarizeContainer(SummarizeContainerCommon):
131 uuid = '9tee4-dz642-lymtndkpy39eibk'
132 reportfile = os.path.join(TESTS_DIR, 'container_%s.txt.gz' % uuid)
133 arg_strings = ['--container', uuid, '-v', '-v']
135 def test_container(self):
139 class SummarizeContainerRequest(SummarizeContainerCommon):
140 uuid = '9tee4-xvhdp-kk0ja1cl8b2kr1y'
141 reportfile = os.path.join(TESTS_DIR, 'container_request_%s.txt.gz' % uuid)
142 arg_strings = ['--container-request', uuid, '-v', '-v']
144 def test_container_request(self):
146 self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
147 self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')