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