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 tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
28 'stl': mock.MagicMock(),
29 'stl.trex_stl_lib': mock.MagicMock(),
30 'stl.trex_stl_lib.base64': mock.MagicMock(),
31 'stl.trex_stl_lib.binascii': mock.MagicMock(),
32 'stl.trex_stl_lib.collections': mock.MagicMock(),
33 'stl.trex_stl_lib.copy': mock.MagicMock(),
34 'stl.trex_stl_lib.datetime': mock.MagicMock(),
35 'stl.trex_stl_lib.functools': mock.MagicMock(),
36 'stl.trex_stl_lib.imp': mock.MagicMock(),
37 'stl.trex_stl_lib.inspect': mock.MagicMock(),
38 'stl.trex_stl_lib.json': mock.MagicMock(),
39 'stl.trex_stl_lib.linecache': mock.MagicMock(),
40 'stl.trex_stl_lib.math': mock.MagicMock(),
41 'stl.trex_stl_lib.os': mock.MagicMock(),
42 'stl.trex_stl_lib.platform': mock.MagicMock(),
43 'stl.trex_stl_lib.pprint': mock.MagicMock(),
44 'stl.trex_stl_lib.random': mock.MagicMock(),
45 'stl.trex_stl_lib.re': mock.MagicMock(),
46 'stl.trex_stl_lib.scapy': mock.MagicMock(),
47 'stl.trex_stl_lib.socket': mock.MagicMock(),
48 'stl.trex_stl_lib.string': mock.MagicMock(),
49 'stl.trex_stl_lib.struct': mock.MagicMock(),
50 'stl.trex_stl_lib.sys': mock.MagicMock(),
51 'stl.trex_stl_lib.threading': mock.MagicMock(),
52 'stl.trex_stl_lib.time': mock.MagicMock(),
53 'stl.trex_stl_lib.traceback': mock.MagicMock(),
54 'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
55 'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
56 'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
57 'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
58 'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
59 'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
60 'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
61 'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
62 'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
63 'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
64 'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
65 'stl.trex_stl_lib.types': mock.MagicMock(),
66 'stl.trex_stl_lib.utils': mock.MagicMock(),
67 'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
68 'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
69 'stl.trex_stl_lib.utils.common': mock.MagicMock(),
70 'stl.trex_stl_lib.utils.json': mock.MagicMock(),
71 'stl.trex_stl_lib.utils.os': mock.MagicMock(),
72 'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
73 'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
74 'stl.trex_stl_lib.utils.random': mock.MagicMock(),
75 'stl.trex_stl_lib.utils.re': mock.MagicMock(),
76 'stl.trex_stl_lib.utils.string': mock.MagicMock(),
77 'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
78 'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
79 'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
80 'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
81 'stl.trex_stl_lib.warnings': mock.MagicMock(),
82 'stl.trex_stl_lib.yaml': mock.MagicMock(),
83 'stl.trex_stl_lib.zlib': mock.MagicMock(),
84 'stl.trex_stl_lib.zmq': mock.MagicMock(),
87 STLClient = mock.MagicMock()
88 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
92 from yardstick.network_services.vnf_generic.vnf.tg_trex import \
93 TrexTrafficGen, TrexResourceHelper
94 from yardstick.network_services.traffic_profile.base import TrafficProfile
97 class TestTrexTrafficGen(unittest.TestCase):
98 VNFD = {'vnfd:vnfd-catalog':
100 [{'short-name': 'VpeVnf',
103 [{'network': '152.16.100.20',
104 'netmask': '255.255.255.0',
105 'gateway': '152.16.100.20',
107 {'network': '152.16.40.20',
108 'netmask': '255.255.255.0',
109 'gateway': '152.16.40.20',
111 'description': 'VPE approximation using DPDK',
112 'name': 'vpevnf-baremetal',
114 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
116 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
118 {'network': '0064:ff9b:0:0:0:0:9810:2814',
120 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
122 'id': 'vpevnf-baremetal',
123 'external-interface':
124 [{'virtual-interface':
125 {'dst_mac': '00:00:00:00:00:04',
126 'vpci': '0000:05:00.0',
127 'local_ip': '152.16.100.19',
128 'type': 'PCI-PASSTHROUGH',
129 'netmask': '255.255.255.0',
130 'dpdk_port_num': '0',
131 'bandwidth': '10 Gbps',
133 'dst_ip': '152.16.100.20',
134 'local_iface_name': 'xe0',
135 'local_mac': '00:00:00:00:00:02'},
136 'vnfd-connection-point-ref': 'xe0',
138 {'virtual-interface':
139 {'dst_mac': '00:00:00:00:00:03',
140 'vpci': '0000:05:00.1',
141 'local_ip': '152.16.40.19',
142 'type': 'PCI-PASSTHROUGH',
144 'netmask': '255.255.255.0',
145 'dpdk_port_num': '1',
146 'bandwidth': '10 Gbps',
147 'dst_ip': '152.16.40.20',
148 'local_iface_name': 'xe1',
149 'local_mac': '00:00:00:00:00:01'},
150 'vnfd-connection-point-ref': 'xe1',
152 'description': 'Vpe approximation using DPDK',
154 {'vdu-id': 'vpevnf-baremetal',
160 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
161 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
162 {'type': 'VPORT', 'name': 'xe1'}],
163 'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
166 "schema": "isb:traffic_profile:0.1",
168 "description": "Fixed traffic profile to run UDP traffic",
170 "traffic_type": "FixedTraffic",
171 "frame_rate": 100, # pps
175 @mock.patch("yardstick.ssh.SSH")
176 def test___init__(self, ssh):
178 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
179 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
180 self.assertIsInstance(trex_traffic_gen.resource_helper, TrexResourceHelper)
182 @mock.patch("yardstick.ssh.SSH")
183 def test_collect_kpi(self, ssh):
185 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
186 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
187 trex_traffic_gen.resource_helper._queue.put({})
188 result = trex_traffic_gen.collect_kpi()
189 self.assertEqual({}, result)
191 @mock.patch("yardstick.ssh.SSH")
192 def test_listen_traffic(self, ssh):
194 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
195 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
196 self.assertIsNone(trex_traffic_gen.listen_traffic({}))
198 @mock.patch("yardstick.ssh.SSH")
199 def test_instantiate(self, ssh):
202 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
203 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
204 trex_traffic_gen._start_server = mock.Mock(return_value=0)
205 trex_traffic_gen._tg_process = mock.MagicMock()
206 trex_traffic_gen._tg_process.start = mock.Mock()
207 trex_traffic_gen._tg_process.exitcode = 0
208 trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=1)
209 trex_traffic_gen.ssh_helper = mock.MagicMock()
210 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
211 self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
213 @mock.patch("yardstick.ssh.SSH")
214 def test_instantiate_error(self, ssh):
215 mock_ssh(ssh, exec_result=(1, "", ""))
217 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
218 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
219 trex_traffic_gen._start_server = mock.Mock(return_value=0)
220 trex_traffic_gen._tg_process = mock.MagicMock()
221 trex_traffic_gen._tg_process.start = mock.Mock()
222 trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=0)
223 trex_traffic_gen.ssh_helper = mock.MagicMock()
224 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
225 self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
227 @mock.patch("yardstick.ssh.SSH")
228 def test__start_server(self, ssh):
230 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
231 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
232 trex_traffic_gen.ssh_helper = mock.MagicMock()
233 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
234 self.assertIsNone(trex_traffic_gen._start_server())
236 @mock.patch("yardstick.ssh.SSH")
237 def test__traffic_runner(self, ssh):
240 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
241 mock_traffic_profile.get_traffic_definition.return_value = "64"
242 mock_traffic_profile.execute.return_value = "64"
243 mock_traffic_profile.params = self.TRAFFIC_PROFILE
245 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
246 self.sut = TrexTrafficGen(NAME, vnfd)
247 self.sut.ssh_helper = mock.Mock()
248 self.sut.ssh_helper.run = mock.Mock()
249 self.sut._vpci_ascending = ["0000:05:00.0", "0000:05:00.1"]
250 self.sut._connect_client = mock.Mock(autospec=STLClient)
251 self.sut._connect_client.get_stats = mock.Mock(return_value="0")
252 self.sut.resource_helper.RUN_DURATION = 0
253 self.sut.resource_helper.QUEUE_WAIT_TIME = 0
254 self.sut._traffic_runner(mock_traffic_profile)
256 @mock.patch("yardstick.ssh.SSH")
257 def test__generate_trex_cfg(self, ssh):
259 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
260 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
261 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
262 self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
264 @mock.patch("yardstick.ssh.SSH")
265 def test_run_traffic(self, ssh):
268 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
269 mock_traffic_profile.get_traffic_definition.return_value = "64"
270 mock_traffic_profile.params = self.TRAFFIC_PROFILE
272 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
273 self.sut = TrexTrafficGen(NAME, vnfd)
274 self.sut.ssh_helper = mock.Mock()
275 self.sut.ssh_helper.run = mock.Mock()
276 self.sut._traffic_runner = mock.Mock(return_value=0)
277 self.sut.resource_helper.client_started.value = 1
278 result = self.sut.run_traffic(mock_traffic_profile)
279 self.sut._traffic_process.terminate()
280 self.assertIsNotNone(result)
282 @mock.patch("yardstick.ssh.SSH")
283 def test_scale(self, ssh):
284 mock_ssh(ssh, exec_result=(1, "", ""))
285 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
286 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
287 trex_traffic_gen.scale('')
289 @mock.patch("yardstick.ssh.SSH")
290 def test_setup_vnf_environment(self, ssh):
291 mock_ssh(ssh, exec_result=(1, "", ""))
292 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
293 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
294 self.assertIsNone(trex_traffic_gen.setup_helper.setup_vnf_environment())
296 @mock.patch("yardstick.ssh.SSH")
297 def test_terminate(self, ssh):
299 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
300 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
301 trex_traffic_gen.ssh_helper = mock.MagicMock()
302 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
303 self.assertIsNone(trex_traffic_gen.terminate())
305 @mock.patch("yardstick.ssh.SSH")
306 def test__connect_client(self, ssh):
308 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
309 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
310 client = mock.Mock(autospec=STLClient)
311 client.connect = mock.Mock(return_value=0)
312 self.assertIsNotNone(trex_traffic_gen.resource_helper._connect(client))
314 if __name__ == '__main__':