5f060dd1e0adb0ec47ddbe95e167c8c1f1afb661
[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'metric4', u'fieldType': u'integer'},
27     {u'fieldKey': u'metric2', u'fieldType': u'integer'},
28     {u'fieldKey': u'metric3', 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         data_act = None
77         time_act = None
78         keys_act = None
79         tree_act = None
80         with open(tmpfile.name) as f:
81             for l in f.readlines():
82                  if "var report_data = {" in l:
83                      data_act = ast.literal_eval(l.strip()[18:-1])
84                  elif "var report_time = [" in l:
85                      time_act = ast.literal_eval(l.strip()[18:-1])
86                  elif "var report_keys = [" in l:
87                      keys_act = ast.literal_eval(l.strip()[18:-1])
88                  elif "var report_tree = [" in l:
89                      tree_act = ast.literal_eval(l.strip()[18:-1])
90
91         data_exp = {
92             'metric1': [1, 1, 2, 3, 5, 8],
93             'metric2': [0, 1, 2, 3, 4, 5],
94             'metric3': [8, 5, 3, 2, 1, 1],
95             'metric4': [5, 4, 3, 2, 1, 0],
96         }
97         time_exp = [
98             '16:49:26.372662', '16:49:27.374208', '16:49:28.375742',
99             '16:49:29.377299', '16:49:30.378252', '16:49:30.379359',
100         ]
101         keys_exp = [
102             'metric1', 'metric2', 'metric3', 'metric4',
103         ]
104         tree_exp = [
105             {'parent': '#', 'text': 'metric1', 'id': 'metric1'},
106             {'parent': '#', 'text': 'metric2', 'id': 'metric2'},
107             {'parent': '#', 'text': 'metric3', 'id': 'metric3'},
108             {'parent': '#', 'text': 'metric4', 'id': 'metric4'},
109         ]
110
111         self.assertEqual(data_exp, data_act)
112         self.assertEqual(time_exp, time_act)
113         self.assertEqual(keys_exp, keys_act)
114         self.assertEqual(tree_exp, tree_act)