Cleanup TestProxBinSearchProfile unit tests
[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         tp_config = {
42             'traffic_profile': {
43                 'packet_sizes': [200],
44                 'test_precision': 2.0,
45                 'tolerated_loss': 0.001,
46             },
47         }
48
49         runs = []
50         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
51         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
52
53         traffic_generator = mock.MagicMock()
54         attrs1 = {'get.return_value' : 10}
55         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
56
57         attrs2 = {'__getitem__.return_value' : 10, 'get.return_value': 10}
58         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
59
60         profile_helper = mock.MagicMock()
61         profile_helper.run_test = target
62
63         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
64         profile.init(mock.MagicMock())
65         profile._profile_helper = profile_helper
66
67         profile.execute_traffic(traffic_generator)
68
69         self.assertEqual(round(profile.current_lower, 2), 74.69)
70         self.assertEqual(round(profile.current_upper, 2), 76.09)
71         self.assertEqual(len(runs), 77)
72
73         # Result Samples inc theor_max
74         result_tuple = {'Result_Actual_throughput': 5e-07,
75                         'Result_theor_max_throughput': 7.5e-07,
76                         'Result_pktSize': 200}
77
78         profile.queue.put.assert_called_with(result_tuple)
79
80         success_result_tuple = {"Success_CurrentDropPackets": 0.5,
81                                 "Success_DropPackets": 0.5,
82                                 "Success_LatencyAvg": 5.3,
83                                 "Success_LatencyMax": 5.2,
84                                 "Success_LatencyMin": 5.1,
85                                 "Success_PktSize": 200,
86                                 "Success_RxThroughput": 7.5e-07,
87                                 "Success_Throughput": 7.5e-07,
88                                 "Success_TxThroughput": 0.00012340000000000002}
89
90         calls = profile.queue.put(success_result_tuple)
91         profile.queue.put.assert_has_calls(calls)
92
93         success_result_tuple2 = {"Success_CurrentDropPackets": 0.5,
94                                 "Success_DropPackets": 0.5,
95                                 "Success_LatencyAvg": 5.3,
96                                 "Success_LatencyMax": 5.2,
97                                 "Success_LatencyMin": 5.1,
98                                 "Success_PktSize": 200,
99                                 "Success_RxThroughput": 7.5e-07,
100                                 "Success_Throughput": 7.5e-07,
101                                 "Success_TxThroughput": 123.4,
102                                 "Success_can_be_lost": 409600,
103                                 "Success_drop_total": 20480,
104                                 "Success_rx_total": 4075520,
105                                 "Success_tx_total": 4096000}
106
107         calls = profile.queue.put(success_result_tuple2)
108         profile.queue.put.assert_has_calls(calls)
109
110     def test_execute_2(self):
111         def target(*args, **_):
112             runs.append(args[2])
113             if args[2] < 0 or args[2] > 100:
114                 raise RuntimeError(' '.join([str(args), str(runs)]))
115             if args[2] > 25.0:
116                 return fail_tuple, {}
117             return success_tuple, {}
118
119         tp_config = {
120             'traffic_profile': {
121                 'packet_sizes': [200],
122                 'test_precision': 2.0,
123                 'tolerated_loss': 0.001,
124             },
125         }
126
127         runs = []
128         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
129         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
130
131         traffic_generator = mock.MagicMock()
132         attrs1 = {'get.return_value': 10}
133         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
134
135         attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
136         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
137
138         profile_helper = mock.MagicMock()
139         profile_helper.run_test = target
140
141         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
142         profile.init(mock.MagicMock())
143         profile._profile_helper = profile_helper
144
145         profile.execute_traffic(traffic_generator)
146         self.assertEqual(round(profile.current_lower, 2), 24.06)
147         self.assertEqual(round(profile.current_upper, 2), 25.47)
148         self.assertEqual(len(runs), 7)
149
150     def test_execute_3(self):
151         def target(*args, **_):
152             runs.append(args[2])
153             if args[2] < 0 or args[2] > 100:
154                 raise RuntimeError(' '.join([str(args), str(runs)]))
155             if args[2] > 75.0:
156                 return fail_tuple, {}
157             return success_tuple, {}
158
159         tp_config = {
160             'traffic_profile': {
161                 'packet_sizes': [200],
162                 'test_precision': 2.0,
163                 'tolerated_loss': 0.001,
164             },
165         }
166
167         runs = []
168         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
169         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
170
171         traffic_generator = mock.MagicMock()
172
173         profile_helper = mock.MagicMock()
174         profile_helper.run_test = target
175
176         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
177         profile.init(mock.MagicMock())
178         profile._profile_helper = profile_helper
179
180         profile.upper_bound = 100.0
181         profile.lower_bound = 99.0
182         profile.execute_traffic(traffic_generator)
183
184
185         # Result Samples
186         result_tuple = {'Result_Actual_throughput': 0, "Result_theor_max_throughput": 0,
187                         "Result_pktSize": 200}
188         profile.queue.put.assert_called_with(result_tuple)
189
190         # Check for success_ tuple (None expected)
191         calls = profile.queue.put.mock_calls
192         for call in calls:
193             for call_detail in call[1]:
194                 for k in call_detail:
195                     if "Success_" in k:
196                         self.assertRaises(AttributeError)
197
198     def test_execute_4(self):
199
200         def target(*args, **_):
201             runs.append(args[2])
202             if args[2] < 0 or args[2] > 100:
203                 raise RuntimeError(' '.join([str(args), str(runs)]))
204             if args[2] > 75.0:
205                 return fail_tuple, {}
206             return success_tuple, {}
207
208         tp_config = {
209             'traffic_profile': {
210                 'packet_sizes': [200],
211                 'test_precision': 2.0,
212                 'tolerated_loss': 0.001,
213             },
214         }
215
216         runs = []
217         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
218         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
219
220         traffic_generator = mock.MagicMock()
221         attrs1 = {'get.return_value': 100000}
222         traffic_generator.scenario_helper.all_options.configure_mock(**attrs1)
223
224         attrs2 = {'__getitem__.return_value': 0, 'get.return_value': 0}
225         traffic_generator.scenario_helper.scenario_cfg["runner"].configure_mock(**attrs2)
226
227         profile_helper = mock.MagicMock()
228         profile_helper.run_test = target
229
230         profile = prox_binsearch.ProxBinSearchProfile(tp_config)
231         profile.init(mock.MagicMock())
232         profile._profile_helper = profile_helper
233
234         profile.execute_traffic(traffic_generator)
235         self.assertEqual(round(profile.current_lower, 2), 74.69)
236         self.assertEqual(round(profile.current_upper, 2), 76.09)
237         self.assertEqual(len(runs), 7)
238
239         # Result Samples inc theor_max
240         result_tuple = {'Result_Actual_throughput': 5e-07,
241                         'Result_theor_max_throughput': 7.5e-07,
242                         'Result_pktSize': 200}
243
244         profile.queue.put.assert_called_with(result_tuple)
245
246         success_result_tuple = {"Success_CurrentDropPackets": 0.5,
247                                 "Success_DropPackets": 0.5,
248                                 "Success_LatencyAvg": 5.3,
249                                 "Success_LatencyMax": 5.2,
250                                 "Success_LatencyMin": 5.1,
251                                 "Success_PktSize": 200,
252                                 "Success_RxThroughput": 7.5e-07,
253                                 "Success_Throughput": 7.5e-07,
254                                 "Success_TxThroughput": 0.00012340000000000002}
255
256         calls = profile.queue.put(success_result_tuple)
257         profile.queue.put.assert_has_calls(calls)
258
259         success_result_tuple2 = {"Success_CurrentDropPackets": 0.5,
260                                  "Success_DropPackets": 0.5,
261                                  "Success_LatencyAvg": 5.3,
262                                  "Success_LatencyMax": 5.2,
263                                  "Success_LatencyMin": 5.1,
264                                  "Success_PktSize": 200,
265                                  "Success_RxThroughput": 7.5e-07,
266                                  "Success_Throughput": 7.5e-07,
267                                  "Success_TxThroughput": 123.4,
268                                  "Success_can_be_lost": 409600,
269                                  "Success_drop_total": 20480,
270                                  "Success_rx_total": 4075520,
271                                  "Success_tx_total": 4096000}
272
273         calls = profile.queue.put(success_result_tuple2)
274         profile.queue.put.assert_has_calls(calls)