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