Merge "[docs] Resolve "document isn't included in any toctree" warning"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_vfw_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 import unittest
16 import mock
17 import os
18
19 from yardstick.common import utils
20 from yardstick.benchmark.contexts import base as ctx_base
21 from yardstick.network_services.vnf_generic.vnf.vfw_vnf import FWApproxVnf
22 from yardstick.network_services.nfvi.resource import ResourceProfile
23 from yardstick.network_services.vnf_generic.vnf.vfw_vnf import FWApproxSetupEnvHelper
24 from yardstick.tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
25
26
27 TEST_FILE_YAML = 'nsb_test_case.yaml'
28 SSH_HELPER = 'yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper'
29
30 name = 'vnf__1'
31
32
33 @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.Process")
34 class TestFWApproxVnf(unittest.TestCase):
35     VNFD = {'vnfd:vnfd-catalog':
36             {'vnfd':
37              [{'short-name': 'VpeVnf',
38                'vdu':
39                [{'routing_table':
40                  [{'network': '152.16.100.20',
41                    'netmask': '255.255.255.0',
42                    'gateway': '152.16.100.20',
43                    'if': 'xe0'},
44                   {'network': '152.16.40.20',
45                    'netmask': '255.255.255.0',
46                    'gateway': '152.16.40.20',
47                    'if': 'xe1'}],
48                  'description': 'VPE approximation using DPDK',
49                  'name': 'vpevnf-baremetal',
50                  'nd_route_tbl':
51                  [{'network': '0064:ff9b:0:0:0:0:9810:6414',
52                    'netmask': '112',
53                    'gateway': '0064:ff9b:0:0:0:0:9810:6414',
54                    'if': 'xe0'},
55                   {'network': '0064:ff9b:0:0:0:0:9810:2814',
56                    'netmask': '112',
57                    'gateway': '0064:ff9b:0:0:0:0:9810:2814',
58                    'if': 'xe1'}],
59                  'id': 'vpevnf-baremetal',
60                  'external-interface':
61                  [{'virtual-interface':
62                    {'dst_mac': '00:00:00:00:00:04',
63                     'vpci': '0000:05:00.0',
64                     'local_ip': '152.16.100.19',
65                     'type': 'PCI-PASSTHROUGH',
66                     'netmask': '255.255.255.0',
67                     'dpdk_port_num': 0,
68                     'bandwidth': '10 Gbps',
69                     'driver': "i40e",
70                     'dst_ip': '152.16.100.20',
71                     'local_iface_name': 'xe0',
72                     'local_mac': '00:00:00:00:00:02'},
73                    'vnfd-connection-point-ref': 'xe0',
74                    'name': 'xe0'},
75                   {'virtual-interface':
76                    {'dst_mac': '00:00:00:00:00:03',
77                     'vpci': '0000:05:00.1',
78                     'local_ip': '152.16.40.19',
79                     'type': 'PCI-PASSTHROUGH',
80                     'driver': "i40e",
81                     'netmask': '255.255.255.0',
82                     'dpdk_port_num': 1,
83                     'bandwidth': '10 Gbps',
84                     'dst_ip': '152.16.40.20',
85                     'local_iface_name': 'xe1',
86                     'local_mac': '00:00:00:00:00:01'},
87                    'vnfd-connection-point-ref': 'xe1',
88                    'name': 'xe1'}]}],
89                'description': 'Vpe approximation using DPDK',
90                'mgmt-interface':
91                    {'vdu-id': 'vpevnf-baremetal',
92                     'host': '1.2.1.1',
93                     'password': 'r00t',
94                     'user': 'root',
95                     'ip': '1.2.1.1'},
96                'benchmark':
97                    {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
98                'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
99                                     {'type': 'VPORT', 'name': 'xe1'}],
100                'id': 'FWApproxVnf', 'name': 'VPEVnfSsh'}]}}
101
102     scenario_cfg = {'options': {'packetsize': 64, 'traffic_type': 4,
103                                 'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
104                                 'vnf__1': {'rules': 'acl_1rule.yaml',
105                                            'vnf_config': {'lb_config': 'SW',
106                                                           'lb_count': 1,
107                                                           'worker_config':
108                                                               '1C/1T',
109                                                           'worker_threads': 1}}
110                                 },
111                     'task_id': 'a70bdf4a-8e67-47a3-9dc1-273c14506eb7',
112                     'task_path': '/tmp',
113                     'tc': 'tc_ipv4_1Mflow_64B_packetsize',
114                     'runner': {'object': 'NetworkServiceTestCase',
115                                'interval': 35,
116                                'output_filename': '/tmp/yardstick.out',
117                                'runner_id': 74476, 'duration': 400,
118                                'type': 'Duration'},
119                     'traffic_profile': 'ipv4_throughput_vfw.yaml',
120                     'traffic_options': {'flow': 'ipv4_Packets_vfw.yaml',
121                                         'imix': 'imix_voice.yaml'},
122                     'type': 'ISB',
123                     'nodes': {'tg__2': 'trafficgen_2.yardstick',
124                               'tg__1': 'trafficgen_1.yardstick',
125                               'vnf__1': 'vnf.yardstick'},
126                     'topology': 'vpe-tg-topology-baremetal.yaml'}
127
128     context_cfg = {'nodes': {'tg__2':
129                              {'member-vnf-index': '3',
130                               'role': 'TrafficGen',
131                               'name': 'trafficgen_2.yardstick',
132                               'vnfd-id-ref': 'tg__2',
133                               'ip': '1.2.1.1',
134                               'interfaces':
135                               {'xe0': {'local_iface_name': 'ens513f0',
136                                        'vld_id': FWApproxVnf.DOWNLINK,
137                                        'netmask': '255.255.255.0',
138                                        'local_ip': '152.16.40.20',
139                                        'dst_mac': '00:00:00:00:00:01',
140                                        'local_mac': '00:00:00:00:00:03',
141                                        'dst_ip': '152.16.40.19',
142                                        'driver': 'ixgbe',
143                                        'vpci': '0000:02:00.0',
144                                        'dpdk_port_num': 0},
145                                'xe1': {'local_iface_name': 'ens513f1',
146                                        'netmask': '255.255.255.0',
147                                        'network': '202.16.100.0',
148                                        'local_ip': '202.16.100.20',
149                                        'local_mac': '00:1e:67:d0:60:5d',
150                                        'driver': 'ixgbe',
151                                        'vpci': '0000:02:00.1',
152                                        'dpdk_port_num': 1}},
153                               'password': 'r00t',
154                               'VNF model': 'l3fwd_vnf.yaml',
155                               'user': 'root'},
156                              'tg__1':
157                              {'member-vnf-index': '1',
158                               'role': 'TrafficGen',
159                               'name': 'trafficgen_1.yardstick',
160                               'vnfd-id-ref': 'tg__1',
161                               'ip': '1.2.1.1',
162                               'interfaces':
163                               {'xe0': {'local_iface_name': 'ens785f0',
164                                        'vld_id': FWApproxVnf.UPLINK,
165                                        'netmask': '255.255.255.0',
166                                        'local_ip': '152.16.100.20',
167                                        'dst_mac': '00:00:00:00:00:02',
168                                        'local_mac': '00:00:00:00:00:04',
169                                        'dst_ip': '152.16.100.19',
170                                        'driver': 'i40e',
171                                        'vpci': '0000:05:00.0',
172                                        'dpdk_port_num': 0},
173                                'xe1': {'local_iface_name': 'ens785f1',
174                                        'netmask': '255.255.255.0',
175                                        'local_ip': '152.16.100.21',
176                                        'local_mac': '00:00:00:00:00:01',
177                                        'driver': 'i40e',
178                                        'vpci': '0000:05:00.1',
179                                        'dpdk_port_num': 1}},
180                               'password': 'r00t',
181                               'VNF model': 'tg_rfc2544_tpl.yaml',
182                               'user': 'root'},
183                              'vnf__1':
184                              {'name': 'vnf.yardstick',
185                               'vnfd-id-ref': 'vnf__1',
186                               'ip': '1.2.1.1',
187                               'interfaces':
188                               {'xe0': {'local_iface_name': 'ens786f0',
189                                        'vld_id': FWApproxVnf.UPLINK,
190                                        'netmask': '255.255.255.0',
191                                        'local_ip': '152.16.100.19',
192                                        'dst_mac': '00:00:00:00:00:04',
193                                        'local_mac': '00:00:00:00:00:02',
194                                        'dst_ip': '152.16.100.20',
195                                        'driver': 'i40e',
196                                        'vpci': '0000:05:00.0',
197                                        'dpdk_port_num': 0},
198                                'xe1': {'local_iface_name': 'ens786f1',
199                                        'vld_id': FWApproxVnf.DOWNLINK,
200                                        'netmask': '255.255.255.0',
201                                        'local_ip': '152.16.40.19',
202                                        'dst_mac': '00:00:00:00:00:03',
203                                        'local_mac': '00:00:00:00:00:01',
204                                        'dst_ip': '152.16.40.20',
205                                        'driver': 'i40e',
206                                        'vpci': '0000:05:00.1',
207                                        'dpdk_port_num': 1}},
208                               'routing_table':
209                               [{'netmask': '255.255.255.0',
210                                 'gateway': '152.16.100.20',
211                                 'network': '152.16.100.20',
212                                 'if': 'xe0'},
213                                {'netmask': '255.255.255.0',
214                                 'gateway': '152.16.40.20',
215                                 'network': '152.16.40.20',
216                                 'if': 'xe1'}],
217                               'member-vnf-index': '2',
218                               'host': '1.2.1.1',
219                               'role': 'vnf',
220                               'user': 'root',
221                               'nd_route_tbl':
222                               [{'netmask': '112',
223                                 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
224                                 'network': '0064:ff9b:0:0:0:0:9810:6414',
225                                 'if': 'xe0'},
226                                {'netmask': '112',
227                                 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
228                                 'network': '0064:ff9b:0:0:0:0:9810:2814',
229                                 'if': 'xe1'}],
230                               'password': 'r00t',
231                               'VNF model': 'vfw_vnf.yaml'}}}
232
233     def test___init__(self, *args):
234         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
235         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
236         self.assertIsNone(vfw_approx_vnf._vnf_process)
237
238     STATS = """\
239 p vfw stats
240
241 VFW Stats
242 {"VFW_counters" : {"id" : "PIPELINE4", " pkts_received": 6007180, " pkts_fw_forwarded": 6007180, " pkts_drop_fw": 0, " pkts_acl_forwarded": 6007180, "pkts_drop_without_rule" : 0, "average_pkts_in_batch" : 31, "average_internal_time_in_clocks" : 17427, "average_external_time_in_clocks" : 261120, "total_time_measures" : 189829, "ct_packets_forwarded" : 6007148, "ct_packets_dropped" : 0, "bytes_processed ": 360430800, "ct_sessions" : {"active" : 130050, "open_attempt" : 130050, "re-open_attempt" : 0, "established" : 0, "closed" : 0, "timeout" : 0}, "ct_drops" : {"out_of_window" : 0, "invalid_conn" : 0, "invalid_state_transition" : 0 "RST" : 0}}
243 VFW TOTAL: pkts_received: 6007180, "pkts_fw_forwarded": 6007180, "pkts_drop_fw": 0, "fw_drops" : {"TTL_zero" : 0, "bad_size" : 0, "fragmented_packet" : 0, "unsupported_packet_types" : 0, "no_arp_entry" : 6007180}, "pkts_acl_forwarded": 6007180, "pkts_drop_without_rule": 0, "packets_last_sec" : 0, "average_packets_per_sec" : 0, "bytes_last_sec" : 0, "average_bytes_per_sec" : 0, "bytes_processed ": 360430800
244 "CT TOTAL: ct_packets_forwarded" : 6007180, " ct_packets_dropped" : 0, "ct_sessions" : {"active" : 130050, "open_attempt" : 130050, "re-open_attempt" : 0, "established" : 0, "closed" : 0, "timeout" : 0}, "ct_drops" : {"out_of_window" : 0, "invalid_conn" : 0, "invalid_state_transition" : 0 "RST" : 0}
245 Action ID: 00, packetCount: 2954633, byteCount: 177277980
246 Action ID: 01, packetCount: 3052547, byteCount: 183152820
247 pipeline>
248
249 pipeline>
250 """  # noqa
251
252     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
253     @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server', return_value='mock_node')
254     @mock.patch(SSH_HELPER)
255     def test_collect_kpi(self, ssh, *args):
256         mock_ssh(ssh)
257         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
258         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
259         vfw_approx_vnf.scenario_helper.scenario_cfg = {
260             'nodes': {vfw_approx_vnf.name: "mock"}
261         }
262         vfw_approx_vnf.q_in = mock.MagicMock()
263         vfw_approx_vnf.q_out = mock.MagicMock()
264         vfw_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
265         vfw_approx_vnf.resource = mock.Mock(autospec=ResourceProfile)
266         vfw_approx_vnf.resource_helper = mock.MagicMock(
267             **{'collect_kpi.return_value': {"core": {}}})
268         vfw_approx_vnf.vnf_execute = mock.Mock(return_value=self.STATS)
269         result = {
270             'physical_node': 'mock_node',
271             'packets_dropped': 0,
272             'packets_fwd': 6007180,
273             'packets_in': 6007180,
274             'collect_stats': {'core': {}},
275         }
276         self.assertEqual(result, vfw_approx_vnf.collect_kpi())
277
278     @mock.patch("yardstick.network_services.vnf_generic.vnf.sample_vnf.time")
279     @mock.patch(SSH_HELPER)
280     def test_vnf_execute_command(self, ssh, *args):
281         mock_ssh(ssh)
282
283         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
284         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
285         vfw_approx_vnf.q_in = mock.MagicMock()
286         vfw_approx_vnf.q_out = mock.MagicMock()
287         vfw_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
288         cmd = "quit"
289         self.assertEqual(vfw_approx_vnf.vnf_execute(cmd), "")
290
291     @mock.patch(SSH_HELPER)
292     def test_get_stats(self, ssh, *args):
293         mock_ssh(ssh)
294
295         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
296         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
297         vfw_approx_vnf.q_in = mock.MagicMock()
298         vfw_approx_vnf.q_out = mock.MagicMock()
299         vfw_approx_vnf.q_out.qsize = mock.Mock(return_value=0)
300         vfw_approx_vnf.vnf_execute = mock.Mock(return_value=self.STATS)
301         self.assertEqual(self.STATS, vfw_approx_vnf.get_stats())
302
303     def _get_file_abspath(self, filename):
304         curr_path = os.path.dirname(os.path.abspath(__file__))
305         file_path = os.path.join(curr_path, filename)
306         return file_path
307
308     @mock.patch("yardstick.network_services.vnf_generic.vnf.vfw_vnf.hex")
309     @mock.patch("yardstick.network_services.vnf_generic.vnf.vfw_vnf.eval")
310     @mock.patch("yardstick.network_services.vnf_generic.vnf.vfw_vnf.open")
311     @mock.patch(SSH_HELPER)
312     def test_run_vfw(self, ssh, *args):
313         mock_ssh(ssh)
314
315         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
316         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
317         vfw_approx_vnf._build_config = mock.MagicMock()
318         vfw_approx_vnf.queue_wrapper = mock.MagicMock()
319         vfw_approx_vnf.ssh_helper = mock.MagicMock()
320         vfw_approx_vnf.ssh_helper.run = mock.MagicMock()
321         vfw_approx_vnf.scenario_helper.scenario_cfg = self.scenario_cfg
322         vfw_approx_vnf.vnf_cfg = {'lb_config': 'SW',
323                                   'lb_count': 1,
324                                   'worker_config': '1C/1T',
325                                   'worker_threads': 1}
326         vfw_approx_vnf.all_options = {'traffic_type': '4',
327                                       'topology': 'nsb_test_case.yaml'}
328         vfw_approx_vnf._run()
329         vfw_approx_vnf.ssh_helper.run.assert_called_once()
330
331     @mock.patch.object(utils, 'find_relative_file')
332     @mock.patch.object(ctx_base.Context, 'get_context_from_server')
333     @mock.patch(SSH_HELPER)
334     def test_instantiate(self, ssh, *args):
335         mock_ssh(ssh)
336
337         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
338         vfw_approx_vnf = FWApproxVnf(name, vnfd, 'task_id')
339         vfw_approx_vnf.ssh_helper = ssh
340         vfw_approx_vnf.deploy_helper = mock.MagicMock()
341         vfw_approx_vnf.resource_helper = mock.MagicMock()
342         vfw_approx_vnf._build_config = mock.MagicMock()
343         self.scenario_cfg['vnf_options'] = {'acl': {'cfg': "",
344                                                     'rules': ""}}
345         self.scenario_cfg.update({"nodes": {"vnf__1": ""}})
346         self.assertIsNone(vfw_approx_vnf.instantiate(self.scenario_cfg, self.context_cfg))
347
348
349 class TestFWApproxSetupEnvHelper(unittest.TestCase):
350
351     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.open')
352     @mock.patch.object(utils, 'find_relative_file')
353     @mock.patch('yardstick.network_services.vnf_generic.vnf.sample_vnf.MultiPortConfig')
354     @mock.patch.object(utils, 'open_relative_file')
355     def test_build_config(self, *args):
356         vnfd_helper = mock.Mock()
357         ssh_helper = mock.Mock()
358         scenario_helper = mock.Mock()
359         scenario_helper.vnf_cfg = {'lb_config': 'HW'}
360         scenario_helper.options = {}
361         scenario_helper.all_options = {}
362
363         vfw_approx_setup_helper = FWApproxSetupEnvHelper(vnfd_helper, ssh_helper, scenario_helper)
364         vfw_approx_setup_helper.get_flows_config = mock.Mock()
365
366         vfw_approx_setup_helper.ssh_helper.provision_tool = mock.Mock(return_value='tool_path')
367         vfw_approx_setup_helper.ssh_helper.all_ports = mock.Mock()
368         vfw_approx_setup_helper.vnfd_helper.port_nums = mock.Mock(return_value=[0, 1])
369         expected = 'sudo tool_path -p 0x3 -f /tmp/vfw_config -s /tmp/vfw_script  --hwlb 3'
370         self.assertEqual(vfw_approx_setup_helper.build_config(), expected)
371         vfw_approx_setup_helper.get_flows_config.assert_called_once()