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 == 'smcroute -k':
47 command_counter[0] += 1
49 elif command == 'ip link delete interface.100':
50 command_counter[1] += 1
52 raise Exception(command)
55 def dummy_run_command_2(command, get_counters=None):
57 return command_counter
58 if command == 'ip link add link interface name interface.' \
59 '100 type vlan id 100':
60 command_counter[0] += 1
62 elif command == 'ifconfig interface.100 10.254.254.254 up':
63 command_counter[1] += 1
65 elif command == "echo 'mgroup from interface.100 group 224.192.16.1' > " \
67 command_counter[2] += 1
69 elif command == "smcroute -d":
70 command_counter[3] += 1
72 elif command == "test_sniff interface.100 128 &":
73 command_counter[4] += 1
77 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
79 return replace_counter
81 if str_from == 'local out_file = "result_file"':
82 if str_to == 'local out_file = ""':
83 replace_counter[0] += 1
85 if str_from == 'local traffic_rate = 100':
86 if str_to == 'local traffic_rate = 0':
87 replace_counter[1] += 1
89 if str_from == 'local traffic_delay = 60':
90 if str_to == 'local traffic_delay = 0':
91 replace_counter[2] += 1
93 if str_from == 'local out_file = ""':
94 if str_to == 'local out_file = "result_file"':
95 replace_counter[3] += 1
97 if str_from == 'local traffic_rate = 0':
98 if str_to == 'local traffic_rate = 100':
99 replace_counter[4] += 1
101 if str_from == 'local traffic_delay = 0':
102 if str_to == 'local traffic_delay = 60':
103 replace_counter[5] += 1
105 raise Exception(file + ' ' + str_from + ' ' + str_to)
108 class DummyDpdkPacketGenerator():
113 DummyDpdkPacketGenerator.counter = [0, 0]
115 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
116 pcap_file_1, vlan_0, vlan_1):
117 if dpdk_interfaces == 1:
118 if lua_script == 'constant_traffic.lua':
119 if pcap_file_0 == 'packet_512.pcap':
120 if pcap_file_1 == 'igmp.pcap':
123 DummyDpdkPacketGenerator.counter[0] += 1
125 def send_traffic(self):
126 DummyDpdkPacketGenerator.counter[1] += 1
130 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
132 counter = [0, 0, 0, 0, 0]
134 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
135 DummyInstantiaionValidationBenchmark.counter[0] += 1
137 def _init_packet_checker(self):
138 DummyInstantiaionValidationBenchmark.counter[1] += 1
140 def _finalize_packet_checker(self):
141 DummyInstantiaionValidationBenchmark.counter[2] += 1
143 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
144 if traffic_rate_percentage == '1' and traffic_time == '10':
145 DummyInstantiaionValidationBenchmark.counter[3] += 1
147 def _get_results(self):
148 DummyInstantiaionValidationBenchmark.counter[4] += 1
149 res = {'test': 'result'}
153 class InstantiationValidationInitTest(unittest.TestCase):
156 common.BASE_DIR = os.getcwd()
157 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
161 common.BASE_DIR = None
163 @mock.patch('experimental_framework.common.get_base_dir')
164 def test___init___for_success(self, mock_base_dir):
165 mock_base_dir.return_value = 'base_dir/'
166 iv = InstantiationValidationBenchmark('InstantiationValidation',
168 self.assertEqual(iv.base_dir,
169 'base_dir/experimental_framework/'
170 'packet_generators/dpdk_pktgen/')
171 self.assertEqual(iv.results_file,
172 'base_dir/experimental_framework/'
173 'packet_generators/dpdk_pktgen/packets.res')
174 self.assertEqual(iv.lua_file,
175 'base_dir/experimental_framework/'
176 'packet_generators/dpdk_pktgen/constant_traffic.lua')
177 self.assertEqual(iv.pkt_checker_command,
178 'base_dir/experimental_framework/'
179 'libraries/packet_checker/test_sniff ')
180 self.assertEqual(iv.res_dir, '')
181 self.assertEqual(iv.interface_name, '')
183 def test_init_for_success(self):
186 def test_finalize_for_success(self):
189 def test_get_features_for_success(self):
192 expected['description'] = 'Instantiation Validation Benchmark'
193 expected['parameters'] = [
194 iv_module.THROUGHPUT,
195 iv_module.VLAN_SENDER,
196 iv_module.VLAN_RECEIVER
198 expected['allowed_values'] = dict()
199 expected['allowed_values'][iv_module.THROUGHPUT] = \
200 map(str, range(0, 100))
201 expected['allowed_values'][iv_module.VLAN_SENDER] = \
202 map(str, range(-1, 4096))
203 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
204 map(str, range(-1, 4096))
205 expected['default_values'] = dict()
206 expected['default_values'][iv_module.THROUGHPUT] = '1'
207 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
208 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
209 output = self.iv.get_features()
210 self.assertEqual(expected, output)
212 @mock.patch('commands.getoutput')
213 def test__get_pids_for_success(self, mock_getoutput):
215 mock_getoutput.return_value = '1234'
216 output = self.iv._get_pids()
217 self.assertEqual(expected, output)
219 expected = [1234, 4321]
220 mock_getoutput.return_value = '1234\n4321'
221 output = self.iv._get_pids()
222 self.assertEqual(expected, output)
225 mock_getoutput.return_value = None
226 output = self.iv._get_pids()
227 self.assertEqual(expected, output)
229 @mock.patch('experimental_framework.common.run_command',
230 side_effect=dummy_run_command)
231 @mock.patch('os.kill', side_effect=dummy_os_kill)
232 @mock.patch('experimental_framework.benchmarks.'
233 'instantiation_validation_benchmark.'
234 'InstantiationValidationBenchmark._get_pids')
235 def test__finalize_packet_checker_for_success(self,
239 global command_counter
241 command_counter = [0, 0, 0, 0, 0]
242 kill_counter = [0, 0]
243 mock_pids.return_value = [1234, 4321]
244 self.iv.interface_name = 'interface'
245 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
246 self.iv._finalize_packet_checker()
247 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
248 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
250 @mock.patch('experimental_framework.common.run_command',
251 side_effect=dummy_run_command_2)
252 @mock.patch('experimental_framework.benchmarks.'
253 'instantiation_validation_benchmark.'
254 'InstantiationValidationBenchmark._get_pids')
255 @mock.patch('os.kill', side_effect=dummy_os_kill)
256 def test__init_packet_checker_for_success(self, mock_kill,
257 mock_pids, mock_run_command):
258 global command_counter
259 command_counter = [0, 0, 0, 0, 0]
260 mock_pids.return_value = [1234, 4321]
261 self.iv.pkt_checker_command = 'test_sniff '
262 self.iv.interface_name = 'interface'
263 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
264 self.iv._init_packet_checker()
265 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
267 @mock.patch('experimental_framework.common.get_file_first_line')
268 def test__get_results_for_success(self, mock_get_file):
269 self.iv.res_dir = 'directory'
270 mock_get_file.side_effect = ['100', '50']
271 expected = {'failure': '0'}
272 output = self.iv._get_results()
273 self.assertEqual(expected, output)
275 mock_get_file.side_effect = ['10', '50']
276 expected = {'failure': '1'}
277 output = self.iv._get_results()
278 self.assertEqual(expected, output)
280 @mock.patch('experimental_framework.common.replace_in_file',
281 side_effect=dummy_replace_in_file)
282 def test__reset_lua_file_for_success(self, mock_replace):
283 global replace_counter
284 replace_counter = [0, 0, 0, 0, 0, 0]
285 traffic_rate_percentage = '100'
287 self.iv.lua_file = 'file'
288 self.iv.results_file = 'result_file'
289 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
290 self.assertEqual(dummy_replace_in_file('', '', '', True),
293 @mock.patch('experimental_framework.common.replace_in_file',
294 side_effect=dummy_replace_in_file)
295 def test__configure_lua_file_for_success(self, mock_replace):
296 global replace_counter
297 replace_counter = [0, 0, 0, 0, 0, 0]
298 traffic_rate_percentage = '100'
300 self.iv.lua_file = 'file'
301 self.iv.results_file = 'result_file'
302 self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
303 self.assertEqual(dummy_replace_in_file('', '', '', True),
306 @mock.patch('experimental_framework.common.LOG')
307 @mock.patch('experimental_framework.packet_generators.'
308 'dpdk_packet_generator.DpdkPacketGenerator',
309 side_effect=DummyDpdkPacketGenerator)
310 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
311 def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
314 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
315 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
316 mock_common_get_vars.return_value = rval
317 expected = {'test': 'result'}
318 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
320 iv.params[iv_module.THROUGHPUT] = '1'
322 self.assertEqual(expected, output)
323 self.assertEqual(DummyDpdkPacketGenerator.counter,
325 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,