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.
17 import experimental_framework.constants.conf_file_sections as cfs
18 import experimental_framework.benchmarks.\
19 instantiation_validation_benchmark as iv_module
20 from experimental_framework.benchmarks.\
21 instantiation_validation_benchmark import InstantiationValidationBenchmark
25 command_counter = [0, 0, 0, 0, 0]
26 replace_counter = [0, 0, 0]
29 def dummy_os_kill(pid, signal, get_counters=None):
41 def dummy_run_command(command, get_counters=None):
43 return command_counter
44 if command == 'smcroute -k':
45 command_counter[0] += 1
47 elif command == 'ip link delete interface.100':
48 command_counter[1] += 1
50 raise Exception(command)
53 def dummy_run_command_2(command, get_counters=None):
55 return command_counter
56 if command == 'ip link add link interface name interface.' \
57 '100 type vlan id 100':
58 command_counter[0] += 1
60 elif command == 'ifconfig interface.100 10.254.254.254 up':
61 command_counter[1] += 1
63 elif command == "echo 'mgroup from interface.100 group 224.192.16.1' > " \
65 command_counter[2] += 1
67 elif command == "smcroute -d":
68 command_counter[3] += 1
70 elif command == "test_sniff interface.100 128 &":
71 command_counter[4] += 1
73 raise Exception(command)
76 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
78 return replace_counter
80 if str_from == 'local out_file = "result_file"':
81 if str_to == 'local out_file = ""':
82 replace_counter[0] += 1
84 if str_from == 'local traffic_rate = 100':
85 if str_to == 'local traffic_rate = 0':
86 replace_counter[1] += 1
88 if str_from == 'local traffic_delay = 60':
89 if str_to == 'local traffic_delay = 0':
90 replace_counter[2] += 1
92 if str_from == 'local out_file = ""':
93 if str_to == 'local out_file = "result_file"':
94 replace_counter[3] += 1
96 if str_from == 'local traffic_rate = 0':
97 if str_to == 'local traffic_rate = 100':
98 replace_counter[4] += 1
100 if str_from == 'local traffic_delay = 0':
101 if str_to == 'local traffic_delay = 60':
102 replace_counter[5] += 1
104 raise Exception(file + ' ' + str_from + ' ' + str_to)
107 class DummyDpdkPacketGenerator():
112 DummyDpdkPacketGenerator.counter = [0, 0]
114 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
115 pcap_file_1, vlan_0, vlan_1):
116 if dpdk_interfaces == 1:
117 if lua_script == 'constant_traffic.lua':
118 if pcap_file_0 == 'packet_512.pcap':
119 if pcap_file_1 == 'igmp.pcap':
122 DummyDpdkPacketGenerator.counter[0] += 1
124 def send_traffic(self):
125 DummyDpdkPacketGenerator.counter[1] += 1
129 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
131 counter = [0, 0, 0, 0, 0]
133 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
134 DummyInstantiaionValidationBenchmark.counter[0] += 1
136 def _init_packet_checker(self):
137 DummyInstantiaionValidationBenchmark.counter[1] += 1
139 def _finalize_packet_checker(self):
140 DummyInstantiaionValidationBenchmark.counter[2] += 1
142 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
143 if traffic_rate_percentage == '1' and traffic_time == '10':
144 DummyInstantiaionValidationBenchmark.counter[3] += 1
146 def _get_results(self):
147 DummyInstantiaionValidationBenchmark.counter[4] += 1
148 res = {'test': 'result'}
152 class InstantiationValidationInitTest(unittest.TestCase):
155 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
161 @mock.patch('experimental_framework.common.get_base_dir')
162 def test___init___for_success(self, mock_base_dir):
163 mock_base_dir.return_value = 'base_dir/'
164 iv = InstantiationValidationBenchmark('InstantiationValidation',
166 self.assertEqual(iv.base_dir,
167 'base_dir/experimental_framework/'
168 'packet_generators/dpdk_pktgen/')
169 self.assertEqual(iv.results_file,
170 'base_dir/experimental_framework/'
171 'packet_generators/dpdk_pktgen/packets.res')
172 self.assertEqual(iv.lua_file,
173 'base_dir/experimental_framework/'
174 'packet_generators/dpdk_pktgen/constant_traffic.lua')
175 self.assertEqual(iv.pkt_checker_command,
176 'base_dir/experimental_framework/'
177 'libraries/packet_checker/test_sniff ')
178 self.assertEqual(iv.res_dir, '')
179 self.assertEqual(iv.interface_name, '')
181 def test_init_for_success(self):
184 def test_finalize_for_success(self):
187 def test_get_features_for_success(self):
190 expected['description'] = 'Instantiation Validation Benchmark'
191 expected['parameters'] = [
192 iv_module.THROUGHPUT,
193 iv_module.VLAN_SENDER,
194 iv_module.VLAN_RECEIVER
196 expected['allowed_values'] = dict()
197 expected['allowed_values'][iv_module.THROUGHPUT] = \
198 map(str, range(0, 100))
199 expected['allowed_values'][iv_module.VLAN_SENDER] = \
200 map(str, range(-1, 4096))
201 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
202 map(str, range(-1, 4096))
203 expected['default_values'] = dict()
204 expected['default_values'][iv_module.THROUGHPUT] = '1'
205 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
206 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
207 output = self.iv.get_features()
208 self.assertEqual(expected, output)
210 @mock.patch('commands.getoutput')
211 def test__get_pids_for_success(self, mock_getoutput):
213 mock_getoutput.return_value = '1234'
214 output = self.iv._get_pids()
215 self.assertEqual(expected, output)
217 expected = [1234, 4321]
218 mock_getoutput.return_value = '1234\n4321'
219 output = self.iv._get_pids()
220 self.assertEqual(expected, output)
223 mock_getoutput.return_value = None
224 output = self.iv._get_pids()
225 self.assertEqual(expected, output)
227 @mock.patch('experimental_framework.common.run_command',
228 side_effect=dummy_run_command)
229 @mock.patch('os.kill', side_effect=dummy_os_kill)
230 @mock.patch('experimental_framework.benchmarks.'
231 'instantiation_validation_benchmark.'
232 'InstantiationValidationBenchmark._get_pids')
233 def test__finalize_packet_checker_for_success(self,
237 global command_counter
239 command_counter = [0, 0, 0, 0, 0]
240 kill_counter = [0, 0]
241 mock_pids.return_value = [1234, 4321]
242 self.iv.interface_name = 'interface'
243 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
244 self.iv._finalize_packet_checker()
245 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
246 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
248 @mock.patch('experimental_framework.common.run_command',
249 side_effect=dummy_run_command_2)
250 @mock.patch('experimental_framework.benchmarks.'
251 'instantiation_validation_benchmark.'
252 'InstantiationValidationBenchmark._get_pids')
253 @mock.patch('os.kill', side_effect=dummy_os_kill)
254 def test__init_packet_checker_for_success(self, mock_kill,
255 mock_pids, mock_run_command):
256 global command_counter
257 command_counter = [0, 0, 0, 0, 0]
258 mock_pids.return_value = [1234, 4321]
259 self.iv.pkt_checker_command = 'test_sniff '
260 self.iv.interface_name = 'interface'
261 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
262 self.iv._init_packet_checker()
263 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
265 @mock.patch('experimental_framework.common.get_file_first_line')
266 def test__get_results_for_success(self, mock_get_file):
267 self.iv.res_dir = 'directory'
268 mock_get_file.side_effect = ['100', '50']
269 expected = {'failure': '0'}
270 output = self.iv._get_results()
271 self.assertEqual(expected, output)
273 mock_get_file.side_effect = ['10', '50']
274 expected = {'failure': '1'}
275 output = self.iv._get_results()
276 self.assertEqual(expected, output)
278 @mock.patch('experimental_framework.common.replace_in_file',
279 side_effect=dummy_replace_in_file)
280 def test__reset_lua_file_for_success(self, mock_replace):
281 global replace_counter
282 replace_counter = [0, 0, 0, 0, 0, 0]
283 traffic_rate_percentage = '100'
285 self.iv.lua_file = 'file'
286 self.iv.results_file = 'result_file'
287 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
288 self.assertEqual(dummy_replace_in_file('', '', '', True),
291 @mock.patch('experimental_framework.common.replace_in_file',
292 side_effect=dummy_replace_in_file)
293 def test__configure_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._configure_lua_file(traffic_rate_percentage, traffic_time)
301 self.assertEqual(dummy_replace_in_file('', '', '', True),
304 @mock.patch('experimental_framework.packet_generators.'
305 'dpdk_packet_generator.DpdkPacketGenerator',
306 side_effect=DummyDpdkPacketGenerator)
307 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
308 def test_run_for_success(self, mock_common_get_vars, mock_pktgen):
310 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
311 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
312 mock_common_get_vars.return_value = rval
313 expected = {'test': 'result'}
314 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
316 iv.params[iv_module.THROUGHPUT] = '1'
318 self.assertEqual(expected, output)
319 self.assertEqual(DummyDpdkPacketGenerator.counter,
321 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,