Merge changes from topic 'doc/nsb-qsg'
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_prox_binsearch.py
1 # Copyright (c) 2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import unittest
16 import mock
17
18 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxTestDataTuple
19 from yardstick.network_services.traffic_profile import prox_binsearch
20
21
22 class TestProxBinSearchProfile(unittest.TestCase):
23
24     def setUp(self):
25         self._mock_log_info = mock.patch.object(prox_binsearch.LOG, 'info')
26         self.mock_log_info = self._mock_log_info.start()
27         self.addCleanup(self._stop_mocks)
28
29     def _stop_mocks(self):
30         self._mock_log_info.stop()
31
32     def test_execute_1(self):
33         def target(*args, **_):
34             runs.append(args[2])
35             if args[2] < 0 or args[2] > 100:
36                 raise RuntimeError(' '.join([str(args), str(runs)]))
37             if args[2] > 75.0:
38                 return fail_tuple, {}
39             return success_tuple, {}
40
41         def side_effect_func(arg1, arg2):
42             if arg1 == "confirmation":
43                 return arg2
44             else:
45                 return {}
46
47         tp_config = {
48             'traffic_profile': {
49                 'packet_sizes': [200],
50                 'test_precision': 2.0,
51                 'tolerated_loss': 0.001,
52             },
53         }
54
55         runs = []
56         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
57         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
58
59         traffic_generator = mock.MagicMock()
60         attrs1 = {'get.return_value': 10}
61         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
62
63         attrs2 = {'__getitem__.return_value': 10, 'get.return_value': 10}
64         attrs3 = {'get.side_effect': side_effect_func}
65         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
66         traffic_generator.scenario_helper.scenario_cfg["options"].configure_mock(**attrs3)
67
68         profile_helper = mock.MagicMock()
69         profile_helper.run_test = target
70
71         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
72         profile.init(mock.MagicMock())
73         profile._profile_helper = profile_helper
74
75         profile.execute_traffic(traffic_generator)
76
77         self.assertEqual(round(profile.current_lower, 2), 74.69)
78         self.assertEqual(round(profile.current_upper, 2), 76.09)
79         self.assertEqual(len(runs), 7)
80
81         # Result Samples inc theor_max
82         result_tuple = {'Actual_throughput': 5e-07,
83                         'theor_max_throughput': 7.5e-07,
84                         'PktSize': 200,
85                         'Status': 'Result'}
86
87         test_results = profile.queue.put.call_args[0]
88         for k in result_tuple:
89             self.assertEqual(result_tuple[k], test_results[0][k])
90
91         success_result_tuple = {"CurrentDropPackets": 0.5,
92                                 "DropPackets": 0.5,
93                                 "LatencyAvg": 5.3,
94                                 "LatencyMax": 5.2,
95                                 "LatencyMin": 5.1,
96                                 "PktSize": 200,
97                                 "RxThroughput": 7.5e-07,
98                                 "Throughput": 7.5e-07,
99                                 "TxThroughput": 0.00012340000000000002,
100                                 "Status": 'Success'}
101
102         calls = profile.queue.put(success_result_tuple)
103         profile.queue.put.assert_has_calls(calls)
104
105         success_result_tuple2 = {"CurrentDropPackets": 0.5,
106                                 "DropPackets": 0.5,
107                                 "LatencyAvg": 5.3,
108                                 "LatencyMax": 5.2,
109                                 "LatencyMin": 5.1,
110                                 "PktSize": 200,
111                                 "RxThroughput": 7.5e-07,
112                                 "Throughput": 7.5e-07,
113                                 "TxThroughput": 123.4,
114                                 "can_be_lost": 409600,
115                                 "drop_total": 20480,
116                                 "rx_total": 4075520,
117                                 "tx_total": 4096000,
118                                 "Status": 'Success'}
119
120         calls = profile.queue.put(success_result_tuple2)
121         profile.queue.put.assert_has_calls(calls)
122
123     def test_execute_2(self):
124         def target(*args, **_):
125             runs.append(args[2])
126             if args[2] < 0 or args[2] > 100:
127                 raise RuntimeError(' '.join([str(args), str(runs)]))
128             if args[2] > 25.0:
129                 return fail_tuple, {}
130             return success_tuple, {}
131
132         def side_effect_func(arg1, _):
133             if arg1 == "confirmation":
134                 return 2
135             else:
136                 return {}
137
138         tp_config = {
139             'traffic_profile': {
140                 'packet_sizes': [200],
141                 'test_precision': 2.0,
142                 'tolerated_loss': 0.001,
143             },
144         }
145
146         runs = []
147         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
148         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
149
150         traffic_generator = mock.MagicMock()
151         attrs1 = {'get.return_value': 10}
152         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
153
154         attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
155         attrs3 = {'get.side_effect': side_effect_func}
156
157         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
158         traffic_generator.scenario_helper.scenario_cfg["options"].configure_mock(**attrs3)
159
160         profile_helper = mock.MagicMock()
161         profile_helper.run_test = target
162
163         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
164         profile.init(mock.MagicMock())
165         profile._profile_helper = profile_helper
166
167         profile.execute_traffic(traffic_generator)
168         self.assertEqual(round(profile.current_lower, 2), 24.06)
169         self.assertEqual(round(profile.current_upper, 2), 25.47)
170         self.assertEqual(len(runs), 21)
171
172     def test_execute_3(self):
173         def target(*args, **_):
174             runs.append(args[2])
175             if args[2] < 0 or args[2] > 100:
176                 raise RuntimeError(' '.join([str(args), str(runs)]))
177             if args[2] > 75.0:
178                 return fail_tuple, {}
179             return success_tuple, {}
180
181         tp_config = {
182             'traffic_profile': {
183                 'packet_sizes': [200],
184                 'test_precision': 2.0,
185                 'tolerated_loss': 0.001,
186             },
187         }
188
189         runs = []
190         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
191         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
192
193         traffic_generator = mock.MagicMock()
194
195         profile_helper = mock.MagicMock()
196         profile_helper.run_test = target
197
198         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
199         profile.init(mock.MagicMock())
200         profile._profile_helper = profile_helper
201
202         profile.upper_bound = 100.0
203         profile.lower_bound = 99.0
204         profile.execute_traffic(traffic_generator)
205
206         result_tuple = {'Actual_throughput': 0, 'theor_max_throughput': 0,
207                         "Status": 'Result', "Next_Step": ''}
208         profile.queue.put.assert_called_with(result_tuple)
209
210         # Check for success_ tuple (None expected)
211         calls = profile.queue.put.mock_calls
212         for call in calls:
213             for call_detail in call[1]:
214                 if call_detail["Status"] == 'Success':
215                     self.assertRaises(AttributeError)
216
217     def test_execute_4(self):
218
219         def target(*args, **_):
220             runs.append(args[2])
221             if args[2] < 0 or args[2] > 100:
222                 raise RuntimeError(' '.join([str(args), str(runs)]))
223             if args[2] > 75.0:
224                 return fail_tuple, {}
225             return success_tuple, {}
226
227         tp_config = {
228             'traffic_profile': {
229                 'packet_sizes': [200],
230                 'test_precision': 2.0,
231                 'tolerated_loss': 0.001,
232             },
233         }
234
235         runs = []
236         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
237         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
238
239         traffic_generator = mock.MagicMock()
240         attrs1 = {'get.return_value': 100000}
241         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
242
243         attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
244
245         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
246
247         profile_helper = mock.MagicMock()
248         profile_helper.run_test = target
249
250         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
251         profile.init(mock.MagicMock())
252         profile._profile_helper = profile_helper
253
254         profile.execute_traffic(traffic_generator)
255         self.assertEqual(round(profile.current_lower, 2), 74.69)
256         self.assertEqual(round(profile.current_upper, 2), 76.09)
257         self.assertEqual(len(runs), 7)
258
259         # Result Samples inc theor_max
260         result_tuple = {'Actual_throughput': 5e-07,
261                         'theor_max_throughput': 7.5e-07,
262                         'PktSize': 200,
263                         "Status": 'Result'}
264
265         test_results = profile.queue.put.call_args[0]
266         for k in result_tuple:
267             self.assertEqual(result_tuple[k], test_results[0][k])
268
269         success_result_tuple = {"CurrentDropPackets": 0.5,
270                                 "DropPackets": 0.5,
271                                 "LatencyAvg": 5.3,
272                                 "LatencyMax": 5.2,
273                                 "LatencyMin": 5.1,
274                                 "PktSize": 200,
275                                 "RxThroughput": 7.5e-07,
276                                 "Throughput": 7.5e-07,
277                                 "TxThroughput": 0.00012340000000000002,
278                                 "Status": 'Success'}
279
280         calls = profile.queue.put(success_result_tuple)
281         profile.queue.put.assert_has_calls(calls)
282
283         success_result_tuple2 = {"CurrentDropPackets": 0.5,
284                                  "DropPackets": 0.5,
285                                  "LatencyAvg": 5.3,
286                                  "LatencyMax": 5.2,
287                                  "LatencyMin": 5.1,
288                                  "PktSize": 200,
289                                  "RxThroughput": 7.5e-07,
290                                  "Throughput": 7.5e-07,
291                                  "TxThroughput": 123.4,
292                                  "can_be_lost": 409600,
293                                  "drop_total": 20480,
294                                  "rx_total": 4075520,
295                                  "tx_total": 4096000,
296                                  "Status": 'Success'}
297
298         calls = profile.queue.put(success_result_tuple2)
299         profile.queue.put.assert_has_calls(calls)