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
21 from multiprocessing import Queue
23 from yardstick.network_services.vnf_generic.vnf.tg_ping import \
24 PingParser, PingTrafficGen
25 from yardstick.network_services.traffic_profile.base import TrafficProfile
28 class TestPingParser(unittest.TestCase):
29 def test___init__(self):
31 ping_parser = PingParser(q_out)
32 self.assertIsNotNone(ping_parser.queue)
36 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
39 ping_parser = PingParser(q_out)
40 ping_parser.write(sample_out)
42 self.assertEqual(True, q_out.empty())
46 ping_parser = PingParser(q_out)
47 self.assertIsNone(ping_parser.close())
51 64 bytes from 10.102.22.93: icmp_seq=3 ttl=64 time=0.296 ms
54 ping_parser = PingParser(q_out)
55 ping_parser.write(sample_out)
57 self.assertEqual({"packets_received": 3.0, "rtt": 0.296}, q_out.get())
60 class TestPingTrafficGen(unittest.TestCase):
61 VNFD = {'vnfd:vnfd-catalog':
63 [{'short-name': 'VpeVnf',
66 [{'network': '152.16.100.20',
67 'netmask': '255.255.255.0',
68 'gateway': '152.16.100.20',
70 {'network': '152.16.40.20',
71 'netmask': '255.255.255.0',
72 'gateway': '152.16.40.20',
74 'description': 'VPE approximation using DPDK',
75 'name': 'vpevnf-baremetal',
77 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
79 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
81 {'network': '0064:ff9b:0:0:0:0:9810:2814',
83 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
85 'id': 'vpevnf-baremetal',
87 [{'virtual-interface':
88 {'dst_mac': '00:00:00:00:00:04',
89 'vpci': '0000:05:00.0',
90 'local_ip': '152.16.100.19',
91 'type': 'PCI-PASSTHROUGH',
92 'netmask': '255.255.255.0',
94 'bandwidth': '10 Gbps',
96 'dst_ip': '152.16.100.20',
97 'local_iface_name': 'xe0',
98 'local_mac': '00:00:00:00:00:02'},
99 'vnfd-connection-point-ref': 'xe0',
101 {'virtual-interface':
102 {'dst_mac': '00:00:00:00:00:03',
103 'vpci': '0000:05:00.1',
104 'local_ip': '152.16.40.19',
105 'type': 'PCI-PASSTHROUGH',
107 'netmask': '255.255.255.0',
108 'dpdk_port_num': '1',
109 'bandwidth': '10 Gbps',
110 'dst_ip': '152.16.40.20',
111 'local_iface_name': 'xe1',
112 'local_mac': '00:00:00:00:00:01'},
113 'vnfd-connection-point-ref': 'xe1',
115 'description': 'Vpe approximation using DPDK',
117 {'vdu-id': 'vpevnf-baremetal',
123 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
124 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
125 {'type': 'VPORT', 'name': 'xe1'}],
126 'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
129 "schema": "isb:traffic_profile:0.1",
131 "description": "Fixed traffic profile to run UDP traffic",
133 "traffic_type": "FixedTraffic",
134 "frame_rate": 100, # pps
138 def test___init__(self):
139 with mock.patch("yardstick.ssh.SSH") as ssh:
140 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
141 ssh_mock = mock.Mock(autospec=ssh.SSH)
143 mock.Mock(return_value=(0, "", ""))
144 ssh.from_node.return_value = ssh_mock
145 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
146 ping_traffic_gen = PingTrafficGen(vnfd)
147 self.assertEqual(ping_traffic_gen._queue, None)
149 def test_collect_kpi(self):
150 with mock.patch("yardstick.ssh.SSH") as ssh:
151 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
152 ssh_mock = mock.Mock(autospec=ssh.SSH)
154 mock.Mock(return_value=(0, "", ""))
155 ssh.from_node.return_value = ssh_mock
156 ping_traffic_gen = PingTrafficGen(vnfd)
157 ping_traffic_gen._queue = Queue()
158 ping_traffic_gen._queue.put({})
159 ping_traffic_gen.collect_kpi()
160 self.assertEqual({}, ping_traffic_gen._result)
162 def test_instantiate(self):
163 with mock.patch("yardstick.ssh.SSH") as ssh:
164 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
165 ssh_mock = mock.Mock(autospec=ssh.SSH)
167 mock.Mock(return_value=(0, "", ""))
168 ssh.from_node.return_value = ssh_mock
169 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
170 ping_traffic_gen = PingTrafficGen(vnfd)
171 self.assertEqual(None, ping_traffic_gen.instantiate({}, {}))
173 def test_listen_traffic(self):
174 with mock.patch("yardstick.ssh.SSH") as ssh:
175 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
176 ssh_mock = mock.Mock(autospec=ssh.SSH)
178 mock.Mock(return_value=(0, "", ""))
179 ssh.from_node.return_value = ssh_mock
180 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
181 ping_traffic_gen = PingTrafficGen(vnfd)
182 self.assertEqual(None, ping_traffic_gen.listen_traffic({}))
184 @mock.patch("yardstick.network_services.vnf_generic.vnf.tg_ping.time")
185 def test_run_traffic(self, mock_time):
186 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
187 mock_traffic_profile.get_traffic_definition.return_value = "64"
188 mock_traffic_profile.params = self.TRAFFIC_PROFILE
189 with mock.patch("yardstick.ssh.SSH") as ssh:
190 ssh_mock = mock.Mock(autospec=ssh.SSH)
192 mock.Mock(return_value=(0, "", ""))
194 mock.Mock(return_value=(0, "", ""))
195 ssh.from_node.return_value = ssh_mock
196 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
197 self.sut = PingTrafficGen(vnfd)
198 self.sut.connection = mock.Mock()
199 self.sut.connection.run = mock.Mock()
200 self.sut._traffic_runner = mock.Mock(return_value=0)
201 self.assertIn(self.sut.run_traffic(mock_traffic_profile), {True, False})
203 def test_run_traffic_process(self):
204 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
205 mock_traffic_profile.get_traffic_definition.return_value = "64"
206 mock_traffic_profile.params = self.TRAFFIC_PROFILE
207 with mock.patch("yardstick.ssh.SSH") as ssh:
208 ssh_mock = mock.Mock(autospec=ssh.SSH)
210 mock.Mock(return_value=(0, "", ""))
212 mock.Mock(return_value=(0, "", ""))
213 ssh.from_node.return_value = ssh_mock
214 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
215 self.sut = PingTrafficGen(vnfd)
216 self.sut.connection = mock.Mock()
217 self.sut.connection.run = mock.Mock()
219 self.sut._traffic_runner(mock_traffic_profile, q)
220 self.sut.connection.run.assert_called_with(
221 "ping -s 64 152.16.100.20",
222 stdout=q, keep_stdin_open=True, pty=True)
224 def test_scale(self):
225 with mock.patch("yardstick.ssh.SSH") as ssh:
226 ssh_mock = mock.Mock(autospec=ssh.SSH)
228 mock.Mock(return_value=(0, "", ""))
230 mock.Mock(return_value=(0, "", ""))
231 ssh.from_node.return_value = ssh_mock
232 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
234 ping_traffic_gen = PingTrafficGen(vnfd)
235 self.assertRaises(NotImplementedError, ping_traffic_gen.scale, flavor)
237 def test_terminate(self):
238 with mock.patch("yardstick.ssh.SSH") as ssh:
239 ssh_mock = mock.Mock(autospec=ssh.SSH)
241 mock.Mock(return_value=(0, "", ""))
243 mock.Mock(return_value=(0, "", ""))
244 ssh.from_node.return_value = ssh_mock
245 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
246 ping_traffic_gen = PingTrafficGen(vnfd)
247 self.assertEqual(None, ping_traffic_gen.terminate())