3 # Copyright (c) 2016-2017 Intel Corporation
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
24 from tests.unit import STL_MOCKS
27 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
30 STLClient = mock.MagicMock()
31 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
35 from yardstick.network_services.vnf_generic.vnf.tg_trex import \
36 TrexTrafficGen, TrexResourceHelper
37 from yardstick.network_services.traffic_profile.base import TrafficProfile
40 class TestTrexTrafficGen(unittest.TestCase):
41 VNFD = {'vnfd:vnfd-catalog':
43 [{'short-name': 'VpeVnf',
46 [{'network': '152.16.100.20',
47 'netmask': '255.255.255.0',
48 'gateway': '152.16.100.20',
50 {'network': '152.16.40.20',
51 'netmask': '255.255.255.0',
52 'gateway': '152.16.40.20',
54 'description': 'VPE approximation using DPDK',
55 'name': 'vpevnf-baremetal',
57 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
59 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
61 {'network': '0064:ff9b:0:0:0:0:9810:2814',
63 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
65 'id': 'vpevnf-baremetal',
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',
74 'bandwidth': '10 Gbps',
76 'dst_ip': '152.16.100.20',
77 'local_iface_name': 'xe0',
78 'vld_id': 'downlink_0',
80 'local_mac': '00:00:00:00:00:02'},
81 'vnfd-connection-point-ref': 'xe0',
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',
89 'netmask': '255.255.255.0',
91 'bandwidth': '10 Gbps',
92 'dst_ip': '152.16.40.20',
93 'local_iface_name': 'xe1',
96 'local_mac': '00:00:00:00:00:01'},
97 'vnfd-connection-point-ref': 'xe1',
99 'description': 'Vpe approximation using DPDK',
101 {'vdu-id': 'vpevnf-baremetal',
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'}]}}
113 "schema": "isb:traffic_profile:0.1",
115 "description": "Fixed traffic profile to run UDP traffic",
117 "traffic_type": "FixedTraffic",
118 "frame_rate": 100, # pps
129 "allowed_drop_rate": "0.8 - 1",
132 "rules": "acl_1rule.yaml",
136 "worker_config": "1C/1T",
141 "task_id": "a70bdf4a-8e67-47a3-9dc1-273c14506eb7",
142 "tc": "tc_ipv4_1Mflow_64B_packetsize",
144 "object": "NetworkServiceTestCase",
146 "output_filename": "/tmp/yardstick.out",
147 "runner_id": 74476, "duration": 400,
150 "traffic_profile": "ipv4_throughput_acl.yaml",
152 "flow": "ipv4_Packets_acl.yaml",
153 "imix": "imix_voice.yaml"
157 "tg__2": "trafficgen_2.yardstick",
158 "tg__1": "trafficgen_1.yardstick",
159 "vnf__1": "vnf.yardstick"
161 "topology": "udpreplay-tg-topology-baremetal.yaml"
167 "vnfd-id-ref": "vnf__1",
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",
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",
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",
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",
201 "gateway": "0064:ff9b:0:0:0:0:9810:6414",
202 "network": "0064:ff9b:0:0:0:0:9810:6414"
207 "gateway": "0064:ff9b:0:0:0:0:9810:2814",
208 "network": "0064:ff9b:0:0:0:0:9810:2814"
212 "VNF model": "udp_replay.yaml",
213 "name": "vnf.yardstick",
214 "member-vnf-index": "2",
217 "netmask": "255.255.255.0",
219 "gateway": "152.16.100.20",
220 "network": "152.16.100.20"
223 "netmask": "255.255.255.0",
225 "gateway": "152.16.40.20",
226 "network": "152.16.40.20"
231 "trafficgen_2.yardstick": {
232 "member-vnf-index": "3",
233 "role": "TrafficGen",
234 "name": "trafficgen_2.yardstick",
235 "vnfd-id-ref": "tg__2",
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",
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",
251 "local_iface_name": "ens513f1",
252 "netmask": "255.255.255.0",
253 "network": "202.16.100.0",
254 "local_ip": "202.16.100.20",
256 "local_mac": "00:1e:67:d0:60:5d",
257 "vpci": "0000:02:00.1",
262 "VNF model": "l3fwd_vnf.yaml",
265 "trafficgen_1.yardstick": {
266 "member-vnf-index": "1",
267 "role": "TrafficGen",
268 "name": "trafficgen_1.yardstick",
269 "vnfd-id-ref": "tg__1",
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",
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",
285 "local_ip": "152.16.100.21",
287 "vpci": "0000:05:00.1",
289 "local_iface_name": "ens785f1",
290 "netmask": "255.255.255.0",
291 "local_mac": "00:00:00:00:00:01"
295 "VNF model": "tg_rfc2544_tpl.yaml",
301 @mock.patch(SSH_HELPER)
302 def test___init__(self, 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)
308 @mock.patch(SSH_HELPER)
309 def test_collect_kpi(self, 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)
317 @mock.patch(SSH_HELPER)
318 def test_listen_traffic(self, 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({}))
324 @mock.patch(SSH_HELPER)
325 def test_instantiate(self, ssh):
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()
339 self.assertIsNone(trex_traffic_gen.instantiate(self.SCENARIO_CFG, self.CONTEXT_CFG))
341 @mock.patch(SSH_HELPER)
342 def test_instantiate_error(self, ssh):
343 mock_ssh(ssh, exec_result=(1, "", ""))
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))
356 @mock.patch(SSH_HELPER)
357 def test__start_server(self, 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())
366 @mock.patch(SSH_HELPER)
367 def test__start_server_multiple_queues(self, 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())
376 @mock.patch(SSH_HELPER)
377 def test__traffic_runner(self, ssh):
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
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)
397 @mock.patch(SSH_HELPER)
398 def test__generate_trex_cfg(self, 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())
405 @mock.patch(SSH_HELPER)
406 def test_build_ports_reversed_pci_ordering(self, 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',
417 'bandwidth': '10 Gbps',
419 'dst_ip': '152.16.100.20',
420 'local_iface_name': 'xe0',
421 'vld_id': 'downlink_0',
423 'local_mac': '00:00:00:00:00:02'},
424 'vnfd-connection-point-ref': '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',
432 'netmask': '255.255.255.0',
434 'bandwidth': '10 Gbps',
435 'dst_ip': '152.16.40.20',
436 'local_iface_name': 'xe1',
437 'vld_id': 'uplink_0',
439 'local_mac': '00:00:00:00:00:01'},
440 'vnfd-connection-point-ref': '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),
451 @mock.patch(SSH_HELPER)
452 def test_run_traffic(self, ssh):
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
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)
469 @mock.patch(SSH_HELPER)
470 def test_terminate(self, 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())
478 @mock.patch(SSH_HELPER)
479 def test__connect_client(self, 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))
488 if __name__ == '__main__':