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