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 ' netmask 255.255.255.248':
70 command_counter[1] += 1
72 elif command == "sudo echo 'mgroup from interface.100 group" \
73 " 224.192.16.1' > /etc/smcroute.conf":
74 command_counter[2] += 1
76 elif command == "sudo smcroute -d":
77 command_counter[3] += 1
79 elif command == "sudo test_sniff interface.100 128 &":
80 command_counter[4] += 1
84 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
86 return replace_counter
88 if str_from == 'local out_file = "result_file"':
89 if str_to == 'local out_file = ""':
90 replace_counter[0] += 1
92 if str_from == 'local traffic_rate = 100':
93 if str_to == 'local traffic_rate = 0':
94 replace_counter[1] += 1
96 if str_from == 'local traffic_delay = 60':
97 if str_to == 'local traffic_delay = 0':
98 replace_counter[2] += 1
100 if str_from == 'local out_file = ""':
101 if str_to == 'local out_file = "result_file"':
102 replace_counter[3] += 1
104 if str_from == 'local traffic_rate = 0':
105 if str_to == 'local traffic_rate = 100':
106 replace_counter[4] += 1
108 if str_from == 'local traffic_delay = 0':
109 if str_to == 'local traffic_delay = 60':
110 replace_counter[5] += 1
112 raise Exception(file + ' ' + str_from + ' ' + str_to)
115 class DummyDpdkPacketGenerator():
120 DummyDpdkPacketGenerator.counter = [0, 0]
122 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
123 pcap_file_1, vlan_0, vlan_1):
124 if dpdk_interfaces == 1:
125 if lua_script == 'constant_traffic.lua':
126 if pcap_file_0 == 'packet_512.pcap':
127 if pcap_file_1 == 'igmp.pcap':
130 DummyDpdkPacketGenerator.counter[0] += 1
132 def send_traffic(self):
133 DummyDpdkPacketGenerator.counter[1] += 1
137 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
139 counter = [0, 0, 0, 0, 0]
141 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
142 DummyInstantiaionValidationBenchmark.counter[0] += 1
144 def _init_packet_checker(self):
145 DummyInstantiaionValidationBenchmark.counter[1] += 1
147 def _finalize_packet_checker(self):
148 DummyInstantiaionValidationBenchmark.counter[2] += 1
150 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
151 if traffic_rate_percentage == '1' and traffic_time == '10':
152 DummyInstantiaionValidationBenchmark.counter[3] += 1
154 def _get_results(self):
155 DummyInstantiaionValidationBenchmark.counter[4] += 1
156 res = {'test': 'result'}
160 class InstantiationValidationInitTest(unittest.TestCase):
163 common.BASE_DIR = os.getcwd()
164 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
168 common.BASE_DIR = None
170 @mock.patch('experimental_framework.common.get_base_dir')
171 def test___init___for_success(self, mock_base_dir):
172 mock_base_dir.return_value = 'base_dir/'
173 iv = InstantiationValidationBenchmark('InstantiationValidation',
175 self.assertEqual(iv.base_dir,
176 'base_dir/experimental_framework/'
177 'packet_generators/dpdk_pktgen/')
178 self.assertEqual(iv.results_file,
179 'base_dir/experimental_framework/'
180 'packet_generators/dpdk_pktgen/packets.res')
181 self.assertEqual(iv.lua_file,
182 'base_dir/experimental_framework/'
183 'packet_generators/dpdk_pktgen/constant_traffic.lua')
184 self.assertEqual(iv.pkt_checker_command,
185 'base_dir/experimental_framework/'
186 'libraries/packet_checker/test_sniff ')
187 self.assertEqual(iv.res_dir, '')
188 self.assertEqual(iv.interface_name, '')
190 def test_init_for_success(self):
193 def test_finalize_for_success(self):
196 def test_get_features_for_success(self):
199 expected['description'] = 'Instantiation Validation Benchmark'
200 expected['parameters'] = [
201 iv_module.THROUGHPUT,
202 iv_module.VLAN_SENDER,
203 iv_module.VLAN_RECEIVER
205 expected['allowed_values'] = dict()
206 expected['allowed_values'][iv_module.THROUGHPUT] = \
207 map(str, range(0, 100))
208 expected['allowed_values'][iv_module.VLAN_SENDER] = \
209 map(str, range(-1, 4096))
210 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
211 map(str, range(-1, 4096))
212 expected['default_values'] = dict()
213 expected['default_values'][iv_module.THROUGHPUT] = '1'
214 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
215 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
216 output = self.iv.get_features()
217 self.assertEqual(expected, output)
219 @mock.patch('commands.getoutput')
220 def test__get_pids_for_success(self, mock_getoutput):
222 mock_getoutput.return_value = '1234'
223 output = self.iv._get_pids()
224 self.assertEqual(expected, output)
226 expected = [1234, 4321]
227 mock_getoutput.return_value = '1234\n4321'
228 output = self.iv._get_pids()
229 self.assertEqual(expected, output)
232 mock_getoutput.return_value = None
233 output = self.iv._get_pids()
234 self.assertEqual(expected, output)
236 @mock.patch('experimental_framework.common.run_command',
237 side_effect=dummy_run_command)
238 @mock.patch('os.kill', side_effect=dummy_os_kill)
239 @mock.patch('experimental_framework.benchmarks.'
240 'instantiation_validation_benchmark.'
241 'InstantiationValidationBenchmark._get_pids')
242 def test__finalize_packet_checker_for_success(self,
246 global command_counter
248 command_counter = [0, 0, 0, 0, 0]
249 kill_counter = [0, 0]
250 mock_pids.return_value = [1234, 4321]
251 self.iv.interface_name = 'interface'
252 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
253 self.iv._finalize_packet_checker()
254 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
255 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
257 @mock.patch('os.chdir')
258 @mock.patch('experimental_framework.common.run_command',
259 side_effect=dummy_run_command_2)
260 @mock.patch('experimental_framework.benchmarks.'
261 'instantiation_validation_benchmark.'
262 'InstantiationValidationBenchmark._get_pids')
263 @mock.patch('os.kill', side_effect=dummy_os_kill)
264 def test__init_packet_checker_for_success(self, mock_kill, mock_pids,
265 mock_run_command, mock_chdir):
266 global command_counter
267 command_counter = [0, 0, 0, 0, 0]
268 mock_pids.return_value = [1234, 4321]
269 self.iv.pkt_checker_command = 'test_sniff '
270 self.iv.interface_name = 'interface'
271 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
272 self.iv._init_packet_checker()
273 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
275 @mock.patch('experimental_framework.common.get_file_first_line')
276 def test__get_results_for_success(self, mock_get_file):
277 self.iv.res_dir = 'directory'
278 mock_get_file.side_effect = ['100', '50']
279 expected = {'failure': '0'}
280 output = self.iv._get_results()
281 self.assertEqual(expected, output)
283 mock_get_file.side_effect = ['10', '50']
284 expected = {'failure': '1'}
285 output = self.iv._get_results()
286 self.assertEqual(expected, output)
288 @mock.patch('experimental_framework.common.replace_in_file',
289 side_effect=dummy_replace_in_file)
290 def test__reset_lua_file_for_success(self, mock_replace):
291 global replace_counter
292 replace_counter = [0, 0, 0, 0, 0, 0]
293 traffic_rate_percentage = '100'
295 self.iv.lua_file = 'file'
296 self.iv.results_file = 'result_file'
297 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
298 self.assertEqual(dummy_replace_in_file('', '', '', True),
301 @mock.patch('experimental_framework.common.replace_in_file',
302 side_effect=dummy_replace_in_file)
303 def test__configure_lua_file_for_success(self, mock_replace):
304 global replace_counter
305 replace_counter = [0, 0, 0, 0, 0, 0]
306 traffic_rate_percentage = '100'
308 self.iv.lua_file = 'file'
309 self.iv.results_file = 'result_file'
310 self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
311 self.assertEqual(dummy_replace_in_file('', '', '', True),
314 @mock.patch('experimental_framework.common.LOG')
315 @mock.patch('experimental_framework.packet_generators.'
316 'dpdk_packet_generator.DpdkPacketGenerator',
317 side_effect=DummyDpdkPacketGenerator)
318 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
319 def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
322 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
323 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
324 mock_common_get_vars.return_value = rval
325 expected = {'test': 'result'}
326 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
328 iv.params[iv_module.THROUGHPUT] = '1'
330 self.assertEqual(expected, output)
331 self.assertEqual(DummyDpdkPacketGenerator.counter,
333 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,