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