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