21700: Install Bundler system-wide in Rails postinst
[arvados.git] / tools / crunchstat-summary / tests / test_examples.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 import arvados
6 import collections
7 import crunchstat_summary.command
8 import difflib
9 import glob
10 import gzip
11 import io
12 import logging
13 import os
14 import sys
15 import unittest
16
17 from unittest import mock
18
19 from crunchstat_summary.command import UTF8Decode
20 from crunchstat_summary import logger, reader
21
22 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
23
24
25 class TestCase(unittest.TestCase):
26     def setUp(self):
27         self.logbuf = io.StringIO()
28         self.loghandler = logging.StreamHandler(stream=self.logbuf)
29         logger.addHandler(self.loghandler)
30         logger.setLevel(logging.WARNING)
31
32     def tearDown(self):
33         logger.removeHandler(self.loghandler)
34
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)
40
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)")))
45
46
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)
54             cmd.run()
55             self.diff_known_report(logfile, cmd)
56
57
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)
68             cmd.run()
69             self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
70
71
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))
76         s.run()
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')
81
82 class SummarizeContainerCommon(TestCase):
83     fake_container = {
84         'uuid': '9tee4-dz642-lymtndkpy39eibk',
85         'created_at': '2017-08-18T14:27:25.371388141',
86         'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
87     }
88     fake_request = {
89         'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
90         'name': 'container',
91         'created_at': '2017-08-18T14:27:25.242339223Z',
92         'container_uuid': fake_container['uuid'],
93         'runtime_constraints': {
94             'vcpus': 1,
95             'ram': 2621440000
96             },
97         'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
98         }
99
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')
104
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']
115         def _open(n, mode):
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(
124             self.arg_strings)
125         cmd = crunchstat_summary.command.Command(args)
126         cmd.run()
127         self.diff_known_report(self.reportfile, cmd)
128
129
130
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']
135
136     def test_container(self):
137         self.check_common()
138
139
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']
144
145     def test_container_request(self):
146         self.check_common()
147         self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
148         self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')