NSB: fix port topology
[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 import mock
22
23 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
24
25 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
26 from tests.unit import STL_MOCKS
27
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                                         'local_mac': '00:00:00:00:00:02'},
79                                    'vnfd-connection-point-ref': 'xe0',
80                                    'name': 'xe0'},
81                                   {'virtual-interface':
82                                        {'dst_mac': '00:00:00:00:00:03',
83                                         'vpci': '0000:05:00.1',
84                                         'local_ip': '152.16.40.19',
85                                         'type': 'PCI-PASSTHROUGH',
86                                         'driver': "i40e",
87                                         'netmask': '255.255.255.0',
88                                         'dpdk_port_num': 1,
89                                         'bandwidth': '10 Gbps',
90                                         'dst_ip': '152.16.40.20',
91                                         'local_iface_name': 'xe1',
92                                         'local_mac': '00:00:00:00:00:01'},
93                                    'vnfd-connection-point-ref': 'xe1',
94                                    'name': 'xe1'}]}],
95                        'description': 'Vpe approximation using DPDK',
96                        'mgmt-interface':
97                            {'vdu-id': 'vpevnf-baremetal',
98                             'host': '1.1.1.1',
99                             'password': 'r00t',
100                             'user': 'root',
101                             'ip': '1.1.1.1'},
102                        'benchmark':
103                            {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
104                        'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
105                                             {'type': 'VPORT', 'name': 'xe1'}],
106                        'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
107
108     TRAFFIC_PROFILE = {
109         "schema": "isb:traffic_profile:0.1",
110         "name": "fixed",
111         "description": "Fixed traffic profile to run UDP traffic",
112         "traffic_profile": {
113             "traffic_type": "FixedTraffic",
114             "frame_rate": 100,  # pps
115             "flow_number": 10,
116             "frame_size": 64
117         },
118     }
119
120     SCENARIO_CFG = {
121         "options": {
122             "packetsize": 64,
123             "traffic_type": 4,
124             "rfc2544": {
125                 "allowed_drop_rate": "0.8 - 1",
126             },
127             "vnf__1": {
128                 "rules": "acl_1rule.yaml",
129                 "vnf_config": {
130                     "lb_config": "SW",
131                     "lb_count": 1,
132                     "worker_config": "1C/1T",
133                     "worker_threads": 1,
134                 }
135             }
136         },
137         "task_id": "a70bdf4a-8e67-47a3-9dc1-273c14506eb7",
138         "tc": "tc_ipv4_1Mflow_64B_packetsize",
139         "runner": {
140             "object": "NetworkServiceTestCase",
141             "interval": 35,
142             "output_filename": "/tmp/yardstick.out",
143             "runner_id": 74476, "duration": 400,
144             "type": "Duration"
145         },
146         "traffic_profile": "ipv4_throughput_acl.yaml",
147         "traffic_options": {
148             "flow": "ipv4_Packets_acl.yaml",
149             "imix": "imix_voice.yaml"
150         },
151         "type": "ISB",
152         "nodes": {
153             "tg__2": "trafficgen_2.yardstick",
154             "tg__1": "trafficgen_1.yardstick",
155             "vnf__1": "vnf.yardstick"
156         },
157         "topology": "udpreplay-tg-topology-baremetal.yaml"
158     }
159
160     CONTEXT_CFG = {
161         "nodes": {
162             "vnf__1": {
163                 "vnfd-id-ref": "vnf__1",
164                 "ip": "1.2.1.1",
165                 "interfaces": {
166                     "xe0": {
167                         "local_iface_name": "ens786f0",
168                         "vld_id": "private",
169                         "netmask": "255.255.255.0",
170                         "vpci": "0000:05:00.0",
171                         "local_ip": "152.16.100.19",
172                         "driver": "i40e",
173                         "dst_ip": "152.16.100.20",
174                         "local_mac": "00:00:00:00:00:02",
175                         "dst_mac": "00:00:00:00:00:04",
176                         "dpdk_port_num": 0
177                     },
178                     "xe1": {
179                         "local_iface_name": "ens786f1",
180                         "vld_id": "public",
181                         "netmask": "255.255.255.0",
182                         "vpci": "0000:05:00.1",
183                         "local_ip": "152.16.40.19",
184                         "driver": "i40e",
185                         "dst_ip": "152.16.40.20",
186                         "local_mac": "00:00:00:00:00:01",
187                         "dst_mac": "00:00:00:00:00:03",
188                         "dpdk_port_num": 1
189                     }
190                 },
191                 "host": "1.2.1.1",
192                 "user": "root",
193                 "nd_route_tbl": [
194                     {
195                         "netmask": "112",
196                         "if": "xe0",
197                         "gateway": "0064:ff9b:0:0:0:0:9810:6414",
198                         "network": "0064:ff9b:0:0:0:0:9810:6414"
199                     },
200                     {
201                         "netmask": "112",
202                         "if": "xe1",
203                         "gateway": "0064:ff9b:0:0:0:0:9810:2814",
204                         "network": "0064:ff9b:0:0:0:0:9810:2814"
205                     }
206                 ],
207                 "password": "r00t",
208                 "VNF model": "udp_replay.yaml",
209                 "name": "vnf.yardstick",
210                 "member-vnf-index": "2",
211                 "routing_table": [
212                     {
213                         "netmask": "255.255.255.0",
214                         "if": "xe0",
215                         "gateway": "152.16.100.20",
216                         "network": "152.16.100.20"
217                     },
218                     {
219                         "netmask": "255.255.255.0",
220                         "if": "xe1",
221                         "gateway": "152.16.40.20",
222                         "network": "152.16.40.20"
223                     }
224                 ],
225                 "role": "vnf"
226             },
227             "trafficgen_2.yardstick": {
228                 "member-vnf-index": "3",
229                 "role": "TrafficGen",
230                 "name": "trafficgen_2.yardstick",
231                 "vnfd-id-ref": "tg__2",
232                 "ip": "1.2.1.1",
233                 "interfaces": {
234                     "xe0": {
235                         "local_iface_name": "ens513f0",
236                         "vld_id": "public",
237                         "netmask": "255.255.255.0",
238                         "vpci": "0000:02:00.0",
239                         "local_ip": "152.16.40.20",
240                         "driver": "ixgbe",
241                         "dst_ip": "152.16.40.19",
242                         "local_mac": "00:00:00:00:00:03",
243                         "dst_mac": "00:00:00:00:00:01",
244                         "dpdk_port_num": 0
245                     },
246                     "xe1": {
247                         "local_iface_name": "ens513f1",
248                         "netmask": "255.255.255.0",
249                         "network": "202.16.100.0",
250                         "local_ip": "202.16.100.20",
251                         "driver": "ixgbe",
252                         "local_mac": "00:1e:67:d0:60:5d",
253                         "vpci": "0000:02:00.1",
254                         "dpdk_port_num": 1
255                     }
256                 },
257                 "password": "r00t",
258                 "VNF model": "l3fwd_vnf.yaml",
259                 "user": "root"
260             },
261             "trafficgen_1.yardstick": {
262                 "member-vnf-index": "1",
263                 "role": "TrafficGen",
264                 "name": "trafficgen_1.yardstick",
265                 "vnfd-id-ref": "tg__1",
266                 "ip": "1.2.1.1",
267                 "interfaces": {
268                     "xe0": {
269                         "local_iface_name": "ens785f0",
270                         "vld_id": "private",
271                         "netmask": "255.255.255.0",
272                         "vpci": "0000:05:00.0",
273                         "local_ip": "152.16.100.20",
274                         "driver": "i40e",
275                         "dst_ip": "152.16.100.19",
276                         "local_mac": "00:00:00:00:00:04",
277                         "dst_mac": "00:00:00:00:00:02",
278                         "dpdk_port_num": 0
279                     },
280                     "xe1": {
281                         "local_ip": "152.16.100.21",
282                         "driver": "i40e",
283                         "vpci": "0000:05:00.1",
284                         "dpdk_port_num": 1,
285                         "local_iface_name": "ens785f1",
286                         "netmask": "255.255.255.0",
287                         "local_mac": "00:00:00:00:00:01"
288                     }
289                 },
290                 "password": "r00t",
291                 "VNF model": "tg_rfc2544_tpl.yaml",
292                 "user": "root"
293             }
294         }
295     }
296
297     @mock.patch(SSH_HELPER)
298     def test___init__(self, ssh):
299         mock_ssh(ssh)
300         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
301         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
302         self.assertIsInstance(trex_traffic_gen.resource_helper, TrexResourceHelper)
303
304     @mock.patch(SSH_HELPER)
305     def test_collect_kpi(self, ssh):
306         mock_ssh(ssh)
307         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
308         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
309         trex_traffic_gen.resource_helper._queue.put({})
310         result = trex_traffic_gen.collect_kpi()
311         self.assertEqual({}, result)
312
313     @mock.patch(SSH_HELPER)
314     def test_listen_traffic(self, ssh):
315         mock_ssh(ssh)
316         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
317         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
318         self.assertIsNone(trex_traffic_gen.listen_traffic({}))
319
320     @mock.patch(SSH_HELPER)
321     def test_instantiate(self, ssh):
322         mock_ssh(ssh)
323
324         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
325         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
326         trex_traffic_gen._start_server = mock.Mock(return_value=0)
327         trex_traffic_gen._tg_process = mock.MagicMock()
328         trex_traffic_gen._tg_process.start = mock.Mock()
329         trex_traffic_gen._tg_process.exitcode = 0
330         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=1)
331         trex_traffic_gen.ssh_helper = mock.MagicMock()
332         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
333         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
334
335         self.assertIsNone(trex_traffic_gen.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
336
337     @mock.patch(SSH_HELPER)
338     def test_instantiate_error(self, ssh):
339         mock_ssh(ssh, exec_result=(1, "", ""))
340
341         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
342         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
343         trex_traffic_gen._start_server = mock.Mock(return_value=0)
344         trex_traffic_gen._tg_process = mock.MagicMock()
345         trex_traffic_gen._tg_process.start = mock.Mock()
346         trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=0)
347         trex_traffic_gen.ssh_helper = mock.MagicMock()
348         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
349         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
350         self.assertIsNone(trex_traffic_gen.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
351
352     @mock.patch(SSH_HELPER)
353     def test__start_server(self, ssh):
354         mock_ssh(ssh)
355         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
356         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
357         trex_traffic_gen.ssh_helper = mock.MagicMock()
358         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
359         self.assertIsNone(trex_traffic_gen._start_server())
360
361     @mock.patch(SSH_HELPER)
362     def test__traffic_runner(self, ssh):
363         mock_ssh(ssh)
364
365         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
366         mock_traffic_profile.get_traffic_definition.return_value = "64"
367         mock_traffic_profile.execute_traffic.return_value = "64"
368         mock_traffic_profile.params = self.TRAFFIC_PROFILE
369
370         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
371         self.sut = TrexTrafficGen(NAME, vnfd)
372         self.sut.ssh_helper = mock.Mock()
373         self.sut.ssh_helper.run = mock.Mock()
374         self.sut._vpci_ascending = ["0000:05:00.0", "0000:05:00.1"]
375         self.sut._connect_client = mock.Mock(autospec=STLClient)
376         self.sut._connect_client.get_stats = mock.Mock(return_value="0")
377         self.sut.resource_helper.RUN_DURATION = 0
378         self.sut.resource_helper.QUEUE_WAIT_TIME = 0
379         self.sut._traffic_runner(mock_traffic_profile)
380
381     @mock.patch(SSH_HELPER)
382     def test__generate_trex_cfg(self, ssh):
383         mock_ssh(ssh)
384         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
385         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
386         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
387         self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
388
389     @mock.patch(SSH_HELPER)
390     def test_run_traffic(self, ssh):
391         mock_ssh(ssh)
392
393         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
394         mock_traffic_profile.get_traffic_definition.return_value = "64"
395         mock_traffic_profile.params = self.TRAFFIC_PROFILE
396
397         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
398         self.sut = TrexTrafficGen(NAME, vnfd)
399         self.sut.ssh_helper = mock.Mock()
400         self.sut.ssh_helper.run = mock.Mock()
401         self.sut._traffic_runner = mock.Mock(return_value=0)
402         self.sut.resource_helper.client_started.value = 1
403         result = self.sut.run_traffic(mock_traffic_profile)
404         self.sut._traffic_process.terminate()
405         self.assertIsNotNone(result)
406
407     @mock.patch(SSH_HELPER)
408     def test_scale(self, ssh):
409         mock_ssh(ssh, exec_result=(1, "", ""))
410         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
411         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
412         trex_traffic_gen.scale('')
413
414     @mock.patch(SSH_HELPER)
415     def test_terminate(self, ssh):
416         mock_ssh(ssh)
417         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
418         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
419         trex_traffic_gen.ssh_helper = mock.MagicMock()
420         trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
421         self.assertIsNone(trex_traffic_gen.terminate())
422
423     @mock.patch(SSH_HELPER)
424     def test__connect_client(self, ssh):
425         mock_ssh(ssh)
426         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
427         trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
428         client = mock.Mock(autospec=STLClient)
429         client.connect = mock.Mock(return_value=0)
430         self.assertIsNotNone(trex_traffic_gen.resource_helper._connect(client))
431
432
433 if __name__ == '__main__':
434     unittest.main()