Merge "NSB: update vfw tc_heat_external tests"
[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, **kwargs):
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             },
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), 75.39)
65         self.assertEqual(len(runs), 8)
66
67     def test_execute_2(self):
68         def target(*args, **kwargs):
69             runs.append(args[2])
70             if args[2] < 0 or args[2] > 100:
71                 raise RuntimeError(' '.join([str(args), str(runs)]))
72             if args[2] > 25.0:
73                 return fail_tuple, {}
74             return success_tuple, {}
75
76         tp_config = {
77             'traffic_profile': {
78                 'packet_sizes': [200],
79                 'test_precision': 2.0,
80             },
81         }
82
83         runs = []
84         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
85         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
86
87         traffic_generator = mock.MagicMock()
88
89         profile_helper = mock.MagicMock()
90         profile_helper.run_test = target
91
92         profile = ProxBinSearchProfile(tp_config)
93         profile.init(mock.MagicMock())
94         profile._profile_helper = profile_helper
95
96         profile.execute_traffic(traffic_generator)
97         self.assertEqual(round(profile.current_lower, 2), 24.06)
98         self.assertEqual(round(profile.current_upper, 2), 25.47)
99         self.assertEqual(len(runs), 7)