3 # Copyright (c) 2016-2017 Intel Corporation
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from __future__ import absolute_import
22 from multiprocessing import Queue
24 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
25 from tests.unit import STL_MOCKS
27 SSH_HELPER = "yardstick.network_services.vnf_generic.vnf.sample_vnf.VnfSshHelper"
29 STLClient = mock.MagicMock()
30 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
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
39 class TestPingParser(unittest.TestCase):
40 def test___init__(self):
42 ping_parser = PingParser(q_out)
43 self.assertIsNotNone(ping_parser.queue)
47 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
50 ping_parser = PingParser(q_out)
51 ping_parser.write(sample_out)
53 self.assertEqual(True, q_out.empty())
57 ping_parser = PingParser(q_out)
58 self.assertIsNone(ping_parser.close())
62 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
65 ping_parser = PingParser(q_out)
66 ping_parser.write(sample_out)
68 self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())
71 class TestPingTrafficGen(unittest.TestCase):
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',
81 'bandwidth': '10 Gbps',
83 'dst_ip': u'152.16.100.20',
84 'local_iface_name': 'xe0',
85 'local_mac': '00:00:00:00:00:02',
87 'vnfd-connection-point-ref': 'xe0',
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',
98 'netmask': '255.255.255.0',
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',
105 'vnfd-connection-point-ref': 'xe1',
109 VNFD_0_EXT_IF_LIST = [
115 'short-name': 'VpeVnf',
120 'network': u'152.16.100.20',
121 'netmask': u'255.255.255.0',
122 'gateway': u'152.16.100.20',
126 'network': u'152.16.40.20',
127 'netmask': u'255.255.255.0',
128 'gateway': u'152.16.40.20',
132 'description': 'VPE approximation using DPDK',
133 'name': 'vpevnf-baremetal',
136 'network': '0064:ff9b:0:0:0:0:9810:6414',
138 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
142 'network': '0064:ff9b:0:0:0:0:9810:2814',
144 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
148 'id': 'vpevnf-baremetal',
149 'external-interface': VNFD_0_EXT_IF_LIST,
152 'description': 'Vpe approximation using DPDK',
154 'vdu-id': 'vpevnf-baremetal',
167 'connection-point': [
177 'id': 'VpeApproxVnf',
182 'vnfd:vnfd-catalog': {
190 "schema": "isb:traffic_profile:0.1",
192 "description": "Fixed traffic profile to run UDP traffic",
194 "traffic_type": "FixedTraffic",
195 "frame_rate": 100, # pps
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)
207 @mock.patch("yardstick.ssh.SSH")
208 def test__bind_device_kernel_with_failure(self, ssh):
211 execute_result_data = [
212 (1, 'bad stdout messages', 'error messages'),
214 (0, 'if_name_1', ''),
215 (0, 'if_name_2', ''),
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')
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, {})
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)
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({}))
249 @mock.patch(SSH_HELPER)
250 def test_run_traffic_process(self, ssh):
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
257 ssh.from_node.return_value.execute.return_value = 0, "success", ""
258 ssh.from_node.return_value.run.return_value = 0, "success", ""
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)
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", ""
271 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
272 ping_traffic_gen.scale()
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", ""
279 ping_traffic_gen = PingTrafficGen('vnf1', self.VNFD_0)
280 self.assertIsNone(ping_traffic_gen.terminate())