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