b6191ed8fbe5b8e8f3d791ed81b229c8c94b5374
[yardstick.git] / yardstick / vTC / apexlake / tests / api_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 from __future__ import absolute_import
17 import unittest
18 import mock
19 import os
20 import experimental_framework.common as common
21 from experimental_framework import APEX_LAKE_ROOT
22 from experimental_framework.api import FrameworkApi
23 from experimental_framework.benchmarking_unit import BenchmarkingUnit
24 import experimental_framework.benchmarks.\
25     instantiation_validation_benchmark as iv
26 from six.moves import map
27 from six.moves import range
28
29
30 class DummyBenchmarkingUnit(BenchmarkingUnit):
31
32     def __init__(self):
33         BenchmarkingUnit.__init__(self)
34
35     @staticmethod
36     def get_available_test_cases():
37         return ['BenchA', 'BenchB']
38
39     @staticmethod
40     def get_required_benchmarks(required_benchmarks):
41         common.BASE_DIR = "base_dir/"
42         return [iv.InstantiationValidationBenchmark('benchmark', dict())]
43
44
45 class DummyBenchmarkingUnit2(BenchmarkingUnit):
46
47     counter_init = 0
48     counter_finalize = 0
49     counter_run = 0
50
51     def __init__(self, base_heat_template, credentials,
52                  heat_template_parameters, iterations, test_cases):
53         DummyBenchmarkingUnit.counter_init = 0
54         DummyBenchmarkingUnit.counter_finalize = 0
55         DummyBenchmarkingUnit.counter_run = 0
56
57     def initialize(self):
58         DummyBenchmarkingUnit2.counter_init += 1
59
60     def run_benchmarks(self):
61         DummyBenchmarkingUnit2.counter_run += 1
62
63     def finalize(self):
64         DummyBenchmarkingUnit2.counter_finalize += 1
65
66
67 class TestGeneratesTemplate(unittest.TestCase):
68
69     def setUp(self):
70         pass
71
72     def tearDown(self):
73         pass
74
75     @mock.patch('experimental_framework.common.init')
76     def test_init_for_success(self, mock_init):
77         FrameworkApi.init()
78         mock_init.assert_called_once_with(api=True)
79
80     # @mock.patch('experimental_framework.benchmarking_unit.BenchmarkingUnit.'
81     #             'get_available_test_cases',
82     #             side_effect=DummyBenchmarkingUnit.get_available_test_cases)
83     # def test_get_available_test_cases_for_success(self, mock_bench):
84     #     expected = ['BenchA', 'BenchB']
85     #     output = FrameworkApi.get_available_test_cases()
86     #     self.assertEqual(expected, output)
87
88     @mock.patch('experimental_framework.benchmarking_unit.BenchmarkingUnit.'
89                 'get_required_benchmarks',
90                 side_effect=DummyBenchmarkingUnit.get_required_benchmarks)
91     def test_get_test_case_features_for_success(self, mock_get_req_bench):
92         expected = dict()
93         expected['description'] = 'Instantiation Validation Benchmark'
94         expected['parameters'] = [
95             iv.THROUGHPUT,
96             iv.VLAN_SENDER,
97             iv.VLAN_RECEIVER]
98         expected['allowed_values'] = dict()
99         expected['allowed_values'][iv.THROUGHPUT] = \
100             list(map(str, list(range(0, 100))))
101         expected['allowed_values'][iv.VLAN_SENDER] = \
102             list(map(str, list(range(-1, 4096))))
103         expected['allowed_values'][iv.VLAN_RECEIVER] = \
104             list(map(str, list(range(-1, 4096))))
105         expected['default_values'] = dict()
106         expected['default_values'][iv.THROUGHPUT] = '1'
107         expected['default_values'][iv.VLAN_SENDER] = '-1'
108         expected['default_values'][iv.VLAN_RECEIVER] = '-1'
109
110         test_case = 'instantiation_validation_benchmark.' \
111                     'InstantiationValidationBenchmark'
112         output = FrameworkApi.get_test_case_features(test_case)
113         self.assertEqual(expected, output)
114
115     def test__get_test_case_features__for_failure(self):
116         self.assertRaises(
117             ValueError, FrameworkApi.get_test_case_features, 111)
118
119     @mock.patch('experimental_framework.common.init')
120     @mock.patch('experimental_framework.common.LOG')
121     @mock.patch('experimental_framework.common.get_credentials')
122     @mock.patch('experimental_framework.heat_template_generation.'
123                 'generates_templates')
124     @mock.patch('experimental_framework.benchmarking_unit.BenchmarkingUnit',
125                 side_effect=DummyBenchmarkingUnit2)
126     def test_execute_framework_for_success(self, mock_b_unit, mock_heat,
127                                            mock_credentials, mock_log,
128                                            mock_common_init):
129         common.TEMPLATE_DIR = os.path.join(APEX_LAKE_ROOT,
130                                            'tests/data/generated_templates/')
131
132         test_cases = dict()
133         iterations = 1
134         heat_template = 'VTC_base_single_vm_wait.tmp'
135         heat_template_parameters = dict()
136         deployment_configuration = ''
137         openstack_credentials = dict()
138         openstack_credentials['ip_controller'] = ''
139         openstack_credentials['heat_url'] = ''
140         openstack_credentials['user'] = ''
141         openstack_credentials['password'] = ''
142         openstack_credentials['auth_uri'] = ''
143         openstack_credentials['project'] = ''
144         FrameworkApi.execute_framework(
145             test_cases, iterations, heat_template,
146             heat_template_parameters, deployment_configuration,
147             openstack_credentials)