1 # Copyright (c) 2016-2019 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from multiprocessing import Queue
17 import multiprocessing
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
31 SSH_HELPER = "yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper"
34 class TestPingResourceHelper(unittest.TestCase):
35 def test___init__(self):
36 setup_helper = mock.Mock()
37 helper = PingResourceHelper(setup_helper)
39 self.assertIsInstance(helper._queue, multiprocessing.queues.Queue)
40 self.assertIsInstance(helper._parser, PingParser)
42 def test_run_traffic(self):
43 setup_helper = mock.Mock()
44 traffic_profile = mock.Mock()
45 traffic_profile.params = {
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',
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')
61 class TestPingParser(unittest.TestCase):
62 def test___init__(self):
64 ping_parser = PingParser(q_out)
65 self.assertIsNotNone(ping_parser.queue)
69 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
72 ping_parser = PingParser(q_out)
73 ping_parser.write(sample_out)
75 self.assertTrue(q_out.empty())
79 ping_parser = PingParser(q_out)
80 self.assertIsNone(ping_parser.close())
84 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
87 ping_parser = PingParser(q_out)
88 ping_parser.write(sample_out)
90 self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())
93 class TestPingTrafficGen(unittest.TestCase):
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',
103 'dst_ip': u'152.16.100.20',
104 'local_iface_name': 'xe0',
105 'local_mac': '00:00:00:00:00:02',
107 'vnfd-connection-point-ref': 'xe0',
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',
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',
124 'vnfd-connection-point-ref': 'xe1',
128 VNFD_0_EXT_IF_LIST = [
134 'short-name': 'VpeVnf',
139 'network': u'152.16.100.20',
140 'netmask': u'255.255.255.0',
141 'gateway': u'152.16.100.20',
145 'network': u'152.16.40.20',
146 'netmask': u'255.255.255.0',
147 'gateway': u'152.16.40.20',
151 'description': 'VPE approximation using DPDK',
152 'name': 'vpevnf-baremetal',
155 'network': '0064:ff9b:0:0:0:0:9810:6414',
157 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
161 'network': '0064:ff9b:0:0:0:0:9810:2814',
163 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
167 'id': 'vpevnf-baremetal',
168 'external-interface': VNFD_0_EXT_IF_LIST,
171 'description': 'Vpe approximation using DPDK',
173 'vdu-id': 'vpevnf-baremetal',
186 'connection-point': [
196 'id': 'VpeApproxVnf',
201 'vnfd:vnfd-catalog': {
209 "schema": "isb:traffic_profile:0.1",
211 "description": "Fixed traffic profile to run UDP traffic",
213 "traffic_type": "FixedTraffic",
214 "frame_rate": 100, # pps
221 'target_ip': u'152.16.100.20',
222 'local_ip': u'152.16.100.19',
223 'local_if_name': u'xe0_fake',
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)
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, {})
235 @mock.patch("yardstick.ssh.SSH")
236 def test__bind_device_kernel_with_failure(self, ssh):
239 execute_result_data = [
240 (1, 'bad stdout messages', 'error messages'),
242 (0, 'if_name_1', ''),
243 (0, 'if_name_2', ''),
245 ssh.from_node.return_value.execute.side_effect = iter(execute_result_data)
246 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
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')
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", ""))
256 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
257 ping_traffic_gen.scenario_helper.scenario_cfg = {
258 'nodes': {ping_traffic_gen.name: "mock"}
260 ping_traffic_gen._queue = Queue()
261 ping_traffic_gen._queue.put({})
263 'physical_node': 'mock_node',
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)
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)
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, {})
280 self.assertIsNone(ping_traffic_gen.instantiate({}, {}))
283 ping_traffic_gen.vnfd_helper.interfaces[0]['virtual-interface']['local_iface_name'],
285 self.assertEqual(self.CMD_KWARGS, ping_traffic_gen.resource_helper.cmd_kwargs)
286 self.assertIsNotNone(ping_traffic_gen._result)
288 def test_listen_traffic(self):
289 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
290 self.assertIsNone(ping_traffic_gen.listen_traffic({}))
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", ""
297 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
298 self.assertIsNone(ping_traffic_gen.terminate())