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