Fix small bugs for running Apexlake on Montreal testbed
[yardstick.git] / yardstick / vTC / apexlake / tests / instantiation_validation_bench_test.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import unittest
16 import mock
17 import os
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
24
25
26 kill_counter = [0, 0]
27 command_counter = [0, 0, 0, 0, 0]
28 replace_counter = [0, 0, 0]
29
30
31 def dummy_os_kill(pid, signal, get_counters=None):
32     if get_counters:
33         return kill_counter
34     if pid == 1234:
35         kill_counter[0] += 1
36         return
37     if pid == 4321:
38         kill_counter[1] += 1
39         return
40     raise Exception(pid)
41
42
43 def dummy_run_command(command, get_counters=None):
44     if get_counters:
45         return command_counter
46     if command == 'sudo smcroute -k':
47         command_counter[0] += 1
48         return
49     elif command == 'sudo ip link delete interface.100':
50         command_counter[1] += 1
51         return
52     elif command == 'sudo kill 1234':
53         kill_counter[0] += 1
54         return
55     elif command == 'sudo kill 4321':
56         kill_counter[1] += 1
57         return
58     raise Exception(command)
59
60
61 def dummy_run_command_2(command, get_counters=None):
62     if get_counters:
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
67         return
68     elif command == 'sudo ifconfig interface.100 10.254.254.254 up':
69         command_counter[1] += 1
70         return
71     elif command == "sudo echo 'mgroup from interface.100 group" \
72                     " 224.192.16.1' > /etc/smcroute.conf":
73         command_counter[2] += 1
74         return
75     elif command == "sudo smcroute -d":
76         command_counter[3] += 1
77         return
78     elif command == "sudo test_sniff interface.100 128 &":
79         command_counter[4] += 1
80         return
81
82
83 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
84     if get_couters:
85         return replace_counter
86     if file == 'file':
87         if str_from == 'local out_file = "result_file"':
88             if str_to == 'local out_file = ""':
89                 replace_counter[0] += 1
90                 return
91         if str_from == 'local traffic_rate = 100':
92             if str_to == 'local traffic_rate = 0':
93                 replace_counter[1] += 1
94                 return
95         if str_from == 'local traffic_delay = 60':
96             if str_to == 'local traffic_delay = 0':
97                 replace_counter[2] += 1
98                 return
99         if str_from == 'local out_file = ""':
100             if str_to == 'local out_file = "result_file"':
101                 replace_counter[3] += 1
102                 return
103         if str_from == 'local traffic_rate = 0':
104             if str_to == 'local traffic_rate = 100':
105                 replace_counter[4] += 1
106                 return
107         if str_from == 'local traffic_delay = 0':
108             if str_to == 'local traffic_delay = 60':
109                 replace_counter[5] += 1
110                 return
111     raise Exception(file + ' ' + str_from + ' ' + str_to)
112
113
114 class DummyDpdkPacketGenerator():
115
116     counter = 0
117
118     def __init__(self):
119         DummyDpdkPacketGenerator.counter = [0, 0]
120
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':
127                         if vlan_0 == '-1':
128                             if vlan_1 == '-1':
129                                 DummyDpdkPacketGenerator.counter[0] += 1
130
131     def send_traffic(self):
132         DummyDpdkPacketGenerator.counter[1] += 1
133         return
134
135
136 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
137
138     counter = [0, 0, 0, 0, 0]
139
140     def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
141         DummyInstantiaionValidationBenchmark.counter[0] += 1
142
143     def _init_packet_checker(self):
144         DummyInstantiaionValidationBenchmark.counter[1] += 1
145
146     def _finalize_packet_checker(self):
147         DummyInstantiaionValidationBenchmark.counter[2] += 1
148
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
152
153     def _get_results(self):
154         DummyInstantiaionValidationBenchmark.counter[4] += 1
155         res = {'test': 'result'}
156         return res
157
158
159 class InstantiationValidationInitTest(unittest.TestCase):
160
161     def setUp(self):
162         common.BASE_DIR = os.getcwd()
163         self.iv = InstantiationValidationBenchmark('InstantiationValidation',
164                                                    dict())
165
166     def tearDown(self):
167         common.BASE_DIR = None
168
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',
173                                               dict())
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, '')
188
189     def test_init_for_success(self):
190         self.iv.init()
191
192     def test_finalize_for_success(self):
193         self.iv.finalize()
194
195     def test_get_features_for_success(self):
196
197         expected = dict()
198         expected['description'] = 'Instantiation Validation Benchmark'
199         expected['parameters'] = [
200             iv_module.THROUGHPUT,
201             iv_module.VLAN_SENDER,
202             iv_module.VLAN_RECEIVER
203         ]
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)
217
218     @mock.patch('commands.getoutput')
219     def test__get_pids_for_success(self, mock_getoutput):
220         expected = [1234]
221         mock_getoutput.return_value = '1234'
222         output = self.iv._get_pids()
223         self.assertEqual(expected, output)
224
225         expected = [1234, 4321]
226         mock_getoutput.return_value = '1234\n4321'
227         output = self.iv._get_pids()
228         self.assertEqual(expected, output)
229
230         expected = []
231         mock_getoutput.return_value = None
232         output = self.iv._get_pids()
233         self.assertEqual(expected, output)
234
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,
242                                                   mock_pids,
243                                                   mock_os_kill,
244                                                   mock_run_command):
245         global command_counter
246         global kill_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])
255
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])
273
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)
281
282         mock_get_file.side_effect = ['10', '50']
283         expected = {'failure': '1'}
284         output = self.iv._get_results()
285         self.assertEqual(expected, output)
286
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'
293         traffic_time = '60'
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),
298                          [1, 1, 1, 0, 0, 0])
299
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'
306         traffic_time = '60'
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),
311                          [0, 0, 0, 1, 1, 1])
312
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,
319                              mock_log):
320         rval = dict()
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',
326                                                   dict())
327         iv.params[iv_module.THROUGHPUT] = '1'
328         output = iv.run()
329         self.assertEqual(expected, output)
330         self.assertEqual(DummyDpdkPacketGenerator.counter,
331                          [1, 1])
332         self.assertEqual(DummyInstantiaionValidationBenchmark.counter,
333                          [1, 1, 1, 1, 1])