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