Merge "Addition of IRQ Mode to NSB NFVI (PROX)"
[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 import mock
16 import unittest
17
18 from yardstick.benchmark import contexts
19 from yardstick.benchmark.contexts import base as ctx_base
20 from yardstick.network_services.traffic_profile import base as tp_base
21 from yardstick.network_services.vnf_generic.vnf import sample_vnf
22 from yardstick.network_services.vnf_generic.vnf import tg_rfc2544_trex
23
24
25 class TestTrexRfcResouceHelper(unittest.TestCase):
26
27     def test__run_traffic_once(self):
28         mock_setup_helper = mock.Mock()
29         mock_traffic_profile = mock.Mock()
30         mock_traffic_profile.config.duration = 3
31         mock_traffic_profile.execute_traffic.return_value = ('fake_ports',
32                                                              'port_pg_id_map')
33         mock_traffic_profile.get_drop_percentage.return_value = (True,
34                                                                  'percentage')
35         rfc_rh = tg_rfc2544_trex.TrexRfcResourceHelper(mock_setup_helper)
36         rfc_rh.TRANSIENT_PERIOD = 0
37         rfc_rh.rfc2544_helper = mock.Mock()
38
39         with mock.patch.object(rfc_rh, '_get_samples') as mock_get_samples:
40             self.assertTrue(rfc_rh._run_traffic_once(mock_traffic_profile))
41
42         mock_traffic_profile.execute_traffic.assert_called_once_with(rfc_rh)
43         mock_traffic_profile.stop_traffic.assert_called_once_with(rfc_rh)
44         mock_traffic_profile.stop_traffic.assert_called_once()
45         mock_get_samples.assert_has_calls([
46             mock.call('fake_ports', port_pg_id='port_pg_id_map'),
47             mock.call('fake_ports', port_pg_id='port_pg_id_map')])
48
49
50 class TestTrexTrafficGenRFC(unittest.TestCase):
51
52     VNFD_0 = {
53         'short-name': 'VpeVnf',
54         'vdu': [
55             {
56                 'routing_table': [
57                     {
58                         'network': '152.16.100.20',
59                         'netmask': '255.255.255.0',
60                         'gateway': '152.16.100.20',
61                         'if': 'xe0',
62                     },
63                     {
64                         'network': '152.16.40.20',
65                         'netmask': '255.255.255.0',
66                         'gateway': '152.16.40.20',
67                         'if': 'xe1',
68                     },
69                 ],
70                 'description': 'VPE approximation using DPDK',
71                 'name': 'vpevnf-baremetal',
72                 'nd_route_tbl': [
73                     {
74                         'network': '0064:ff9b:0:0:0:0:9810:6414',
75                         'netmask': '112',
76                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
77                         'if': 'xe0',
78                     },
79                     {
80                         'network': '0064:ff9b:0:0:0:0:9810:2814',
81                         'netmask': '112',
82                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
83                         'if': 'xe1',
84                     },
85                 ],
86                 'id': 'vpevnf-baremetal',
87                 'external-interface': [
88                     {
89                         'virtual-interface': {
90                             'ifname': 'xe0',
91                             'dst_mac': '00:00:00:00:00:04',
92                             'vpci': '0000:05:00.0',
93                             'local_ip': '152.16.100.19',
94                             'type': 'PCI-PASSTHROUGH',
95                             'netmask': '255.255.255.0',
96                             'vld_id': 'uplink_0',
97                             'dpdk_port_num': 0,
98                             'bandwidth': '10 Gbps',
99                             'driver': "i40e",
100                             'dst_ip': '152.16.100.20',
101                             'local_iface_name': 'xe0',
102                             'local_mac': '00:00:00:00:00:01',
103                         },
104                         'vnfd-connection-point-ref': 'xe0',
105                         'name': 'xe0',
106                     },
107                     {
108                         'virtual-interface': {
109                             'ifname': 'xe1',
110                             'dst_mac': '00:00:00:00:00:03',
111                             'vpci': '0000:05:00.1',
112                             'local_ip': '152.16.40.19',
113                             'type': 'PCI-PASSTHROUGH',
114                             'driver': "i40e",
115                             'netmask': '255.255.255.0',
116                             'vld_id': 'downlink_0',
117                             'dpdk_port_num': 1,
118                             'bandwidth': '10 Gbps',
119                             'dst_ip': '152.16.40.20',
120                             'local_iface_name': 'xe1',
121                             'local_mac': '00:00:00:00:00:02'
122                         },
123                         'vnfd-connection-point-ref': 'xe1',
124                         'name': 'xe1',
125                     },
126                 ],
127             },
128         ],
129         'description': 'Vpe approximation using DPDK',
130         'mgmt-interface': {
131             'vdu-id': 'vpevnf-baremetal',
132             'host': '1.1.1.1',
133             'password': 'r00t',
134             'user': 'root',
135             'ip': '1.1.1.1',
136         },
137         'benchmark': {
138             'kpi': [
139                 'packets_in',
140                 'packets_fwd',
141                 'packets_dropped',
142             ],
143         },
144         'connection-point': [
145             {
146                 'type': 'VPORT',
147                 'name': 'xe0',
148             },
149             {
150                 'type': 'VPORT',
151                 'name': 'xe1',
152             },
153         ],
154         'id': 'VpeApproxVnf',
155         'name': 'VPEVnfSsh',
156     }
157
158     VNFD = {
159         'vnfd:vnfd-catalog': {
160             'vnfd': [
161                 VNFD_0,
162             ],
163         },
164     }
165
166     TRAFFIC_PROFILE = {
167         "schema": "isb:traffic_profile:0.1",
168         "name": "fixed",
169         "description": "Fixed traffic profile to run UDP traffic",
170         "traffic_profile": {
171             "traffic_type": "FixedTraffic",
172             "frame_rate": 100,  # pps
173             "flow_number": 10,
174             "frame_size": 64,
175         },
176     }
177
178     TC_YAML = {
179         'scenarios': [
180             {
181                 'tc_options': {
182                     'rfc2544': {
183                         'allowed_drop_rate': '0.8 - 1',
184                     },
185                 },
186                 'runner': {
187                     'duration': 400,
188                     'interval': 35,
189                     'type': 'Duration',
190                 },
191                 'traffic_options': {
192                     'flow': 'ipv4_1flow_Packets_vpe.yaml',
193                     'imix': 'imix_voice.yaml',
194                 },
195                 'vnf_options': {
196                     'vpe': {
197                         'cfg': 'vpe_config',
198                     },
199                 },
200                 'traffic_profile': 'ipv4_throughput_vpe.yaml',
201                 'type': 'NSPerf',
202                 'nodes': {
203                     'tg__1': 'trafficgen_1.yardstick',
204                     'vnf__1': 'vnf.yardstick',
205                 },
206                 'topology': 'vpe_vnf_topology.yaml',
207             },
208         ],
209         'context': {
210             'nfvi_type': 'baremetal',
211             'type': contexts.CONTEXT_NODE,
212             'name': 'yardstick',
213             'file': '/etc/yardstick/nodes/pod.yaml',
214         },
215         'schema': 'yardstick:task:0.1',
216     }
217
218     def setUp(self):
219         self._mock_ssh_helper = mock.patch.object(sample_vnf, 'VnfSshHelper')
220         self.mock_ssh_helper = self._mock_ssh_helper.start()
221         self.addCleanup(self._stop_mocks)
222
223     def _stop_mocks(self):
224         self._mock_ssh_helper.stop()
225
226     def test___init__(self):
227         trex_traffic_gen = tg_rfc2544_trex.TrexTrafficGenRFC(
228             'vnf1', self.VNFD_0, 'task_id')
229         self.assertIsNotNone(trex_traffic_gen.resource_helper._terminated.value)
230
231     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
232     def test_collect_kpi(self, *args):
233         trex_traffic_gen = tg_rfc2544_trex.TrexTrafficGenRFC(
234             'vnf1', self.VNFD_0, 'task_id')
235         trex_traffic_gen.scenario_helper.scenario_cfg = {
236             'nodes': {trex_traffic_gen.name: "mock"}
237         }
238         expected = {
239             'physical_node': 'mock_node',
240             'collect_stats': {},
241         }
242         self.assertEqual(trex_traffic_gen.collect_kpi(), expected)
243
244     @mock.patch.object(ctx_base.Context, 'get_context_from_server', return_value='fake_context')
245     def test_instantiate(self, *args):
246         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
247         mock_traffic_profile.get_traffic_definition.return_value = "64"
248         mock_traffic_profile.params = self.TRAFFIC_PROFILE
249
250         trex_traffic_gen = tg_rfc2544_trex.TrexTrafficGenRFC(
251             'vnf1', self.VNFD_0, 'task_id')
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": {}, "vnf1": {}}})
278         self.assertIsNone(trex_traffic_gen.instantiate(scenario_cfg, {}))
279
280     @mock.patch.object(ctx_base.Context, 'get_context_from_server', return_value='fake_context')
281     def test_instantiate_error(self, *args):
282         mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
283         mock_traffic_profile.get_traffic_definition.return_value = "64"
284         mock_traffic_profile.params = self.TRAFFIC_PROFILE
285
286         trex_traffic_gen = tg_rfc2544_trex.TrexTrafficGenRFC(
287             'vnf1', self.VNFD_0, 'task_id')
288         trex_traffic_gen.resource_helper = mock.MagicMock()
289         trex_traffic_gen.setup_helper.setup_vnf_environment = mock.MagicMock()
290         scenario_cfg = {
291             "tc": "tc_baremetal_rfc2544_ipv4_1flow_64B",
292             "nodes": {
293                 "tg_1": {},
294                 "vnf1": {}
295             },
296             "topology": 'nsb_test_case.yaml',
297             'options': {
298                 'packetsize': 64,
299                 'traffic_type': 4,
300                 'rfc2544': {
301                     'allowed_drop_rate': '0.8 - 1',
302                 },
303                 'vnf__1': {
304                     'rules': 'acl_1rule.yaml',
305                     'vnf_config': {
306                         'lb_config': 'SW',
307                         'lb_count': 1,
308                         'worker_config': '1C/1T',
309                         'worker_threads': 1,
310                     },
311                 },
312             },
313         }
314         trex_traffic_gen.instantiate(scenario_cfg, {})