1 # Copyright (c) 2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxTestDataTuple
19 from yardstick.network_services.traffic_profile import prox_binsearch
22 class TestProxBinSearchProfile(unittest.TestCase):
24 THEOR_MAX_THROUGHPUT = 0.00012340000000000002
27 self._mock_log_info = mock.patch.object(prox_binsearch.LOG, 'info')
28 self.mock_log_info = self._mock_log_info.start()
29 self.addCleanup(self._stop_mocks)
31 def _stop_mocks(self):
32 self._mock_log_info.stop()
34 def test_execute_1(self):
35 def target(*args, **_):
37 if args[2] < 0 or args[2] > 100:
38 raise RuntimeError(' '.join([str(args), str(runs)]))
41 return success_tuple, {}
43 def side_effect_func(arg1, arg2):
44 if arg1 == "confirmation":
51 'packet_sizes': [200],
52 'test_precision': 2.0,
53 'tolerated_loss': 0.001,
58 success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
59 fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
61 traffic_generator = mock.MagicMock()
62 attrs1 = {'get.return_value': 10}
63 traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
65 attrs2 = {'__getitem__.return_value': 10, 'get.return_value': 10}
66 attrs3 = {'get.side_effect': side_effect_func}
67 traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
68 traffic_generator.scenario_helper.scenario_cfg["options"].configure_mock(**attrs3)
70 profile_helper = mock.MagicMock()
71 profile_helper.run_test = target
73 profile = prox_binsearch.ProxBinSearchProfile(tp_config)
74 profile.init(mock.MagicMock())
75 profile._profile_helper = profile_helper
77 profile.execute_traffic(traffic_generator)
79 self.assertEqual(round(profile.current_lower, 2), 74.69)
80 self.assertEqual(round(profile.current_upper, 2), 76.09)
81 self.assertEqual(len(runs), 7)
83 # Result Samples inc theor_max
84 result_tuple = {'Actual_throughput': 5e-07,
85 'theor_max_throughput': self.THEOR_MAX_THROUGHPUT,
89 test_results = profile.queue.put.call_args[0]
90 for k in result_tuple:
91 self.assertEqual(result_tuple[k], test_results[0][k])
93 success_result_tuple = {"CurrentDropPackets": 0.5,
99 "RxThroughput": 7.5e-07,
100 "Throughput": 7.5e-07,
101 "TxThroughput": self.THEOR_MAX_THROUGHPUT,
104 calls = profile.queue.put(success_result_tuple)
105 profile.queue.put.assert_has_calls(calls)
107 success_result_tuple2 = {"CurrentDropPackets": 0.5,
113 "RxThroughput": 7.5e-07,
114 "Throughput": 7.5e-07,
115 "TxThroughput": 123.4,
116 "can_be_lost": 409600,
122 calls = profile.queue.put(success_result_tuple2)
123 profile.queue.put.assert_has_calls(calls)
125 def test_execute_2(self):
126 def target(*args, **_):
128 if args[2] < 0 or args[2] > 100:
129 raise RuntimeError(' '.join([str(args), str(runs)]))
131 return fail_tuple, {}
132 return success_tuple, {}
134 def side_effect_func(arg1, _):
135 if arg1 == "confirmation":
142 'packet_sizes': [200],
143 'test_precision': 2.0,
144 'tolerated_loss': 0.001,
149 success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
150 fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
152 traffic_generator = mock.MagicMock()
153 attrs1 = {'get.return_value': 10}
154 traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
156 attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
157 attrs3 = {'get.side_effect': side_effect_func}
159 traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
160 traffic_generator.scenario_helper.scenario_cfg["options"].configure_mock(**attrs3)
162 profile_helper = mock.MagicMock()
163 profile_helper.run_test = target
165 profile = prox_binsearch.ProxBinSearchProfile(tp_config)
166 profile.init(mock.MagicMock())
167 profile._profile_helper = profile_helper
169 profile.execute_traffic(traffic_generator)
170 self.assertEqual(round(profile.current_lower, 2), 24.06)
171 self.assertEqual(round(profile.current_upper, 2), 25.47)
172 self.assertEqual(len(runs), 21)
174 def test_execute_3(self):
175 def target(*args, **_):
177 if args[2] < 0 or args[2] > 100:
178 raise RuntimeError(' '.join([str(args), str(runs)]))
180 return fail_tuple, {}
181 return success_tuple, {}
185 'packet_sizes': [200],
186 'test_precision': 2.0,
187 'tolerated_loss': 0.001,
192 success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
193 fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
195 traffic_generator = mock.MagicMock()
197 profile_helper = mock.MagicMock()
198 profile_helper.run_test = target
200 profile = prox_binsearch.ProxBinSearchProfile(tp_config)
201 profile.init(mock.MagicMock())
202 profile._profile_helper = profile_helper
204 profile.upper_bound = 100.0
205 profile.lower_bound = 99.0
206 profile.execute_traffic(traffic_generator)
208 result_tuple = {'Actual_throughput': 0, 'theor_max_throughput': 0,
209 "Status": 'Result', "Next_Step": ''}
210 profile.queue.put.assert_called_with(result_tuple)
212 # Check for success_ tuple (None expected)
213 calls = profile.queue.put.mock_calls
215 for call_detail in call[1]:
216 if call_detail["Status"] == 'Success':
217 self.assertRaises(AttributeError)
219 def test_execute_4(self):
221 def target(*args, **_):
223 if args[2] < 0 or args[2] > 100:
224 raise RuntimeError(' '.join([str(args), str(runs)]))
226 return fail_tuple, {}
228 return success_tuple, {}
232 'packet_sizes': [200],
233 'test_precision': 2.0,
234 'tolerated_loss': 0.001,
239 success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
240 fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
242 traffic_generator = mock.MagicMock()
243 attrs1 = {'get.return_value': 100000}
244 traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
246 attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
248 traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
250 profile_helper = mock.MagicMock()
251 profile_helper.run_test = target
253 profile = prox_binsearch.ProxBinSearchProfile(tp_config)
254 profile.init(mock.MagicMock())
255 profile._profile_helper = profile_helper
257 profile.execute_traffic(traffic_generator)
258 self.assertEqual(round(profile.current_lower, 2), 74.69)
259 self.assertEqual(round(profile.current_upper, 2), 76.09)
260 self.assertEqual(len(runs), 7)
262 # Result Samples inc theor_max
263 result_tuple = {'Actual_throughput': 5e-07,
264 'theor_max_throughput': self.THEOR_MAX_THROUGHPUT,
268 test_results = profile.queue.put.call_args[0]
269 for k in result_tuple:
270 self.assertEqual(result_tuple[k], test_results[0][k])
272 success_result_tuple = {"CurrentDropPackets": 0.5,
278 "RxThroughput": 7.5e-07,
279 "Throughput": 7.5e-07,
280 "TxThroughput": self.THEOR_MAX_THROUGHPUT,
283 calls = profile.queue.put(success_result_tuple)
284 profile.queue.put.assert_has_calls(calls)
286 success_result_tuple2 = {"CurrentDropPackets": 0.5,
292 "RxThroughput": 7.5e-07,
293 "Throughput": 7.5e-07,
294 "TxThroughput": 123.4,
295 "can_be_lost": 409600,
301 calls = profile.queue.put(success_result_tuple2)
302 profile.queue.put.assert_has_calls(calls)