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