~ubuntu-branches/ubuntu/vivid/sahara/vivid-proposed

« back to all changes in this revision

Viewing changes to sahara/tests/unit/db/templates/test_delete.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2015-04-09 15:13:04 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20150409151304-ds2wzht638m8c74k
Tags: 2015.1~rc1-0ubuntu1
* New upstream milestone release:
  - debian/control: Align with upstream dependencies.
  - debian/patches/fix-alembic-migration-for-sqlite-1.patch: Refreshed.
  - debian/rules: Install the correct sample configuration file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015 Red Hat, Inc.
 
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
# Unless required by applicable law or agreed to in writing, software
 
9
# distributed under the License is distributed on an "AS IS" BASIS,
 
10
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
11
# implied.
 
12
# See the License for the specific language governing permissions and
 
13
# limitations under the License.
 
14
 
 
15
import copy
 
16
 
 
17
from sahara import context
 
18
from sahara.db.templates import api as template_api
 
19
from sahara.db.templates import utils as u
 
20
from sahara.tests.unit.conductor import base
 
21
from sahara.tests.unit.db.templates import common as c
 
22
 
 
23
 
 
24
class Config(c.Config):
 
25
    def __init__(self, option_values={}):
 
26
        if "name" not in option_values:
 
27
            option_values["name"] = "delete"
 
28
        super(Config, self).__init__(option_values)
 
29
 
 
30
 
 
31
class TemplateDeleteTestCase(base.ConductorManagerTestCase):
 
32
    def setUp(self):
 
33
        super(TemplateDeleteTestCase, self).setUp()
 
34
        self.logger = c.Logger()
 
35
        template_api.set_logger(self.logger)
 
36
 
 
37
    def test_node_group_template_delete_by_id(self):
 
38
        self.logger.clear_log()
 
39
        ctx = context.ctx()
 
40
        t = self.api.node_group_template_create(ctx, c.SAMPLE_NGT)
 
41
 
 
42
        option_values = {"tenant_id": t["tenant_id"],
 
43
                         "id": t["id"]}
 
44
        template_api.set_conf(Config(option_values))
 
45
 
 
46
        template_api.do_node_group_template_delete_by_id()
 
47
        msg = 'Deleted node group template {info}'.format(
 
48
            info=u.name_and_id(t))
 
49
        self.assertIn(msg, self.logger.infos)
 
50
 
 
51
        t = self.api.node_group_template_get(ctx, t["id"])
 
52
        self.assertIsNone(t)
 
53
 
 
54
    def test_node_group_template_delete_by_id_skipped(self):
 
55
        self.logger.clear_log()
 
56
        ctx = context.ctx()
 
57
        template_values = copy.copy(c.SAMPLE_NGT)
 
58
        template_values["is_default"] = False
 
59
        t = self.api.node_group_template_create(ctx, template_values)
 
60
 
 
61
        option_values = {"tenant_id": t["tenant_id"],
 
62
                         "id": t["id"]}
 
63
        template_api.set_conf(Config(option_values))
 
64
 
 
65
        template_api.do_node_group_template_delete_by_id()
 
66
        msg = ("Deletion of node group template {info} skipped, "
 
67
               "not a default template".format(info=u.name_and_id(t)))
 
68
        self.assertIn(msg, self.logger.warnings)
 
69
 
 
70
        t = self.api.node_group_template_get(ctx, t["id"])
 
71
        self.assertIsNotNone(t)
 
72
 
 
73
    def test_node_group_template_delete_bad_id(self):
 
74
        self.logger.clear_log()
 
75
        option_values = {"tenant_id": 1,
 
76
                         "id": "badid"}
 
77
        template_api.set_conf(Config(option_values))
 
78
        template_api.do_node_group_template_delete_by_id()
 
79
        msg = ("Deletion of node group template {id} failed, "
 
80
               "no such template".format(id=option_values["id"]))
 
81
        self.assertIn(msg, self.logger.warnings)
 
82
 
 
83
    def test_node_group_template_delete_by_name(self):
 
84
        self.logger.clear_log()
 
85
        ctx = context.ctx()
 
86
        t = self.api.node_group_template_create(ctx, c.SAMPLE_NGT)
 
87
 
 
88
        option_values = {"tenant_id": t["tenant_id"],
 
89
                         "template_name": t["name"]}
 
90
        template_api.set_conf(Config(option_values))
 
91
 
 
92
        template_api.do_node_group_template_delete()
 
93
        msg = 'Deleted node group template {info}'.format(
 
94
            info=u.name_and_id(t))
 
