NSB: move interface probe to VNF, and attempt driver-only probe first
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_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 import copy
19 import mock
20
21 import unittest
22
23 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
24 from tests.unit import STL_MOCKS
25
26
27 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
28 NAME = 'vnf_1'
29
30 STLClient = mock.MagicMock()
31 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
32 stl_patch.start()
33
34 if stl_patch:
35     from yardstick.network_services.vnf_generic.vnf.tg_trex import \
36         TrexTrafficGen, TrexResourceHelper
37     from yardstick.network_services.traffic_profile.base import TrafficProfile
38
39
40 class TestTrexTrafficGen(unittest.TestCase):
41     VNFD = {'vnfd:vnfd-catalog':
42                 {'vnfd':
43                      [{'short-name': 'VpeVnf',
44                        'vdu':
45                            [{'routing_table':
46                                  [{'network': '152.16.100.20',
47                                    'netmask': '255.255.255.0',
48                                    'gateway': '152.16.100.20',
49                                    'if': 'xe0'},
50                                   {'network': '152.16.40.20',
51                                    'netmask': '255.255.255.0',
52                                    'gateway': '152.16.40.20',
53                                    'if': 'xe1'}],
54                              'description': 'VPE approximation using DPDK',
55                              'name': 'vpevnf-baremetal',
56                              'nd_route_tbl':
57                                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
58                                    'netmask': '112',
59                                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
60                                    'if': 'xe0'},
61                                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
62                                    'netmask': '112',
63                                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
64                                    'if': 'xe1'}],
65                              'id': 'vpevnf-baremetal',
66                              'external-interface':
67                                  [{'virtual-interface':
68                                        {'dst_mac': '00:00:00:00:00:04',
69                                         'vpci': '0000:05:00.0',
70                                         'local_ip': '152.16.100.19',
71                                         'type': 'PCI-PASSTHROUGH',
72                                         'netmask': '255.255.255.0',
73                                         'dpdk_port_num': 0,
74                                         'bandwidth': '10 Gbps',
75                                         'driver': "i40e",
76                                         'dst_ip': '152.16.100.20',
77                                         'local_iface_name': 'xe0',
78                                         'vld_id': 'downlink_0',
79                                         'ifname': 'xe0',
80                                         'local_mac': '00:00:00:00:00:02'},
81                                    'vnfd-connection-point-ref': 'xe0',
82                                    'name': 'xe0'},
83                                   {'virtual-interface':
84                                        {'dst_mac': '00:00:00:00:00:03',
85                                         'vpci': '0000:05:00.1',
86                                         'local_ip': '152.16.40.19',
87                                         'type': 'PCI-PASSTHROUGH',
88                                         'driver': "i40e",
89                                         'netmask': '255.255.255.0',
90                                         'dpdk_port_num': 1,
91                                         'bandwidth': '10 Gbps',
92                                         'dst_ip': '152.16.40.20',
93                                         'local_iface_name': 'xe1',
94                                         'vld_id': 'uplink_0',
95                                         'ifname': 'xe1',
96                                         'local_mac': '00:00:00:00:00:01'},
97                                    'vnfd-connection-point-ref': 'xe1',
98                                    'name': 'xe1'}]}],
99                        'description': 'Vpe approximation using DPDK',
100                        'mgmt-interface':
101                            {'vdu-id': 'vpevnf-baremetal',
102                             'host': '1.1.1.1',
103                             'password': 'r00t',
104                             'user': 'root',
105                             'ip': '1.1.1.1'},
106                        'benchmark':
107                            {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
108                        'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
109                                             {'type': 'VPORT', 'name': 'xe1'}],
110                        'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
111
112     TRAFFIC_PROFILE = {
113         "schema": "isb:traffic_profile:0.1",
114         "name": "fixed",
115         "description": "Fixed traffic profile to run UDP traffic",
116         "traffic_profile": {
117             "traffic_type": "FixedTraffic",
118             "frame_rate": 100,  # pps
119             "flow_number": 10,
120             "frame_size": 64
121         },
122     }
123
124     SCENARIO_CFG = {
125         "options": {
126             "packetsize": 64,
127             "traffic_type": 4,
128             "rfc2544": {
129                 "allowed_drop_rate": "0.8 - 1",
130             },
131             "vnf__1": {
132                 "rules": "acl_1rule.yaml",
133                 "vnf_config": {
134                     "lb_config": "SW",
135                     "lb_count": 1,
136                     "worker_config": "1C/1T",
137                     "worker_threads": 1,
138                 }
139             }
140         },
141         "task_id": "a70bdf4a-8e67-47a3-9dc1-273c14506eb7",
142         "tc": "tc_ipv4_1Mflow_64B_packetsize",
143         "runner": {
144             "object": "NetworkServiceTestCase",
145             "interval": 35,
146             "output_filename": "/tmp/yardstick.out",
147             "runner_id": 74476, "duration": 400,
148             "type": "Duration"
149         },
150         "traffic_profile": "ipv4_throughput_acl.yaml",
151         "traffic_options": {
152             "flow": "ipv4_Packets_acl.yaml",
153             "imix": "imix_voice.yaml"
154         },
155         "type": "ISB",
156         "nodes": {
157             "tg__2": "trafficgen_2.yardstick",
158             "tg__1": "trafficgen_1.yardstick",
159             "vnf__1": "vnf.yardstick"
160         },
161         "topology": "udpreplay-tg-topology-baremetal.yaml"
162     }
163
164     CONTEXT_CFG = {
165         "nodes": {
166             "vnf__1": {
167                 "vnfd-id-ref": "vnf__1",
168                 "ip": "1.2.1.1",
169                 "interfaces": {
170                     "xe0": {
171                         "local_iface_name": "ens786f0",
172                         "vld_id": TrafficProfile.UPLINK,
173                         "netmask": "255.255.255.0",
174                         "vpci": "0000:05:00.0",
175                         "local_ip": "152.16.100.19",
176                         "driver": "i40e",
177                         "dst_ip": "152.16.100.20",
178                         "local_mac": "00:00:00:00:00:02",
179                         "dst_mac": "00:00:00:00:00:04",
180                         "dpdk_port_num": 0
181                     },
182                     "xe1": {
183                         "local_iface_name": "ens786f1",
184                         "vld_id": TrafficProfile.DOWNLINK,
185                         "netmask": "255.255.255.0",
186                         "vpci": "0000:05:00.1",
187                         "local_ip": "152.16.40.19",
188                         "driver": "i40e",
189                         "dst_ip": "152.16.40.20",
190                         "local_mac": "00:00:00:00:00:01",
191                         "dst_mac": "00:00:00:00:00:03",
192                         "dpdk_port_num": 1
193                     }
194                 },
195                 "host": "1.2.1.1",
196                 "user": "root",
197                 "nd_route_tbl": [
198                     {
199                         "netmask": "112",
200                         "if": "xe0",
201                         "gateway": "0064:ff9b:0:0:0:0:9810:6414",
202                         "network": "0064:ff9b:0:0:0:0:9810:6414"
203                     },
204                     {
205                         "netmask": "112",
206                         "if": "xe1",
207                         "gateway": "0064:ff9b:0:0:0:0:9810:2814",
208                         "network": "0064:ff9b:0:0:0:0:9810:2814"
209                     }
210                 ],
211                 "password": "r00t",
212                 "VNF model": "udp_replay.yaml",
213                 "name": "vnf.yardstick",
214                 "member-vnf-index": "2",
215                 "routing_table": [
216                     {
217                         "netmask": "255.255.255.0",
218                         "if": "xe0",
219                         "gateway": "152.16.100.20",
220                         "network": "152.16.100.20"
221                     },
222                     {
223                         "netmask": "255.255.255.0",
224                         "if": "xe1",
225                         "gateway": "152.16.40.20",
226                         "network": "152.16.40.20"
227                     }
228                 ],
229                 "role": "vnf"
230             },
231             "trafficgen_2.yardstick": {
232                 "member-vnf-index": "3",
233                 "role": "TrafficGen",
234                 "name": "trafficgen_2.yardstick",
235                 "vnfd-id-ref": "tg__2",
236                 "ip": "1.2.1.1",
237                 "interfaces": {
238                     "xe0": {
239                         "local_iface_name": "ens513f0",
240                         "vld_id": TrafficProfile.DOWNLINK,
241                         "netmask": "255.255.255.0",
242                         "vpci": "0000:02:00.0",
243                         "local_ip": "152.16.40.20",
244                         "driver": "ixgbe",
245                         "dst_ip": "152.16.40.19",
246                         "local_mac": "00:00:00:00:00:03",
247                         "dst_mac": "00:00:00:00:00:01",
248                         "dpdk_port_num": 0
249                     },
250                     "xe1": {
251                         "local_iface_name": "ens513f1",
252                         "netmask": "255.255.255.0",
253                         "network": "202.16.100.0",
254                         "local_ip": "202.16.100.20",
255                         "driver": "ixgbe",
256                         "local_mac": "00:1e:67:d0:60:5d",
257                         "vpci": "0000:02:00.1",
258                         "dpdk_port_num": 1
259                     }
260                 },
261                 "password": "r00t",
262                 "VNF model": "l3fwd_vnf.yaml",
263                 "user": "root"
264             },
265             "trafficgen_1.yardstick": {
266                 "member-vnf-index": "1",
267                 "role": "TrafficGen",
268                 "name": "trafficgen_1.yardstick",
269                 "vnfd-id-ref": "tg__1",
270                 "ip": "1.2.1.1",
271                 "interfaces": {
272                     "xe0": {
273                         "local_iface_name": "ens785f0",
274                         "vld_id": TrafficProfile.UPLINK,
275                         "netmask": "255.255.255.0",
276                         "vpci": "0000:05:00.0",
277                         "local_ip": "152.16.100.20",
278                         "driver": "i40e",
279                         "dst_ip": "152.16.100.19",
280                         "local_mac": "00:00:00:00:00:04",
281                         "dst_mac": "00:00:00:00:00:02",
282                         "dpdk_port_num": 0
283                     },
284                     "xe1": {
285                         "local_ip": "152.16.100.21",
286                         "driver": "i40e",
287                         "vpci": "0000:05:00.1",
288                         "dpdk_port_num": 1,
289                         "local_iface_name": "ens785f1",
290                         "netmask": "255.255.255.0",
291                         "local_mac": "00:00:00:00:00:01"
292                     }
293                 },
294                 "password": "r00t",
295                 "VNF model": "tg_rfc2544_tpl.yaml",
296                 "user": "root"
297             }
298         }
299     }
300
301     @mock.patch(SSH_HELPER)
302     def test___init__(self, ssh):
303         mock_ssh(ssh)
304         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
305         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
306         self.assertIsInstance(trex_traffic_gen.resource_helper, TrexResourceHelper)
307
308     @mock.patch(SSH_HELPER)
309     def test_collect_kpi(self, ssh):
310         mock_ssh(ssh)
311         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
312         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
313         trex_traffic_gen.resource_helper._queue.put({})
314         result = trex_traffic_gen.collect_kpi()
315         self.assertEqual({}, result)
316
317     @mock.patch(SSH_HELPER)
318     def test_listen_traffic(self, ssh):
319         mock_ssh(ssh)
320         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
321         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
322         self.assertIsNone(trex_traffic_gen.listen_traffic({}))
323
324     @mock.patch(SSH_HELPER)
325     def test_instantiate(self, ssh):
326         mock_ssh(ssh)
327
328         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
329         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
330         trex_traffic_gen._start_server = mock.Mock(return_value=0)
331         trex_traffic_gen._tg_process = mock.MagicMock()
332         trex_traffic_gen._tg_process.start = mock.Mock()
333         trex_traffic_gen._tg_process.exitcode = 0
334         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=1)
335         trex_traffic_gen.ssh_helper = mock.MagicMock()
336         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
337         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
338
339         self.assertIsNone(trex_traffic_gen.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
340
341     @mock.patch(SSH_HELPER)
342     def test_instantiate_error(self, ssh):
343         mock_ssh(ssh, exec_result=(1, "", ""))
344
345         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
346         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
347         trex_traffic_gen._start_server = mock.Mock(return_value=0)
348         trex_traffic_gen._tg_process = mock.MagicMock()
349         trex_traffic_gen._tg_process.start = mock.Mock()
350         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=0)
351         trex_traffic_gen.ssh_helper = mock.MagicMock()
352         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
353         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
354         self.assertIsNone(trex_traffic_gen.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
355
356     @mock.patch(SSH_HELPER)
357     def test__start_server(self, ssh):
358         mock_ssh(ssh)
359         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
360         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
361         trex_traffic_gen.ssh_helper = mock.MagicMock()
362         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
363         trex_traffic_gen.scenario_helper.scenario_cfg = {}
364         self.assertIsNone(trex_traffic_gen._start_server())
365
366     @mock.patch(SSH_HELPER)
367     def test__start_server_multiple_queues(self, ssh):
368         mock_ssh(ssh)
369         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
370         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
371         trex_traffic_gen.ssh_helper = mock.MagicMock()
372         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
373         trex_traffic_gen.scenario_helper.scenario_cfg = {"options": {NAME: {"queues_per_port": 2}}}
374         self.assertIsNone(trex_traffic_gen._start_server())
375
376     @mock.patch(SSH_HELPER)
377     def test__traffic_runner(self, ssh):
378         mock_ssh(ssh)
379
380         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
381         mock_traffic_profile.get_traffic_definition.return_value = "64"
382         mock_traffic_profile.execute_traffic.return_value = "64"
383         mock_traffic_profile.params = self.TRAFFIC_PROFILE
384
385         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
386         self.sut = TrexTrafficGen(NAME, vnfd)
387         self.sut.ssh_helper = mock.Mock()
388         self.sut.ssh_helper.run = mock.Mock()
389         self.sut._connect_client = mock.Mock(autospec=STLClient)
390         self.sut._connect_client.get_stats = mock.Mock(return_value="0")
391         self.sut.resource_helper.RUN_DURATION = 0
392         self.sut.resource_helper.QUEUE_WAIT_TIME = 0
393         # must generate cfg before we can run traffic so Trex port mapping is created
394         self.sut.resource_helper.generate_cfg()
395         self.sut._traffic_runner(mock_traffic_profile)
396
397     @mock.patch(SSH_HELPER)
398     def test__generate_trex_cfg(self, ssh):
399         mock_ssh(ssh)
400         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
401         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
402         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
403         self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
404
405     @mock.patch(SSH_HELPER)
406     def test_build_ports_reversed_pci_ordering(self, ssh):
407         mock_ssh(ssh)
408         vnfd = copy.deepcopy(self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
409         vnfd['vdu'][0]['external-interface'] = [
410             {'virtual-interface':
411                  {'dst_mac': '00:00:00:00:00:04',
412                   'vpci': '0000:05:00.0',
413                   'local_ip': '152.16.100.19',
414                   'type': 'PCI-PASSTHROUGH',
415                   'netmask': '255.255.255.0',
416                   'dpdk_port_num': 2,
417                   'bandwidth': '10 Gbps',
418                   'driver': "i40e",
419                   'dst_ip': '152.16.100.20',
420                   'local_iface_name': 'xe0',
421                   'vld_id': 'downlink_0',
422                   'ifname': 'xe0',
423                   'local_mac': '00:00:00:00:00:02'},
424              'vnfd-connection-point-ref': 'xe0',
425              'name': 'xe0'},
426             {'virtual-interface':
427                  {'dst_mac': '00:00:00:00:00:03',
428                   'vpci': '0000:04:00.0',
429                   'local_ip': '152.16.40.19',
430                   'type': 'PCI-PASSTHROUGH',
431                   'driver': "i40e",
432                   'netmask': '255.255.255.0',
433                   'dpdk_port_num': 0,
434                   'bandwidth': '10 Gbps',
435                   'dst_ip': '152.16.40.20',
436                   'local_iface_name': 'xe1',
437                   'vld_id': 'uplink_0',
438                   'ifname': 'xe1',
439                   'local_mac': '00:00:00:00:00:01'},
440              'vnfd-connection-point-ref': 'xe1',
441              'name': 'xe1'}]
442         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
443         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
444         trex_traffic_gen.resource_helper.generate_cfg()
445         trex_traffic_gen.resource_helper._build_ports()
446         self.assertEqual(sorted(trex_traffic_gen.resource_helper.all_ports), [0, 1])
447         # there is a gap in ordering
448         self.assertEqual(dict(trex_traffic_gen.resource_helper.dpdk_to_trex_port_map),
449                          {0: 0, 2: 1})
450
451     @mock.patch(SSH_HELPER)
452     def test_run_traffic(self, ssh):
453         mock_ssh(ssh)
454
455         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
456         mock_traffic_profile.get_traffic_definition.return_value = "64"
457         mock_traffic_profile.params = self.TRAFFIC_PROFILE
458
459         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
460         self.sut = TrexTrafficGen(NAME, vnfd)
461         self.sut.ssh_helper = mock.Mock()
462         self.sut.ssh_helper.run = mock.Mock()
463         self.sut._traffic_runner = mock.Mock(return_value=0)
464         self.sut.resource_helper.client_started.value = 1
465         result = self.sut.run_traffic(mock_traffic_profile)
466         self.sut._traffic_process.terminate()
467         self.assertIsNotNone(result)
468
469     @mock.patch(SSH_HELPER)
470     def test_terminate(self, ssh):
471         mock_ssh(ssh)
472         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
473         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
474         trex_traffic_gen.ssh_helper = mock.MagicMock()
475         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
476         self.assertIsNone(trex_traffic_gen.terminate())
477
478     @mock.patch(SSH_HELPER)
479     def test__connect_client(self, ssh):
480         mock_ssh(ssh)
481         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
482         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
483         client = mock.Mock(autospec=STLClient)
484         client.connect = mock.Mock(return_value=0)
485         self.assertIsNotNone(trex_traffic_gen.resource_helper._connect(client))
486
487
488 if __name__ == '__main__':
489     unittest.main()