Add utility tools to ApexLake
[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     raise Exception(command)
76
77
78 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
79     if get_couters:
80         return replace_counter
81     if file == 'file':
82         if str_from == 'local out_file = "result_file"':
83             if str_to == 'local out_file = ""':
84                 replace_counter[0] += 1
85                 return
86         if str_from == 'local traffic_rate = 100':
87             if str_to == 'local traffic_rate = 0':
88                 replace_counter[1] += 1
89                 return
90         if str_from == 'local traffic_delay = 60':
91             if str_to == 'local traffic_delay = 0':
92                 replace_counter[2] += 1
93                 return
94         if str_from == 'local out_file = ""':
95             if str_to == 'local out_file = "result_file"':
96                 replace_counter[3] += 1
97                 return
98         if str_from == 'local traffic_rate = 0':
99             if str_to == 'local traffic_rate = 100':
100                 replace_counter[4] += 1
101                 return
102         if str_from == 'local traffic_delay = 0':
103             if str_to == 'local traffic_delay = 60':
104                 replace_counter[5] += 1
105                 return
106     raise Exception(file + ' ' + str_from + ' ' + str_to)
107
108
109 class DummyDpdkPacketGenerator():
110
111     counter = 0
112
113     def __init__(self):
114         DummyDpdkPacketGenerator.counter = [0, 0]
115
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':
122                         if vlan_0 == '-1':
123                             if vlan_1 == '-1':
124                                 DummyDpdkPacketGenerator.counter[0] += 1
125
126     def send_traffic(self):
127         DummyDpdkPacketGenerator.counter[1] += 1
128         return
129
130
131 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
132
133     counter = [0, 0, 0, 0, 0]
134
135     def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
136         DummyInstantiaionValidationBenchmark.counter[0] += 1
137
138     def _init_packet_checker(self):
139         DummyInstantiaionValidationBenchmark.counter[1] += 1
140
141     def _finalize_packet_checker(self):
142         DummyInstantiaionValidationBenchmark.counter[2] += 1
143
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
147
148     def _get_results(self):
149         DummyInstantiaionValidationBenchmark.counter[4] += 1
150         res = {'test': 'result'}
151         return res
152
153
154 class InstantiationValidationInitTest(unittest.TestCase):
155
156     def setUp(self):
157         common.BASE_DIR = os.getcwd()
158         self.iv = InstantiationValidationBenchmark('InstantiationValidation',
159                                                    dict())
160
161     def tearDown(self):
162         common.BASE_DIR = None
163
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',
168                                               dict())
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, '')
183
184     def test_init_for_success(self):
185         self.iv.init()
186
187     def test_finalize_for_success(self):
188         self.iv.finalize()
189
190     def test_get_features_for_success(self):
191
192         expected = dict()
193         expected['description'] = 'Instantiation Validation Benchmark'
194         expected['parameters'] = [
195             iv_module.THROUGHPUT,
196             iv_module.VLAN_SENDER,
197             iv_module.VLAN_RECEIVER
198         ]
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)
212
213     @mock.patch('commands.getoutput')
214     def test__get_pids_for_success(self, mock_getoutput):
215         expected = [1234]
216         mock_getoutput.return_value = '1234'
217         output = self.iv._get_pids()
218         self.assertEqual(expected, output)
219
220         expected = [1234, 4321]
221         mock_getoutput.return_value = '1234\n4321'
222         output = self.iv._get_pids()
223         self.assertEqual(expected, output)
224
225         expected = []
226         mock_getoutput.return_value = None
227         output = self.iv._get_pids()
228         self.assertEqual(expected, output)
229
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,
237                                                   mock_pids,
238                                                   mock_os_kill,
239                                                   mock_run_command):
240         global command_counter
241         global kill_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])
250
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])
267
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)
275
276         mock_get_file.side_effect = ['10', '50']
277         expected = {'failure': '1'}
278         output = self.iv._get_results()
279         self.assertEqual(expected, output)
280
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'
287         traffic_time = '60'
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),
292                          [1, 1, 1, 0, 0, 0])
293
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'
300         traffic_time = '60'
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),
305                          [0, 0, 0, 1, 1, 1])
306
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,
313                              mock_log):
314         rval = dict()
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',
320                                                   dict())
321         iv.params[iv_module.THROUGHPUT] = '1'
322         output = iv.run()
323         self.assertEqual(expected, output)
324         self.assertEqual(DummyDpdkPacketGenerator.counter,
325                          [1, 1])
326         self.assertEqual(DummyInstantiaionValidationBenchmark.counter,
327                          [1, 1, 1, 1, 1])