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