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