Merge "Add common openstack opertation scenarios: network"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_base.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # Unittest for yardstick.network_services.vnf_generic.vnf.test_base
19
20 from __future__ import absolute_import
21 import unittest
22 import os
23 import mock
24 from multiprocessing import Queue
25
26 from yardstick.network_services.vnf_generic.vnf.base import \
27     QueueFileWrapper, GenericVNF, GenericTrafficGen
28
29 IP_PIPELINE_CFG_FILE_TPL = """
30 arp_route_tbl = ({port0_local_ip_hex},{port0_netmask_hex},1,"""
31 """{port1_local_ip_hex}) ({port1_local_ip_hex},{port1_netmask_hex},0,"""
32 """{port0_local_ip_hex})"""
33
34 IP_PIPELINE_ND_CFG_FILE_TPL = """
35 nd_route_tbl = ({port1_dst_ip_hex6},"""
36 """{port1_dst_netmask_hex6},1,{port1_dst_ip_hex6})"""
37
38 _LOCAL_OBJECT = object()
39
40
41 class FileAbsPath(object):
42     def __init__(self, module_file):
43         super(FileAbsPath, self).__init__()
44         self.module_path = os.path.dirname(os.path.abspath(module_file))
45
46     def get_path(self, filename):
47         file_path = os.path.join(self.module_path, filename)
48         return file_path
49
50
51 def mock_ssh(ssh, spec=None, exec_result=_LOCAL_OBJECT, run_result=_LOCAL_OBJECT):
52     if spec is None:
53         spec = ssh.SSH
54
55     if exec_result is _LOCAL_OBJECT:
56         exec_result = 0, "", ""
57
58     if run_result is _LOCAL_OBJECT:
59         run_result = 0, "", ""
60
61     ssh_mock = mock.Mock(autospec=spec)
62     ssh_mock._get_client.return_value = mock.Mock()
63     ssh_mock.execute.return_value = exec_result
64     ssh_mock.run.return_value = run_result
65     ssh.from_node.return_value = ssh_mock
66
67
68 class TestQueueFileWrapper(unittest.TestCase):
69     def setUp(self):
70         self.prompt = "pipeline>"
71         self.q_in = Queue()
72         self.q_out = Queue()
73
74     def test___init__(self):
75         queue_file_wrapper = \
76             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
77         self.assertEqual(queue_file_wrapper.prompt, self.prompt)
78
79     def test_clear(self):
80         queue_file_wrapper = \
81             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
82         queue_file_wrapper.bufsize = 5
83         queue_file_wrapper.write("pipeline>")
84         queue_file_wrapper.close()
85         self.assertIsNone(queue_file_wrapper.clear())
86         self.assertIsNotNone(queue_file_wrapper.q_out.empty())
87
88     def test_close(self):
89         queue_file_wrapper = \
90             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
91         self.assertEqual(None, queue_file_wrapper.close())
92
93     def test_read(self):
94         queue_file_wrapper = \
95             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
96         queue_file_wrapper.q_in.put("pipeline>")
97         self.assertEqual("pipeline>", queue_file_wrapper.read(20))
98
99     def test_write(self):
100         queue_file_wrapper = \
101             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
102         queue_file_wrapper.write("pipeline>")
103         self.assertIsNotNone(queue_file_wrapper.q_out.empty())
104
105
106 class TestGenericVNF(unittest.TestCase):
107
108     VNFD_0 = {
109         'short-name': 'VpeVnf',
110         'vdu': [
111             {
112                 'routing_table': [
113                     {
114                         'network': '152.16.100.20',
115                         'netmask': '255.255.255.0',
116                         'gateway': '152.16.100.20',
117                         'if': 'xe0'
118                     },
119                     {
120                         'network': '152.16.40.20',
121                         'netmask': '255.255.255.0',
122                         'gateway': '152.16.40.20',
123                         'if': 'xe1'
124                     },
125                 ],
126                 'description': 'VPE approximation using DPDK',
127                 'name': 'vpevnf-baremetal',
128                 'nd_route_tbl': [
129                     {
130                         'network': '0064:ff9b:0:0:0:0:9810:6414',
131                         'netmask': '112',
132                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
133                         'if': 'xe0'
134                     },
135                     {
136                         'network': '0064:ff9b:0:0:0:0:9810:2814',
137                         'netmask': '112',
138                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
139                         'if': 'xe1'
140                     },
141                 ],
142                 'id': 'vpevnf-baremetal',
143                 'external-interface': [
144                     {
145                         'virtual-interface': {
146                             'dst_mac': '00:00:00:00:00:03',
147                             'vpci': '0000:05:00.0',
148                             'local_ip': '152.16.100.19',
149                             'type': 'PCI-PASSTHROUGH',
150                             'netmask': '255.255.255.0',
151                             'dpdk_port_num': '0',
152                             'bandwidth': '10 Gbps',
153                             'dst_ip': '152.16.100.20',
154                             'local_mac': '00:00:00:00:00:01'
155                         },
156                         'vnfd-connection-point-ref': 'xe0',
157                         'name': 'xe0'
158                     },
159                     {
160                         'virtual-interface': {
161                             'dst_mac': '00:00:00:00:00:04',
162                             'vpci': '0000:05:00.1',
163                             'local_ip': '152.16.40.19',
164                             'type': 'PCI-PASSTHROUGH',
165                             'netmask': '255.255.255.0',
166                             'dpdk_port_num': '1',
167                             'bandwidth': '10 Gbps',
168                             'dst_ip': '152.16.40.20',
169                             'local_mac': '00:00:00:00:00:02'
170                         },
171                         'vnfd-connection-point-ref': 'xe1',
172                         'name': 'xe1'
173                     },
174                 ],
175             },
176         ],
177         'description': 'Vpe approximation using DPDK',
178         'mgmt-interface': {
179             'vdu-id': 'vpevnf-baremetal',
180             'host': '1.1.1.1',
181             'password': 'r00t',
182             'user': 'root',
183             'ip': '1.1.1.1'
184         },
185         'benchmark': {
186             'kpi': [
187                 'packets_in',
188                 'packets_fwd',
189                 'packets_dropped',
190             ],
191         },
192         'connection-point': [
193             {
194                 'type': 'VPORT',
195                 'name': 'xe0',
196             },
197             {
198                 'type': 'VPORT',
199                 'name': 'xe1',
200             },
201         ],
202         'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'
203     }
204
205     VNFD = {
206         'vnfd:vnfd-catalog': {
207             'vnfd': [
208                 VNFD_0,
209             ]
210         }
211     }
212
213     def test___init__(self):
214         generic_vnf = GenericVNF('vnf1', self.VNFD_0)
215         assert generic_vnf.kpi
216
217     def test_collect_kpi(self):
218         generic_vnf = GenericVNF('vnf1', self.VNFD_0)
219         self.assertRaises(NotImplementedError, generic_vnf.collect_kpi)
220
221     def test__get_kpi_definition(self):
222         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
223         generic_vnf = GenericVNF('vnf1', vnfd)
224         kpi = generic_vnf._get_kpi_definition()
225         self.assertEqual(kpi, ['packets_in', 'packets_fwd', 'packets_dropped'])
226
227     def test_instantiate(self):
228         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
229         with self.assertRaises(NotImplementedError):
230             generic_vnf.instantiate({}, {})
231
232     def test_scale(self):
233         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
234         with self.assertRaises(NotImplementedError):
235             generic_vnf.scale()
236
237     def test_terminate(self):
238         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
239         with self.assertRaises(NotImplementedError):
240             generic_vnf.terminate()
241
242
243 class TestGenericTrafficGen(unittest.TestCase):
244     def test___init__(self):
245         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
246         generic_traffic_gen = GenericTrafficGen('vnf1', vnfd)
247         assert generic_traffic_gen.name == "vnf1"
248
249     def test_listen_traffic(self):
250         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
251         generic_traffic_gen = GenericTrafficGen('vnf1', vnfd)
252         traffic_profile = {}
253         self.assertIsNone(generic_traffic_gen.listen_traffic(traffic_profile))
254
255     def test_run_traffic(self):
256         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
257         generic_traffic_gen = GenericTrafficGen('vnf1', vnfd)
258         traffic_profile = {}
259         self.assertRaises(NotImplementedError,
260                           generic_traffic_gen.run_traffic, traffic_profile)
261
262     def test_terminate(self):
263         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
264         generic_traffic_gen = GenericTrafficGen('vnf1', vnfd)
265         self.assertRaises(NotImplementedError, generic_traffic_gen.terminate)
266
267     def test_verify_traffic(self):
268         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
269         generic_traffic_gen = GenericTrafficGen('vnf1', vnfd)
270         traffic_profile = {}
271         self.assertIsNone(generic_traffic_gen.verify_traffic(traffic_profile))