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