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