Merge "Open storperf testcase to huawei-pod2"
[yardstick.git] / yardstick / vTC / apexlake / tests / heat_manager_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 print_function
16
17 from __future__ import absolute_import
18 import os
19 import unittest
20 import logging
21 import experimental_framework.common as common
22
23 from experimental_framework import heat_manager, APEX_LAKE_ROOT
24 import mock
25
26 __author__ = 'gpetralx'
27
28
29 def get_mock_heat(version, *args, **kwargs):
30     return MockHeat()
31
32
33 class MockStacks(object):
34
35     def __init__(self, stacks):
36         self.stacks = stacks
37
38     def list(self):
39         list_name = list()
40         for stack in self.stacks:
41             list_name.append(stack.stack_name)
42         print(list_name)
43         return self.stacks
44
45     def validate(self, template=None):
46         return False
47
48     def delete(self, id):
49         for stack in self.stacks:
50             if stack.id == id:
51                 return self.stacks.remove(stack)
52
53     def create(self, stack_name=None, files=None, template=None,
54                parameters=None):
55         print(stack_name)
56         self.stacks.append(MockStack(stack_name))
57
58
59 class MockStacks_2(object):
60
61     def __init__(self, stacks):
62         self.stacks = stacks
63
64     def list(self):
65         raise Exception
66
67
68 class MockStack(object):
69
70     def __init__(self, stack_name):
71         self.name = stack_name
72
73     @property
74     def stack_status(self):
75         return self.stack_name + '_status'
76
77     @property
78     def stack_name(self):
79         return self.name
80
81     @property
82     def id(self):
83         return self.name
84
85     def __eq__(self, other):
86         return self.name == other
87
88
89 class MockHeat(object):
90
91     def __init__(self):
92         stacks = [MockStack('stack_1'), MockStack('stack_2')]
93         self.stacks_list = MockStacks(stacks)
94
95     @property
96     def stacks(self):
97         return self.stacks_list
98
99
100 class MockHeat_2(MockHeat):
101
102     def __init__(self):
103         stacks = [MockStack('stack_1'), MockStack('stack_2')]
104         self.stacks_list = MockStacks_2(stacks)
105
106
107 class HeatManagerMock(heat_manager.HeatManager):
108
109     def init_heat(self):
110         if self.heat is None:
111             self.heat = MockHeat()
112
113
114 class HeatManagerMock_2(heat_manager.HeatManager):
115
116     def init_heat(self):
117         if self.heat is None:
118             self.heat = MockHeat_2()
119
120
121 class TestHeatManager(unittest.TestCase):
122
123     def setUp(self):
124         credentials = dict()
125         credentials['ip_controller'] = '1.1.1.1'
126         credentials['heat_url'] = 'http://heat_url'
127         credentials['user'] = 'user'
128         credentials['password'] = 'password'
129         credentials['auth_uri'] = 'auth_uri'
130         credentials['project'] = 'project'
131         self.heat_manager = HeatManagerMock(credentials)
132         self.heat_manager.init_heat()
133
134     def tearDown(self):
135         pass
136
137     def test_is_stack_deployed_for_success(self):
138         self.assertTrue(self.heat_manager.is_stack_deployed('stack_1'))
139         self.assertFalse(self.heat_manager.is_stack_deployed('stack_n'))
140
141     def test_check_status_for_success(self):
142         self.assertEqual('stack_1_status',
143                          self.heat_manager.check_stack_status('stack_1'))
144         self.assertEqual('NOT_FOUND',
145                          self.heat_manager.check_stack_status('stack_x'))
146
147     def test_validate_template_for_success(self):
148         template_file = os.path.join(
149             APEX_LAKE_ROOT,
150             'tests/data/test_templates/VTC_base_single_vm_wait_1.yaml')
151         with self.assertRaises(ValueError):
152             self.heat_manager.validate_heat_template(template_file)
153
154     def test_delete_stack_for_success(self):
155         self.assertTrue(self.heat_manager.delete_stack('stack_1'))
156         self.assertFalse(self.heat_manager.delete_stack('stack_x'))
157
158     def test_delete_stack_for_success_2(self):
159         self.assertTrue(self.heat_manager.delete_stack('stack_1'))
160
161     @mock.patch('experimental_framework.common.LOG')
162     @mock.patch('heatclient.common.template_utils.get_template_contents')
163     @mock.patch('heatclient.client.Client')
164     # @mock.patch('heatclient.client.Client', side_effect=DummyHeatClient)
165     def test_create_stack_for_success(self, mock_stack_create,
166                                       mock_get_template_contents,
167                                       mock_log):
168         return_value = ({'template': 'template'}, 'template')
169         mock_get_template_contents.return_value = return_value
170         self.heat_manager.create_stack('template', 'stack_n', 'parameters')
171         self.assertTrue(self.heat_manager.is_stack_deployed('stack_n'))
172
173
174 class TestHeatManager_2(unittest.TestCase):
175
176     def setUp(self):
177         credentials = dict()
178         credentials['ip_controller'] = '1.1.1.1'
179         credentials['heat_url'] = 'http://heat_url'
180         credentials['user'] = 'user'
181         credentials['password'] = 'password'
182         credentials['auth_uri'] = 'auth_uri'
183         credentials['project'] = 'project'
184         self.heat_manager = HeatManagerMock_2(credentials)
185
186     def tearDown(self):
187         pass
188
189     def test_delete_stack_for_success_2(self):
190         common.LOG = logging.getLogger()
191         self.assertFalse(self.heat_manager.delete_stack('stack_1'))
192
193
194 class ServiceCatalog():
195
196     def url_for(self, service_type):
197         return 'http://heat_url'
198
199
200 class KeystoneMock(object):
201
202     @property
203     def auth_token(self):
204         return 'token'
205
206     service_catalog = ServiceCatalog()
207
208
209 class TestHeatInit(unittest.TestCase):
210
211     def setUp(self):
212         credentials = dict()
213         credentials['ip_controller'] = '1.1.1.1'
214         credentials['heat_url'] = 'http://heat_url'
215         credentials['user'] = 'user'
216         credentials['password'] = 'password'
217         credentials['auth_uri'] = 'auth_uri'
218         credentials['project'] = 'project'
219         self.heat_manager = heat_manager.HeatManager(credentials)
220
221     def tearDown(self):
222         pass
223
224     @mock.patch('heatclient.client.Client')
225     @mock.patch('keystoneclient.v2_0.client.Client')
226     def test_heat_init_for_sanity(self, keystone_client, heat_client):
227         keystone_client.return_value = KeystoneMock()
228         heat_client.return_value = MockHeat()
229         self.heat_manager.init_heat()
230         keystone_client.assert_called_once_with(username='user',
231                                                 tenant_name='project',
232                                                 password='password',
233                                                 auth_url='auth_uri')
234         heat_client.assert_called_once_with('1', endpoint='http://heat_url',
235                                             token='token')