95
        self.assertIn(msg, self.logger.infos)
 
96
 
 
97
        t = self.api.node_group_template_get(ctx, t["id"])
 
98
        self.assertIsNone(t)
 
99
 
 
100
    def test_node_group_template_delete_by_name_skipped(self):
 
101
        self.logger.clear_log()
 
102
        ctx = context.ctx()
 
103
        template_values = copy.copy(c.SAMPLE_NGT)
 
104
        template_values["is_default"] = False
 
105
        t = self.api.node_group_template_create(ctx, template_values)
 
106
 
 
107
        option_values = {"tenant_id": t["tenant_id"],
 
108
                         "template_name": t["name"]}
 
109
        template_api.set_conf(Config(option_values))
 
110
 
 
111
        template_api.do_node_group_template_delete()
 
112
        msg = ("Deletion of node group template {name} failed, "
 
113
               "no such template".format(name=t["name"]))
 
114
        self.assertIn(msg, self.logger.warnings)
 
115
 
 
116
        t = self.api.node_group_template_get(ctx, t["id"])
 
117
        self.assertIsNotNone(t)
 
118
 
 
119
    def test_node_group_template_delete_in_use(self):
 
120
        self.logger.clear_log()
 
121
        ctx = context.ctx()
 
122
        t = self.api.node_group_template_create(ctx, c.SAMPLE_NGT)
 
123
 
 
124
        # Make a cluster that references the node group template
 
125
        cluster_values = copy.deepcopy(c.SAMPLE_CLUSTER)
 
126
        cluster_values["node_groups"][0]["node_group_template_id"] = t["id"]
 
127
        cl = self.api.cluster_create(ctx, cluster_values)
 
128
 
 
129
        # Make a cluster template that references the node group template
 
130
        cluster_temp_values = copy.deepcopy(c.SAMPLE_CLT)
 
131
        cluster_temp_values["node_groups"] = cluster_values["node_groups"]
 
132
        clt = self.api.cluster_template_create(ctx, cluster_temp_values)
 
133
 
 
134
        # Set up the expected messages
 
135
        msgs = ["Node group template {info} in use "
 
136
                "by clusters {clusters}".format(
 
137
                    info=u.name_and_id(t), clusters=[cl["name"]])]
 
138
 
 
139
        msgs += ["Node group template {info} in use "
 
140
                 "by cluster templates {cluster_temps}".format(
 
141
                     info=u.name_and_id(t), cluster_temps=[clt["name"]])]
 
142
 
 
143
        msgs += ["Deletion of node group template {info} failed".format(
 
144
            info=u.name_and_id(t))]
 
145
 
 
146
        # Check delete by name
 
147
        option_values = {"tenant_id": t["tenant_id"],
 
148
                         "template_name": t["name"]}
 
149
        template_api.set_conf(Config(option_values))
 
150
        template_api.do_node_group_template_delete()
 
151
        for msg in msgs:
 
152
            self.assertIn(msg, self.logger.warnings)
 
153
        self.logger.clear_log()
 
154
 
 
155
        # Check again with delete by id
 
156
        option_values = {"tenant_id": t["tenant_id"],
 
157
                         "id": t["id"]}
 
158
        template_api.set_conf(Config(option_values))
 
159
        template_api.do_node_group_template_delete_by_id()
 
160
        for msg in msgs:
 
161
            self.assertIn(msg, self.logger.warnings)
 
162
        self.logger.clear_log()
 
163
 
 
164
    def test_cluster_template_delete_by_id(self):
 
165
        self.logger.clear_log()
 
166
        ctx = context.ctx()
 
167
        t = self.api.cluster_template_create(ctx, c.SAMPLE_CLT)
 
168
 
 
169
        option_values = {"tenant_id": t["tenant_id"],
 
170
                         "id": t["id"]}
 
171
        template_api.set_conf(Config(option_values))
 
172
 
 
173
        template_api.do_cluster_template_delete_by_id()
 
174
        msg = 'Deleted cluster template {info}'.format(
 
175
            info=u.name_and_id(t))
 
176
        self.assertIn(msg, self.logger.infos)
 
177
 
 
178
        t = self.api.cluster_template_get(ctx, t["id"])
 
179
        self.assertIsNone(t)
 
180
 
 
181
    def test_cluster_template_delete_by_id_skipped(self):
 
182
        self.logger.clear_log()
 
183
        ctx = context.ctx()
 
184
        template_values = copy.copy(c.SAMPLE_CLT)
 
185
        template_values["is_default"] = False
 
