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