Merge "KVMFORNFV: Reverting LiveMigration changes"
[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 from yardstick.ssh import SSH
29
30 IP_PIPELINE_CFG_FILE_TPL = """
31 arp_route_tbl = ({port0_local_ip_hex},{port0_netmask_hex},1,"""
32 """{port1_local_ip_hex}) ({port1_local_ip_hex},{port1_netmask_hex},0,"""
33 """{port0_local_ip_hex})"""
34
35 IP_PIPELINE_ND_CFG_FILE_TPL = """
36 nd_route_tbl = ({port1_dst_ip_hex6},"""
37 """{port1_dst_netmask_hex6},1,{port1_dst_ip_hex6})"""
38
39 _LOCAL_OBJECT = object()
40
41
42 class FileAbsPath(object):
43     def __init__(self, module_file):
44         super(FileAbsPath, self).__init__()
45         self.module_path = os.path.dirname(os.path.abspath(module_file))
46
47     def get_path(self, filename):
48         file_path = os.path.join(self.module_path, filename)
49         return file_path
50
51
52 def mock_ssh(mock_ssh_type, spec=None, exec_result=_LOCAL_OBJECT, run_result=_LOCAL_OBJECT):
53     if spec is None:
54         spec = SSH
55
56     if exec_result is _LOCAL_OBJECT:
57         exec_result = 0, "", ""
58
59     if run_result is _LOCAL_OBJECT:
60         run_result = 0, "", ""
61
62     mock_ssh_instance = mock.Mock(autospec=spec)
63     mock_ssh_instance._get_client.return_value = mock.Mock()
64     mock_ssh_instance.execute.return_value = exec_result
65     mock_ssh_instance.run.return_value = run_result
66     mock_ssh_type.from_node.return_value = mock_ssh_instance
67     return mock_ssh_instance
68
69
70 class TestQueueFileWrapper(unittest.TestCase):
71     def setUp(self):
72         self.prompt = "pipeline>"
73         self.q_in = Queue()
74         self.q_out = Queue()
75
76     def test___init__(self):
77         queue_file_wrapper = \
78             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
79         self.assertEqual(queue_file_wrapper.prompt, self.prompt)
80
81     def test_clear(self):
82         queue_file_wrapper = \
83             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
84         queue_file_wrapper.bufsize = 5
85         queue_file_wrapper.write("pipeline>")
86         queue_file_wrapper.close()
87         self.assertIsNone(queue_file_wrapper.clear())
88         self.assertIsNotNone(queue_file_wrapper.q_out.empty())
89
90     def test_close(self):
91         queue_file_wrapper = \
92             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
93         self.assertEqual(None, queue_file_wrapper.close())
94
95     def test_read(self):
96         queue_file_wrapper = \
97             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
98         queue_file_wrapper.q_in.put("pipeline>")
99         self.assertEqual("pipeline>", queue_file_wrapper.read(20))
100
101     def test_write(self):
102         queue_file_wrapper = \
103             QueueFileWrapper(self.q_in, self.q_out, self.prompt)
104         queue_file_wrapper.write("pipeline>")
105         self.assertIsNotNone(queue_file_wrapper.q_out.empty())
106
107
108 class TestGenericVNF(unittest.TestCase):
109
110     VNFD_0 = {
111         'short-name': 'VpeVnf',
112         'vdu': [
113             {
114                 'routing_table': [
115                     {
116                         'network': '152.16.100.20',
117                         'netmask': '255.255.255.0',
118                         'gateway': '152.16.100.20',
119                         'if': 'xe0'
120                     },
121                     {
122                         'network': '152.16.40.20',
123                         'netmask': '255.255.255.0',
124                         'gateway': '152.16.40.20',
125                         'if': 'xe1'
126                     },
127                 ],
128                 'description': 'VPE approximation using DPDK',
129                 'name': 'vpevnf-baremetal',
130                 'nd_route_tbl': [
131                     {
132                         'network': '0064:ff9b:0:0:0:0:9810:6414',
133                         'netmask': '112',
134                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
135                         'if': 'xe0'
136                     },
137                     {
138                         'network': '0064:ff9b:0:0:0:0:9810:2814',
139                         'netmask': '112',
140                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
141                         'if': 'xe1'
142                     },
143                 ],
144                 'id': 'vpevnf-baremetal',
145                 'external-interface': [
146                     {
147                         'virtual-interface': {
148                             'dst_mac': '00:00:00:00:00:03',
149                             'vpci': '0000:05:00.0',
150                             'local_ip': '152.16.100.19',
151                             'type': 'PCI-PASSTHROUGH',
152                             'netmask': '255.255.255.0',
153                             'dpdk_port_num': 0,
154                             'bandwidth': '10 Gbps',
155                             'dst_ip': '152.16.100.20',
156                             'local_mac': '00:00:00:00:00:01'
157                         },
158                         'vnfd-connection-point-ref': 'xe0',
159                         'name': 'xe0'
160                     },
161                     {
162                         'virtual-interface': {
163                             'dst_mac': '00:00:00:00:00:04',
164                             'vpci': '0000:05:00.1',
165                             'local_ip': '152.16.40.19',
166                             'type': 'PCI-PASSTHROUGH',
167                             'netmask': '255.255.255.0',
168                             'dpdk_port_num': 1,
169                             'bandwidth': '10 Gbps',
170                             'dst_ip': '152.16.40.20',
171                             'local_mac': '00:00:00:00:00:02'
172                         },
173                         'vnfd-connection-point-ref': 'xe1',
174                         'name': 'xe1'
175                     },
176                 ],
177             },
178         ],
179         'description': 'Vpe approximation using DPDK',
180         'mgmt-interface': {
181             'vdu-id': 'vpevnf-baremetal',
182             'host': '1.1.1.1',
183             'password': 'r00t',
184             'user': 'root',
185             'ip': '1.1.1.1'
186         },
187         'benchmark': {
188             'kpi': [
189                 'packets_in',
190                 'packets_fwd',
191                 'packets_dropped',
192             ],
193         },
194         'connection-point': [
195             {
196                 'type': 'VPORT',
197                 'name': 'xe0',
198             },
199             {
200                 'type': 'VPORT',
201                 'name': 'xe1',
202             },
203         ],
204         'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'
205     }
206
207     VNFD = {
208         'vnfd:vnfd-catalog': {
209             'vnfd': [
210                 VNFD_0,
211             ]
212         }
213     }
214
215     def test___init__(self):
216         generic_vnf = GenericVNF('vnf1', self.VNFD_0)
217         assert generic_vnf.kpi
218
219     def test_collect_kpi(self):
220         generic_vnf = GenericVNF('vnf1', self.VNFD_0)
221         self.assertRaises(NotImplementedError, generic_vnf.collect_kpi)
222
223     def test__get_kpi_definition(self):
224         vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
225         generic_vnf = GenericVNF('vnf1', vnfd)
226         kpi = generic_vnf._get_kpi_definition()
227         self.assertEqual(kpi, ['packets_in', 'packets_fwd', 'packets_dropped'])
228
229     def test_instantiate(self):
230         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
231         with self.assertRaises(NotImplementedError):
232             generic_vnf.instantiate({}, {})
233
234     def test_scale(self):
235         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
236         with self.assertRaises(NotImplementedError):
237             generic_vnf.scale()
238
239     def test_terminate(self):
240         generic_vnf = GenericVNF('vnf1', self.VNFD['vnfd:vnfd-catalog']['vnfd'][0])
241         with self.assertRaises(NotImplementedError):
242             generic_vnf.terminate()
243
244
245 class TestGenericTrafficGen(unittest.TestCase):
246
247     def test_definition(self):
248         """Make sure that the abstract class cannot be instantiated"""
249         vnfd = TestGenericVNF.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
250         name = 'vnf1'
251         with self.assertRaises(TypeError) as exc:
252             GenericTrafficGen(name, vnfd)
253         msg = ("Can't instantiate abstract class GenericTrafficGen with "
254                "abstract methods run_traffic, terminate")
255         self.assertEqual(msg, str(exc.exception))