186
        t = self.api.cluster_template_create(ctx, template_values)
 
187
 
 
188
        option_values = {"tenant_id": t["tenant_id"],
 
189
                         "id": t["id"]}
 
190
        template_api.set_conf(Config(option_values))
 
191
        template_api.do_cluster_template_delete_by_id()
 
192
        msg = ("Deletion of cluster template {info} skipped, "
 
193
               "not a default template".format(info=u.name_and_id(t)))
 
194
        self.assertIn(msg, self.logger.warnings)
 
195
 
 
196
        t = self.api.cluster_template_get(ctx, t["id"])
 
197
        self.assertIsNotNone(t)
 
198
 
 
199
    def test_cluster_template_delete_bad_id(self):
 
200
        self.logger.clear_log()
 
201
        option_values = {"tenant_id": 1,
 
202
                         "id": "badid"}
 
203
        template_api.set_conf(Config(option_values))
 
204
        template_api.do_cluster_template_delete_by_id()
 
205
        msg = ("Deletion of cluster template {id} failed, "
 
206
               "no such template".format(id=option_values["id"]))
 
207
        self.assertIn(msg, self.logger.warnings)
 
208
 
 
209
    def test_cluster_template_delete_by_name(self):
 
210
        self.logger.clear_log()
 
211
        ctx = context.ctx()
 
212
        t = self.api.cluster_template_create(ctx, c.SAMPLE_NGT)
 
213
 
 
214
        option_values = {"tenant_id": t["tenant_id"],
 
215
                         "template_name": t["name"]}
 
216
        template_api.set_conf(Config(option_values))
 
217
 
 
218
        template_api.do_cluster_template_delete()
 
219
        msg = 'Deleted cluster template {info}'.format(
 
220
            info=u.name_and_id(t))
 
221
        self.assertIn(msg, self.logger.infos)
 
222
 
 
223
        t = self.api.cluster_template_get(ctx, t["id"])
 
224
        self.assertIsNone(t)
 
225
 
 
226
    def test_cluster_template_delete_by_name_skipped(self):
 
227
        self.logger.clear_log()
 
228
        ctx = context.ctx()
 
229
        template_values = copy.copy(c.SAMPLE_NGT)
 
230
        template_values["is_default"] = False
 
231
        t = self.api.cluster_template_create(ctx, template_values)
 
232
 
 
233
        option_values = {"tenant_id": t["tenant_id"],
 
234
                         "template_name": t["name"]}
 
235
        template_api.set_conf(Config(option_values))
 
236
 
 
237
        template_api.do_cluster_template_delete()
 
238
        msg = ("Deletion of cluster template {name} failed, "
 
239
               "no such template".format(name=t["name"]))
 
240
        self.assertIn(msg, self.logger.warnings)
 
241
 
 
242
        t = self.api.cluster_template_get(ctx, t["id"])
 
243
        self.assertIsNotNone(t)
 
244
 
 
245
    def test_cluster_template_delete_in_use(self):
 
246
        self.logger.clear_log()
 
247
        ctx = context.ctx()
 
248
        t = self.api.cluster_template_create(ctx, c.SAMPLE_CLT)
 
249
 
 
250
        # Make a cluster that references the cluster template
 
251
        cluster_values = copy.deepcopy(c.SAMPLE_CLUSTER)
 
252
        cluster_values["cluster_template_id"] = t["id"]
 
253
        del cluster_values["node_groups"]
 
254
        cl = self.api.cluster_create(ctx, cluster_values)
 
255
 
 
256
        # Set up the expected messages
 
257
        msgs = ["Cluster template {info} in use "
 
258
                "by clusters {clusters}".format(
 
259
                    info=u.name_and_id(t), clusters=[cl["name"]])]
 
260
 
 
261
        msgs += ["Deletion of cluster template {info} failed".format(
 
262
            info=u.name_and_id(t))]
 
263
 
 
264
        # Check delete by name
 
265
        option_values = {"tenant_id": t["tenant_id"],
 
266
                         "template_name": t["name"]}
 
267
        template_api.set_conf(Config(option_values))
 
268
        template_api.do_cluster_template_delete()
 
269
        for msg in msgs:
 
270
            self.assertIn(msg, self.logger.warnings)
 
271
        self.logger.clear_log()
 
272
 
 
273
        # Check again with delete by id
 
274
        option_values = {"tenant_id": t["tenant_id"],
 
275
                         "id": t["id"]}
 
276
        template_api.set_conf(Config(option_values))
 
277
        template_api.do_cluster_template_delete_by_id()
 
278
        for msg in msgs:
 
