Fix small things for integration of ApexLake with Yardstick
[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 == 'smcroute -k':
47         command_counter[0] += 1
48         return
49     elif command == 'ip link delete interface.100':
50         command_counter[1] += 1
51         return
52     raise Exception(command)
53
54
55 def dummy_run_command_2(command, get_counters=None):
56     if get_counters:
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
61         return
62     elif command == 'ifconfig interface.100 10.254.254.254 up':
63         command_counter[1] += 1
64         return
65     elif command == "echo 'mgroup from interface.100 group 224.192.16.1' > " \
66                     "/etc/smcroute.conf":
67         command_counter[2] += 1
68         return
69     elif command == "smcroute -d":
70         command_counter[3] += 1
71         return
72     elif command == "test_sniff interface.100 128 &":
73         command_counter[4] += 1
74         return
75
76
77 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
78     if get_couters:
79         return replace_counter
80     if file == 'file':
81         if str_from == 'local out_file = "result_file"':
82             if str_to == 'local out_file = ""':
83                 replace_counter[0] += 1
84                 return
85         if str_from == 'local traffic_rate = 100':
86             if str_to == 'local traffic_rate = 0':
87                 replace_counter[1] += 1
88                 return
89         if str_from == 'local traffic_delay = 60':
90             if str_to == 'local traffic_delay = 0':
91                 replace_counter[2] += 1
92                 return
93         if str_from == 'local out_file = ""':
94             if str_to == 'local out_file = "result_file"':
95                 replace_counter[3] += 1
96                 return
97         if str_from == 'local traffic_rate = 0':
98             if str_to == 'local traffic_rate = 100':
99                 replace_counter[4] += 1
100                 return
101         if str_from == 'local traffic_delay = 0':
102             if str_to == 'local traffic_delay = 60':
103                 replace_counter[5] += 1
104                 return
105     raise Exception(file + ' ' + str_from + ' ' + str_to)
106
107
108 class DummyDpdkPacketGenerator():
109
110     counter = 0
111
112     def __init__(self):
113         DummyDpdkPacketGenerator.counter = [0, 0]
114
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':
121                         if vlan_0 == '-1':
122                             if vlan_1 == '-1':
123                                 DummyDpdkPacketGenerator.counter[0] += 1
124
125     def send_traffic(self):
126         DummyDpdkPacketGenerator.counter[1] += 1
127         return
128
129
130 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
131
132     counter = [0, 0, 0, 0, 0]
133
134     def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
135         DummyInstantiaionValidationBenchmark.counter[0] += 1
136
137     def _init_packet_checker(self):
138         DummyInstantiaionValidationBenchmark.counter[1] += 1
139
140     def _finalize_packet_checker(self):
141         DummyInstantiaionValidationBenchmark.counter[2] += 1
142
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
146
147     def _get_results(self):
148         DummyInstantiaionValidationBenchmark.counter[4] += 1
149         res = {'test': 'result'}
150         return res
151
152
153 class InstantiationValidationInitTest(unittest.TestCase):
154
155     def setUp(self):
156         common.BASE_DIR = os.getcwd()
157         self.iv = InstantiationValidationBenchmark('InstantiationValidation',
158                                                    dict())
159
160     def tearDown(self):
161         common.BASE_DIR = None
162
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',
167                                               dict())
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, '')
182
183     def test_init_for_success(self):
184         self.iv.init()
185
186     def test_finalize_for_success(self):
187         self.iv.finalize()
188
189     def test_get_features_for_success(self):
190
191         expected = dict()
192         expected['description'] = 'Instantiation Validation Benchmark'
193         expected['parameters'] = [
194             iv_module.THROUGHPUT,
195             iv_module.VLAN_SENDER,
196             iv_module.VLAN_RECEIVER
197         ]
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)
211
212     @mock.patch('commands.getoutput')
213     def test__get_pids_for_success(self, mock_getoutput):
214         expected = [1234]
215         mock_getoutput.return_value = '1234'
216         output = self.iv._get_pids()
217         self.assertEqual(expected, output)
218
219         expected = [1234, 4321]
220         mock_getoutput.return_value = '1234\n4321'
221         output = self.iv._get_pids()
222         self.assertEqual(expected, output)
223
224         expected = []
225         mock_getoutput.return_value = None
226         output = self.iv._get_pids()
227         self.assertEqual(expected, output)
228
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,
236                                                   mock_pids,
237                                                   mock_os_kill,
238                                                   mock_run_command):
239         global command_counter
240         global kill_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])
249
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])
266
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)
274
275         mock_get_file.side_effect = ['10', '50']
276         expected = {'failure': '1'}
277         output = self.iv._get_results()
278         self.assertEqual(expected, output)
279
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'
286         traffic_time = '60'
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),
291                          [1, 1, 1, 0, 0, 0])
292
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'
299         traffic_time = '60'
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),
304                          [0, 0, 0, 1, 1, 1])
305
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,
312                              mock_log):
313         rval = dict()
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',
319                                                   dict())
320         iv.params[iv_module.THROUGHPUT] = '1'
321         output = iv.run()
322         self.assertEqual(expected, output)
323         self.assertEqual(DummyDpdkPacketGenerator.counter,
324                          [1, 1])
325         self.assertEqual(DummyInstantiaionValidationBenchmark.counter,
326                          [1, 1, 1, 1, 1])