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.
18 import experimental_framework.constants.conf_file_sections as cfs
19 import experimental_framework.common as common
20 import experimental_framework.benchmarks.\
21 instantiation_validation_benchmark as iv_module
22 from experimental_framework.benchmarks.\
23 instantiation_validation_benchmark import InstantiationValidationBenchmark
27 command_counter = [0, 0, 0, 0, 0]
28 replace_counter = [0, 0, 0]
31 def dummy_os_kill(pid, signal, get_counters=None):
43 def dummy_run_command(command, get_counters=None):
45 return command_counter
46 if command == 'sudo smcroute -k':
47 command_counter[0] += 1
49 elif command == 'sudo ip link delete interface.100':
50 command_counter[1] += 1
52 elif command == 'sudo kill 1234':
55 elif command == 'sudo kill 4321':
58 raise Exception(command)
61 def dummy_run_command_2(command, get_counters=None):
63 return command_counter
64 if command == 'sudo ip link add link interface name interface.' \
65 '100 type vlan id 100':
66 command_counter[0] += 1
68 elif command == 'sudo ifconfig interface.100 10.254.254.254 up':
69 command_counter[1] += 1
71 elif command == "sudo echo 'mgroup from interface.100 group" \
72 " 224.192.16.1' > /etc/smcroute.conf":
73 command_counter[2] += 1
75 elif command == "sudo smcroute -d":
76 command_counter[3] += 1
78 elif command == "sudo test_sniff interface.100 128 &":
79 command_counter[4] += 1
83 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
85 return replace_counter
87 if str_from == 'local out_file = "result_file"':
88 if str_to == 'local out_file = ""':
89 replace_counter[0] += 1
91 if str_from == 'local traffic_rate = 100':
92 if str_to == 'local traffic_rate = 0':
93 replace_counter[1] += 1
95 if str_from == 'local traffic_delay = 60':
96 if str_to == 'local traffic_delay = 0':
97 replace_counter[2] += 1
99 if str_from == 'local out_file = ""':
100 if str_to == 'local out_file = "result_file"':
101 replace_counter[3] += 1
103 if str_from == 'local traffic_rate = 0':
104 if str_to == 'local traffic_rate = 100':
105 replace_counter[4] += 1
107 if str_from == 'local traffic_delay = 0':
108 if str_to == 'local traffic_delay = 60':
109 replace_counter[5] += 1
111 raise Exception(file + ' ' + str_from + ' ' + str_to)
114 class DummyDpdkPacketGenerator():
119 DummyDpdkPacketGenerator.counter = [0, 0]
121 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
122 pcap_file_1, vlan_0, vlan_1):
123 if dpdk_interfaces == 1:
124 if lua_script == 'constant_traffic.lua':
125 if pcap_file_0 == 'packet_512.pcap':
126 if pcap_file_1 == 'igmp.pcap':
129 DummyDpdkPacketGenerator.counter[0] += 1
131 def send_traffic(self):
132 DummyDpdkPacketGenerator.counter[1] += 1
136 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
138 counter = [0, 0, 0, 0, 0]
140 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
141 DummyInstantiaionValidationBenchmark.counter[0] += 1
143 def _init_packet_checker(self):
144 DummyInstantiaionValidationBenchmark.counter[1] += 1
146 def _finalize_packet_checker(self):
147 DummyInstantiaionValidationBenchmark.counter[2] += 1
149 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
150 if traffic_rate_percentage == '1' and traffic_time == '10':
151 DummyInstantiaionValidationBenchmark.counter[3] += 1
153 def _get_results(self):
154 DummyInstantiaionValidationBenchmark.counter[4] += 1
155 res = {'test': 'result'}
159 class InstantiationValidationInitTest(unittest.TestCase):
162 common.BASE_DIR = os.getcwd()
163 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
167 common.BASE_DIR = None
169 @mock.patch('experimental_framework.common.get_base_dir')
170 def test___init___for_success(self, mock_base_dir):
171 mock_base_dir.return_value = 'base_dir/'
172 iv = InstantiationValidationBenchmark('InstantiationValidation',
174 self.assertEqual(iv.base_dir,
175 'base_dir/experimental_framework/'
176 'packet_generators/dpdk_pktgen/')
177 self.assertEqual(iv.results_file,
178 'base_dir/experimental_framework/'
179 'packet_generators/dpdk_pktgen/packets.res')
180 self.assertEqual(iv.lua_file,
181 'base_dir/experimental_framework/'
182 'packet_generators/dpdk_pktgen/constant_traffic.lua')
183 self.assertEqual(iv.pkt_checker_command,
184 'base_dir/experimental_framework/'
185 'libraries/packet_checker/test_sniff ')
186 self.assertEqual(iv.res_dir, '')
187 self.assertEqual(iv.interface_name, '')
189 def test_init_for_success(self):
192 def test_finalize_for_success(self):
195 def test_get_features_for_success(self):
198 expected['description'] = 'Instantiation Validation Benchmark'
199 expected['parameters'] = [
200 iv_module.THROUGHPUT,
201 iv_module.VLAN_SENDER,
202 iv_module.VLAN_RECEIVER
204 expected['allowed_values'] = dict()
205 expected['allowed_values'][iv_module.THROUGHPUT] = \
206 map(str, range(0, 100))
207 expected['allowed_values'][iv_module.VLAN_SENDER] = \
208 map(str, range(-1, 4096))
209 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
210 map(str, range(-1, 4096))
211 expected['default_values'] = dict()
212 expected['default_values'][iv_module.THROUGHPUT] = '1'
213 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
214 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
215 output = self.iv.get_features()
216 self.assertEqual(expected, output)
218 @mock.patch('commands.getoutput')
219 def test__get_pids_for_success(self, mock_getoutput):
221 mock_getoutput.return_value = '1234'
222 output = self.iv._get_pids()
223 self.assertEqual(expected, output)
225 expected = [1234, 4321]
226 mock_getoutput.return_value = '1234\n4321'
227 output = self.iv._get_pids()
228 self.assertEqual(expected, output)
231 mock_getoutput.return_value = None
232 output = self.iv._get_pids()
233 self.assertEqual(expected, output)
235 @mock.patch('experimental_framework.common.run_command',
236 side_effect=dummy_run_command)
237 @mock.patch('os.kill', side_effect=dummy_os_kill)
238 @mock.patch('experimental_framework.benchmarks.'
239 'instantiation_validation_benchmark.'
240 'InstantiationValidationBenchmark._get_pids')
241 def test__finalize_packet_checker_for_success(self,
245 global command_counter
247 command_counter = [0, 0, 0, 0, 0]
248 kill_counter = [0, 0]
249 mock_pids.return_value = [1234, 4321]
250 self.iv.interface_name = 'interface'
251 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
252 self.iv._finalize_packet_checker()
253 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
254 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
256 @mock.patch('os.chdir')
257 @mock.patch('experimental_framework.common.run_command',
258 side_effect=dummy_run_command_2)
259 @mock.patch('experimental_framework.benchmarks.'
260 'instantiation_validation_benchmark.'
261 'InstantiationValidationBenchmark._get_pids')
262 @mock.patch('os.kill', side_effect=dummy_os_kill)
263 def test__init_packet_checker_for_success(self, mock_kill, mock_pids,
264 mock_run_command, mock_chdir):
265 global command_counter
266 command_counter = [0, 0, 0, 0, 0]
267 mock_pids.return_value = [1234, 4321]
268 self.iv.pkt_checker_command = 'test_sniff '
269 self.iv.interface_name = 'interface'
270 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
271 self.iv._init_packet_checker()
272 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
274 @mock.patch('experimental_framework.common.get_file_first_line')
275 def test__get_results_for_success(self, mock_get_file):
276 self.iv.res_dir = 'directory'
277 mock_get_file.side_effect = ['100', '50']
278 expected = {'failure': '0'}
279 output = self.iv._get_results()
280 self.assertEqual(expected, output)
282 mock_get_file.side_effect = ['10', '50']
283 expected = {'failure': '1'}
284 output = self.iv._get_results()
285 self.assertEqual(expected, output)
287 @mock.patch('experimental_framework.common.replace_in_file',
288 side_effect=dummy_replace_in_file)
289 def test__reset_lua_file_for_success(self, mock_replace):
290 global replace_counter
291 replace_counter = [0, 0, 0, 0, 0, 0]
292 traffic_rate_percentage = '100'
294 self.iv.lua_file = 'file'
295 self.iv.results_file = 'result_file'
296 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
297 self.assertEqual(dummy_replace_in_file('', '', '', True),
300 @mock.patch('experimental_framework.common.replace_in_file',
301 side_effect=dummy_replace_in_file)
302 def test__configure_lua_file_for_success(self, mock_replace):
303 global replace_counter
304 replace_counter = [0, 0, 0, 0, 0, 0]
305 traffic_rate_percentage = '100'
307 self.iv.lua_file = 'file'
308 self.iv.results_file = 'result_file'
309 self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
310 self.assertEqual(dummy_replace_in_file('', '', '', True),
313 @mock.patch('experimental_framework.common.LOG')
314 @mock.patch('experimental_framework.packet_generators.'
315 'dpdk_packet_generator.DpdkPacketGenerator',
316 side_effect=DummyDpdkPacketGenerator)
317 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
318 def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
321 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
322 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
323 mock_common_get_vars.return_value = rval
324 expected = {'test': 'result'}
325 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
327 iv.params[iv_module.THROUGHPUT] = '1'
329 self.assertEqual(expected, output)
330 self.assertEqual(DummyDpdkPacketGenerator.counter,
332 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,