279
            self.assertIn(msg, self.logger.warnings)
 
280
        self.logger.clear_log()
 
281
 
 
282
    def _make_templates(self, ctx, name, plugin_name, plugin_version):
 
283
        # Make a node group template
 
284
        values = copy.copy(c.SAMPLE_NGT)
 
285
        values["name"] = "ngt_" + name
 
286
        values["plugin_name"] = plugin_name
 
287
        values["hadoop_version"] = plugin_version
 
288
        ngt = self.api.node_group_template_create(ctx, values)
 
289
 
 
290
        # Make a cluster template that references the node group template
 
291
        values = copy.deepcopy(c.SAMPLE_CLT)
 
292
        values["name"] = "clt_" + name
 
293
        values["plugin_name"] = plugin_name
 
294
        values["hadoop_version"] = plugin_version
 
295
        values["node_groups"][0]["node_group_template_id"] = ngt["id"]
 
296
        clt = self.api.cluster_template_create(ctx, values)
 
297
 
 
298
        return ngt, clt
 
299
 
 
300
    def test_do_delete(self):
 
301
        self.logger.clear_log()
 
302
        ctx = context.ctx()
 
303
 
 
304
        # Make some plugins to delete
 
305
        ngt, clt = self._make_templates(ctx, "first", "plugin", "v1")
 
306
 
 
307
        # Make some more for the same plugin, different version
 
308
        ngt2, clt2 = self._make_templates(ctx, "second", "plugin", "v2")
 
309
 
 
310
        # Make another set for a different plugin, overlapping version
 
311
        safe_ngt, safe_clt = self._make_templates(ctx, "third", "plugin2",
 
312
                                                  "v1")
 
313
 
 
314
        # Run a delete by plugin name/version for the first set
 
315
        option_values = {"tenant_id": ngt["tenant_id"],
 
316
                         "plugin_name": [ngt["plugin_name"]],
 
317
                         "plugin_version": [ngt["hadoop_version"]]}
 
318
        template_api.set_conf(Config(option_values))
 
319
 
 
320
        # Should delete clt and then ngt, check for messages in order
 
321
        template_api.do_delete()
 
322
        msgs = ["Deleted cluster template {info}".format(
 
323
            info=u.name_and_id(clt))]
 
324
        msgs += ["Deleted node group template {info}".format(
 
325
            info=u.name_and_id(ngt))]
 
326
 
 
327
        self.assertEqual(msgs, self.logger.infos)
 
328
        self.assertIsNone(self.api.node_group_template_get(ctx, ngt["id"]))
 
329
        self.assertIsNone(self.api.cluster_template_get(ctx, clt["id"]))
 
330
 
 
331
        # Make sure the other templates are still there
 
332
        self.assertIsNotNone(self.api.node_group_template_get(ctx,
 
333
                                                              ngt2["id"]))
 
334
        self.assertIsNotNone(self.api.cluster_template_get(ctx,
 
335
                                                           clt2["id"]))
 
336
        self.assertIsNotNone(self.api.node_group_template_get(ctx,
 
337
                                                              safe_ngt["id"]))
 
338
        self.assertIsNotNone(self.api.cluster_template_get(ctx,
 
339
                                                           safe_clt["id"]))
 
340
 
 
341
        # Run delete again for the plugin but with no version specified
 
342
        self.logger.clear_log()
 
343
        option_values = {"tenant_id": ngt2["tenant_id"],
 
344
                         "plugin_name": [ngt2["plugin_name"]],
 
345
                         "plugin_version": None}
 
346
        template_api.set_conf(Config(option_values))
 
347
 
 
348
        # Should delete clt2 and then ngt2, check for messages in order
 
349
        template_api.do_delete()
 
350
        msgs = ["Deleted cluster template {info}".format(
 
351
            info=u.name_and_id(clt2))]
 
352
        msgs += ["Deleted node group template {info}".format(
 
353
            info=u.name_and_id(ngt2))]
 
354
 
 
355
        self.assertEqual(msgs, self.logger.infos)
 
356
        self.assertIsNone(self.api.node_group_template_get(ctx, ngt2["id"]))
 
357
        self.assertIsNone(self.api.cluster_template_get(ctx, clt2["id"]))
 
358
 
 
359
        # Make sure the other templates are still there
 
360
        self.assertIsNotNone(self.api.node_group_template_get(ctx,
 
361
                                                              safe_ngt["id"]))
 
362
        self.assertIsNotNone(self.api.cluster_template_get(ctx,
 
363
                                                           safe_clt["id"]))