b498299a96b5afcee9ad1c7cf6afd59e538c0c03
[yardstick.git] / yardstick / tests / unit / benchmark / core / test_report.py
1 ##############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka.
3 # Copyright (c) 2018-2019 Intel Corporation.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 import mock
12 import six
13 import unittest
14 import uuid
15
16 from api.utils import influx
17 from yardstick.benchmark.core import report
18 from yardstick.cmd.commands import change_osloobj_to_paras
19
20 GOOD_YAML_NAME = 'fake_name'
21 GOOD_TASK_ID = str(uuid.uuid4())
22 GOOD_DB_FIELDKEYS = [{'fieldKey': 'fake_key'}]
23 GOOD_DB_METRICS = [{
24         'fake_key': 1.234,
25         'time': '0000-00-00T12:34:56.789012Z',
26         }]
27 GOOD_TIMESTAMP = ['12:34:56.789012']
28 BAD_YAML_NAME = 'F@KE_NAME'
29 BAD_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa'
30 MORE_DB_FIELDKEYS = [
31         {'fieldKey': 'fake_key'},
32         {'fieldKey': 'str_str'},
33         {'fieldKey': u'str_unicode'},
34         {u'fieldKey': 'unicode_str'},
35         {u'fieldKey': u'unicode_unicode'},
36         ]
37 MORE_DB_METRICS = [{
38         'fake_key': None,
39         'time': '0000-00-00T00:00:00.000000Z',
40         }, {
41         'fake_key': 123,
42         'time': '0000-00-00T00:00:01.000000Z',
43         }, {
44         'fake_key': 4.56,
45         'time': '0000-00-00T00:00:02.000000Z',
46         }, {
47         'fake_key': 9876543210987654321,
48         'time': '0000-00-00T00:00:03.000000Z',
49         }, {
50         'fake_key': 'str_str value',
51         'time': '0000-00-00T00:00:04.000000Z',
52         }, {
53         'fake_key': u'str_unicode value',
54         'time': '0000-00-00T00:00:05.000000Z',
55         }, {
56         u'fake_key': 'unicode_str value',
57         'time': '0000-00-00T00:00:06.000000Z',
58         }, {
59         u'fake_key': u'unicode_unicode value',
60         'time': '0000-00-00T00:00:07.000000Z',
61         }, {
62         'fake_key': '7.89',
63         'time': '0000-00-00T00:00:08.000000Z',
64         }, {
65         'fake_key': '1011',
66         'time': '0000-00-00T00:00:09.000000Z',
67         }, {
68         'fake_key': '9876543210123456789',
69         'time': '0000-00-00T00:00:10.000000Z',
70         }]
71 MORE_TIMESTAMP = ['00:00:%02d.000000' % n for n in range(len(MORE_DB_METRICS))]
72 MORE_EMPTY_DATA = [None] * len(MORE_DB_METRICS)
73 MORE_EXPECTED_TABLE_VALS = {
74         'Timestamp': MORE_TIMESTAMP,
75         'fake_key': [
76             None,
77             123,
78             4.56,
79             9876543210987654321 if six.PY3 else 9.876543210987655e+18,
80             'str_str value',
81             'str_unicode value',
82             'unicode_str value',
83             'unicode_unicode value',
84             7.89,
85             1011,
86             9876543210123456789 if six.PY3 else 9.876543210123457e+18,
87             ],
88         'str_str': MORE_EMPTY_DATA,
89         'str_unicode': MORE_EMPTY_DATA,
90         'unicode_str': MORE_EMPTY_DATA,
91         'unicode_unicode': MORE_EMPTY_DATA,
92         }
93 MORE_EXPECTED_DATASETS = [{
94         'label': key,
95         'data': MORE_EXPECTED_TABLE_VALS[key],
96         }
97         for key in map(str, [field['fieldKey'] for field in MORE_DB_FIELDKEYS])
98         ]
99
100
101 class JSTreeTestCase(unittest.TestCase):
102
103     def setUp(self):
104         self.jstree = report.JSTree()
105
106     def test__create_node(self):
107         _id = "tg__0.DropPackets"
108
109         expected_data = [
110             {"id": "tg__0", "text": "tg__0", "parent": "#"},
111             {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"}
112         ]
113         self.jstree._create_node(_id)
114
115         self.assertEqual(self.jstree._created_nodes, ['#', 'tg__0', 'tg__0.DropPackets'])
116         self.assertEqual(self.jstree.jstree_data, expected_data)
117
118     def test_format_for_jstree(self):
119         data = [
120             'tg__0.DropPackets',
121             'tg__0.LatencyAvg.5', 'tg__0.LatencyAvg.6',
122             'tg__0.LatencyMax.5', 'tg__0.LatencyMax.6',
123             'tg__0.RxThroughput', 'tg__0.TxThroughput',
124             'tg__1.DropPackets',
125             'tg__1.LatencyAvg.5', 'tg__1.LatencyAvg.6',
126             'tg__1.LatencyMax.5', 'tg__1.LatencyMax.6',
127             'tg__1.RxThroughput', 'tg__1.TxThroughput',
128             'vnf__0.curr_packets_in', 'vnf__0.packets_dropped', 'vnf__0.packets_fwd',
129         ]
130
131         expected_output = [
132             {"id": "tg__0", "text": "tg__0", "parent": "#"},
133                 {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"},
134                 {"id": "tg__0.LatencyAvg", "text": "LatencyAvg", "parent": "tg__0"},
135                     {"id": "tg__0.LatencyAvg.5", "text": "5", "parent": "tg__0.LatencyAvg"},
136                     {"id": "tg__0.LatencyAvg.6", "text": "6", "parent": "tg__0.LatencyAvg"},
137                 {"id": "tg__0.LatencyMax", "text": "LatencyMax", "parent": "tg__0"},
138                     {"id": "tg__0.LatencyMax.5", "text": "5", "parent": "tg__0.LatencyMax"},
139                     {"id": "tg__0.LatencyMax.6", "text": "6", "parent": "tg__0.LatencyMax"},
140                 {"id": "tg__0.RxThroughput", "text": "RxThroughput", "parent": "tg__0"},
141                 {"id": "tg__0.TxThroughput", "text": "TxThroughput", "parent": "tg__0"},
142             {"id": "tg__1", "text": "tg__1", "parent": "#"},
143                 {"id": "tg__1.DropPackets", "text": "DropPackets", "parent": "tg__1"},
144                 {"id": "tg__1.LatencyAvg", "text": "LatencyAvg", "parent": "tg__1"},
145                     {"id": "tg__1.LatencyAvg.5", "text": "5", "parent": "tg__1.LatencyAvg"},
146                     {"id": "tg__1.LatencyAvg.6", "text": "6", "parent": "tg__1.LatencyAvg"},
147                 {"id": "tg__1.LatencyMax", "text": "LatencyMax", "parent": "tg__1"},
148                     {"id": "tg__1.LatencyMax.5", "text": "5", "parent": "tg__1.LatencyMax"},
149                     {"id": "tg__1.LatencyMax.6", "text": "6", "parent": "tg__1.LatencyMax"},
150                 {"id": "tg__1.RxThroughput", "text": "RxThroughput", "parent": "tg__1"},
151                 {"id": "tg__1.TxThroughput", "text": "TxThroughput", "parent": "tg__1"},
152             {"id": "vnf__0", "text": "vnf__0", "parent": "#"},
153                 {"id": "vnf__0.curr_packets_in", "text": "curr_packets_in", "parent": "vnf__0"},
154                 {"id": "vnf__0.packets_dropped", "text": "packets_dropped", "parent": "vnf__0"},
155                 {"id": "vnf__0.packets_fwd", "text": "packets_fwd", "parent": "vnf__0"},
156         ]
157
158         result = self.jstree.format_for_jstree(data)
159         self.assertEqual(expected_output, result)
160
161
162 class ReportTestCase(unittest.TestCase):
163
164     def setUp(self):
165         super(ReportTestCase, self).setUp()
166         self.param = change_osloobj_to_paras({})
167         self.param.yaml_name = [GOOD_YAML_NAME]
168         self.param.task_id = [GOOD_TASK_ID]
169         self.rep = report.Report()
170
171     def test___init__(self):
172         self.assertEqual([], self.rep.Timestamp)
173         self.assertEqual("", self.rep.yaml_name)
174         self.assertEqual("", self.rep.task_id)
175
176     def test__validate(self):
177         self.rep._validate(GOOD_YAML_NAME, GOOD_TASK_ID)
178         self.assertEqual(GOOD_YAML_NAME, self.rep.yaml_name)
179         self.assertEqual(GOOD_TASK_ID, str(self.rep.task_id))
180
181     def test__validate_invalid_yaml_name(self):
182         with six.assertRaisesRegex(self, ValueError, "yaml*"):
183             self.rep._validate(BAD_YAML_NAME, GOOD_TASK_ID)
184
185     def test__validate_invalid_task_id(self):
186         with six.assertRaisesRegex(self, ValueError, "task*"):
187             self.rep._validate(GOOD_YAML_NAME, BAD_TASK_ID)
188
189     @mock.patch.object(influx, 'query')
190     def test__get_fieldkeys(self, mock_query):
191         mock_query.return_value = GOOD_DB_FIELDKEYS
192         self.rep.yaml_name = GOOD_YAML_NAME
193         self.rep.task_id = GOOD_TASK_ID
194         self.assertEqual(GOOD_DB_FIELDKEYS, self.rep._get_fieldkeys())
195
196     @mock.patch.object(influx, 'query')
197     def test__get_fieldkeys_nodbclient(self, mock_query):
198         mock_query.side_effect = RuntimeError
199         self.assertRaises(RuntimeError, self.rep._get_fieldkeys)
200
201     @mock.patch.object(influx, 'query')
202     def test__get_fieldkeys_testcase_not_found(self, mock_query):
203         mock_query.return_value = []
204         self.rep.yaml_name = GOOD_YAML_NAME
205         self.rep.task_id = GOOD_TASK_ID
206         six.assertRaisesRegex(self, KeyError, "Test case", self.rep._get_fieldkeys)
207
208     @mock.patch.object(influx, 'query')
209     def test__get_metrics(self, mock_query):
210         mock_query.return_value = GOOD_DB_METRICS
211         self.rep.yaml_name = GOOD_YAML_NAME
212         self.rep.task_id = GOOD_TASK_ID
213         self.assertEqual(GOOD_DB_METRICS, self.rep._get_metrics())
214
215     @mock.patch.object(influx, 'query')
216     def test__get_metrics_task_not_found(self, mock_query):
217         mock_query.return_value = []
218         self.rep.yaml_name = GOOD_YAML_NAME
219         self.rep.task_id = GOOD_TASK_ID
220         six.assertRaisesRegex(self, KeyError, "Task ID", self.rep._get_metrics)
221
222     def test__get_timestamps(self):
223
224         metrics = MORE_DB_METRICS
225         self.assertEqual(
226             MORE_TIMESTAMP,
227             self.rep._get_timestamps(metrics)
228         )
229
230     @mock.patch.object(report.Report, '_get_metrics')
231     @mock.patch.object(report.Report, '_get_fieldkeys')
232     def test__generate_common(self, mock_keys, mock_metrics):
233         mock_metrics.return_value = MORE_DB_METRICS
234         mock_keys.return_value = MORE_DB_FIELDKEYS
235         datasets, table_vals = self.rep._generate_common(self.param)
236         self.assertEqual(MORE_EXPECTED_DATASETS, datasets)
237         self.assertEqual(MORE_EXPECTED_TABLE_VALS, table_vals)
238
239     @mock.patch.object(report.Report, '_get_metrics')
240     @mock.patch.object(report.Report, '_get_fieldkeys')
241     @mock.patch.object(report.Report, '_validate')
242     def test_generate(self, mock_valid, mock_keys, mock_metrics):
243         mock_metrics.return_value = GOOD_DB_METRICS
244         mock_keys.return_value = GOOD_DB_FIELDKEYS
245         self.rep.generate(self.param)
246         mock_valid.assert_called_once_with(GOOD_YAML_NAME, GOOD_TASK_ID)
247         mock_metrics.assert_called_once_with()
248         mock_keys.assert_called_once_with()
249         self.assertEqual(GOOD_TIMESTAMP, self.rep.Timestamp)
250
251     @mock.patch.object(report.Report, '_get_metrics')
252     @mock.patch.object(report.Report, '_get_fieldkeys')
253     @mock.patch.object(report.Report, '_validate')
254     def test_generate_nsb(self, mock_valid, mock_keys, mock_metrics):
255         mock_metrics.return_value = GOOD_DB_METRICS
256         mock_keys.return_value = GOOD_DB_FIELDKEYS
257         self.rep.generate_nsb(self.param)
258         mock_valid.assert_called_once_with(GOOD_YAML_NAME, GOOD_TASK_ID)
259         mock_metrics.assert_called_once_with()
260         mock_keys.assert_called_once_with()
261         self.assertEqual(GOOD_TIMESTAMP, self.rep.Timestamp)