Add support for Python 3
[yardstick.git] / yardstick / vTC / apexlake / tests / multi_tenancy_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 from __future__ import absolute_import
16 import unittest
17 import mock
18 import os
19 import experimental_framework.common as common
20 from experimental_framework.benchmarks \
21     import multi_tenancy_throughput_benchmark as bench
22 from six.moves import range
23
24 __author__ = 'gpetralx'
25
26
27 class MockDeploymentUnit(object):
28
29     def deploy_heat_template(self, temp_file, stack_name, heat_param):
30         pass
31
32     def destroy_heat_template(self, stack_name):
33         pass
34
35
36 def get_deployment_unit():
37     return MockDeploymentUnit()
38
39
40 class TestMultiTenancyThroughputBenchmark(unittest.TestCase):
41
42     def setUp(self):
43         name = 'benchmark'
44         params = dict()
45         common.BASE_DIR = os.getcwd()
46         self.benchmark = bench.MultiTenancyThroughputBenchmark(name, params)
47
48     def tearDown(self):
49         pass
50
51     def test_get_features_for_sanity(self):
52         output = self.benchmark.get_features()
53         self.assertIsInstance(output, dict)
54         self.assertIn('parameters', list(output.keys()))
55         self.assertIn('allowed_values', list(output.keys()))
56         self.assertIn('default_values', list(output.keys()))
57         self.assertIsInstance(output['parameters'], list)
58         self.assertIsInstance(output['allowed_values'], dict)
59         self.assertIsInstance(output['default_values'], dict)
60
61     @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT',
62                 side_effect=get_deployment_unit)
63     @mock.patch('experimental_framework.common.replace_in_file')
64     def test_init_for_success(self, replace_in_file, deployment_unit):
65         num_of_neighbours = 5
66         num_of_cores = '3'
67         amount_of_ram = '250M'
68
69         self.benchmark.lua_file = 'lua_file'
70         self.benchmark.results_file = 'result_file'
71         self.benchmark.params['num_of_neighbours'] = str(num_of_neighbours)
72         self.benchmark.params['number_of_cores'] = num_of_cores
73         self.benchmark.params['amount_of_ram'] = amount_of_ram
74         self.benchmark.init()
75
76         param_1 = 'lua_file'
77         param_2 = 'local out_file = ""'
78         param_3 = 'local out_file = "result_file"'
79         replace_in_file.assert_called_once_with(param_1, param_2, param_3)
80
81         heat_param = dict()
82         heat_param['cores'] = num_of_cores
83         heat_param['memory'] = amount_of_ram
84         heat_param['network'] = ''
85         heat_param['subnet'] = ''
86         neighbor_stack_names = list()
87
88         deployment_unit.\
89             deploy_heat_template.assert_called_with(
90                 self.benchmark.template_file,
91                 'neighbour' + str(num_of_neighbours - 1), heat_param)
92
93         for i in range(0, num_of_neighbours):
94             neighbor_stack_names.append('neighbour' + str(i))
95
96         self.assertListEqual(neighbor_stack_names,
97                              self.benchmark.neighbor_stack_names)
98
99     @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT',
100                 side_effect=get_deployment_unit)
101     @mock.patch('experimental_framework.common.replace_in_file')
102     def test_finalize_for_success(self, replace_in_file, deployment_unit):
103         num_of_neighbours = 5
104         self.benchmark.lua_file = 'lua_file'
105         self.benchmark.results_file = 'result_file'
106         self.benchmark.params['num_of_neighbours'] = str(num_of_neighbours)
107         self.benchmark.neighbor_stack_names = list()
108         self.benchmark.neighbor_stack_names.append(str(num_of_neighbours - 1))
109         self.benchmark.finalize()
110
111         param_1 = 'lua_file'
112         param_2 = 'local out_file = "result_file"'
113         param_3 = 'local out_file = ""'
114         replace_in_file.assert_called_once_with(param_1, param_2, param_3)
115
116         deployment_unit.\
117             destroy_heat_template.\
118             assert_called_with(str(num_of_neighbours - 1))
119         self.assertListEqual(list(), self.benchmark.neighbor_stack_names)