Merge "Collectd Fixes"
[yardstick.git] / tests / unit / network_services / vnf_generic / vnf / test_tg_ping.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 from __future__ import absolute_import
19
20 import unittest
21 import mock
22 from multiprocessing import Queue
23
24 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
25 from tests.unit import STL_MOCKS
26
27 SSH_HELPER = "yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper"
28
29 STLClient = mock.MagicMock()
30 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
31 stl_patch.start()
32
33 if stl_patch:
34     from yardstick.network_services.vnf_generic.vnf.tg_ping import PingParser, PingTrafficGen
35     from yardstick.network_services.traffic_profile.base import TrafficProfile
36     from yardstick.network_services.vnf_generic.vnf.sample_vnf import VnfSshHelper
37
38
39 class TestPingParser(unittest.TestCase):
40     def test___init__(self):
41         q_out = Queue()
42         ping_parser = PingParser(q_out)
43         self.assertIsNotNone(ping_parser.queue)
44
45     def test_clear(self):
46         sample_out = """
47 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
48          """
49         q_out = Queue()
50         ping_parser = PingParser(q_out)
51         ping_parser.write(sample_out)
52         ping_parser.clear()
53         self.assertEqual(True, q_out.empty())
54
55     def test_close(self):
56         q_out = Queue()
57         ping_parser = PingParser(q_out)
58         self.assertIsNone(ping_parser.close())
59
60     def test_write(self):
61         sample_out = """
62 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
63          """
64         q_out = Queue()
65         ping_parser = PingParser(q_out)
66         ping_parser.write(sample_out)
67
68         self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())
69
70
71 class TestPingTrafficGen(unittest.TestCase):
72
73     VNFD_0_EXT_IF_0 = {
74         'virtual-interface': {
75             'dst_mac': '00:00:00:00:00:04',
76             'vpci': '0000:05:00.0',
77             'local_ip': u'152.16.100.19',
78             'type': 'PCI-PASSTHROUGH',
79             'netmask': '255.255.255.0',
80             'dpdk_port_num': '0',
81             'bandwidth': '10 Gbps',
82             'driver': "i40e",
83             'dst_ip': u'152.16.100.20',
84             'local_iface_name': 'xe0',
85             'local_mac': '00:00:00:00:00:02',
86         },
87         'vnfd-connection-point-ref': 'xe0',
88         'name': 'xe0',
89     }
90
91     VNFD_0_EXT_IF_1 = {
92         'virtual-interface': {
93             'dst_mac': '00:00:00:00:00:03',
94             'vpci': '0000:05:00.1',
95             'local_ip': u'152.16.40.19',
96             'type': 'PCI-PASSTHROUGH',
97             'driver': "i40e",
98             'netmask': '255.255.255.0',
99             'dpdk_port_num': '1',
100             'bandwidth': '10 Gbps',
101             'dst_ip': u'152.16.40.20',
102             'local_iface_name': 'xe1',
103             'local_mac': '00:00:00:00:00:01',
104         },
105        'vnfd-connection-point-ref': 'xe1',
106        'name': 'xe1',
107     }
108
109     VNFD_0_EXT_IF_LIST = [
110         VNFD_0_EXT_IF_0,
111         VNFD_0_EXT_IF_1,
112     ]
113
114     VNFD_0 = {
115         'short-name': 'VpeVnf',
116         'vdu': [
117             {
118                 'routing_table': [
119                     {
120                         'network': u'152.16.100.20',
121                         'netmask': u'255.255.255.0',
122                         'gateway': u'152.16.100.20',
123                         'if': 'xe0',
124                     },
125                     {
126                         'network': u'152.16.40.20',
127                         'netmask': u'255.255.255.0',
128                         'gateway': u'152.16.40.20',
129                         'if': 'xe1',
130                     },
131                 ],
132                 'description': 'VPE approximation using DPDK',
133                 'name': 'vpevnf-baremetal',
134                 'nd_route_tbl': [
135                     {
136                         'network': '0064:ff9b:0:0:0:0:9810:6414',
137                         'netmask': '112',
138                         'gateway': '0064:ff9b:0:0:0:0:9810:6414',
139                         'if': 'xe0',
140                     },
141                     {
142                         'network': '0064:ff9b:0:0:0:0:9810:2814',
143                         'netmask': '112',
144                         'gateway': '0064:ff9b:0:0:0:0:9810:2814',
145                         'if': 'xe1',
146                     },
147                 ],
148                 'id': 'vpevnf-baremetal',
149                 'external-interface': VNFD_0_EXT_IF_LIST,
150             },
151         ],
152         'description': 'Vpe approximation using DPDK',
153         'mgmt-interface': {
154         'vdu-id': 'vpevnf-baremetal',
155             'host': '1.1.1.1',
156             'password': 'r00t',
157             'user': 'root',
158             'ip': '1.1.1.1',
159         },
160         'benchmark': {
161             'kpi': [
162                 'packets_in',
163                 'packets_fwd',
164                 'packets_dropped',
165             ],
166         },
167         'connection-point': [
168             {
169                 'type': 'VPORT',
170                 'name': 'xe0',
171             },
172             {
173                 'type': 'VPORT',
174                 'name': 'xe1',
175             },
176         ],
177         'id': 'VpeApproxVnf',
178         'name': 'VPEVnfSsh',
179     }
180
181     VNFD = {
182         'vnfd:vnfd-catalog': {
183             'vnfd': [
184                 VNFD_0,
185             ],
186         },
187     }
188
189     TRAFFIC_PROFILE = {
190         "schema": "isb:traffic_profile:0.1",
191         "name": "fixed",
192         "description": "Fixed traffic profile to run UDP traffic",
193         "traffic_profile": {
194             "traffic_type": "FixedTraffic",
195             "frame_rate": 100,  # pps
196             "flow_number": 10,
197             "frame_size": 64,
198         },
199     }
200
201     @mock.patch("yardstick.ssh.SSH")
202     def test___init__(self, ssh):
203         ssh.from_node.return_value.execute.return_value = 0, "success", ""
204         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
205         self.assertIsNotNone(ping_traffic_gen._queue)
206
207     @mock.patch("yardstick.ssh.SSH")
208     def test__bind_device_kernel_with_failure(self, ssh):
209         mock_ssh(ssh)
210
211         execute_result_data = [
212             (1, 'bad stdout messages', 'error messages'),
213             (0, '', ''),
214             (0, 'if_name_1', ''),
215             (0, 'if_name_2', ''),
216         ]
217         ssh.from_node.return_value.execute.side_effect = iter(execute_result_data)
218         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
219         ext_ifs = ping_traffic_gen.vnfd_helper.interfaces
220         self.assertNotEqual(ext_ifs[0]['virtual-interface']['local_iface_name'], 'if_name_1')
221         self.assertNotEqual(ext_ifs[1]['virtual-interface']['local_iface_name'], 'if_name_2')
222
223     @mock.patch("yardstick.ssh.SSH")
224     def test_collect_kpi(self, ssh):
225         mock_ssh(ssh, exec_result=(0, "success", ""))
226         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
227         ping_traffic_gen._queue = Queue()
228         ping_traffic_gen._queue.put({})
229         ping_traffic_gen.collect_kpi()
230         self.assertEqual(ping_traffic_gen._result, {})
231
232     @mock.patch(SSH_HELPER)
233     def test_instantiate(self, ssh):
234         mock_ssh(ssh, spec=VnfSshHelper, exec_result=(0, "success", ""))
235         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
236         ping_traffic_gen.setup_helper.ssh_helper = mock.MagicMock(
237             **{"execute.return_value": (0, "", "")})
238         self.assertIsInstance(ping_traffic_gen.ssh_helper, mock.Mock)
239         self.assertEqual(ping_traffic_gen._result, {})
240         self.assertIsNone(ping_traffic_gen.instantiate({}, {}))
241         self.assertIsNotNone(ping_traffic_gen._result)
242
243     @mock.patch("yardstick.ssh.SSH")
244     def test_listen_traffic(self, ssh):
245         ssh.from_node.return_value.execute.return_value = 0, "success", ""
246         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
247         self.assertIsNone(ping_traffic_gen.listen_traffic({}))
248
249     @mock.patch(SSH_HELPER)
250     def test_run_traffic_process(self, ssh):
251         mock_ssh(ssh)
252
253         mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
254         mock_traffic_profile.get_traffic_definition.return_value = "64"
255         mock_traffic_profile.params = self.TRAFFIC_PROFILE
256
257         ssh.from_node.return_value.execute.return_value = 0, "success", ""
258         ssh.from_node.return_value.run.return_value = 0, "success", ""
259
260         sut = PingTrafficGen('vnf1', self.VNFD_0)
261         sut._traffic_runner(mock_traffic_profile)
262         sut.ssh_helper.run.assert_called_with(
263             "ping -s 64 152.16.100.20",
264             stdout=sut._parser, keep_stdin_open=True, pty=True)
265
266     @mock.patch("yardstick.ssh.SSH")
267     def test_scale_negative(self, ssh):
268         ssh.from_node.return_value.execute.return_value = 0, "success", ""
269         ssh.from_node.return_value.run.return_value = 0, "success", ""
270
271         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
272         ping_traffic_gen.scale()
273
274     @mock.patch("yardstick.ssh.SSH")
275     def test_terminate(self, ssh):
276         ssh.from_node.return_value.execute.return_value = 0, "success", ""
277         ssh.from_node.return_value.run.return_value = 0, "success", ""
278
279         ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
280         self.assertIsNone(ping_traffic_gen.terminate())