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