19744: Remove jobs/pipeline templates from crunchstat-summary
[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 mock
14 import os
15 import sys
16 import unittest
17
18 from crunchstat_summary.command import UTF8Decode
19 from crunchstat_summary import logger, reader
20
21 TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
22
23
24 class TestCase(unittest.TestCase):
25     def setUp(self):
26         self.logbuf = io.StringIO()
27         self.loghandler = logging.StreamHandler(stream=self.logbuf)
28         logger.addHandler(self.loghandler)
29         logger.setLevel(logging.WARNING)
30
31     def tearDown(self):
32         logger.removeHandler(self.loghandler)
33
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)
39
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)")))
44
45
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)
53             cmd.run()
54             self.diff_known_report(logfile, cmd)
55
56
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)
67             cmd.run()
68             self.assertRegex(cmd.report(), r'(?is)<html>.*</html>\s*$')
69
70
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))
75         s.run()
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')
80
81 class SummarizeContainerCommon(TestCase):
82     fake_container = {
83         'uuid': '9tee4-dz642-lymtndkpy39eibk',
84         'created_at': '2017-08-18T14:27:25.371388141',
85         'log': '9tee4-4zz18-ihyzym9tcwjwg4r',
86     }
87     fake_request = {
88         'uuid': '9tee4-xvhdp-kk0ja1cl8b2kr1y',
89         'name': 'container',
90         'created_at': '2017-08-18T14:27:25.242339223Z',
91         'container_uuid': fake_container['uuid'],
92         'runtime_constraints': {
93             'vcpus': 1,
94             'ram': 2621440000
95             },
96         'log_uuid' : '9tee4-4zz18-m2swj50nk0r8b6y'
97         }
98
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')
103
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']
114         def _open(n, mode):
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(
123             self.arg_strings)
124         cmd = crunchstat_summary.command.Command(args)
125         cmd.run()
126         self.diff_known_report(self.reportfile, cmd)
127
128
129
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']
134
135     def test_container(self):
136         self.check_common()
137
138
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']
143
144     def test_container_request(self):
145         self.check_common()
146         self.assertNotRegex(self.logbuf.getvalue(), r'stats are missing')
147         self.assertNotRegex(self.logbuf.getvalue(), r'possible cluster configuration issue')