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
23 from tests.unit.network_services.vnf_generic.vnf.test_base import mock_ssh
24 from tests.unit import STL_MOCKS
29 STLClient = mock.MagicMock()
30 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
34 from yardstick.network_services.vnf_generic.vnf.tg_trex import \
35 TrexTrafficGen, TrexResourceHelper
36 from yardstick.network_services.traffic_profile.base import TrafficProfile
39 class TestTrexTrafficGen(unittest.TestCase):
40 VNFD = {'vnfd:vnfd-catalog':
42 [{'short-name': 'VpeVnf',
45 [{'network': '152.16.100.20',
46 'netmask': '255.255.255.0',
47 'gateway': '152.16.100.20',
49 {'network': '152.16.40.20',
50 'netmask': '255.255.255.0',
51 'gateway': '152.16.40.20',
53 'description': 'VPE approximation using DPDK',
54 'name': 'vpevnf-baremetal',
56 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
58 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
60 {'network': '0064:ff9b:0:0:0:0:9810:2814',
62 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
64 'id': 'vpevnf-baremetal',
66 [{'virtual-interface':
67 {'dst_mac': '00:00:00:00:00:04',
68 'vpci': '0000:05:00.0',
69 'local_ip': '152.16.100.19',
70 'type': 'PCI-PASSTHROUGH',
71 'netmask': '255.255.255.0',
73 'bandwidth': '10 Gbps',
75 'dst_ip': '152.16.100.20',
76 'local_iface_name': 'xe0',
77 'local_mac': '00:00:00:00:00:02'},
78 'vnfd-connection-point-ref': 'xe0',
81 {'dst_mac': '00:00:00:00:00:03',
82 'vpci': '0000:05:00.1',
83 'local_ip': '152.16.40.19',
84 'type': 'PCI-PASSTHROUGH',
86 'netmask': '255.255.255.0',
88 'bandwidth': '10 Gbps',
89 'dst_ip': '152.16.40.20',
90 'local_iface_name': 'xe1',
91 'local_mac': '00:00:00:00:00:01'},
92 'vnfd-connection-point-ref': 'xe1',
94 'description': 'Vpe approximation using DPDK',
96 {'vdu-id': 'vpevnf-baremetal',
102 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
103 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
104 {'type': 'VPORT', 'name': 'xe1'}],
105 'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
108 "schema": "isb:traffic_profile:0.1",
110 "description": "Fixed traffic profile to run UDP traffic",
112 "traffic_type": "FixedTraffic",
113 "frame_rate": 100, # pps
117 @mock.patch("yardstick.ssh.SSH")
118 def test___init__(self, ssh):
120 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
121 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
122 self.assertIsInstance(trex_traffic_gen.resource_helper, TrexResourceHelper)
124 @mock.patch("yardstick.ssh.SSH")
125 def test_collect_kpi(self, ssh):
127 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
128 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
129 trex_traffic_gen.resource_helper._queue.put({})
130 result = trex_traffic_gen.collect_kpi()
131 self.assertEqual({}, result)
133 @mock.patch("yardstick.ssh.SSH")
134 def test_listen_traffic(self, ssh):
136 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
137 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
138 self.assertIsNone(trex_traffic_gen.listen_traffic({}))
140 @mock.patch("yardstick.ssh.SSH")
141 def test_instantiate(self, ssh):
144 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
145 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
146 trex_traffic_gen._start_server = mock.Mock(return_value=0)
147 trex_traffic_gen._tg_process = mock.MagicMock()
148 trex_traffic_gen._tg_process.start = mock.Mock()
149 trex_traffic_gen._tg_process.exitcode = 0
150 trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=1)
151 trex_traffic_gen.ssh_helper = mock.MagicMock()
152 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
153 self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
155 @mock.patch("yardstick.ssh.SSH")
156 def test_instantiate_error(self, ssh):
157 mock_ssh(ssh, exec_result=(1, "", ""))
159 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
160 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
161 trex_traffic_gen._start_server = mock.Mock(return_value=0)
162 trex_traffic_gen._tg_process = mock.MagicMock()
163 trex_traffic_gen._tg_process.start = mock.Mock()
164 trex_traffic_gen._tg_process._is_alive = mock.Mock(return_value=0)
165 trex_traffic_gen.ssh_helper = mock.MagicMock()
166 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
167 self.assertIsNone(trex_traffic_gen.instantiate({}, {}))
169 @mock.patch("yardstick.ssh.SSH")
170 def test__start_server(self, ssh):
172 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
173 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
174 trex_traffic_gen.ssh_helper = mock.MagicMock()
175 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
176 self.assertIsNone(trex_traffic_gen._start_server())
178 @mock.patch("yardstick.ssh.SSH")
179 def test__traffic_runner(self, ssh):
182 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
183 mock_traffic_profile.get_traffic_definition.return_value = "64"
184 mock_traffic_profile.execute.return_value = "64"
185 mock_traffic_profile.params = self.TRAFFIC_PROFILE
187 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
188 self.sut = TrexTrafficGen(NAME, vnfd)
189 self.sut.ssh_helper = mock.Mock()
190 self.sut.ssh_helper.run = mock.Mock()
191 self.sut._vpci_ascending = ["0000:05:00.0", "0000:05:00.1"]
192 self.sut._connect_client = mock.Mock(autospec=STLClient)
193 self.sut._connect_client.get_stats = mock.Mock(return_value="0")
194 self.sut.resource_helper.RUN_DURATION = 0
195 self.sut.resource_helper.QUEUE_WAIT_TIME = 0
196 self.sut._traffic_runner(mock_traffic_profile)
198 @mock.patch("yardstick.ssh.SSH")
199 def test__generate_trex_cfg(self, ssh):
201 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
202 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
203 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
204 self.assertIsNone(trex_traffic_gen.resource_helper.generate_cfg())
206 @mock.patch("yardstick.ssh.SSH")
207 def test_run_traffic(self, ssh):
210 mock_traffic_profile = mock.Mock(autospec=TrafficProfile)
211 mock_traffic_profile.get_traffic_definition.return_value = "64"
212 mock_traffic_profile.params = self.TRAFFIC_PROFILE
214 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
215 self.sut = TrexTrafficGen(NAME, vnfd)
216 self.sut.ssh_helper = mock.Mock()
217 self.sut.ssh_helper.run = mock.Mock()
218 self.sut._traffic_runner = mock.Mock(return_value=0)
219 self.sut.resource_helper.client_started.value = 1
220 result = self.sut.run_traffic(mock_traffic_profile)
221 self.sut._traffic_process.terminate()
222 self.assertIsNotNone(result)
224 @mock.patch("yardstick.ssh.SSH")
225 def test_scale(self, ssh):
226 mock_ssh(ssh, exec_result=(1, "", ""))
227 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
228 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
229 trex_traffic_gen.scale('')
231 @mock.patch("yardstick.ssh.SSH")
232 def test_setup_vnf_environment(self, ssh):
233 mock_ssh(ssh, exec_result=(1, "", ""))
234 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
235 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
236 self.assertIsNone(trex_traffic_gen.setup_helper.setup_vnf_environment())
238 @mock.patch("yardstick.ssh.SSH")
239 def test_terminate(self, ssh):
241 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
242 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
243 trex_traffic_gen.ssh_helper = mock.MagicMock()
244 trex_traffic_gen.resource_helper.ssh_helper = mock.MagicMock()
245 self.assertIsNone(trex_traffic_gen.terminate())
247 @mock.patch("yardstick.ssh.SSH")
248 def test__connect_client(self, ssh):
250 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
251 trex_traffic_gen = TrexTrafficGen(NAME, vnfd)
252 client = mock.Mock(autospec=STLClient)
253 client.connect = mock.Mock(return_value=0)
254 self.assertIsNotNone(trex_traffic_gen.resource_helper._connect(client))
256 if __name__ == '__main__':