Add support to the test case required by YARDSTICK-35
[yardstick.git] / yardstick / vTC / apexlake / tests / rfc2544_throughput_benchmark_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 __author__ = 'vmriccox'
16
17
18 import unittest
19 import mock
20 from experimental_framework.benchmarks import rfc2544_throughput_benchmark \
21     as mut
22 import experimental_framework.common as common
23
24
25 class RFC2544ThroughputBenchmarkRunTest(unittest.TestCase):
26
27     def setUp(self):
28         name = 'benchmark'
29         params = dict()
30         params[mut.VLAN_SENDER] = '1'
31         params[mut.VLAN_RECEIVER] = '2'
32         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
33         common.init_log()
34
35     def tearDown(self):
36         pass
37
38     def test_get_features_for_sanity(self):
39         output = self.benchmark.get_features()
40         self.assertIsInstance(output, dict)
41         self.assertIn('parameters', output.keys())
42         self.assertIn('allowed_values', output.keys())
43         self.assertIn('default_values', output.keys())
44         self.assertIsInstance(output['parameters'], list)
45         self.assertIsInstance(output['allowed_values'], dict)
46         self.assertIsInstance(output['default_values'], dict)
47
48     def test_init(self):
49         self.assertEqual(self.benchmark.init(), None)
50
51     def test_finalize(self):
52         self.assertEqual(self.benchmark.finalize(), None)
53
54     @mock.patch('experimental_framework.benchmarks.'
55                 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark.'
56                 '_reset_lua_file')
57     @mock.patch('experimental_framework.benchmarks.'
58                 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark.'
59                 '_configure_lua_file')
60     @mock.patch('experimental_framework.benchmarks.'
61                 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark.'
62                 '_extract_packet_size_from_params')
63     @mock.patch('experimental_framework.benchmarks.'
64                 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark.'
65                 '_get_results')
66     @mock.patch('experimental_framework.benchmarks.'
67                 'rfc2544_throughput_benchmark.dpdk.DpdkPacketGenerator')
68     def test_run_for_success(self, mock_dpdk, mock_get_results,
69                              mock_extract_size, conf_lua_file_mock,
70                              reset_lua_file_mock):
71         expected = {'results': 0, 'packet_size': '1'}
72         mock_extract_size.return_value = '1'
73         mock_get_results.return_value = {'results': 0}
74         output = self.benchmark.run()
75         self.assertEqual(expected, output)
76         conf_lua_file_mock.assert_called_once()
77         reset_lua_file_mock.assert_called_once()
78         dpdk_instance = mock_dpdk()
79         dpdk_instance.init_dpdk_pktgen.assert_called_once_with(
80             dpdk_interfaces=2, pcap_file_0='packet_1.pcap',
81             pcap_file_1='igmp.pcap', lua_script='rfc2544.lua',
82             vlan_0='1', vlan_1='2')
83         dpdk_instance.send_traffic.assert_called_once_with()
84
85
86 class RFC2544ThroughputBenchmarkOthers(unittest.TestCase):
87
88     def setUp(self):
89         name = 'benchmark'
90         params = {'packet_size': '128'}
91         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
92
93     def tearDown(self):
94         pass
95
96     def test__extract_packet_size_from_params_for_success(self):
97         expected = '128'
98         output = self.benchmark._extract_packet_size_from_params()
99         self.assertEqual(expected, output)
100
101     @mock.patch('experimental_framework.common.replace_in_file')
102     def test__configure_lua_file(self, mock_common_replace_in_file):
103         self.benchmark.lua_file = 'lua_file'
104         self.benchmark.results_file = 'result_file'
105         self.benchmark._configure_lua_file()
106         mock_common_replace_in_file.\
107             assert_called_once_with('lua_file', 'local out_file = ""',
108                                     'local out_file = "result_file"')
109
110     @mock.patch('experimental_framework.common.replace_in_file')
111     def test__reset_lua_file(self, mock_common_replace_in_file):
112         self.benchmark.lua_file = 'lua_file'
113         self.benchmark.results_file = 'result_file'
114         self.benchmark._reset_lua_file()
115         mock_common_replace_in_file.\
116             assert_called_once_with('lua_file',
117                                     'local out_file = "result_file"',
118                                     'local out_file = ""')
119
120
121 class RFC2544ThroughputBenchmarkGetResultsTest(unittest.TestCase):
122
123     def setUp(self):
124         pass
125
126     def tearDown(self):
127         pass
128
129     @mock.patch('experimental_framework.common.get_file_first_line')
130     def test__get_results_for_success(self, mock_common_file_line):
131         name = 'benchmark'
132         params = {'packet_size': '128'}
133         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
134         self.benchmark.results_file = 'base_dir/experimental_framework/' \
135                                       'packet_generators/dpdk_pktgen/' \
136                                       'experiment.res'
137         mock_common_file_line.return_value = '10'
138         expected = {'throughput': 10}
139         output = self.benchmark._get_results()
140         self.assertEqual(expected, output)
141         mock_common_file_line.\
142             assert_called_once_with('base_dir/experimental_framework/'
143                                     'packet_generators/dpdk_pktgen/'
144                                     'experiment.res')
145
146     @mock.patch('experimental_framework.common.get_file_first_line')
147     def test__get_results_for_success_2(self, mock_common_file_line):
148         name = 'benchmark'
149         params = {'packet_size': '128'}
150         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
151         self.benchmark.results_file = 'base_dir/experimental_framework/' \
152                                       'packet_generators/dpdk_pktgen/' \
153                                       'experiment.res'
154         mock_common_file_line.return_value = '1XXX0'
155         expected = {'throughput': 0}
156         output = self.benchmark._get_results()
157         self.assertEqual(expected, output)
158         mock_common_file_line.\
159             assert_called_once_with('base_dir/experimental_framework/'
160                                     'packet_generators/dpdk_pktgen/'
161                                     'experiment.res')