Merge "pip_license: quick hack script to add license info to requirements.txt"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_rfc2544_trex.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 import unittest
20 import mock
21
22 SSH_HELPER = "yardstick.ssh.SSH"
23
24 STL_MOCKS = {
25     'stl': mock.MagicMock(),
26     'stl.trex_stl_lib': mock.MagicMock(),
27     'stl.trex_stl_lib.base64': mock.MagicMock(),
28     'stl.trex_stl_lib.binascii': mock.MagicMock(),
29     'stl.trex_stl_lib.collections': mock.MagicMock(),
30     'stl.trex_stl_lib.copy': mock.MagicMock(),
31     'stl.trex_stl_lib.datetime': mock.MagicMock(),
32     'stl.trex_stl_lib.functools': mock.MagicMock(),
33     'stl.trex_stl_lib.imp': mock.MagicMock(),
34     'stl.trex_stl_lib.inspect': mock.MagicMock(),
35     'stl.trex_stl_lib.json': mock.MagicMock(),
36     'stl.trex_stl_lib.linecache': mock.MagicMock(),
37     'stl.trex_stl_lib.math': mock.MagicMock(),
38     'stl.trex_stl_lib.os': mock.MagicMock(),
39     'stl.trex_stl_lib.platform': mock.MagicMock(),
40     'stl.trex_stl_lib.pprint': mock.MagicMock(),
41     'stl.trex_stl_lib.random': mock.MagicMock(),
42     'stl.trex_stl_lib.re': mock.MagicMock(),
43     'stl.trex_stl_lib.scapy': mock.MagicMock(),
44     'stl.trex_stl_lib.socket': mock.MagicMock(),
45     'stl.trex_stl_lib.string': mock.MagicMock(),
46     'stl.trex_stl_lib.struct': mock.MagicMock(),
47     'stl.trex_stl_lib.sys': mock.MagicMock(),
48     'stl.trex_stl_lib.threading': mock.MagicMock(),
49     'stl.trex_stl_lib.time': mock.MagicMock(),
50     'stl.trex_stl_lib.traceback': mock.MagicMock(),
51     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
52     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
53     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
54     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
59     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
60     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
61     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
62     'stl.trex_stl_lib.types': mock.MagicMock(),
63     'stl.trex_stl_lib.utils': mock.MagicMock(),
64     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
65     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
66     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
67     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
75     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
76     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
77     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
78     'stl.trex_stl_lib.warnings': mock.MagicMock(),
79     'stl.trex_stl_lib.yaml': mock.MagicMock(),
80     'stl.trex_stl_lib.zlib': mock.MagicMock(),
81     'stl.trex_stl_lib.zmq': mock.MagicMock(),
82 }
83
84 STLClient = mock.MagicMock()
85 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
86 stl_patch.start()
87
88 if stl_patch:
89     from yardstick.network_services.vnf_generic.vnf.tg_rfc2544_trex import TrexTrafficGenRFC, \
90         TrexRfcResourceHelper
91     from yardstick.network_services.vnf_generic.vnf import tg_rfc2544_trex
92     from yardstick.network_services.traffic_profile.base import TrafficProfile
93     from tests.unit.network_services.vnf_generic.vnf.test_base import FileAbsPath, mock_ssh
94
95 MODULE_PATH = FileAbsPath(__file__)
96 get_file_abspath = MODULE_PATH.get_path
97
98
99 class TestTrexRfcResouceHelper(unittest.TestCase):
100
101     @mock.patch('yardstick.network_services.helpers.samplevnf_helper.MultiPortConfig')
102     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_rfc2544_trex.time")
103     @mock.patch(SSH_HELPER)
104     def test__run_traffic_once(self, ssh, *_):
105         mock_ssh(ssh)
106
107         mock_traffic_profile = mock.MagicMock(autospec=TrafficProfile,
108                                               **{'get_drop_percentage.return_value': {}})
109         sut = TrexRfcResourceHelper(mock.MagicMock(), mock.MagicMock())
110         sut.client = mock.MagicMock()
111         sut._run_traffic_once(mock_traffic_profile)
112
113
114 class TestTrexTrafficGenRFC(unittest.TestCase):
115
116     VNFD_0 = {
117         'short-name': 'VpeVnf',
118         'vdu': [
119             {
120                 'routing_table': [
121                     {
122                         'network': '152.16.100.20',
123                         'netmask': '255.255.255.0',
124                         'gateway': '152.16.100.20',
125                         'if': 'xe0',
126                     },
127                     {
128                         'network': '152.16.40.20',
129                         'netmask': '255.255.255.0',
130                         'gateway': '152.16.40.20',
131                         'if': 'xe1',
132                     },
133                 ],
134                 'description': 'VPE approximation using DPDK',
135                 'name': 'vpevnf-baremetal',
136                 'nd_route_tbl': [
137                     {
138                         'network': '0064:ff9b:0:0:0:0:9810:6414',
139                         'netmask': '112',
140                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
141                         'if': 'xe0',
142                     },
143                     {
144                         'network': '0064:ff9b:0:0:0:0:9810:2814',
145                         'netmask': '112',
146                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
147                         'if': 'xe1',
148                     },
149                 ],
150                 'id': 'vpevnf-baremetal',
151                 'external-interface': [
152                     {
153                         'virtual-interface': {
154                             'ifname': 'xe0',
155                             'dst_mac': '00:00:00:00:00:04',
156                             'vpci': '0000:05:00.0',
157                             'local_ip': '152.16.100.19',
158                             'type': 'PCI-PASSTHROUGH',
159                             'netmask': '255.255.255.0',
160                             'vld_id': 'private_1',
161                             'dpdk_port_num': '0',
162                             'bandwidth': '10 Gbps',
163                             'driver': "i40e",
164                             'dst_ip': '152.16.100.20',
165                             'local_iface_name': 'xe0',
166                             'local_mac': '00:00:00:00:00:01',
167                         },
168                         'vnfd-connection-point-ref': 'xe0',
169                         'name': 'xe0',
170                     },
171                     {
172                         'virtual-interface': {
173                             'ifname': 'xe1',
174                             'dst_mac': '00:00:00:00:00:03',
175                             'vpci': '0000:05:00.1',
176                             'local_ip': '152.16.40.19',
177                             'type': 'PCI-PASSTHROUGH',
178                             'driver': "i40e",
179                             'netmask': '255.255.255.0',
180                             'vld_id': 'public_1',
181                             'dpdk_port_num': '1',
182                             'bandwidth': '10 Gbps',
183                             'dst_ip': '152.16.40.20',
184                             'local_iface_name': 'xe1',
185                             'local_mac': '00:00:00:00:00:02'
186                         },
187                         'vnfd-connection-point-ref': 'xe1',
188                         'name': 'xe1',
189                     },
190                 ],
191             },
192         ],
193         'description': 'Vpe approximation using DPDK',
194         'mgmt-interface': {
195             'vdu-id': 'vpevnf-baremetal',
196             'host': '1.1.1.1',
197             'password': 'r00t',
198             'user': 'root',
199             'ip': '1.1.1.1',
200         },
201         'benchmark': {
202             'kpi': [
203                 'packets_in',
204                 'packets_fwd',
205                 'packets_dropped',
206             ],
207         },
208         'connection-point': [
209             {
210                 'type': 'VPORT',
211                 'name': 'xe0',
212             },
213             {
214                 'type': 'VPORT',
215                 'name': 'xe1',
216             },
217         ],
218         'id': 'VpeApproxVnf',
219         'name': 'VPEVnfSsh',
220     }
221
222     VNFD = {
223         'vnfd:vnfd-catalog': {
224             'vnfd': [
225                 VNFD_0,
226             ],
227         },
228     }
229
230     TRAFFIC_PROFILE = {
231         "schema": "isb:traffic_profile:0.1",
232         "name": "fixed",
233         "description": "Fixed traffic profile to run UDP traffic",
234         "traffic_profile": {
235             "traffic_type": "FixedTraffic",
236             "frame_rate": 100,  # pps
237             "flow_number": 10,
238             "frame_size": 64,
239         },
240     }
241
242     TC_YAML = {
243         'scenarios': [
244             {
245                 'tc_options': {
246                     'rfc2544': {
247                         'allowed_drop_rate': '0.8 - 1',
248                     },
249                 },
250                 'runner': {
251                     'duration': 400,
252                     'interval': 35,
253                     'type': 'Duration',
254                 },
255                 'traffic_options': {
256                     'flow': 'ipv4_1flow_Packets_vpe.yaml',
257                     'imix': 'imix_voice.yaml',
258                 },
259                 'vnf_options': {
260                     'vpe': {
261                         'cfg': 'vpe_config',
262                     },
263                 },
264                 'traffic_profile': 'ipv4_throughput_vpe.yaml',
265                 'type': 'NSPerf',
266                 'nodes': {
267                     'tg__1': 'trafficgen_1.yardstick',
268                     'vnf__1': 'vnf.yardstick',
269                 },
270                 'topology': 'vpe_vnf_topology.yaml',
271             },
272         ],
273         'context': {
274             'nfvi_type': 'baremetal',
275             'type': 'Node',
276             'name': 'yardstick',
277             'file': '/etc/yardstick/nodes/pod.yaml',
278         },
279         'schema': 'yardstick:task:0.1',
280     }
281
282     @mock.patch(SSH_HELPER)
283     def test___init__(self, ssh):
284         mock_ssh(ssh)
285         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
286         self.assertIsNotNone(trex_traffic_gen.resource_helper._terminated.value)
287
288     @mock.patch(SSH_HELPER)
289     def test_collect_kpi(self, ssh):
290         mock_ssh(ssh)
291         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
292         self.assertIsNone(trex_traffic_gen.collect_kpi())
293
294     @mock.patch(SSH_HELPER)
295     def test_listen_traffic(self, ssh):
296         mock_ssh(ssh)
297         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
298         self.assertIsNone(trex_traffic_gen.listen_traffic({}))
299
300     @mock.patch(SSH_HELPER)
301     def test_instantiate(self, ssh):
302         mock_ssh(ssh)
303
304         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
305         mock_traffic_profile.get_traffic_definition.return_value = "64"
306         mock_traffic_profile.params = self.TRAFFIC_PROFILE
307
308         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
309         trex_traffic_gen._start_server = mock.Mock(return_value=0)
310         trex_traffic_gen.resource_helper = mock.MagicMock()
311         scenario_cfg = {
312             "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
313             "topology": 'nsb_test_case.yaml',
314             'options': {
315                 'packetsize': 64,
316                 'traffic_type': 4,
317                 'rfc2544': {
318                     'allowed_drop_rate': '0.8 - 1',
319                 },
320                 'vnf__1': {
321                     'rules': 'acl_1rule.yaml',
322                     'vnf_config': {
323                         'lb_config': 'SW',
324                         'lb_count': 1,
325                         'worker_config': '1C/1T',
326                         'worker_threads': 1
327                     },
328                 },
329             },
330         }
331         tg_rfc2544_trex.WAIT_TIME = 3
332         scenario_cfg.update({"nodes": ["tg_1", "vnf_1"]})
333         self.assertIsNone(trex_traffic_gen.instantiate(scenario_cfg, {}))
334
335     @mock.patch(SSH_HELPER)
336     def test_instantiate_error(self, ssh):
337         mock_ssh(ssh, exec_result=(1, "", ""))
338
339         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
340         mock_traffic_profile.get_traffic_definition.return_value = "64"
341         mock_traffic_profile.params = self.TRAFFIC_PROFILE
342
343         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
344         trex_traffic_gen.resource_helper = mock.MagicMock()
345         scenario_cfg = {
346             "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
347             "nodes": [
348                 "tg_1",
349                 "vnf_1",
350             ],
351             "topology": 'nsb_test_case.yaml',
352             'options': {
353                 'packetsize': 64,
354                 'traffic_type': 4,
355                 'rfc2544': {
356                     'allowed_drop_rate': '0.8 - 1',
357                 },
358                 'vnf__1': {
359                     'rules': 'acl_1rule.yaml',
360                     'vnf_config': {
361                         'lb_config': 'SW',
362                         'lb_count': 1,
363                         'worker_config': '1C/1T',
364                         'worker_threads': 1,
365                     },
366                 },
367             },
368         }
369         trex_traffic_gen.instantiate(scenario_cfg, {})
370
371     @mock.patch(SSH_HELPER)
372     def test__start_server(self, ssh):
373         mock_ssh(ssh)
374         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
375         trex_traffic_gen.resource_helper = mock.MagicMock()
376         self.assertIsNone(trex_traffic_gen._start_server())
377
378     @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_rfc2544_trex.time")
379     @mock.patch(SSH_HELPER)
380     def test__generate_trex_cfg(self, ssh, _):
381         mock_ssh(ssh)
382
383         trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
384         trex_traffic_gen.ssh_helper = mock.MagicMock()
385         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
386         self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
387
388     def test_scale(self):
389         with mock.patch(SSH_HELPER) as ssh:
390             ssh_mock = mock.Mock(autospec=ssh.SSH)
391             ssh_mock.execute = mock.Mock(return_value=(0, "", ""))
392             ssh_mock.run = mock.Mock(return_value=(0, "", ""))
393             ssh.from_node.return_value = ssh_mock
394             trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
395             trex_traffic_gen.scale('')
396
397     def test_terminate(self):
398         with mock.patch(SSH_HELPER) as ssh:
399             ssh_mock = mock.Mock(autospec=ssh.SSH)
400             ssh_mock.execute = mock.Mock(return_value=(0, "", ""))
401             ssh.from_node.return_value = ssh_mock
402             trex_traffic_gen = TrexTrafficGenRFC('vnf1', self.VNFD_0)
403             trex_traffic_gen.resource_helper = mock.MagicMock()
404             self.assertIsNone(trex_traffic_gen.terminate())