1 # Copyright (c) 2016-2017 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.
20 from yardstick import ssh
21 from yardstick.benchmark.contexts import base as ctx_base
22 from yardstick.common import utils
23 from yardstick.network_services.vnf_generic.vnf import tg_ixload
24 from yardstick.network_services.traffic_profile import base as tp_base
25 from yardstick.tests.unit import base as ut_base
31 class TestIxLoadTrafficGen(ut_base.BaseUnitTestCase):
32 VNFD = {'vnfd:vnfd-catalog':
34 [{'short-name': 'VpeVnf',
37 [{'network': '152.16.100.20',
38 'netmask': '255.255.255.0',
39 'gateway': '152.16.100.20',
41 {'network': '152.16.40.20',
42 'netmask': '255.255.255.0',
43 'gateway': '152.16.40.20',
45 'description': 'VPE approximation using DPDK',
46 'name': 'vpevnf-baremetal',
48 [{'network': '0064:ff9b:0:0:0:0:9810:6414',
50 'gateway': '0064:ff9b:0:0:0:0:9810:6414',
52 {'network': '0064:ff9b:0:0:0:0:9810:2814',
54 'gateway': '0064:ff9b:0:0:0:0:9810:2814',
56 'id': 'vpevnf-baremetal',
58 [{'virtual-interface':
59 {'dst_mac': '00:00:00:00:00:04',
60 'vpci': '0000:05:00.0',
61 'local_ip': '152.16.100.19',
62 'type': 'PCI-PASSTHROUGH',
63 'netmask': '255.255.255.0',
65 'bandwidth': '10 Gbps',
67 'dst_ip': '152.16.100.20',
68 'local_iface_name': 'xe0',
69 'local_mac': '00:00:00:00:00:02'},
70 'vnfd-connection-point-ref': 'xe0',
73 {'dst_mac': '00:00:00:00:00:03',
74 'vpci': '0000:05:00.1',
75 'local_ip': '152.16.40.19',
76 'type': 'PCI-PASSTHROUGH',
78 'netmask': '255.255.255.0',
80 'bandwidth': '10 Gbps',
81 'dst_ip': '152.16.40.20',
82 'local_iface_name': 'xe1',
83 'local_mac': '00:00:00:00:00:01'},
84 'vnfd-connection-point-ref': 'xe1',
86 'description': 'Vpe approximation using DPDK',
88 {'vdu-id': 'vpevnf-baremetal',
94 {'kpi': ['packets_in', 'packets_fwd', 'packets_dropped']},
95 'connection-point': [{'type': 'VPORT', 'name': 'xe0'},
96 {'type': 'VPORT', 'name': 'xe1'}],
97 'id': 'VpeApproxVnf', 'name': 'VPEVnfSsh'}]}}
100 "schema": "isb:traffic_profile:0.1",
102 "description": "Fixed traffic profile to run UDP traffic",
104 "traffic_type": "FixedTraffic",
105 "frame_rate": 100, # pps
110 self._mock_call = mock.patch.object(subprocess, 'call')
111 self.mock_call = self._mock_call.start()
112 self._mock_open = mock.patch.object(tg_ixload, 'open')
113 self.mock_open = self._mock_open.start()
114 self._mock_ssh = mock.patch.object(ssh, 'SSH')
115 self.mock_ssh = self._mock_ssh.start()
116 ssh_obj_mock = mock.Mock(autospec=ssh.SSH)
117 ssh_obj_mock.execute = mock.Mock(return_value=(0, '', ''))
118 ssh_obj_mock.run = mock.Mock(return_value=(0, '', ''))
119 self.mock_ssh.from_node.return_value = ssh_obj_mock
120 self.addCleanup(self._stop_mock)
122 def _stop_mock(self):
123 self._mock_call.stop()
124 self._mock_open.stop()
125 self._mock_ssh.stop()
127 def test___init__(self):
128 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
129 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
130 self.assertIsNone(ixload_traffic_gen.resource_helper.data)
132 @mock.patch.object(ctx_base.Context, 'get_physical_node_from_server',
133 return_value='mock_node')
134 def test_collect_kpi(self, *args):
135 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
136 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
137 ixload_traffic_gen.scenario_helper.scenario_cfg = {
138 'nodes': {ixload_traffic_gen.name: "mock"}
140 ixload_traffic_gen.data = {}
141 result = ixload_traffic_gen.collect_kpi()
144 'physical_node': 'mock_node',
146 self.assertEqual(expected, result)
148 def test_listen_traffic(self):
149 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
150 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
151 self.assertIsNone(ixload_traffic_gen.listen_traffic({}))
153 @mock.patch.object(utils, 'find_relative_file')
154 @mock.patch.object(utils, 'makedirs')
155 @mock.patch.object(ctx_base.Context, 'get_context_from_server')
156 @mock.patch.object(tg_ixload, 'shutil')
157 def test_instantiate(self, mock_shutil, *args):
158 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
159 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
160 scenario_cfg = {'tc': "nsb_test_case",
161 'ixia_profile': "ixload.cfg",
162 'task_path': "/path/to/task"}
163 ixload_traffic_gen.RESULTS_MOUNT = "/tmp/result"
164 mock_shutil.copy = mock.Mock()
167 {'packetsize': 64, 'traffic_type': 4,
168 'rfc2544': {'allowed_drop_rate': '0.8 - 1'},
169 'vnf__1': {'rules': 'acl_1rule.yaml',
170 'vnf_config': {'lb_config': 'SW',
174 'worker_threads': 1}}
178 scenario_cfg.update({'nodes': {ixload_traffic_gen.name: "mock"}})
179 with mock.patch.object(six.moves.builtins, 'open',
180 create=True) as mock_open:
181 mock_open.return_value = mock.MagicMock()
182 ixload_traffic_gen.instantiate(scenario_cfg, {})
184 @mock.patch.object(tg_ixload, 'open')
185 @mock.patch.object(tg_ixload, 'min')
186 @mock.patch.object(tg_ixload, 'max')
187 @mock.patch.object(tg_ixload, 'len')
188 @mock.patch.object(tg_ixload, 'shutil')
189 def test_run_traffic(self, *args):
190 mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
191 mock_traffic_profile.get_traffic_definition.return_value = '64'
192 mock_traffic_profile.params = self.TRAFFIC_PROFILE
193 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
194 vnfd['mgmt-interface'].update({'tg-config': {}})
195 vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
196 vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
197 sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
198 sut.connection = mock.Mock()
199 sut._traffic_runner = mock.Mock(return_value=0)
200 result = sut.run_traffic(mock_traffic_profile)
201 self.assertIsNone(result)
203 @mock.patch.object(tg_ixload, 'open')
204 @mock.patch.object(tg_ixload, 'min')
205 @mock.patch.object(tg_ixload, 'max')
206 @mock.patch.object(tg_ixload, 'len')
207 @mock.patch.object(tg_ixload, 'shutil')
208 def test_run_traffic_csv(self, *args):
209 mock_traffic_profile = mock.Mock(autospec=tp_base.TrafficProfile)
210 mock_traffic_profile.get_traffic_definition.return_value = '64'
211 mock_traffic_profile.params = self.TRAFFIC_PROFILE
212 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
213 vnfd['mgmt-interface'].update({'tg-config': {}})
214 vnfd['mgmt-interface']['tg-config'].update({'ixchassis': '1.1.1.1'})
215 vnfd['mgmt-interface']['tg-config'].update({'py_bin_path': '/root'})
216 sut = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
217 sut.connection = mock.Mock()
218 sut._traffic_runner = mock.Mock(return_value=0)
219 subprocess.call(['touch', '/tmp/1.csv'])
220 sut.rel_bin_path = mock.Mock(return_value='/tmp/*.csv')
221 result = sut.run_traffic(mock_traffic_profile)
222 self.assertIsNone(result)
224 def test_terminate(self):
225 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
226 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
227 self.assertIsNone(ixload_traffic_gen.terminate())
229 def test_parse_csv_read(self):
230 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
232 'HTTP Total Throughput (Kbps)': 1,
233 'HTTP Simulated Users': 2,
234 'HTTP Concurrent Connections': '3',
235 'HTTP Connection Rate': 4.3,
236 'HTTP Transaction Rate': True,
238 http_reader = [kpi_data]
239 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
240 result = ixload_traffic_gen.resource_helper.result
241 ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
242 for k_left, k_right in tg_ixload.IxLoadResourceHelper.KPI_LIST.items():
243 self.assertEqual(result[k_left][-1], int(kpi_data[k_right]))
245 def test_parse_csv_read_value_error(self):
246 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
248 'HTTP Total Throughput (Kbps)': 1,
249 'HTTP Simulated Users': 2,
250 'HTTP Concurrent Connections': "not a number",
251 'HTTP Connection Rate': 4,
252 'HTTP Transaction Rate': 5,
254 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
255 init_value = ixload_traffic_gen.resource_helper.result
256 ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)
257 self.assertDictEqual(ixload_traffic_gen.resource_helper.result,
260 def test_parse_csv_read_error(self,):
261 vnfd = self.VNFD['vnfd:vnfd-catalog']['vnfd'][0]
263 'HTTP Total Throughput (Kbps)': 1,
264 'HTTP Simulated Users': 2,
265 'HTTP Concurrent Connections': 3,
266 'HTTP Transaction Rate': 5,
268 ixload_traffic_gen = tg_ixload.IxLoadTrafficGen(NAME, vnfd, 'task_id')
270 with self.assertRaises(KeyError):
271 ixload_traffic_gen.resource_helper.parse_csv_read(http_reader)