1 ##############################################################################
2 # Copyright (c) 2017 Rajesh Kudaka.
3 # Copyright (c) 2018 Intel Corporation.
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 ##############################################################################
15 from api.utils import influx
16 from yardstick.benchmark.core import report
17 from yardstick.cmd.commands import change_osloobj_to_paras
19 GOOD_YAML_NAME = 'fake_name'
20 GOOD_TASK_ID = str(uuid.uuid4())
21 GOOD_DB_FIELDKEYS = [{'fieldKey': 'fake_key'}]
24 'time': '0000-00-00T12:34:56.789012Z',
26 GOOD_TIMESTAMP = ['12:34:56.789012']
27 BAD_YAML_NAME = 'F@KE_NAME'
28 BAD_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa'
31 class JSTreeTestCase(unittest.TestCase):
34 self.jstree = report.JSTree()
36 def test__create_node(self):
37 _id = "tg__0.DropPackets"
40 {"id": "tg__0", "text": "tg__0", "parent": "#"},
41 {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"}
43 self.jstree._create_node(_id)
45 self.assertEqual(self.jstree._created_nodes, ['#', 'tg__0', 'tg__0.DropPackets'])
46 self.assertEqual(self.jstree.jstree_data, expected_data)
48 def test_format_for_jstree(self):
50 {'data': [0, ], 'label': 'tg__0.DropPackets'},
51 {'data': [548, ], 'label': 'tg__0.LatencyAvg.5'},
52 {'data': [1172, ], 'label': 'tg__0.LatencyAvg.6'},
53 {'data': [1001, ], 'label': 'tg__0.LatencyMax.5'},
54 {'data': [1468, ], 'label': 'tg__0.LatencyMax.6'},
55 {'data': [18.11, ], 'label': 'tg__0.RxThroughput'},
56 {'data': [18.11, ], 'label': 'tg__0.TxThroughput'},
57 {'data': [0, ], 'label': 'tg__1.DropPackets'},
58 {'data': [548, ], 'label': 'tg__1.LatencyAvg.5'},
59 {'data': [1172, ], 'label': 'tg__1.LatencyAvg.6'},
60 {'data': [1001, ], 'label': 'tg__1.LatencyMax.5'},
61 {'data': [1468, ], 'label': 'tg__1.LatencyMax.6'},
62 {'data': [18.1132084505, ], 'label': 'tg__1.RxThroughput'},
63 {'data': [18.1157260383, ], 'label': 'tg__1.TxThroughput'},
64 {'data': [9057888, ], 'label': 'vnf__0.curr_packets_in'},
65 {'data': [0, ], 'label': 'vnf__0.packets_dropped'},
66 {'data': [617825443, ], 'label': 'vnf__0.packets_fwd'},
70 {"id": "tg__0", "text": "tg__0", "parent": "#"},
71 {"id": "tg__0.DropPackets", "text": "DropPackets", "parent": "tg__0"},
72 {"id": "tg__0.LatencyAvg", "text": "LatencyAvg", "parent": "tg__0"},
73 {"id": "tg__0.LatencyAvg.5", "text": "5", "parent": "tg__0.LatencyAvg"},
74 {"id": "tg__0.LatencyAvg.6", "text": "6", "parent": "tg__0.LatencyAvg"},
75 {"id": "tg__0.LatencyMax", "text": "LatencyMax", "parent": "tg__0"},
76 {"id": "tg__0.LatencyMax.5", "text": "5", "parent": "tg__0.LatencyMax"},
77 {"id": "tg__0.LatencyMax.6", "text": "6", "parent": "tg__0.LatencyMax"},
78 {"id": "tg__0.RxThroughput", "text": "RxThroughput", "parent": "tg__0"},
79 {"id": "tg__0.TxThroughput", "text": "TxThroughput", "parent": "tg__0"},
80 {"id": "tg__1", "text": "tg__1", "parent": "#"},
81 {"id": "tg__1.DropPackets", "text": "DropPackets", "parent": "tg__1"},
82 {"id": "tg__1.LatencyAvg", "text": "LatencyAvg", "parent": "tg__1"},
83 {"id": "tg__1.LatencyAvg.5", "text": "5", "parent": "tg__1.LatencyAvg"},
84 {"id": "tg__1.LatencyAvg.6", "text": "6", "parent": "tg__1.LatencyAvg"},
85 {"id": "tg__1.LatencyMax", "text": "LatencyMax", "parent": "tg__1"},
86 {"id": "tg__1.LatencyMax.5", "text": "5", "parent": "tg__1.LatencyMax"},
87 {"id": "tg__1.LatencyMax.6", "text": "6", "parent": "tg__1.LatencyMax"},
88 {"id": "tg__1.RxThroughput", "text": "RxThroughput", "parent": "tg__1"},
89 {"id": "tg__1.TxThroughput", "text": "TxThroughput", "parent": "tg__1"},
90 {"id": "vnf__0", "text": "vnf__0", "parent": "#"},
91 {"id": "vnf__0.curr_packets_in", "text": "curr_packets_in", "parent": "vnf__0"},
92 {"id": "vnf__0.packets_dropped", "text": "packets_dropped", "parent": "vnf__0"},
93 {"id": "vnf__0.packets_fwd", "text": "packets_fwd", "parent": "vnf__0"},
96 result = self.jstree.format_for_jstree(data)
97 self.assertEqual(expected_output, result)
100 class ReportTestCase(unittest.TestCase):
103 super(ReportTestCase, self).setUp()
104 self.param = change_osloobj_to_paras({})
105 self.param.yaml_name = [GOOD_YAML_NAME]
106 self.param.task_id = [GOOD_TASK_ID]
107 self.rep = report.Report()
109 def test___init__(self):
110 self.assertEqual([], self.rep.Timestamp)
111 self.assertEqual("", self.rep.yaml_name)
112 self.assertEqual("", self.rep.task_id)
114 def test__validate(self):
115 self.rep._validate(GOOD_YAML_NAME, GOOD_TASK_ID)
116 self.assertEqual(GOOD_YAML_NAME, self.rep.yaml_name)
117 self.assertEqual(GOOD_TASK_ID, str(self.rep.task_id))
119 def test__validate_invalid_yaml_name(self):
120 with self.assertRaisesRegexp(ValueError, "yaml*"):
121 self.rep._validate(BAD_YAML_NAME, GOOD_TASK_ID)
123 def test__validate_invalid_task_id(self):
124 with self.assertRaisesRegexp(ValueError, "task*"):
125 self.rep._validate(GOOD_YAML_NAME, BAD_TASK_ID)
127 @mock.patch.object(influx, 'query')
128 def test__get_fieldkeys(self, mock_query):
129 mock_query.return_value = GOOD_DB_FIELDKEYS
130 self.rep.yaml_name = GOOD_YAML_NAME
131 self.rep.task_id = GOOD_TASK_ID
132 self.assertEqual(GOOD_DB_FIELDKEYS, self.rep._get_fieldkeys())
134 @mock.patch.object(influx, 'query')
135 def test__get_fieldkeys_nodbclient(self, mock_query):
136 mock_query.side_effect = RuntimeError
137 self.assertRaises(RuntimeError, self.rep._get_fieldkeys)
139 @mock.patch.object(influx, 'query')
140 def test__get_fieldkeys_testcase_not_found(self, mock_query):
141 mock_query.return_value = []
142 self.rep.yaml_name = GOOD_YAML_NAME
143 self.rep.task_id = GOOD_TASK_ID
144 self.assertRaisesRegexp(KeyError, "Test case", self.rep._get_fieldkeys)
146 @mock.patch.object(influx, 'query')
147 def test__get_tasks(self, mock_query):
148 mock_query.return_value = GOOD_DB_TASK
149 self.rep.yaml_name = GOOD_YAML_NAME
150 self.rep.task_id = GOOD_TASK_ID
151 self.assertEqual(GOOD_DB_TASK, self.rep._get_tasks())
153 @mock.patch.object(influx, 'query')
154 def test__get_tasks_task_not_found(self, mock_query):
155 mock_query.return_value = []
156 self.rep.yaml_name = GOOD_YAML_NAME
157 self.rep.task_id = GOOD_TASK_ID
158 self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_tasks)
160 @mock.patch.object(report.Report, '_get_tasks')
161 @mock.patch.object(report.Report, '_get_fieldkeys')
162 @mock.patch.object(report.Report, '_validate')
163 def test_generate(self, mock_valid, mock_keys, mock_tasks):
164 mock_tasks.return_value = GOOD_DB_TASK
165 mock_keys.return_value = GOOD_DB_FIELDKEYS
166 self.rep.generate(self.param)
167 mock_valid.assert_called_once_with(GOOD_YAML_NAME, GOOD_TASK_ID)
168 mock_tasks.assert_called_once_with()
169 mock_keys.assert_called_once_with()
170 self.assertEqual(GOOD_TIMESTAMP, self.rep.Timestamp)
172 @mock.patch.object(report.Report, '_get_tasks')
173 @mock.patch.object(report.Report, '_get_fieldkeys')
174 @mock.patch.object(report.Report, '_validate')
175 def test_generate_nsb(self, mock_valid, mock_keys, mock_tasks):
176 mock_tasks.return_value = GOOD_DB_TASK
177 mock_keys.return_value = GOOD_DB_FIELDKEYS
178 self.rep.generate_nsb(self.param)
179 mock_valid.assert_called_once_with(GOOD_YAML_NAME, GOOD_TASK_ID)
180 mock_tasks.assert_called_once_with()
181 mock_keys.assert_called_once_with()
182 self.assertEqual(GOOD_TIMESTAMP, self.rep.Timestamp)