Merge "Remove __init__ method overriding in HeatContextTestCase"
[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
16 import unittest
17 import mock
18
19 from yardstick.tests import STL_MOCKS
20
21 STLClient = mock.MagicMock()
22 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
23 stl_patch.start()
24
25 if stl_patch:
26     from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxTestDataTuple
27     from yardstick.network_services.traffic_profile.prox_binsearch import ProxBinSearchProfile
28
29
30 class TestProxBinSearchProfile(unittest.TestCase):
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
55         profile_helper = mock.MagicMock()
56         profile_helper.run_test = target
57
58         profile = ProxBinSearchProfile(tp_config)
59         profile.init(mock.MagicMock())
60         profile._profile_helper = profile_helper
61
62         profile.execute_traffic(traffic_generator)
63         self.assertEqual(round(profile.current_lower, 2), 74.69)
64         self.assertEqual(round(profile.current_upper, 2), 76.09)
65         self.assertEqual(len(runs), 7)
66
67         # Result Samples inc theor_max
68         result_tuple = {'Result_Actual_throughput': 5e-07,
69                         'Result_theor_max_throughput': 0.00012340000000000002,
70                         'Result_pktSize': 200}
71
72         profile.queue.put.assert_called_with(result_tuple)
73
74         success_result_tuple = {"Success_CurrentDropPackets": 0.5,
75                                 "Success_DropPackets": 0.5,
76                                 "Success_LatencyAvg": 5.3,
77                                 "Success_LatencyMax": 5.2,
78                                 "Success_LatencyMin": 5.1,
79                                 "Success_PktSize": 200,
80                                 "Success_RxThroughput": 7.5e-07,
81                                 "Success_Throughput": 7.5e-07,
82                                 "Success_TxThroughput": 0.00012340000000000002}
83
84         calls = profile.queue.put(success_result_tuple)
85         profile.queue.put.assert_has_calls(calls)
86
87         success_result_tuple2 = {"Success_CurrentDropPackets": 0.5,
88                                 "Success_DropPackets": 0.5,
89                                 "Success_LatencyAvg": 5.3,
90                                 "Success_LatencyMax": 5.2,
91                                 "Success_LatencyMin": 5.1,
92                                 "Success_PktSize": 200,
93                                 "Success_RxThroughput": 7.5e-07,
94                                 "Success_Throughput": 7.5e-07,
95                                 "Success_TxThroughput": 123.4,
96                                 "Success_can_be_lost": 409600,
97                                 "Success_drop_total": 20480,
98                                 "Success_rx_total": 4075520,
99                                 "Success_tx_total": 4096000}
100
101         calls = profile.queue.put(success_result_tuple2)
102         profile.queue.put.assert_has_calls(calls)
103
104     def test_execute_2(self):
105         def target(*args, **_):
106             runs.append(args[2])
107             if args[2] < 0 or args[2] > 100:
108                 raise RuntimeError(' '.join([str(args), str(runs)]))
109             if args[2] > 25.0:
110                 return fail_tuple, {}
111             return success_tuple, {}
112
113         tp_config = {
114             'traffic_profile': {
115                 'packet_sizes': [200],
116                 'test_precision': 2.0,
117                 'tolerated_loss': 0.001,
118             },
119         }
120
121         runs = []
122         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
123         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
124
125         traffic_generator = mock.MagicMock()
126
127         profile_helper = mock.MagicMock()
128         profile_helper.run_test = target
129
130         profile = ProxBinSearchProfile(tp_config)
131         profile.init(mock.MagicMock())
132         profile._profile_helper = profile_helper
133
134         profile.execute_traffic(traffic_generator)
135         self.assertEqual(round(profile.current_lower, 2), 24.06)
136         self.assertEqual(round(profile.current_upper, 2), 25.47)
137         self.assertEqual(len(runs), 7)
138
139     def test_execute_3(self):
140         def target(*args, **_):
141             runs.append(args[2])
142             if args[2] < 0 or args[2] > 100:
143                 raise RuntimeError(' '.join([str(args), str(runs)]))
144             if args[2] > 75.0:
145                 return fail_tuple, {}
146             return success_tuple, {}
147
148         tp_config = {
149             'traffic_profile': {
150                 'packet_sizes': [200],
151                 'test_precision': 2.0,
152                 'tolerated_loss': 0.001,
153             },
154         }
155
156         runs = []
157         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
158         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
159
160         traffic_generator = mock.MagicMock()
161
162         profile_helper = mock.MagicMock()
163         profile_helper.run_test = target
164
165         profile = ProxBinSearchProfile(tp_config)
166         profile.init(mock.MagicMock())
167         profile._profile_helper = profile_helper
168
169         profile.upper_bound = 100.0
170         profile.lower_bound = 99.0
171         profile.execute_traffic(traffic_generator)
172
173
174         # Result Samples
175         result_tuple = {"Result_theor_max_throughput": 0, "Result_pktSize": 200}
176         profile.queue.put.assert_called_with(result_tuple)
177
178         # Check for success_ tuple (None expected)
179         calls = profile.queue.put.mock_calls
180         for call in calls:
181             for call_detail in call[1]:
182                 for k in call_detail:
183                     if "Success_" in k:
184                         self.assertRaises(AttributeError)