Add functional tests for HTML report
[yardstick.git] / yardstick / tests / functional / benchmark / core / test_report.py
1 ##############################################################################
2 # Copyright (c) 2018 Intel Corporation.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import ast
11 import tempfile
12 import unittest
13
14 import mock
15 from six.moves import configparser
16
17 from yardstick.benchmark import core
18 from yardstick.benchmark.core import report
19 from yardstick.cmd.commands import change_osloobj_to_paras
20
21
22 GOOD_YAML_NAME = 'fake_name'
23 GOOD_TASK_ID = "9cbe74b6-df09-4535-8bdc-dc3a43b8a4e2"
24 GOOD_DB_FIELDKEYS = [
25     {u'fieldKey': u'metric1', u'fieldType': u'integer'},
26     {u'fieldKey': u'metric2', u'fieldType': u'integer'},
27     {u'fieldKey': u'metric3', u'fieldType': u'integer'},
28     {u'fieldKey': u'metric4', u'fieldType': u'integer'},
29 ]
30 GOOD_DB_METRICS = [
31     {u'time': u'2018-08-20T16:49:26.372662016Z',
32      u'metric1': 1, u'metric2': 0, u'metric3': 8, u'metric4': 5},
33     {u'time': u'2018-08-20T16:49:27.374208000Z',
34      u'metric1': 1, u'metric2': 1, u'metric3': 5, u'metric4': 4},
35     {u'time': u'2018-08-20T16:49:28.375742976Z',
36      u'metric1': 2, u'metric2': 2, u'metric3': 3, u'metric4': 3},
37     {u'time': u'2018-08-20T16:49:29.377299968Z',
38      u'metric1': 3, u'metric2': 3, u'metric3': 2, u'metric4': 2},
39     {u'time': u'2018-08-20T16:49:30.378252032Z',
40      u'metric1': 5, u'metric2': 4, u'metric3': 1, u'metric4': 1},
41     {u'time': u'2018-08-20T16:49:30.379359421Z',
42      u'metric1': 8, u'metric2': 5, u'metric3': 1, u'metric4': 0},
43 ]
44
45 yardstick_config = """
46 [DEFAULT]
47 dispatcher = influxdb
48 """
49
50
51 def my_query(query_sql):
52     get_fieldkeys_cmd = 'show field keys'
53     get_metrics_cmd = 'select * from'
54
55     if get_fieldkeys_cmd in query_sql:
56         return GOOD_DB_FIELDKEYS
57     elif get_metrics_cmd in query_sql:
58         return GOOD_DB_METRICS
59     return []
60
61
62 class ReportTestCase(unittest.TestCase):
63
64     @mock.patch.object(report.influx, 'query', new=my_query)
65     @mock.patch.object(configparser.ConfigParser,
66         'read', side_effect=mock.mock_open(read_data=yardstick_config))
67     def test_report_generate_nsb_simple(self, *args):
68         tmpfile = tempfile.NamedTemporaryFile(delete=True)
69
70         args = core.Param({"task_id": [GOOD_TASK_ID], "yaml_name": [GOOD_YAML_NAME]})
71         params = change_osloobj_to_paras(args)
72
73         with mock.patch.object(report.consts, 'DEFAULT_HTML_FILE', tmpfile.name):
74             report.Report().generate_nsb(params)
75
76         with open(tmpfile.name) as f:
77             for l in f.readlines():
78                  if " arr = {" in l:
79                      arr_act = ast.literal_eval(l.strip()[6:-1])
80                  elif " jstree_data = [" in l:
81                      jstree_data_act = ast.literal_eval(l.strip()[14:-1])
82
83         arr_exp = {
84             'Timestamp':
85                 ['16:49:26.372662', '16:49:27.374208', '16:49:28.375742',
86                  '16:49:29.377299', '16:49:30.378252', '16:49:30.379359'],
87             'metric1': [1, 1, 2, 3, 5, 8],
88             'metric2': [0, 1, 2, 3, 4, 5],
89             'metric3': [8, 5, 3, 2, 1, 1],
90             'metric4': [5, 4, 3, 2, 1, 0],
91         }
92         jstree_data_exp = [
93             {'parent': '#', 'text': 'metric1', 'id': 'metric1'},
94             {'parent': '#', 'text': 'metric2', 'id': 'metric2'},
95             {'parent': '#', 'text': 'metric3', 'id': 'metric3'},
96             {'parent': '#', 'text': 'metric4', 'id': 'metric4'},
97         ]
98
99         self.assertEqual(arr_exp, arr_act)
100         self.assertEqual(jstree_data_exp, jstree_data_act)