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
75 raise Exception(command)
78 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
80 return replace_counter
82 if str_from == 'local out_file = "result_file"':
83 if str_to == 'local out_file = ""':
84 replace_counter[0] += 1
86 if str_from == 'local traffic_rate = 100':
87 if str_to == 'local traffic_rate = 0':
88 replace_counter[1] += 1
90 if str_from == 'local traffic_delay = 60':
91 if str_to == 'local traffic_delay = 0':
92 replace_counter[2] += 1
94 if str_from == 'local out_file = ""':
95 if str_to == 'local out_file = "result_file"':
96 replace_counter[3] += 1
98 if str_from == 'local traffic_rate = 0':
99 if str_to == 'local traffic_rate = 100':
100 replace_counter[4] += 1
102 if str_from == 'local traffic_delay = 0':
103 if str_to == 'local traffic_delay = 60':
104 replace_counter[5] += 1
106 raise Exception(file + ' ' + str_from + ' ' + str_to)
109 class DummyDpdkPacketGenerator():
114 DummyDpdkPacketGenerator.counter = [0, 0]
116 def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
117 pcap_file_1, vlan_0, vlan_1):
118 if dpdk_interfaces == 1:
119 if lua_script == 'constant_traffic.lua':
120 if pcap_file_0 == 'packet_512.pcap':
121 if pcap_file_1 == 'igmp.pcap':
124 DummyDpdkPacketGenerator.counter[0] += 1
126 def send_traffic(self):
127 DummyDpdkPacketGenerator.counter[1] += 1
131 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
133 counter = [0, 0, 0, 0, 0]
135 def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
136 DummyInstantiaionValidationBenchmark.counter[0] += 1
138 def _init_packet_checker(self):
139 DummyInstantiaionValidationBenchmark.counter[1] += 1
141 def _finalize_packet_checker(self):
142 DummyInstantiaionValidationBenchmark.counter[2] += 1
144 def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
145 if traffic_rate_percentage == '1' and traffic_time == '10':
146 DummyInstantiaionValidationBenchmark.counter[3] += 1
148 def _get_results(self):
149 DummyInstantiaionValidationBenchmark.counter[4] += 1
150 res = {'test': 'result'}
154 class InstantiationValidationInitTest(unittest.TestCase):
157 common.BASE_DIR = os.getcwd()
158 self.iv = InstantiationValidationBenchmark('InstantiationValidation',
162 common.BASE_DIR = None
164 @mock.patch('experimental_framework.common.get_base_dir')
165 def test___init___for_success(self, mock_base_dir):
166 mock_base_dir.return_value = 'base_dir/'
167 iv = InstantiationValidationBenchmark('InstantiationValidation',
169 self.assertEqual(iv.base_dir,
170 'base_dir/experimental_framework/'
171 'packet_generators/dpdk_pktgen/')
172 self.assertEqual(iv.results_file,
173 'base_dir/experimental_framework/'
174 'packet_generators/dpdk_pktgen/packets.res')
175 self.assertEqual(iv.lua_file,
176 'base_dir/experimental_framework/'
177 'packet_generators/dpdk_pktgen/constant_traffic.lua')
178 self.assertEqual(iv.pkt_checker_command,
179 'base_dir/experimental_framework/'
180 'libraries/packet_checker/test_sniff ')
181 self.assertEqual(iv.res_dir, '')
182 self.assertEqual(iv.interface_name, '')
184 def test_init_for_success(self):
187 def test_finalize_for_success(self):
190 def test_get_features_for_success(self):
193 expected['description'] = 'Instantiation Validation Benchmark'
194 expected['parameters'] = [
195 iv_module.THROUGHPUT,
196 iv_module.VLAN_SENDER,
197 iv_module.VLAN_RECEIVER
199 expected['allowed_values'] = dict()
200 expected['allowed_values'][iv_module.THROUGHPUT] = \
201 map(str, range(0, 100))
202 expected['allowed_values'][iv_module.VLAN_SENDER] = \
203 map(str, range(-1, 4096))
204 expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
205 map(str, range(-1, 4096))
206 expected['default_values'] = dict()
207 expected['default_values'][iv_module.THROUGHPUT] = '1'
208 expected['default_values'][iv_module.VLAN_SENDER] = '-1'
209 expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
210 output = self.iv.get_features()
211 self.assertEqual(expected, output)
213 @mock.patch('commands.getoutput')
214 def test__get_pids_for_success(self, mock_getoutput):
216 mock_getoutput.return_value = '1234'
217 output = self.iv._get_pids()
218 self.assertEqual(expected, output)
220 expected = [1234, 4321]
221 mock_getoutput.return_value = '1234\n4321'
222 output = self.iv._get_pids()
223 self.assertEqual(expected, output)
226 mock_getoutput.return_value = None
227 output = self.iv._get_pids()
228 self.assertEqual(expected, output)
230 @mock.patch('experimental_framework.common.run_command',
231 side_effect=dummy_run_command)
232 @mock.patch('os.kill', side_effect=dummy_os_kill)
233 @mock.patch('experimental_framework.benchmarks.'
234 'instantiation_validation_benchmark.'
235 'InstantiationValidationBenchmark._get_pids')
236 def test__finalize_packet_checker_for_success(self,
240 global command_counter
242 command_counter = [0, 0, 0, 0, 0]
243 kill_counter = [0, 0]
244 mock_pids.return_value = [1234, 4321]
245 self.iv.interface_name = 'interface'
246 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
247 self.iv._finalize_packet_checker()
248 self.assertEqual(dummy_os_kill('', '', True), [1, 1])
249 self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
251 @mock.patch('experimental_framework.common.run_command',
252 side_effect=dummy_run_command_2)
253 @mock.patch('experimental_framework.benchmarks.'
254 'instantiation_validation_benchmark.'
255 'InstantiationValidationBenchmark._get_pids')
256 @mock.patch('os.kill', side_effect=dummy_os_kill)
257 def test__init_packet_checker_for_success(self, mock_kill,
258 mock_pids, mock_run_command):
259 global command_counter
260 command_counter = [0, 0, 0, 0, 0]
261 mock_pids.return_value = [1234, 4321]
262 self.iv.pkt_checker_command = 'test_sniff '
263 self.iv.interface_name = 'interface'
264 self.iv.params[iv_module.VLAN_RECEIVER] = '100'
265 self.iv._init_packet_checker()
266 self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
268 @mock.patch('experimental_framework.common.get_file_first_line')
269 def test__get_results_for_success(self, mock_get_file):
270 self.iv.res_dir = 'directory'
271 mock_get_file.side_effect = ['100', '50']
272 expected = {'failure': '0'}
273 output = self.iv._get_results()
274 self.assertEqual(expected, output)
276 mock_get_file.side_effect = ['10', '50']
277 expected = {'failure': '1'}
278 output = self.iv._get_results()
279 self.assertEqual(expected, output)
281 @mock.patch('experimental_framework.common.replace_in_file',
282 side_effect=dummy_replace_in_file)
283 def test__reset_lua_file_for_success(self, mock_replace):
284 global replace_counter
285 replace_counter = [0, 0, 0, 0, 0, 0]
286 traffic_rate_percentage = '100'
288 self.iv.lua_file = 'file'
289 self.iv.results_file = 'result_file'
290 self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
291 self.assertEqual(dummy_replace_in_file('', '', '', True),
294 @mock.patch('experimental_framework.common.replace_in_file',
295 side_effect=dummy_replace_in_file)
296 def test__configure_lua_file_for_success(self, mock_replace):
297 global replace_counter
298 replace_counter = [0, 0, 0, 0, 0, 0]
299 traffic_rate_percentage = '100'
301 self.iv.lua_file = 'file'
302 self.iv.results_file = 'result_file'
303 self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
304 self.assertEqual(dummy_replace_in_file('', '', '', True),
307 @mock.patch('experimental_framework.common.LOG')
308 @mock.patch('experimental_framework.packet_generators.'
309 'dpdk_packet_generator.DpdkPacketGenerator',
310 side_effect=DummyDpdkPacketGenerator)
311 @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
312 def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
315 rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
316 rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
317 mock_common_get_vars.return_value = rval
318 expected = {'test': 'result'}
319 iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
321 iv.params[iv_module.THROUGHPUT] = '1'
323 self.assertEqual(expected, output)
324 self.assertEqual(DummyDpdkPacketGenerator.counter,
326 self.assertEqual(DummyInstantiaionValidationBenchmark.counter,