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