1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
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.
15 from __future__ import absolute_import
19 import experimental_framework.constants.conf_file_sections as cfs
20 import experimental_framework.common as common
21 import experimental_framework.benchmarks.\
22 instantiation_validation_benchmark as iv_module
23 from experimental_framework.benchmarks.\
24 instantiation_validation_benchmark import InstantiationValidationBenchmark
25 from six.moves import map
26 from six.moves import range
30 command_counter = [0, 0, 0, 0, 0]
31 replace_counter = [0, 0, 0]
34 def dummy_os_kill(pid, signal, get_counters=None):
46 def dummy_run_command(command, get_counters=None):
48 return command_counter
49 if command == 'sudo smcroute -k':
50 command_counter[0] += 1
52 elif command == 'sudo ip link delete interface.100':
53 command_counter[1] += 1
55 elif command == 'sudo kill 1234':
58 elif command == 'sudo kill 4321':
61 raise Exception(command)
64 def dummy_run_command_2(command, get_counters=None):
66 return command_counter
67 if command == 'sudo ip link add link interface name interface.' \
68 '100 type vlan id 100':
69 command_counter[0] += 1
71 elif command == 'sudo ifconfig interface.100 10.254.254.254 up' \
72 ' netmask 255.255.255.248':
73 command_counter[1] += 1
75 elif command == "sudo echo 'mgroup from interface.100 group" \
76 " 224.192.16.1' > /etc/smcroute.conf":
77 command_counter[2] += 1
79 elif command == "sudo smcroute -d":
80 command_counter[3] += 1
82 elif command == "sudo test_sniff interface.100 128 &":
83 command_counter[4] += 1
87 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
89 return replace_counter
91 if str_from == 'local out_file = "result_file"':
92 if str_to == 'local out_file = ""':
93 replace_counter[0] += 1
95 if str_from == 'local traffic_rate = 100':
96 if str_to == 'local traffic_rate = 0':
97 replace_counter[1] += 1
99 if str_from == 'local traffic_delay = 60':
100 if str_to == 'local traffic_delay = 0':
101 replace_counter[2] += 1
103 if str_from == 'local out_file = ""':
104 if str_to == 'local out_file = "result_file"':
105 replace_counter[3] += 1
107 if str_from == 'local traffic_rate = 0':
108 if str_to == 'local traffic_rate = 100':
109 replace_counter[4] += 1
111 if str_from == 'local traffic_delay = 0':
112 if str_to == 'local traffic_delay = 60':
113 replace_counter[5] += 1
115 raise Exception(file + ' ' + str_from + ' ' + str_to)
118 class DummyDpdkPacketGenerator():
123 DummyDpdkPacketGenerator.counter = [0, 0]
125 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
126 pcap_file_1, vlan_0, vlan_1):
127 if dpdk_interfaces == 1:
128 if lua_script == 'constant_traffic.lua':
129 if pcap_file_0 == 'packet_512.pcap':
130 if pcap_file_1 == 'igmp.pcap':
133 DummyDpdkPacketGenerator.counter[0] += 1
135 def send_traffic(self):
136 DummyDpdkPacketGenerator.counter[1] += 1
140 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
142 counter = [0, 0, 0, 0, 0]
144 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
145 DummyInstantiaionValidationBenchmark.counter[0] += 1
147 def _init_packet_checker(self):
148 DummyInstantiaionValidationBenchmark.counter[1] += 1
150 def _finalize_packet_checker(self):
151 DummyInstantiaionValidationBenchmark.counter[2] += 1
153 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
154 if traffic_rate_percentage == '1' and traffic_time == '10':
155 DummyInstantiaionValidationBenchmark.counter[3] += 1
157 def _get_results(self):
158 DummyInstantiaionValidationBenchmark.counter[4] += 1
159 res = {'test': 'result'}
163 class InstantiationValidationInitTest(unittest.TestCase):
166 common.BASE_DIR = os.getcwd()
167 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
171 common.BASE_DIR = None
173 @mock.patch('experimental_framework.common.get_base_dir')
174 def test___init___for_success(self, mock_base_dir):
175 mock_base_dir.return_value = 'base_dir/'
176 iv = InstantiationValidationBenchmark('InstantiationValidation',
178 self.assertEqual(iv.base_dir,
179 'base_dir/experimental_framework/'
180 'packet_generators/dpdk_pktgen/')
181 self.assertEqual(iv.results_file,
182 'base_dir/experimental_framework/'
183 'packet_generators/dpdk_pktgen/packets.res')
184 self.assertEqual(iv.lua_file,
185 'base_dir/experimental_framework/'
186 'packet_generators/dpdk_pktgen/constant_traffic.lua')
187 self.assertEqual(iv.pkt_checker_command,
188 'base_dir/experimental_framework/'
189 'libraries/packet_checker/test_sniff ')
190 self.assertEqual(iv.res_dir, '')
191 self.assertEqual(iv.interface_name, '')
193 def test_init_for_success(self):
196 def test_finalize_for_success(self):
199 def test_get_features_for_success(self):
202 expected['description'] = 'Instantiation Validation Benchmark'
203 expected['parameters'] = [
204 iv_module.THROUGHPUT,
205 iv_module.VLAN_SENDER,
206 iv_module.VLAN_RECEIVER
208 expected['allowed_values'] = dict()
209 expected['allowed_values'][iv_module.THROUGHPUT] = \
210 list(map(str, list(range(0, 100))))
211 expected['allowed_values'][iv_module.VLAN_SENDER] = \
212 list(map(str, list(range(-1, 4096))))
213 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
214 list(map(str, list(range(-1, 4096))))
215 expected['default_values'] = dict()
216 expected['default_values'][iv_module.THROUGHPUT] = '1'
217 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
218 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
219 output = self.iv.get_features()
220 self.assertEqual(expected, output)
222 @mock.patch('subprocess.check_output')
223 def test__get_pids_for_success(self, mock_getoutput):
225 mock_getoutput.return_value = '1234'
226 output = self.iv._get_pids()
227 self.assertEqual(expected, output)
229 expected = [1234, 4321]
230 mock_getoutput.return_value = '1234\n4321'
231 output = self.iv._get_pids()
232 self.assertEqual(expected, output)
235 mock_getoutput.return_value = None
236 output = self.iv._get_pids()
237 self.assertEqual(expected, output)
239 @mock.patch('experimental_framework.common.run_command',
240 side_effect=dummy_run_command)
241 @mock.patch('os.kill', side_effect=dummy_os_kill)
242 @mock.patch('experimental_framework.benchmarks.'
243 'instantiation_validation_benchmark.'
244 'InstantiationValidationBenchmark._get_pids')
245 def test__finalize_packet_checker_for_success(self,
249 global command_counter
251 command_counter = [0, 0, 0, 0, 0]
252 kill_counter = [0, 0]
253 mock_pids.return_value = [1234, 4321]
254 self.iv.interface_name = 'interface'
255 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
256 self.iv._finalize_packet_checker()
257 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
258 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
260 @mock.patch('os.chdir')
261 @mock.patch('experimental_framework.common.run_command',
262 side_effect=dummy_run_command_2)
263 @mock.patch('experimental_framework.benchmarks.'
264 'instantiation_validation_benchmark.'
265 'InstantiationValidationBenchmark._get_pids')
266 @mock.patch('os.kill', side_effect=dummy_os_kill)
267 def test__init_packet_checker_for_success(self, mock_kill, mock_pids,
268 mock_run_command, mock_chdir):
269 global command_counter
270 command_counter = [0, 0, 0, 0, 0]
271 mock_pids.return_value = [1234, 4321]
272 self.iv.pkt_checker_command = 'test_sniff '
273 self.iv.interface_name = 'interface'
274 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
275 self.iv._init_packet_checker()
276 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
278 @mock.patch('experimental_framework.common.get_file_first_line')
279 def test__get_results_for_success(self, mock_get_file):
280 self.iv.res_dir = 'directory'
281 mock_get_file.side_effect = ['100', '50']
282 expected = {'failure': '0'}
283 output = self.iv._get_results()
284 self.assertEqual(expected, output)
286 mock_get_file.side_effect = ['10', '50']
287 expected = {'failure': '1'}
288 output = self.iv._get_results()
289 self.assertEqual(expected, output)
291 @mock.patch('experimental_framework.common.replace_in_file',
292 side_effect=dummy_replace_in_file)
293 def test__reset_lua_file_for_success(self, mock_replace):
294 global replace_counter
295 replace_counter = [0, 0, 0, 0, 0, 0]
296 traffic_rate_percentage = '100'
298 self.iv.lua_file = 'file'
299 self.iv.results_file = 'result_file'
300 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
301 self.assertEqual(dummy_replace_in_file('', '', '', True),
304 @mock.patch('experimental_framework.common.replace_in_file',
305 side_effect=dummy_replace_in_file)
306 def test__configure_lua_file_for_success(self, mock_replace):
307 global replace_counter
308 replace_counter = [0, 0, 0, 0, 0, 0]
309 traffic_rate_percentage = '100'
311 self.iv.lua_file = 'file'
312 self.iv.results_file = 'result_file'
313 self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
314 self.assertEqual(dummy_replace_in_file('', '', '', True),
317 @mock.patch('experimental_framework.common.LOG')
318 @mock.patch('experimental_framework.packet_generators.'
319 'dpdk_packet_generator.DpdkPacketGenerator',
320 side_effect=DummyDpdkPacketGenerator)
321 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
322 def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
325 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
326 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
327 mock_common_get_vars.return_value = rval
328 expected = {'test': 'result'}
329 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
331 iv.params[iv_module.THROUGHPUT] = '1'
333 self.assertEqual(expected, output)
334 self.assertEqual(DummyDpdkPacketGenerator.counter,
336 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,