Merge "Add test case file, document and related scripts of yardstick tc058(HA_TC015)"
[yardstick.git] / tests / unit / network_services / traffic_profile / test_prox_ramp.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 import unittest
18
19 import mock
20
21 STL_MOCKS = {
22     'stl': mock.MagicMock(),
23     'stl.trex_stl_lib': mock.MagicMock(),
24     'stl.trex_stl_lib.base64': mock.MagicMock(),
25     'stl.trex_stl_lib.binascii': mock.MagicMock(),
26     'stl.trex_stl_lib.collections': mock.MagicMock(),
27     'stl.trex_stl_lib.copy': mock.MagicMock(),
28     'stl.trex_stl_lib.datetime': mock.MagicMock(),
29     'stl.trex_stl_lib.functools': mock.MagicMock(),
30     'stl.trex_stl_lib.imp': mock.MagicMock(),
31     'stl.trex_stl_lib.inspect': mock.MagicMock(),
32     'stl.trex_stl_lib.json': mock.MagicMock(),
33     'stl.trex_stl_lib.linecache': mock.MagicMock(),
34     'stl.trex_stl_lib.math': mock.MagicMock(),
35     'stl.trex_stl_lib.os': mock.MagicMock(),
36     'stl.trex_stl_lib.platform': mock.MagicMock(),
37     'stl.trex_stl_lib.pprint': mock.MagicMock(),
38     'stl.trex_stl_lib.random': mock.MagicMock(),
39     'stl.trex_stl_lib.re': mock.MagicMock(),
40     'stl.trex_stl_lib.scapy': mock.MagicMock(),
41     'stl.trex_stl_lib.socket': mock.MagicMock(),
42     'stl.trex_stl_lib.string': mock.MagicMock(),
43     'stl.trex_stl_lib.struct': mock.MagicMock(),
44     'stl.trex_stl_lib.sys': mock.MagicMock(),
45     'stl.trex_stl_lib.threading': mock.MagicMock(),
46     'stl.trex_stl_lib.time': mock.MagicMock(),
47     'stl.trex_stl_lib.traceback': mock.MagicMock(),
48     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
49     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
50     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
51     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
52     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
53     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
54     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
59     'stl.trex_stl_lib.types': mock.MagicMock(),
60     'stl.trex_stl_lib.utils': mock.MagicMock(),
61     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
62     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
63     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
64     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
65     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
66     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
67     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
75     'stl.trex_stl_lib.warnings': mock.MagicMock(),
76     'stl.trex_stl_lib.yaml': mock.MagicMock(),
77     'stl.trex_stl_lib.zlib': mock.MagicMock(),
78     'stl.trex_stl_lib.zmq': mock.MagicMock(),
79 }
80
81 STLClient = mock.MagicMock()
82 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
83 stl_patch.start()
84
85 if stl_patch:
86     from yardstick.network_services.traffic_profile.prox_ramp import ProxRampProfile
87     from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxTestDataTuple
88
89
90 class TestProxRampProfile(unittest.TestCase):
91
92     def test_run_test_with_pkt_size(self):
93         tp_config = {
94             'traffic_profile': {
95                 'lower_bound': 10.0,
96                 'upper_bound': 100.0,
97                 'step_value': 10.0,
98             },
99         }
100
101         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
102
103         traffic_gen = mock.MagicMock()
104         traffic_gen.resource_helper.run_test.return_value = success_tuple
105
106         profile = ProxRampProfile(tp_config)
107         profile.fill_samples = fill_samples = mock.MagicMock()
108         profile.queue = mock.MagicMock()
109
110         profile.run_test_with_pkt_size(traffic_gen, 128, 30)
111         self.assertEqual(traffic_gen.resource_helper.run_test.call_count, 10)
112         self.assertEqual(fill_samples.call_count, 10)
113
114     def test_run_test_with_pkt_size_with_fail(self):
115         tp_config = {
116             'traffic_profile': {
117                 'lower_bound': 10.0,
118                 'upper_bound': 100.0,
119                 'step_value': 10.0,
120             },
121         }
122
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_gen = mock.MagicMock()
127         traffic_gen.resource_helper.run_test.side_effect = [
128             success_tuple,
129             success_tuple,
130             success_tuple,
131             fail_tuple,
132             success_tuple,
133             fail_tuple,
134             fail_tuple,
135             fail_tuple,
136         ]
137
138         profile = ProxRampProfile(tp_config)
139         profile.fill_samples = fill_samples = mock.MagicMock()
140         profile.queue = mock.MagicMock()
141
142         profile.run_test_with_pkt_size(traffic_gen, 128, 30)
143         self.assertEqual(traffic_gen.resource_helper.run_test.call_count, 4)
144         self.assertEqual(fill_samples.call_count, 3)