369129a009d93d04fd833d4f7d815ab6cbd28eec
[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                     ' netmask 255.255.255.248':
70         command_counter[1] += 1
71         return
72     elif command == "sudo echo 'mgroup from interface.100 group" \
73                     " 224.192.16.1' > /etc/smcroute.conf":
74         command_counter[2] += 1
75         return
76     elif command == "sudo smcroute -d":
77         command_counter[3] += 1
78         return
79     elif command == "sudo test_sniff interface.100 128 &":
80         command_counter[4] += 1
81         return
82
83
84 def dummy_replace_in_file(file, str_from, str_to, get_couters=None):
85     if get_couters:
86         return replace_counter
87     if file == 'file':
88         if str_from == 'local out_file = "result_file"':
89             if str_to == 'local out_file = ""':
90                 replace_counter[0] += 1
91                 return
92         if str_from == 'local traffic_rate = 100':
93             if str_to == 'local traffic_rate = 0':
94                 replace_counter[1] += 1
95                 return
96         if str_from == 'local traffic_delay = 60':
97             if str_to == 'local traffic_delay = 0':
98                 replace_counter[2] += 1
99                 return
100         if str_from == 'local out_file = ""':
101             if str_to == 'local out_file = "result_file"':
102                 replace_counter[3] += 1
103                 return
104         if str_from == 'local traffic_rate = 0':
105             if str_to == 'local traffic_rate = 100':
106                 replace_counter[4] += 1
107                 return
108         if str_from == 'local traffic_delay = 0':
109             if str_to == 'local traffic_delay = 60':
110                 replace_counter[5] += 1
111                 return
112     raise Exception(file + ' ' + str_from + ' ' + str_to)
113
114
115 class DummyDpdkPacketGenerator():
116
117     counter = 0
118
119     def __init__(self):
120         DummyDpdkPacketGenerator.counter = [0, 0]
121
122     def init_dpdk_pktgen(self, dpdk_interfaces, lua_script, pcap_file_0,
123                          pcap_file_1, vlan_0, vlan_1):
124         if dpdk_interfaces == 1:
125             if lua_script == 'constant_traffic.lua':
126                 if pcap_file_0 == 'packet_512.pcap':
127                     if pcap_file_1 == 'igmp.pcap':
128                         if vlan_0 == '-1':
129                             if vlan_1 == '-1':
130                                 DummyDpdkPacketGenerator.counter[0] += 1
131
132     def send_traffic(self):
133         DummyDpdkPacketGenerator.counter[1] += 1
134         return
135
136
137 class DummyInstantiaionValidationBenchmark(InstantiationValidationBenchmark):
138
139     counter = [0, 0, 0, 0, 0]
140
141     def _configure_lua_file(self, traffic_rate_percentage, traffic_time):
142         DummyInstantiaionValidationBenchmark.counter[0] += 1
143
144     def _init_packet_checker(self):
145         DummyInstantiaionValidationBenchmark.counter[1] += 1
146
147     def _finalize_packet_checker(self):
148         DummyInstantiaionValidationBenchmark.counter[2] += 1
149
150     def _reset_lua_file(self, traffic_rate_percentage, traffic_time):
151         if traffic_rate_percentage == '1' and traffic_time == '10':
152             DummyInstantiaionValidationBenchmark.counter[3] += 1
153
154     def _get_results(self):
155         DummyInstantiaionValidationBenchmark.counter[4] += 1
156         res = {'test': 'result'}
157         return res
158
159
160 class InstantiationValidationInitTest(unittest.TestCase):
161
162     def setUp(self):
163         common.BASE_DIR = os.getcwd()
164         self.iv = InstantiationValidationBenchmark('InstantiationValidation',
165                                                    dict())
166
167     def tearDown(self):
168         common.BASE_DIR = None
169
170     @mock.patch('experimental_framework.common.get_base_dir')
171     def test___init___for_success(self, mock_base_dir):
172         mock_base_dir.return_value = 'base_dir/'
173         iv = InstantiationValidationBenchmark('InstantiationValidation',
174                                               dict())
175         self.assertEqual(iv.base_dir,
176                          'base_dir/experimental_framework/'
177                          'packet_generators/dpdk_pktgen/')
178         self.assertEqual(iv.results_file,
179                          'base_dir/experimental_framework/'
180                          'packet_generators/dpdk_pktgen/packets.res')
181         self.assertEqual(iv.lua_file,
182                          'base_dir/experimental_framework/'
183                          'packet_generators/dpdk_pktgen/constant_traffic.lua')
184         self.assertEqual(iv.pkt_checker_command,
185                          'base_dir/experimental_framework/'
186                          'libraries/packet_checker/test_sniff ')
187         self.assertEqual(iv.res_dir, '')
188         self.assertEqual(iv.interface_name, '')
189
190     def test_init_for_success(self):
191         self.iv.init()
192
193     def test_finalize_for_success(self):
194         self.iv.finalize()
195
196     def test_get_features_for_success(self):
197
198         expected = dict()
199         expected['description'] = 'Instantiation Validation Benchmark'
200         expected['parameters'] = [
201             iv_module.THROUGHPUT,
202             iv_module.VLAN_SENDER,
203             iv_module.VLAN_RECEIVER
204         ]
205         expected['allowed_values'] = dict()
206         expected['allowed_values'][iv_module.THROUGHPUT] = \
207             map(str, range(0, 100))
208         expected['allowed_values'][iv_module.VLAN_SENDER] = \
209             map(str, range(-1, 4096))
210         expected['allowed_values'][iv_module.VLAN_RECEIVER] = \
211             map(str, range(-1, 4096))
212         expected['default_values'] = dict()
213         expected['default_values'][iv_module.THROUGHPUT] = '1'
214         expected['default_values'][iv_module.VLAN_SENDER] = '-1'
215         expected['default_values'][iv_module.VLAN_RECEIVER] = '-1'
216         output = self.iv.get_features()
217         self.assertEqual(expected, output)
218
219     @mock.patch('commands.getoutput')
220     def test__get_pids_for_success(self, mock_getoutput):
221         expected = [1234]
222         mock_getoutput.return_value = '1234'
223         output = self.iv._get_pids()
224         self.assertEqual(expected, output)
225
226         expected = [1234, 4321]
227         mock_getoutput.return_value = '1234\n4321'
228         output = self.iv._get_pids()
229         self.assertEqual(expected, output)
230
231         expected = []
232         mock_getoutput.return_value = None
233         output = self.iv._get_pids()
234         self.assertEqual(expected, output)
235
236     @mock.patch('experimental_framework.common.run_command',
237                 side_effect=dummy_run_command)
238     @mock.patch('os.kill', side_effect=dummy_os_kill)
239     @mock.patch('experimental_framework.benchmarks.'
240                 'instantiation_validation_benchmark.'
241                 'InstantiationValidationBenchmark._get_pids')
242     def test__finalize_packet_checker_for_success(self,
243                                                   mock_pids,
244                                                   mock_os_kill,
245                                                   mock_run_command):
246         global command_counter
247         global kill_counter
248         command_counter = [0, 0, 0, 0, 0]
249         kill_counter = [0, 0]
250         mock_pids.return_value = [1234, 4321]
251         self.iv.interface_name = 'interface'
252         self.iv.params[iv_module.VLAN_RECEIVER] = '100'
253         self.iv._finalize_packet_checker()
254         self.assertEqual(dummy_os_kill('', '', True), [1, 1])
255         self.assertEqual(dummy_run_command('', True), [1, 1, 0, 0, 0])
256
257     @mock.patch('os.chdir')
258     @mock.patch('experimental_framework.common.run_command',
259                 side_effect=dummy_run_command_2)
260     @mock.patch('experimental_framework.benchmarks.'
261                 'instantiation_validation_benchmark.'
262                 'InstantiationValidationBenchmark._get_pids')
263     @mock.patch('os.kill', side_effect=dummy_os_kill)
264     def test__init_packet_checker_for_success(self, mock_kill, mock_pids,
265                                               mock_run_command, mock_chdir):
266         global command_counter
267         command_counter = [0, 0, 0, 0, 0]
268         mock_pids.return_value = [1234, 4321]
269         self.iv.pkt_checker_command = 'test_sniff '
270         self.iv.interface_name = 'interface'
271         self.iv.params[iv_module.VLAN_RECEIVER] = '100'
272         self.iv._init_packet_checker()
273         self.assertEqual(dummy_run_command('', True), [1, 1, 1, 1, 1])
274
275     @mock.patch('experimental_framework.common.get_file_first_line')
276     def test__get_results_for_success(self, mock_get_file):
277         self.iv.res_dir = 'directory'
278         mock_get_file.side_effect = ['100', '50']
279         expected = {'failure': '0'}
280         output = self.iv._get_results()
281         self.assertEqual(expected, output)
282
283         mock_get_file.side_effect = ['10', '50']
284         expected = {'failure': '1'}
285         output = self.iv._get_results()
286         self.assertEqual(expected, output)
287
288     @mock.patch('experimental_framework.common.replace_in_file',
289                 side_effect=dummy_replace_in_file)
290     def test__reset_lua_file_for_success(self, mock_replace):
291         global replace_counter
292         replace_counter = [0, 0, 0, 0, 0, 0]
293         traffic_rate_percentage = '100'
294         traffic_time = '60'
295         self.iv.lua_file = 'file'
296         self.iv.results_file = 'result_file'
297         self.iv._reset_lua_file(traffic_rate_percentage, traffic_time)
298         self.assertEqual(dummy_replace_in_file('', '', '', True),
299                          [1, 1, 1, 0, 0, 0])
300
301     @mock.patch('experimental_framework.common.replace_in_file',
302                 side_effect=dummy_replace_in_file)
303     def test__configure_lua_file_for_success(self, mock_replace):
304         global replace_counter
305         replace_counter = [0, 0, 0, 0, 0, 0]
306         traffic_rate_percentage = '100'
307         traffic_time = '60'
308         self.iv.lua_file = 'file'
309         self.iv.results_file = 'result_file'
310         self.iv._configure_lua_file(traffic_rate_percentage, traffic_time)
311         self.assertEqual(dummy_replace_in_file('', '', '', True),
312                          [0, 0, 0, 1, 1, 1])
313
314     @mock.patch('experimental_framework.common.LOG')
315     @mock.patch('experimental_framework.packet_generators.'
316                 'dpdk_packet_generator.DpdkPacketGenerator',
317                 side_effect=DummyDpdkPacketGenerator)
318     @mock.patch('experimental_framework.common.get_dpdk_pktgen_vars')
319     def test_run_for_success(self, mock_common_get_vars, mock_pktgen,
320                              mock_log):
321         rval = dict()
322         rval[cfs.CFSP_DPDK_BUS_SLOT_NIC_2] = 'bus_2'
323         rval[cfs.CFSP_DPDK_NAME_IF_2] = 'if_2'
324         mock_common_get_vars.return_value = rval
325         expected = {'test': 'result'}
326         iv = DummyInstantiaionValidationBenchmark('InstantiationValidation',
327                                                   dict())
328         iv.params[iv_module.THROUGHPUT] = '1'
329         output = iv.run()
330         self.assertEqual(expected, output)
331         self.assertEqual(DummyDpdkPacketGenerator.counter,
332                          [1, 1])
333         self.assertEqual(DummyInstantiaionValidationBenchmark.counter,
334                          [1, 1, 1, 1, 1])