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