Merge "NSB: Improve get_url in ansible scripts"
[yardstick.git] / yardstick / tests / unit / network_services / vnf_generic / vnf / test_vpp_helpers.py
1 # Copyright (c) 2019 Viosoft 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 unittest
15
16 import mock
17
18 from yardstick.network_services.vnf_generic.vnf.base import VnfdHelper
19 from yardstick.network_services.vnf_generic.vnf.vpp_helpers import \
20     VppSetupEnvHelper
21
22
23 class TestVppSetupEnvHelper(unittest.TestCase):
24     VNFD_0 = {
25         "benchmark": {
26             "kpi": [
27                 "packets_in",
28                 "packets_fwd",
29                 "packets_dropped"
30             ]
31         },
32         "connection-point": [
33             {
34                 "name": "xe0",
35                 "type": "VPORT"
36             },
37             {
38                 "name": "xe1",
39                 "type": "VPORT"
40             }
41         ],
42         "description": "VPP IPsec",
43         "id": "VipsecApproxVnf",
44         "mgmt-interface": {
45             "ip": "10.10.10.101",
46             "password": "r00t",
47             "user": "root",
48             "vdu-id": "ipsecvnf-baremetal"
49         },
50         "name": "IpsecVnf",
51         "short-name": "IpsecVnf",
52         "vdu": [
53             {
54                 "description": "VPP Ipsec",
55                 "external-interface": [
56                     {
57                         "name": "xe0",
58                         "virtual-interface": {
59                             "driver": "igb_uio",
60                             "dst_ip": "192.168.100.1",
61                             "dst_mac": "90:e2:ba:7c:30:e8",
62                             "ifname": "xe0",
63                             "local_ip": "192.168.100.2",
64                             "local_mac": "90:e2:ba:7c:41:a8",
65                             "netmask": "255.255.255.0",
66                             "network": {},
67                             "node_name": "vnf__0",
68                             "peer_ifname": "xe0",
69                             "peer_intf": {
70                                 "dpdk_port_num": 0,
71                                 "driver": "igb_uio",
72                                 "dst_ip": "192.168.100.2",
73                                 "dst_mac": "90:e2:ba:7c:41:a8",
74                                 "ifname": "xe0",
75                                 "local_ip": "192.168.100.1",
76                                 "local_mac": "90:e2:ba:7c:30:e8",
77                                 "netmask": "255.255.255.0",
78                                 "network": {},
79                                 "node_name": "tg__0",
80                                 "peer_ifname": "xe0",
81                                 "peer_name": "vnf__0",
82                                 "vld_id": "uplink_0",
83                                 "vpci": "0000:81:00.0"
84                             },
85                             "peer_name": "tg__0",
86                             "vld_id": "uplink_0",
87                             "vpci": "0000:ff:06.0"
88                         },
89                         "vnfd-connection-point-ref": "xe0"
90                     },
91                     {
92                         "name": "xe1",
93                         "virtual-interface": {
94                             "driver": "igb_uio",
95                             "dst_ip": "1.1.1.2",
96                             "dst_mac": "0a:b1:ec:fd:a2:66",
97                             "ifname": "xe1",
98                             "local_ip": "1.1.1.1",
99                             "local_mac": "4e:90:85:d3:c5:13",
100                             "netmask": "255.255.255.0",
101                             "network": {},
102                             "node_name": "vnf__0",
103                             "peer_ifname": "xe1",
104                             "peer_intf": {
105                                 "driver": "igb_uio",
106                                 "dst_ip": "1.1.1.1",
107                                 "dst_mac": "4e:90:85:d3:c5:13",
108                                 "ifname": "xe1",
109                                 "local_ip": "1.1.1.2",
110                                 "local_mac": "0a:b1:ec:fd:a2:66",
111                                 "netmask": "255.255.255.0",
112                                 "network": {},
113                                 "node_name": "vnf__1",
114                                 "peer_ifname": "xe1",
115                                 "peer_name": "vnf__0",
116                                 "vld_id": "ciphertext",
117                                 "vpci": "0000:00:07.0"
118                             },
119                             "peer_name": "vnf__1",
120                             "vld_id": "ciphertext",
121                             "vpci": "0000:ff:07.0"
122                         },
123                         "vnfd-connection-point-ref": "xe1"
124                     }
125                 ],
126                 "id": "ipsecvnf-baremetal",
127                 "name": "ipsecvnf-baremetal",
128                 "routing_table": []
129             }
130         ]
131     }
132
133     VNFD = {
134         'vnfd:vnfd-catalog': {
135             'vnfd': [
136                 VNFD_0,
137             ],
138         },
139     }
140
141     VPP_INTERFACES_DUMP = [
142         {
143             "sw_if_index": 0,
144             "sup_sw_if_index": 0,
145             "l2_address_length": 0,
146             "l2_address": [0, 0, 0, 0, 0, 0, 0, 0],
147             "interface_name": "local0",
148             "admin_up_down": 0,
149             "link_up_down": 0,
150             "link_duplex": 0,
151             "link_speed": 0,
152             "mtu": 0,
153             "sub_id": 0,
154             "sub_dot1ad": 0,
155             "sub_number_of_tags": 0,
156             "sub_outer_vlan_id": 0,
157             "sub_inner_vlan_id": 0,
158             "sub_exact_match": 0,
159             "sub_default": 0,
160             "sub_outer_vlan_id_any": 0,
161             "sub_inner_vlan_id_any": 0,
162             "vtr_op": 0,
163             "vtr_push_dot1q": 0,
164             "vtr_tag1": 0,
165             "vtr_tag2": 0
166         },
167         {
168             "sw_if_index": 1,
169             "sup_sw_if_index": 1,
170             "l2_address_length": 6,
171             "l2_address": [144, 226, 186, 124, 65, 168, 0, 0],
172             "interface_name": "TenGigabitEthernetff/6/0",
173             "admin_up_down": 0,
174             "link_up_down": 0,
175             "link_duplex": 2,
176             "link_speed": 32,
177             "mtu": 9202,
178             "sub_id": 0,
179             "sub_dot1ad": 0,
180             "sub_number_of_tags": 0,
181             "sub_outer_vlan_id": 0,
182             "sub_inner_vlan_id": 0,
183             "sub_exact_match": 0,
184             "sub_default": 0,
185             "sub_outer_vlan_id_any": 0,
186             "sub_inner_vlan_id_any": 0,
187             "vtr_op": 0,
188             "vtr_push_dot1q": 0,
189             "vtr_tag1": 0,
190             "vtr_tag2": 0
191         },
192         {
193             "sw_if_index": 2,
194             "sup_sw_if_index": 2,
195             "l2_address_length": 6,
196             "l2_address": [78, 144, 133, 211, 197, 19, 0, 0],
197             "interface_name": "VirtualFunctionEthernetff/7/0",
198             "admin_up_down": 0,
199             "link_up_down": 0,
200             "link_duplex": 2,
201             "link_speed": 32,
202             "mtu": 9206,
203             "sub_id": 0,
204             "sub_dot1ad": 0,
205             "sub_number_of_tags": 0,
206             "sub_outer_vlan_id": 0,
207             "sub_inner_vlan_id": 0,
208             "sub_exact_match": 0,
209             "sub_default": 0,
210             "sub_outer_vlan_id_any": 0,
211             "sub_inner_vlan_id_any": 0,
212             "vtr_op": 0,
213             "vtr_push_dot1q": 0,
214             "vtr_tag1": 0,
215             "vtr_tag2": 0
216         }
217     ]
218
219     def test_kill_vnf(self):
220         vnfd_helper = VnfdHelper(self.VNFD_0)
221         ssh_helper = mock.Mock()
222         ssh_helper.execute.return_value = 0, 0, 0
223         scenario_helper = mock.Mock()
224         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
225                                                  scenario_helper)
226         vpp_setup_env_helper.kill_vnf()
227
228     def test_kill_vnf_error(self):
229         vnfd_helper = VnfdHelper(self.VNFD_0)
230         ssh_helper = mock.Mock()
231         ssh_helper.execute.return_value = 1, 0, 0
232         scenario_helper = mock.Mock()
233         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
234                                                  scenario_helper)
235         with self.assertRaises(RuntimeError) as raised:
236             vpp_setup_env_helper.kill_vnf()
237
238         self.assertIn('Failed to stop service vpp', str(raised.exception))
239
240     def test_tear_down(self):
241         vnfd_helper = VnfdHelper(self.VNFD_0)
242         ssh_helper = mock.Mock()
243         scenario_helper = mock.Mock()
244         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
245                                                  scenario_helper)
246         vpp_setup_env_helper.tear_down()
247
248     def test_start_vpp_service(self):
249         vnfd_helper = VnfdHelper(self.VNFD_0)
250         ssh_helper = mock.Mock()
251         ssh_helper.execute.return_value = 0, 0, 0
252         scenario_helper = mock.Mock()
253         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
254                                                  scenario_helper)
255         vpp_setup_env_helper.start_vpp_service()
256
257     def test_start_vpp_service_error(self):
258         vnfd_helper = VnfdHelper(self.VNFD_0)
259         ssh_helper = mock.Mock()
260         ssh_helper.execute.return_value = 1, 0, 0
261         scenario_helper = mock.Mock()
262         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
263                                                  scenario_helper)
264         with self.assertRaises(RuntimeError) as raised:
265             vpp_setup_env_helper.start_vpp_service()
266
267         self.assertIn('Failed to start service vpp', str(raised.exception))
268
269     def test__update_vnfd_helper(self):
270         vnfd_helper = VnfdHelper(self.VNFD_0)
271         ssh_helper = mock.Mock()
272         scenario_helper = mock.Mock()
273         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
274                                                  scenario_helper)
275         vpp_setup_env_helper._update_vnfd_helper(
276             {'vpp-data': {'vpp-key': 'vpp-value'}})
277
278         self.assertEqual({'vpp-key': 'vpp-value'},
279                          vpp_setup_env_helper.vnfd_helper.get('vpp-data', {}))
280
281     def test__update_vnfd_helper_with_key(self):
282         vnfd_helper = VnfdHelper(self.VNFD_0)
283         ssh_helper = mock.Mock()
284         scenario_helper = mock.Mock()
285         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
286                                                  scenario_helper)
287         vpp_setup_env_helper._update_vnfd_helper({'driver': 'qat'}, 'xe0')
288
289         self.assertEqual('qat',
290                          vpp_setup_env_helper.get_value_by_interface_key(
291                              'xe0', 'driver'))
292
293     def test__update_vnfd_helper_dict_without_key(self):
294         vnfd_helper = VnfdHelper(self.VNFD_0)
295         ssh_helper = mock.Mock()
296         scenario_helper = mock.Mock()
297         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
298                                                  scenario_helper)
299         vpp_setup_env_helper._update_vnfd_helper(
300             {'mgmt-interface': {'name': 'net'}})
301
302         self.assertEqual({'ip': '10.10.10.101',
303                           'name': 'net',
304                           'password': 'r00t',
305                           'user': 'root',
306                           'vdu-id': 'ipsecvnf-baremetal'},
307                          vpp_setup_env_helper.vnfd_helper.get('mgmt-interface',
308                                                               {}))
309
310     def test_get_value_by_interface_key(self):
311         vnfd_helper = VnfdHelper(self.VNFD_0)
312         ssh_helper = mock.Mock()
313
314         scenario_helper = mock.Mock()
315         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
316                                                  scenario_helper)
317         vpp_setup_env_helper._update_vnfd_helper(
318             {'vpp-data': {'vpp-key': 'vpp-value'}}, 'xe0')
319
320         self.assertEqual({'vpp-key': 'vpp-value'},
321                          vpp_setup_env_helper.get_value_by_interface_key(
322                              'xe0', 'vpp-data'))
323
324     def test_get_value_by_interface_key_error(self):
325         vnfd_helper = VnfdHelper(self.VNFD_0)
326         ssh_helper = mock.Mock()
327
328         scenario_helper = mock.Mock()
329         vpp_setup_env_helper = VppSetupEnvHelper(vnfd_helper, ssh_helper,
330                                                  scenario_helper)
331         vpp_setup_env_helper._update_vnfd_helper(
332             {'vpp-data': {'vpp-key': 'vpp-value'}}, 'xe0')
333
334         self.assertIsNone(vpp_setup_env_helper.get_value_by_interface_key(
335             'xe2', 'vpp-err'))