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