1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
7 import crunchstat_summary.command
17 from unittest import mock
19 from crunchstat_summary.command import UTF8Decode
20 from crunchstat_summary import logger, reader
22 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
25 class TestCase(unittest.TestCase):
27 self.logbuf = io.StringIO()
28 self.loghandler = logging.StreamHandler(stream=self.logbuf)
29 logger.addHandler(self.loghandler)
30 logger.setLevel(logging.WARNING)
33 logger.removeHandler(self.loghandler)
35 def diff_known_report(self, logfile, cmd):
36 expectfile = logfile+'.report'
37 with io.open(expectfile, encoding='utf-8') as f:
38 expect = f.readlines()
39 self.diff_report(cmd, expect, expectfile=expectfile)
41 def diff_report(self, cmd, expect, expectfile='(expected)'):
42 got = [x+"\n" for x in cmd.report().strip("\n").split("\n")]
43 self.assertEqual(got, expect, "\n"+"".join(difflib.context_diff(
44 expect, got, fromfile=expectfile, tofile="(generated)")))
47 class SummarizeFile(TestCase):
48 def test_example_files(self):
49 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
50 logfile = os.path.join(TESTS_DIR, fnm)
51 args = crunchstat_summary.command.ArgumentParser().parse_args(
52 ['--log-file', logfile])
53 cmd = crunchstat_summary.command.Command(args)
55 self.diff_known_report(logfile, cmd)
58 class HTMLFromFile(TestCase):
59 def test_example_files(self):
60 # Note we don't test the output content at all yet; we're
61 # mainly just verifying the --format=html option isn't ignored
62 # and the HTML code path doesn't crash.
63 for fnm in glob.glob(os.path.join(TESTS_DIR, '*.txt.gz')):
64 logfile = os.path.join(TESTS_DIR, fnm)
65 args = crunchstat_summary.command.ArgumentParser().parse_args(
66 ['--format=html', '--log-file', logfile])
67 cmd = crunchstat_summary.command.Command(args)
69 self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
72 class SummarizeEdgeCases(TestCase):
73 def test_error_messages(self):
74 logfile = io.open(os.path.join(TESTS_DIR, 'crunchstat_error_messages.txt'), encoding='utf-8')
75 s = crunchstat_summary.summarizer.Summarizer(reader.StubReader(logfile))
77 self.assertRegex(self.logbuf.getvalue(), r'CPU stats are missing -- possible cluster configuration issue')
78 self.assertRegex(self.logbuf.getvalue(), r'memory stats are missing -- possible cluster configuration issue')
79 self.assertRegex(self.logbuf.getvalue(), r'network I/O stats are missing -- possible cluster configuration issue')
80 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 mock_api().container_requests().list().execute.side_effect = items # parent request
110 mock_api().container_requests().get().execute.return_value = self.fake_request
111 mock_api().containers().get().execute.return_value = self.fake_container
112 mock_cr().__iter__.return_value = [
113 'crunch-run.txt', 'stderr.txt', 'node-info.txt',
114 'container.json', 'crunchstat.txt', 'arv-mount.txt']
116 if n == "crunchstat.txt":
117 return UTF8Decode(gzip.open(self.logfile))
118 elif n == "arv-mount.txt":
119 return UTF8Decode(gzip.open(self.arvmountlog))
120 elif n == "node.json":
121 return io.StringIO("{}")
122 mock_cr().open.side_effect = _open
123 args = crunchstat_summary.command.ArgumentParser().parse_args(
125 cmd = crunchstat_summary.command.Command(args)
127 self.diff_known_report(self.reportfile, cmd)
131 class SummarizeContainer(SummarizeContainerCommon):
132 uuid = '9tee4-dz642-lymtndkpy39eibk'
133 reportfile = os.path.join(TESTS_DIR, 'container_%s.txt.gz' % uuid)
134 arg_strings = ['--container', uuid, '-v', '-v']
136 def test_container(self):
140 class SummarizeContainerRequest(SummarizeContainerCommon):
141 uuid = '9tee4-xvhdp-kk0ja1cl8b2kr1y'
142 reportfile = os.path.join(TESTS_DIR, 'container_request_%s.txt.gz' % uuid)
143 arg_strings = ['--container-request', uuid, '-v', '-v']
145 def test_container_request(self):
147 self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
148 self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')