Merge "heat: close file before parsing template"
[yardstick.git] / tests / unit / network_services / traffic_profile / test_rfc2544.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 from __future__ import absolute_import
19 from __future__ import division
20 import unittest
21 import mock
22
23 STL_MOCKS = {
24     'stl': mock.MagicMock(),
25     'stl.trex_stl_lib': mock.MagicMock(),
26     'stl.trex_stl_lib.base64': mock.MagicMock(),
27     'stl.trex_stl_lib.binascii': mock.MagicMock(),
28     'stl.trex_stl_lib.collections': mock.MagicMock(),
29     'stl.trex_stl_lib.copy': mock.MagicMock(),
30     'stl.trex_stl_lib.datetime': mock.MagicMock(),
31     'stl.trex_stl_lib.functools': mock.MagicMock(),
32     'stl.trex_stl_lib.imp': mock.MagicMock(),
33     'stl.trex_stl_lib.inspect': mock.MagicMock(),
34     'stl.trex_stl_lib.json': mock.MagicMock(),
35     'stl.trex_stl_lib.linecache': mock.MagicMock(),
36     'stl.trex_stl_lib.math': mock.MagicMock(),
37     'stl.trex_stl_lib.os': mock.MagicMock(),
38     'stl.trex_stl_lib.platform': mock.MagicMock(),
39     'stl.trex_stl_lib.pprint': mock.MagicMock(),
40     'stl.trex_stl_lib.random': mock.MagicMock(),
41     'stl.trex_stl_lib.re': mock.MagicMock(),
42     'stl.trex_stl_lib.scapy': mock.MagicMock(),
43     'stl.trex_stl_lib.socket': mock.MagicMock(),
44     'stl.trex_stl_lib.string': mock.MagicMock(),
45     'stl.trex_stl_lib.struct': mock.MagicMock(),
46     'stl.trex_stl_lib.sys': mock.MagicMock(),
47     'stl.trex_stl_lib.threading': mock.MagicMock(),
48     'stl.trex_stl_lib.time': mock.MagicMock(),
49     'stl.trex_stl_lib.traceback': mock.MagicMock(),
50     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
51     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
52     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
53     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
54     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
59     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
60     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
61     'stl.trex_stl_lib.types': mock.MagicMock(),
62     'stl.trex_stl_lib.utils': mock.MagicMock(),
63     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
64     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
65     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
66     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
67     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
75     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
76     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
77     'stl.trex_stl_lib.warnings': mock.MagicMock(),
78     'stl.trex_stl_lib.yaml': mock.MagicMock(),
79     'stl.trex_stl_lib.zlib': mock.MagicMock(),
80     'stl.trex_stl_lib.zmq': mock.MagicMock(),
81 }
82
83 STLClient = mock.MagicMock()
84 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
85 stl_patch.start()
86
87 if stl_patch:
88     from yardstick.network_services.traffic_profile.traffic_profile \
89         import TrexProfile
90     from yardstick.network_services.traffic_profile.rfc2544 import \
91         RFC2544Profile
92
93
94 class TestRFC2544Profile(unittest.TestCase):
95     TRAFFIC_PROFILE = {
96         "schema": "isb:traffic_profile:0.1",
97         "name": "fixed",
98         "description": "Fixed traffic profile to run UDP traffic",
99         "traffic_profile": {
100             "traffic_type": "FixedTraffic",
101             "frame_rate": 100,  # pps
102             "flow_number": 10,
103             "frame_size": 64}}
104
105     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
106                'name': 'rfc2544',
107                'traffic_profile': {'traffic_type': 'RFC2544Profile',
108                                    'frame_rate': 100},
109                'public': {'ipv4':
110                           {'outer_l2': {'framesize':
111                                         {'64B': '100', '1518B': '0',
112                                          '128B': '0', '1400B': '0',
113                                          '256B': '0', '373b': '0',
114                                          '570B': '0'}},
115                            'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
116                                           'proto': 'udp',
117                                           'srcip4': '90.90.1.1-90.105.255.255',
118                                           'dscp': 0, 'ttl': 32},
119                            'outer_l4': {'srcport': '2001',
120                                         'dsrport': '1234'}}},
121                'private': {'ipv4':
122                            {'outer_l2': {'framesize':
123                                          {'64B': '100', '1518B': '0',
124                                           '128B': '0', '1400B': '0',
125                                           '256B': '0', '373b': '0',
126                                           '570B': '0'}},
127                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
128                                            'proto': 'udp',
129                                            'srcip4': '1.1.1.1-1.15.255.255',
130                                            'dscp': 0, 'ttl': 32},
131                             'outer_l4': {'dstport': '2001',
132                                          'srcport': '1234'}}},
133                'schema': 'isb:traffic_profile:0.1'}
134
135     def test___init__(self):
136         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
137         assert r_f_c2544_profile.rate
138
139     def test_execute(self):
140         traffic_generator = mock.Mock(autospec=TrexProfile)
141         traffic_generator.my_ports = [0, 1]
142         traffic_generator.client = \
143             mock.Mock(return_value=True)
144         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
145         r_f_c2544_profile.params = self.PROFILE
146         r_f_c2544_profile.first_run = True
147         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
148
149     def test_get_drop_percentage(self):
150         traffic_generator = mock.Mock(autospec=TrexProfile)
151         traffic_generator.my_ports = [0, 1]
152         traffic_generator.client = \
153             mock.Mock(return_value=True)
154         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
155         r_f_c2544_profile.params = self.PROFILE
156         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
157         samples = {}
158         for ifname in range(1):
159             name = "xe{}".format(ifname)
160             samples[name] = {"rx_throughput_fps": 20,
161                              "tx_throughput_fps": 20,
162                              "rx_throughput_mbps": 10,
163                              "tx_throughput_mbps": 10,
164                              "in_packets": 1000,
165                              "out_packets": 1000}
166         tol_min = 100.0
167         tolerance = 0.0
168         expected = {'DropPercentage': 0.0, 'RxThroughput': 100/3.0,
169                     'TxThroughput': 100/3.0, 'CurrentDropPercentage': 0.0,
170                     'Throughput': 100/3.0,
171                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
172                             'out_packets': 1000, 'rx_throughput_mbps': 10,
173                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
174         self.assertDictEqual(expected,
175                              r_f_c2544_profile.get_drop_percentage(
176                                  traffic_generator, samples,
177                                  tol_min, tolerance))
178
179     def test_get_drop_percentage_update(self):
180         traffic_generator = mock.Mock(autospec=TrexProfile)
181         traffic_generator.my_ports = [0, 1]
182         traffic_generator.client = \
183             mock.Mock(return_value=True)
184         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
185         r_f_c2544_profile.params = self.PROFILE
186         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
187         samples = {}
188         for ifname in range(1):
189             name = "xe{}".format(ifname)
190             samples[name] = {"rx_throughput_fps": 20,
191                              "tx_throughput_fps": 20,
192                              "rx_throughput_mbps": 10,
193                              "tx_throughput_mbps": 10,
194                              "in_packets": 1000,
195                              "out_packets": 1002}
196         tol_min = 0.0
197         tolerance = 1.0
198         expected = {'DropPercentage': 0.2, 'RxThroughput': 100/3.0,
199                     'TxThroughput': 33.4, 'CurrentDropPercentage': 0.2,
200                     'Throughput': 100/3.0,
201                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
202                             'out_packets': 1002, 'rx_throughput_mbps': 10,
203                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
204         self.assertDictEqual(expected,
205                              r_f_c2544_profile.get_drop_percentage(
206                                  traffic_generator, samples,
207                                  tol_min, tolerance))
208
209     def test_get_drop_percentage_div_zero(self):
210         traffic_generator = mock.Mock(autospec=TrexProfile)
211         traffic_generator.my_ports = [0, 1]
212         traffic_generator.client = \
213             mock.Mock(return_value=True)
214         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
215         r_f_c2544_profile.params = self.PROFILE
216         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
217         samples = {}
218         for ifname in range(1):
219             name = "xe{}".format(ifname)
220             samples[name] = {"rx_throughput_fps": 20,
221                              "tx_throughput_fps": 20,
222                              "rx_throughput_mbps": 10,
223                              "tx_throughput_mbps": 10,
224                              "in_packets": 1000,
225                              "out_packets": 0}
226         tol_min = 0.0
227         tolerance = 0.0
228         r_f_c2544_profile.tmp_throughput = 0
229         expected = {'DropPercentage': 100.0, 'RxThroughput': 100/3.0,
230                     'TxThroughput': 0.0, 'CurrentDropPercentage': 100.0,
231                     'Throughput': 100/3.0,
232                     'xe0': {'tx_throughput_fps': 20, 'in_packets': 1000,
233                             'out_packets': 0, 'rx_throughput_mbps': 10,
234                             'tx_throughput_mbps': 10, 'rx_throughput_fps': 20}}
235         self.assertDictEqual(expected,
236                              r_f_c2544_profile.get_drop_percentage(
237                                  traffic_generator, samples,
238                                  tol_min, tolerance))
239
240     def test_get_multiplier(self):
241         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
242         r_f_c2544_profile.max_rate = 100
243         r_f_c2544_profile.min_rate = 100
244         self.assertEqual("1.0", r_f_c2544_profile.get_multiplier())
245
246 if __name__ == '__main__':
247     unittest.main()