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