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