aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/vTC/apexlake/tests/deployment_unit_test.py
blob: 81171670cd0fa82c371c06276f24a005eede315c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Copyright (c) 2015 Intel Research and Development Ireland Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
import mock
import experimental_framework.deployment_unit as mut

__author__ = 'vmriccox'


class DummyHeatManager:

    def __init__(self, param):
        self.counts = 0
        pass

    def validate_heat_template(self, template_file):
        return True

    def check_stack_status(self, stack_name):
        # return 'CREATE_COMPLETE'
        self.counts += 1
        if self.counts >= 3:
            return 'CREATE_COMPLETE'
        else:
            return 'CREATE_IN_PROGRESS'

    def delete_stack(self, stack_name):
        pass


class DummyHeatManagerFailed(DummyHeatManager):

    def check_stack_status(self, stack_name):
        return 'CREATE_FAILED'

    def create_stack(self, template_file, stack_name, parameters):
        pass


class DummyHeatManagerComplete(DummyHeatManager):

    def check_stack_status(self, stack_name):
        return 'CREATE_COMPLETE'

    def create_stack(self, template_file, stack_name, parameters):
        raise Exception()


class DummyHeatManagerFailedException(DummyHeatManagerFailed):

    def create_stack(self, template_file, stack_name, parameters):
        raise Exception

    def check_stack_status(self, stack_name):
        return ''


class DummyHeatManagerDestroy:

    def __init__(self, credentials):
        self.delete_stack_counter = 0
        self.check_stack_status_counter = 0

    def check_stack_status(self, stack_name):
        if self.check_stack_status_counter < 2:
            self.check_stack_status_counter += 1
            return 'DELETE_IN_PROGRESS'
        else:
            return 'DELETE_COMPLETE'

    def create_stack(self, template_file, stack_name, parameters):
        pass

    def delete_stack(self, stack_name=None):
        if stack_name == 'stack':
            self.delete_stack_counter += 1
        else:
            return self.delete_stack_counter

    def is_stack_deployed(self, stack_name):
        return True


class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):

    def delete_stack(self, stack_name=None):
        raise Exception


class DummyHeatManagerReiteration:

    def __init__(self, param):
        self.counts = 0

    def validate_heat_template(self, template_file):
        return True

    def check_stack_status(self, stack_name):
        return 'CREATE_FAILED'

    def delete_stack(self, stack_name):
        pass

    def create_stack(self, template_file=None, stack_name=None,
                     parameters=None):
        if template_file == 'template_reiteration' and \
            stack_name == 'stack_reiteration' and \
                parameters == 'parameters_reiteration':
            self.counts += 1


class DummyDeploymentUnit(mut.DeploymentUnit):

    def destroy_heat_template(self, stack_name):
        raise Exception


class TestDeploymentUnit(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManager)
    def test_constructor_for_sanity(self, mock_heat_manager):
        du = mut.DeploymentUnit(dict())
        self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
        mock_heat_manager.assert_called_once_with(dict())
        self.assertEqual(du.deployed_stacks, list())

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManager)
    @mock.patch('os.path.isfile')
    def test_deploy_heat_template_for_failure(self, mock_os_is_file,
                                              mock_heat_manager):
        mock_os_is_file.return_value = False
        du = mut.DeploymentUnit(dict())
        template_file = ''
        stack_name = ''
        parameters = ''
        self.assertRaises(ValueError, du.deploy_heat_template, template_file,
                          stack_name, parameters, 0)

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManager)
    @mock.patch('os.path.isfile')
    def test_deploy_heat_template_for_success(self, mock_os_is_file,
                                              mock_heat_manager):
        mock_os_is_file.return_value = True
        du = mut.DeploymentUnit(dict())
        template_file = ''
        stack_name = ''
        parameters = ''
        output = du.deploy_heat_template(template_file, stack_name,
                                         parameters, 0)
        self.assertEqual(output, True)

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerComplete)
    @mock.patch('os.path.isfile')
    def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
                                                mock_heat_manager):
        mock_os_is_file.return_value = True
        du = mut.DeploymentUnit(dict())
        template_file = ''
        stack_name = ''
        parameters = ''
        output = du.deploy_heat_template(template_file, stack_name,
                                         parameters, 0)
        self.assertEqual(output, True)

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerComplete)
    @mock.patch('os.path.isfile')
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
                side_effect=DummyDeploymentUnit)
    def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
                                                mock_os_is_file,
                                                mock_heat_manager):
        mock_os_is_file.return_value = True
        du = mut.DeploymentUnit(dict())
        template_file = ''
        stack_name = ''
        parameters = ''
        output = du.deploy_heat_template(template_file, stack_name,
                                         parameters, 0)
        self.assertEqual(output, True)

    @mock.patch('experimental_framework.common.LOG')
    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerFailed)
    @mock.patch('os.path.isfile')
    def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
                                                mock_heat_manager, mock_log):
        mock_os_is_file.return_value = True
        du = DummyDeploymentUnit(dict())
        template_file = ''
        stack_name = ''
        parameters = ''
        output = du.deploy_heat_template(template_file, stack_name,
                                         parameters, 0)
        self.assertEqual(output, False)

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerDestroy)
    @mock.patch('experimental_framework.common.LOG')
    def test_destroy_heat_template_for_success(self, mock_log,
                                               mock_heat_manager):
        openstack_credentials = dict()
        du = mut.DeploymentUnit(openstack_credentials)
        du.deployed_stacks = ['stack']
        stack_name = 'stack'
        self.assertTrue(du.destroy_heat_template(stack_name))
        self.assertEqual(du.heat_manager.delete_stack(None), 1)

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerDestroyException)
    @mock.patch('experimental_framework.common.LOG')
    def test_destroy_heat_template_for_success_2(self, mock_log,
                                                 mock_heat_manager):
        openstack_credentials = dict()
        du = mut.DeploymentUnit(openstack_credentials)
        du.deployed_stacks = ['stack']
        stack_name = 'stack'
        self.assertFalse(du.destroy_heat_template(stack_name))

    def test_destroy_all_deployed_stacks_for_success(self):
        du = DeploymentUnitDestroy()
        du.destroy_all_deployed_stacks()
        self.assertTrue(du.destroy_heat_template())

    @mock.patch('experimental_framework.heat_manager.HeatManager',
                side_effect=DummyHeatManagerReiteration)
    @mock.patch('os.path.isfile')
    def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
                                                mock_heat_manager):
        mock_os_is_file.return_value = True
        du = mut.DeploymentUnit(dict())
        template = 'template_reiteration'
        stack = 'stack_reiteration'
        parameters = 'parameters_reiteration'
        output = du.deploy_heat_template(template, stack, parameters, 0)
        self.assertFalse(output)
        self.assertEqual(du.heat_manager.counts, 4)


class DeploymentUnitDestroy(mut.DeploymentUnit):

    def __init__(self):
        self.deployed_stacks = ['stack']
        self.heat_manager = DummyHeatManagerDestroy(dict())
        self.destroy_all_deployed_stacks_called_correctly = False

    def destroy_heat_template(self, template_name=None):
        if template_name == 'stack':
            self.destroy_all_deployed_stacks_called_correctly = True
        return self.destroy_all_deployed_stacks_called_correctly