Merge "Create Dockerfile to create a yardstick-image of docker"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_cgnapt_vnf.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 from copy import deepcopy
16 import time
17
18 import mock
19 import unittest
20
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.common import utils
23 from yardstick.common import process
24 from yardstick.network_services.vnf_generic.vnf import cgnapt_vnf
25 from yardstick.network_services.vnf_generic.vnf import sample_vnf
26 from yardstick.network_services.nfvi import resource
27
28
29 TEST_FILE_YAML = 'nsb_test_case.yaml'
30 name = 'vnf__0'
31
32
33 class TestCgnaptApproxSetupEnvHelper(unittest.TestCase):
34
35     def test__generate_ip_from_pool(self):
36
37         _ip = '1.2.3.4'
38         ip = cgnapt_vnf.CgnaptApproxSetupEnvHelper._generate_ip_from_pool(_ip)
39         self.assertEqual(next(ip), _ip)
40         self.assertEqual(next(ip), '1.2.4.4')
41         self.assertEqual(next(ip), '1.2.5.4')
42
43     def test__update_cgnat_script_file(self):
44
45         sample = """\
46 # See the License for the specific language governing permissions and
47 # limitations under the License.
48
49 link 0 down
50 link 0 config {port0_local_ip} {port0_prefixlen}
51 link 0 up
52 link 1 down
53 link 1 config {port1_local_ip} {port1_prefixlen}
54 link 1 up
55 """
56         header = "This is a header"
57
58         out = cgnapt_vnf.CgnaptApproxSetupEnvHelper._update_cgnat_script_file(
59             header, sample.splitlines())
60         self.assertNotIn("This is a header", out)
61
62     def test__get_cgnapt_config(self):
63         vnfd_helper = mock.MagicMock()
64         vnfd_helper.port_pairs.uplink_ports = [{"name": 'a'}, {"name": "b"}, {"name": "c"}]
65
66         helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
67             vnfd_helper, mock.Mock(), mock.Mock())
68         result = helper._get_cgnapt_config()
69         self.assertIsNotNone(result)
70
71     def test_scale(self):
72         helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
73             mock.Mock(), mock.Mock(), mock.Mock())
74         with self.assertRaises(NotImplementedError):
75             helper.scale()
76
77     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open')
78     @mock.patch.object(utils, 'find_relative_file')
79     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig')
80     @mock.patch.object(utils, 'open_relative_file')
81     def test_build_config(self, *args):
82         vnfd_helper = mock.Mock()
83         ssh_helper = mock.Mock()
84         scenario_helper = mock.Mock()
85         scenario_helper.vnf_cfg = {'lb_config': 'HW'}
86         scenario_helper.options = {}
87         scenario_helper.all_options = {}
88
89         cgnat_approx_setup_helper = cgnapt_vnf.CgnaptApproxSetupEnvHelper(
90             vnfd_helper, ssh_helper, scenario_helper)
91
92         cgnat_approx_setup_helper.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
93         cgnat_approx_setup_helper.ssh_helper.all_ports = mock.Mock()
94         cgnat_approx_setup_helper.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
95         expected = 'sudo tool_path -p 0x3 -f /tmp/cgnapt_config -s /tmp/cgnapt_script  --hwlb 3'
96         self.assertEqual(cgnat_approx_setup_helper.build_config(), expected)
97
98
99 @mock.patch.object(sample_vnf, 'Process')
100 class TestCgnaptApproxVnf(unittest.TestCase):
101     VNFD = {'vnfd:vnfd-catalog':
102             {'vnfd':
103              [{'short-name': 'VpeVnf',
104                'vdu':
105                [{'routing_table':
106                  [{'network': '152.16.100.20',
107                    'netmask': '255.255.255.0',
108                    'gateway': '152.16.100.20',
109                    'if': 'xe0'},
110                   {'network': '152.16.40.20',
111                    'netmask': '255.255.255.0',
112                    'gateway': '152.16.40.20',
113                    'if': 'xe1'}],
114                  'description': 'VPE approximation using DPDK',
115                  'name': 'vpevnf-baremetal',
116                  'nd_route_tbl':
117                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
118                    'netmask': '112',
119                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
120                    'if': 'xe0'},
121                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
122                    'netmask': '112',
123                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
124                    'if': 'xe1'}],
125                  'id': 'vpevnf-baremetal',
126                  'external-interface':
127                  [{'virtual-interface':
128                    {'dst_mac': '00:00:00:00:00:04',
129                     'vpci': '0000:05:00.0',
130                     'local_ip': '152.16.100.19',
131                     'type': 'PCI-PASSTHROUGH',
132                     'netmask': '255.255.255.0',
133                     'dpdk_port_num': 0,
134                     'bandwidth': '10 Gbps',
135                     'driver': "i40e",
136                     'dst_ip': '152.16.100.20',
137                     'local_iface_name': 'xe0',
138                     'local_mac': '00:00:00:00:00:02'},
139                    'vnfd-connection-point-ref': 'xe0',
140                    'name': 'xe0'},
141                   {'virtual-interface':
142                    {'dst_mac': '00:00:00:00:00:03',
143                     'vpci': '0000:05:00.1',
144                     'local_ip': '152.16.40.19',
145                     'type': 'PCI-PASSTHROUGH',
146                     'driver': "i40e",
147                     'netmask': '255.255.255.0',
148                     'dpdk_port_num': 1,
149                     'bandwidth': '10 Gbps',
150                     'dst_ip': '152.16.40.20',
151                     'local_iface_name': 'xe1',
152                     'local_mac': '00:00:00:00:00:01'},
153                    'vnfd-connection-point-ref': 'xe1',
154                    'name': 'xe1'}]}],
155                'description': 'Vpe approximation using DPDK',
156                'mgmt-interface':
157                    {'vdu-id': 'vpevnf-baremetal',
158                     'host': '1.2.1.1',
159                     'password': 'r00t',
160                     'user': 'root',
161                     'ip': '1.2.1.1'},
162                'benchmark':
163                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
164                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
165                                     {'type': 'VPORT', 'name': 'xe1'}],
166                'id': 'CgnaptApproxVnf', 'name': 'VPEVnfSsh'}]}}
167
168     SCENARIO_CFG = {
169         'options': {
170             'packetsize': 64,
171             'traffic_type': 4,
172             'rfc2544': {
173                 'allowed_drop_rate': '0.8 - 1',
174             },
175             'vnf__0': {
176                 'napt': 'dynamic',
177                 'vnf_config': {
178                     'lb_config': 'SW',
179                     'lb_count': 1,
180                     'worker_config':
181                     '1C/1T',
182                     'worker_threads': 1,
183                 },
184             },
185             'flow': {'count': 1,
186                      'dst_ip': [{'tg__1': 'xe0'}],
187                      'public_ip': [''],
188                      'src_ip': [{'tg__0': 'xe0'}]},
189         },
190         'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
191         'task_path': '/tmp',
192         'tc': 'tc_ipv4_1Mflow_64B_packetsize',
193         'runner': {
194             'object': 'NetworkServiceTestCase',
195             'interval': 35,
196             'output_filename': '/tmp/yardstick.out',
197             'runner_id': 74476,
198             'duration': 400,
199             'type': 'Duration',
200         },
201         'traffic_profile': 'ipv4_throughput_acl.yaml',
202         'type': 'NSPerf',
203         'nodes': {
204             'tg__1': 'trafficgen_1.yardstick',
205             'tg__0': 'trafficgen_0.yardstick',
206             'vnf__0': 'vnf.yardstick',
207         },
208         'topology': 'vpe-tg-topology-baremetal.yaml',
209     }
210
211     context_cfg = {'nodes': {'tg__2':
212                              {'member-vnf-index': '3',
213                               'role': 'TrafficGen',
214                               'name': 'trafficgen_2.yardstick',
215                               'vnfd-id-ref': 'tg__2',
216                               'ip': '1.2.1.1',
217                               'interfaces':
218                               {'xe0': {'local_iface_name': 'ens513f0',
219                                        'vld_id': cgnapt_vnf.CgnaptApproxVnf.DOWNLINK,
220                                        'netmask': '255.255.255.0',
221                                        'local_ip': '152.16.40.20',
222                                        'dst_mac': '00:00:00:00:00:01',
223                                        'local_mac': '00:00:00:00:00:03',
224                                        'dst_ip': '152.16.40.19',
225                                        'driver': 'ixgbe',
226                                        'vpci': '0000:02:00.0',
227                                        'dpdk_port_num': 0},
228                                'xe1': {'local_iface_name': 'ens513f1',
229                                        'netmask': '255.255.255.0',
230                                        'network': '202.16.100.0',
231                                        'local_ip': '202.16.100.20',
232                                        'local_mac': '00:1e:67:d0:60:5d',
233                                        'driver': 'ixgbe',
234                                        'vpci': '0000:02:00.1',
235                                        'dpdk_port_num': 1}},
236                               'password': 'r00t',
237                               'VNF model': 'l3fwd_vnf.yaml',
238                               'user': 'root'},
239                              'tg__1':
240                              {'member-vnf-index': '1',
241                               'role': 'TrafficGen',
242                               'name': 'trafficgen_1.yardstick',
243                               'vnfd-id-ref': 'tg__1',
244                               'ip': '1.2.1.1',
245                               'interfaces':
246                               {'xe0': {'local_iface_name': 'ens785f0',
247                                        'vld_id': cgnapt_vnf.CgnaptApproxVnf.UPLINK,
248                                        'netmask': '255.255.255.0',
249                                        'local_ip': '152.16.100.20',
250                                        'dst_mac': '00:00:00:00:00:02',
251                                        'local_mac': '00:00:00:00:00:04',
252                                        'dst_ip': '152.16.100.19',
253                                        'driver': 'i40e',
254                                        'vpci': '0000:05:00.0',
255                                        'dpdk_port_num': 0},
256                                'xe1': {'local_iface_name': 'ens785f1',
257                                        'netmask': '255.255.255.0',
258                                        'local_ip': '152.16.100.21',
259                                        'local_mac': '00:00:00:00:00:01',
260                                        'driver': 'i40e',
261                                        'vpci': '0000:05:00.1',
262                                        'dpdk_port_num': 1}},
263                               'password': 'r00t',
264                               'VNF model': 'tg_rfc2544_tpl.yaml',
265                               'user': 'root'},
266                              'vnf__0':
267                              {'name': 'vnf.yardstick',
268                               'vnfd-id-ref': 'vnf__0',
269                               'ip': '1.2.1.1',
270                               'interfaces':
271                               {'xe0': {'local_iface_name': 'ens786f0',
272                                        'vld_id': cgnapt_vnf.CgnaptApproxVnf.UPLINK,
273                                        'netmask': '255.255.255.0',
274                                        'local_ip': '152.16.100.19',
275                                        'dst_mac': '00:00:00:00:00:04',
276                                        'local_mac': '00:00:00:00:00:02',
277                                        'dst_ip': '152.16.100.20',
278                                        'driver': 'i40e',
279                                        'vpci': '0000:05:00.0',
280                                        'dpdk_port_num': 0},
281                                'xe1': {'local_iface_name': 'ens786f1',
282                                        'vld_id': cgnapt_vnf.CgnaptApproxVnf.DOWNLINK,
283                                        'netmask': '255.255.255.0',
284                                        'local_ip': '152.16.40.19',
285                                        'dst_mac': '00:00:00:00:00:03',
286                                        'local_mac': '00:00:00:00:00:01',
287                                        'dst_ip': '152.16.40.20',
288                                        'driver': 'i40e',
289                                        'vpci': '0000:05:00.1',
290                                        'dpdk_port_num': 1}},
291                               'routing_table':
292                               [{'netmask': '255.255.255.0',
293                                 'gateway': '152.16.100.20',
294                                 'network': '152.16.100.20',
295                                 'if': 'xe0'},
296                                {'netmask': '255.255.255.0',
297                                 'gateway': '152.16.40.20',
298                                 'network': '152.16.40.20',
299                                 'if': 'xe1'}],
300                               'member-vnf-index': '2',
301                               'host': '1.2.1.1',
302                               'role': 'vnf',
303                               'user': 'root',
304                               'nd_route_tbl':
305                               [{'netmask': '112',
306                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
307                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
308                                 'if': 'xe0'},
309                                {'netmask': '112',
310                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
311                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
312                                 'if': 'xe1'}],
313                               'password': 'r00t',
314                               'VNF model': 'cgnapt_vnf.yaml'}}}
315
316     def setUp(self):
317         self.scenario_cfg = deepcopy(self.SCENARIO_CFG)
318
319     def test___init__(self, *args):
320         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
321         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
322         self.assertIsNone(cgnapt_approx_vnf._vnf_process)
323
324     @mock.patch.object(process, 'check_if_process_failed')
325     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
326     def test_collect_kpi(self, *args):
327         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
328         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
329         cgnapt_approx_vnf.scenario_helper.scenario_cfg = {
330             'nodes': {cgnapt_approx_vnf.name: "mock"}
331         }
332         cgnapt_approx_vnf._vnf_process = mock.MagicMock(
333             **{"is_alive.return_value": True, "exitcode": None})
334         cgnapt_approx_vnf.q_in = mock.MagicMock()
335         cgnapt_approx_vnf.q_out = mock.MagicMock()
336         cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
337         cgnapt_approx_vnf.resource = mock.Mock(
338             autospec=resource.ResourceProfile)
339         result = {
340             'physical_node': 'mock_node',
341             'packets_dropped': 0,
342             'packets_fwd': 0,
343             'packets_in': 0
344         }
345         with mock.patch.object(cgnapt_approx_vnf, 'get_stats',
346                                return_value=''):
347             self.assertEqual(result, cgnapt_approx_vnf.collect_kpi())
348
349     @mock.patch.object(time, 'sleep')
350     def test_vnf_execute_command(self, *args):
351         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
352         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
353         cgnapt_approx_vnf.q_in = mock.Mock()
354         cgnapt_approx_vnf.q_out = mock.Mock()
355         cgnapt_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
356         self.assertEqual("", cgnapt_approx_vnf.vnf_execute('quit'))
357
358     def test_get_stats(self, *args):
359         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
360         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
361         with mock.patch.object(cgnapt_approx_vnf, 'vnf_execute') as mock_exec:
362             mock_exec.return_value = 'output'
363             self.assertEqual('output', cgnapt_approx_vnf.get_stats())
364
365         mock_exec.assert_called_once_with('p cgnapt stats')
366
367     def test_run_vcgnapt(self, *args):
368         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
369         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
370         cgnapt_approx_vnf.ssh_helper = mock.Mock()
371         cgnapt_approx_vnf.setup_helper = mock.Mock()
372         with mock.patch.object(cgnapt_approx_vnf, '_build_config'), \
373                 mock.patch.object(cgnapt_approx_vnf, '_build_run_kwargs'):
374             cgnapt_approx_vnf._run()
375
376         cgnapt_approx_vnf.ssh_helper.run.assert_called_once()
377         cgnapt_approx_vnf.setup_helper.kill_vnf.assert_called_once()
378
379     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
380     def test_instantiate(self, *args):
381         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
382         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
383         cgnapt_approx_vnf.deploy_helper = mock.MagicMock()
384         cgnapt_approx_vnf.resource_helper = mock.MagicMock()
385         cgnapt_approx_vnf._build_config = mock.MagicMock()
386         self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
387                                                     'rules': ""}}
388         cgnapt_approx_vnf.q_out.put("pipeline>")
389         cgnapt_vnf.WAIT_TIME = 3
390         self.scenario_cfg.update({"nodes": {"vnf__0": ""}})
391         with mock.patch.object(cgnapt_approx_vnf, '_start_vnf'):
392             self.assertIsNone(cgnapt_approx_vnf.instantiate(
393                 self.scenario_cfg, self.context_cfg))
394
395     @mock.patch.object(time, 'sleep')
396     def test__vnf_up_post(self, *args):
397         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
398         self.scenario_cfg['options'][name]['napt'] = 'static'
399         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
400         cgnapt_approx_vnf.vnf_execute = mock.Mock()
401         cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
402         with mock.patch.object(cgnapt_approx_vnf, 'setup_helper') as \
403                 mock_setup_helper:
404             mock_setup_helper._generate_ip_from_pool.return_value = ['ip1']
405             mock_setup_helper._get_cgnapt_config.return_value = ['gw_ip1']
406             cgnapt_approx_vnf._vnf_up_post()
407
408     def test__vnf_up_post_short(self, *args):
409         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
410         cgnapt_approx_vnf = cgnapt_vnf.CgnaptApproxVnf(name, vnfd, 'task_id')
411         cgnapt_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
412         cgnapt_approx_vnf._vnf_up_post()