Fix small things for integration of ApexLake with Yardstick
[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
16 import unittest
17 import mock
18 import os
19 from experimental_framework.benchmarks import rfc2544_throughput_benchmark \
20     as mut
21 import experimental_framework.common as common
22
23
24 class RFC2544ThroughputBenchmarkRunTest(unittest.TestCase):
25
26     def setUp(self):
27         name = 'benchmark'
28         params = dict()
29         params[mut.VLAN_SENDER] = '1'
30         params[mut.VLAN_RECEIVER] = '2'
31         common.BASE_DIR = os.getcwd()
32         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
33
34     def tearDown(self):
35         common.BASE_DIR = None
36
37     def test_get_features_for_sanity(self):
38         output = self.benchmark.get_features()
39         self.assertIsInstance(output, dict)
40         self.assertIn('parameters', output.keys())
41         self.assertIn('allowed_values', output.keys())
42         self.assertIn('default_values', output.keys())
43         self.assertIsInstance(output['parameters'], list)
44         self.assertIsInstance(output['allowed_values'], dict)
45         self.assertIsInstance(output['default_values'], dict)
46
47     def test_init(self):
48         self.assertEqual(self.benchmark.init(), None)
49
50     def test_finalize(self):
51         self.assertEqual(self.benchmark.finalize(), None)
52
53     @mock.patch('experimental_framework.common.LOG')
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, mock_common_log):
71         expected = {'results': 0}
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         common.BASE_DIR = os.getcwd()
92         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
93
94     def tearDown(self):
95         common.BASE_DIR = None
96
97     def test__extract_packet_size_from_params_for_success(self):
98         expected = '128'
99         output = self.benchmark._extract_packet_size_from_params()
100         self.assertEqual(expected, output)
101
102     @mock.patch('experimental_framework.common.replace_in_file')
103     def test__configure_lua_file(self, mock_common_replace_in_file):
104         self.benchmark.lua_file = 'lua_file'
105         self.benchmark.results_file = 'result_file'
106         self.benchmark._configure_lua_file()
107         mock_common_replace_in_file.\
108             assert_called_once_with('lua_file', 'local out_file = ""',
109                                     'local out_file = "result_file"')
110
111     @mock.patch('experimental_framework.common.replace_in_file')
112     def test__reset_lua_file(self, mock_common_replace_in_file):
113         self.benchmark.lua_file = 'lua_file'
114         self.benchmark.results_file = 'result_file'
115         self.benchmark._reset_lua_file()
116         mock_common_replace_in_file.\
117             assert_called_once_with('lua_file',
118                                     'local out_file = "result_file"',
119                                     'local out_file = ""')
120
121
122 class RFC2544ThroughputBenchmarkGetResultsTest(unittest.TestCase):
123
124     def setUp(self):
125         common.BASE_DIR = os.getcwd()
126
127     def tearDown(self):
128         common.BASE_DIR = None
129
130     @mock.patch('experimental_framework.common.get_file_first_line')
131     def test__get_results_for_success(self, mock_common_file_line):
132         name = 'benchmark'
133         params = {'packet_size': '128'}
134         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
135         self.benchmark.results_file = 'base_dir/experimental_framework/' \
136                                       'packet_generators/dpdk_pktgen/' \
137                                       'experiment.res'
138         mock_common_file_line.return_value = '10'
139         expected = {'throughput': 10}
140         output = self.benchmark._get_results()
141         self.assertEqual(expected, output)
142         mock_common_file_line.\
143             assert_called_once_with('base_dir/experimental_framework/'
144                                     'packet_generators/dpdk_pktgen/'
145                                     'experiment.res')
146
147     @mock.patch('experimental_framework.common.get_file_first_line')
148     def test__get_results_for_success_2(self, mock_common_file_line):
149         name = 'benchmark'
150         params = {'packet_size': '128'}
151         self.benchmark = mut.RFC2544ThroughputBenchmark(name, params)
152         self.benchmark.results_file = 'base_dir/experimental_framework/' \
153                                       'packet_generators/dpdk_pktgen/' \
154                                       'experiment.res'
155         mock_common_file_line.return_value = '1XXX0'
156         expected = {'throughput': 0}
157         output = self.benchmark._get_results()
158         self.assertEqual(expected, output)
159         mock_common_file_line.\
160             assert_called_once_with('base_dir/experimental_framework/'
161                                     'packet_generators/dpdk_pktgen/'
162                                     'experiment.res')