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