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