summaryrefslogtreecommitdiff
path: root/trove/tests/unittests/extensions/common/test_service.py
blob: ecb61f21578cc6046a5cde9218beee9a773f7698 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    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.
#

from unittest.mock import Mock
from unittest.mock import patch
from oslo_config.cfg import NoSuchOptError

from trove.common import exception
from trove.common import utils
from trove.extensions.common import models
from trove.extensions.common.service import ClusterRootController
from trove.extensions.common.service import DefaultRootController
from trove.extensions.common.service import RootController
from trove.instance import models as instance_models
from trove.instance.models import DBInstance
from trove.tests.unittests import trove_testtools


class TestDefaultRootController(trove_testtools.TestCase):

    def setUp(self):
        super(TestDefaultRootController, self).setUp()
        self.controller = DefaultRootController()

    @patch.object(models.Root, "load")
    def test_root_index(self, root_load):
        context = Mock()
        req = Mock()
        req.environ = Mock()
        req.environ.__getitem__ = Mock(return_value=context)
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = False
        self.controller.root_index(req, tenant_id, uuid, is_cluster)
        root_load.assert_called_with(context, uuid)

    def test_root_index_with_cluster(self):
        req = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = True
        self.assertRaises(
            exception.ClusterOperationNotSupported,
            self.controller.root_index,
            req, tenant_id, uuid, is_cluster)

    @patch.object(models.Root, "create")
    def test_root_create(self, root_create):
        user = Mock()
        context = Mock()
        context.user = Mock()
        context.user.__getitem__ = Mock(return_value=user)
        req = Mock()
        req.environ = Mock()
        req.environ.__getitem__ = Mock(return_value=context)
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = False
        password = Mock()
        body = {'password': password}
        self.controller.root_create(req, body, tenant_id, uuid, is_cluster)
        root_create.assert_called_with(context, uuid, password)

    def test_root_create_with_cluster(self):
        req = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = True
        password = Mock()
        body = {'password': password}
        self.assertRaises(
            exception.ClusterOperationNotSupported,
            self.controller.root_create,
            req, body, tenant_id, uuid, is_cluster)

    @patch.object(models.Root, "delete")
    @patch.object(models.Root, "load")
    def test_root_delete(self, root_load, root_delete):
        context = Mock()
        req = Mock()
        req.environ = Mock()
        req.environ.__getitem__ = Mock(return_value=context)
        tenant_id = Mock()
        instance_id = utils.generate_uuid()
        is_cluster = False
        root_load.return_value = True
        self.controller.root_delete(req, tenant_id, instance_id, is_cluster)
        root_load.assert_called_with(context, instance_id)
        root_delete.assert_called_with(context, instance_id)

    @patch.object(models.Root, "delete")
    @patch.object(models.Root, "load")
    def test_root_delete_without_root_enabled(self, root_load, root_delete):
        context = Mock()
        req = Mock()
        req.environ = Mock()
        req.environ.__getitem__ = Mock(return_value=context)
        tenant_id = Mock()
        instance_id = utils.generate_uuid()
        is_cluster = False
        root_load.return_value = False
        self.assertRaises(
            exception.RootHistoryNotFound,
            self.controller.root_delete,
            req, tenant_id, instance_id, is_cluster)
        root_load.assert_called_with(context, instance_id)
        root_delete.assert_not_called()

    def test_root_delete_with_cluster(self):
        req = Mock()
        tenant_id = Mock()
        instance_id = utils.generate_uuid()
        is_cluster = True
        self.assertRaises(
            exception.ClusterOperationNotSupported,
            self.controller.root_delete,
            req, tenant_id, instance_id, is_cluster)


class TestRootController(trove_testtools.TestCase):

    def setUp(self):
        super(TestRootController, self).setUp()
        self.context = trove_testtools.TroveTestContext(self)
        self.controller = RootController()

    @patch.object(instance_models.Instance, "load")
    @patch.object(RootController, "load_root_controller")
    @patch.object(RootController, "_get_datastore")
    def test_index(self, service_get_datastore, service_load_root_controller,
                   service_load_instance):
        req = Mock()
        req.environ = {'trove.context': self.context}
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        ds_manager = Mock()
        is_cluster = False
        service_get_datastore.return_value = (ds_manager, is_cluster)
        root_controller = Mock()
        ret = Mock()
        root_controller.root_index = Mock(return_value=ret)
        service_load_root_controller.return_value = root_controller

        self.assertEqual(ret, self.controller.index(req, tenant_id, uuid))
        service_get_datastore.assert_called_with(tenant_id, uuid)
        service_load_root_controller.assert_called_with(ds_manager)
        root_controller.root_index.assert_called_with(
            req, tenant_id, uuid, is_cluster)

    @patch.object(instance_models.Instance, "load")
    @patch.object(RootController, "load_root_controller")
    @patch.object(RootController, "_get_datastore")
    def test_create(self, service_get_datastore, service_load_root_controller,
                    service_load_instance):
        req = Mock()
        req.environ = {'trove.context': self.context}
        body = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        ds_manager = Mock()
        is_cluster = False
        service_get_datastore.return_value = (ds_manager, is_cluster)
        root_controller = Mock()
        ret = Mock()
        root_controller.root_create = Mock(return_value=ret)
        service_load_root_controller.return_value = root_controller

        self.assertEqual(
            ret, self.controller.create(req, tenant_id, uuid, body=body))
        service_get_datastore.assert_called_with(tenant_id, uuid)
        service_load_root_controller.assert_called_with(ds_manager)
        root_controller.root_create.assert_called_with(
            req, body, tenant_id, uuid, is_cluster)

    @patch.object(instance_models.Instance, "load")
    @patch.object(RootController, "load_root_controller")
    @patch.object(RootController, "_get_datastore")
    def test_create_with_no_root_controller(self,
                                            service_get_datastore,
                                            service_load_root_controller,
                                            service_load_instance):
        req = Mock()
        req.environ = {'trove.context': self.context}
        body = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        ds_manager = Mock()
        is_cluster = False
        service_get_datastore.return_value = (ds_manager, is_cluster)
        service_load_root_controller.return_value = None

        self.assertRaises(
            NoSuchOptError,
            self.controller.create,
            req, tenant_id, uuid, body=body)
        service_get_datastore.assert_called_with(tenant_id, uuid)
        service_load_root_controller.assert_called_with(ds_manager)

    @patch.object(instance_models.Instance, "load")
    @patch.object(RootController, "load_root_controller")
    @patch.object(RootController, "_get_datastore")
    def test_delete(self, service_get_datastore, service_load_root_controller,
                    service_load_instance):
        req = Mock()
        req.environ = {'trove.context': self.context}
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        ds_manager = Mock()
        is_cluster = False
        service_get_datastore.return_value = (ds_manager, is_cluster)
        root_controller = Mock()
        ret = Mock()
        root_controller.root_delete = Mock(return_value=ret)
        service_load_root_controller.return_value = root_controller

        self.assertEqual(
            ret, self.controller.delete(req, tenant_id, uuid))
        service_get_datastore.assert_called_with(tenant_id, uuid)
        service_load_root_controller.assert_called_with(ds_manager)
        root_controller.root_delete.assert_called_with(
            req, tenant_id, uuid, is_cluster)

    @patch.object(instance_models.Instance, "load")
    @patch.object(RootController, "load_root_controller")
    @patch.object(RootController, "_get_datastore")
    def test_delete_with_no_root_controller(self,
                                            service_get_datastore,
                                            service_load_root_controller,
                                            service_load_instance):
        req = Mock()
        req.environ = {'trove.context': self.context}
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        ds_manager = Mock()
        is_cluster = False
        service_get_datastore.return_value = (ds_manager, is_cluster)
        service_load_root_controller.return_value = None

        self.assertRaises(
            NoSuchOptError,
            self.controller.delete,
            req, tenant_id, uuid)
        service_get_datastore.assert_called_with(tenant_id, uuid)
        service_load_root_controller.assert_called_with(ds_manager)


class TestClusterRootController(trove_testtools.TestCase):

    def setUp(self):
        super(TestClusterRootController, self).setUp()
        self.context = trove_testtools.TroveTestContext(self)
        self.controller = ClusterRootController()

    @patch.object(ClusterRootController, "cluster_root_index")
    def test_root_index_cluster(self, mock_cluster_root_index):
        req = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = True
        self.controller.root_index(req, tenant_id, uuid, is_cluster)
        mock_cluster_root_index.assert_called_with(req, tenant_id, uuid)

    @patch.object(ClusterRootController, "instance_root_index")
    def test_root_index_instance(self, mock_instance_root_index):
        req = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = False
        self.controller.root_index(req, tenant_id, uuid, is_cluster)
        mock_instance_root_index.assert_called_with(req, tenant_id, uuid)

    @patch.object(ClusterRootController, "cluster_root_create")
    def test_root_create_cluster(self, mock_cluster_root_create):
        req = Mock()
        body = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = True
        self.controller.root_create(req, body, tenant_id, uuid, is_cluster)
        mock_cluster_root_create.assert_called_with(req, body, tenant_id, uuid)

    @patch.object(ClusterRootController, "check_cluster_instance_actions")
    @patch.object(ClusterRootController, "instance_root_create")
    def test_root_create_instance(self, mock_instance_root_create, mock_check):
        req = Mock()
        body = Mock()
        tenant_id = Mock()
        uuid = utils.generate_uuid()
        is_cluster = False
        self.controller.root_create(req, body, tenant_id, uuid, is_cluster)
        mock_check.assert_called_with(uuid)
        mock_instance_root_create.assert_called_with(req, body, uuid)

    @patch.object(models.ClusterRoot, "load")
    def test_instance_root_index(self, mock_cluster_root_load):
        req = Mock()
        req.environ = {'trove.context': self.context}
        tenant_id = Mock()
        instance_id = utils.generate_uuid()
        self.controller.instance_root_index(req, tenant_id, instance_id)
        mock_cluster_root_load.assert_called_with(self.context, instance_id)

    @patch.object(models.ClusterRoot, "load",
                  side_effect=exception.UnprocessableEntity())
    def test_instance_root_index_exception(self, mock_cluster_root_load):
        req = Mock()
        req.environ = {'trove.context': self.context}
        tenant_id = Mock()
        instance_id = utils.generate_uuid()
        self.assertRaises(
            exception.UnprocessableEntity,
            self.controller.instance_root_index,
            req, tenant_id, instance_id
        )
        mock_cluster_root_load.assert_called_with(self.context, instance_id)

    @patch.object(ClusterRootController, "instance_root_index")
    @patch.object(ClusterRootController, "_get_cluster_instance_id")
    def test_cluster_root_index(self, mock_get_cluster_instance,
                                mock_instance_root_index):
        req = Mock()
        tenant_id = Mock()
        cluster_id = utils.generate_uuid()
        single_instance_id = Mock()
        mock_get_cluster_instance.return_value = (single_instance_id, Mock())
        self.controller.cluster_root_index(req, tenant_id, cluster_id)
        mock_get_cluster_instance.assert_called_with(tenant_id, cluster_id)
        mock_instance_root_index.assert_called_with(req, tenant_id,
                                                    single_instance_id)

    @patch.object(ClusterRootController, "instance_root_create")
    @patch.object(ClusterRootController, "_get_cluster_instance_id")
    def test_cluster_root_create(self, mock_get_cluster_instance,
                                 mock_instance_root_create):
        req = Mock()
        body = Mock()
        tenant_id = Mock()
        cluster_id = utils.generate_uuid()
        single_instance_id = Mock()
        cluster_instances = Mock()
        mock_get_cluster_instance.return_value = (single_instance_id,
                                                  cluster_instances)
        self.controller.cluster_root_create(req, body, tenant_id, cluster_id)
        mock_get_cluster_instance.assert_called_with(tenant_id, cluster_id)
        mock_instance_root_create.assert_called_with(req, body,
                                                     single_instance_id,
                                                     cluster_instances)

    @patch.object(DBInstance, "find_all")
    def test_get_cluster_instance_id(self, mock_find_all):
        tenant_id = Mock()
        cluster_id = Mock()
        db_inst_1 = Mock()
        db_inst_1.id.return_value = utils.generate_uuid()
        db_inst_2 = Mock()
        db_inst_2.id.return_value = utils.generate_uuid()
        cluster_instances = [db_inst_1, db_inst_2]
        mock_find_all.return_value.all.return_value = cluster_instances
        ret = self.controller._get_cluster_instance_id(tenant_id, cluster_id)
        self.assertEqual(db_inst_1.id, ret[0])
        self.assertEqual([db_inst_1.id, db_inst_2.id], ret[1])

    @patch.object(models.ClusterRoot, "create")
    def test_instance_root_create(self, mock_cluster_root_create):
        user = Mock()
        self.context.user = Mock()
        self.context.user.__getitem__ = Mock(return_value=user)
        req = Mock()
        req.environ = {'trove.context': self.context}
        password = Mock()
        body = {'password': password}
        instance_id = utils.generate_uuid()
        cluster_instances = Mock()
        self.controller.instance_root_create(
            req, body, instance_id, cluster_instances)
        mock_cluster_root_create.assert_called_with(
            self.context, instance_id, password,
            cluster_instances)

    @patch.object(models.ClusterRoot, "create")
    def test_instance_root_create_no_body(self, mock_cluster_root_create):
        user = Mock()
        self.context.user = Mock()
        self.context.user.__getitem__ = Mock(return_value=user)
        req = Mock()
        req.environ = {'trove.context': self.context}
        password = None
        body = None
        instance_id = utils.generate_uuid()
        cluster_instances = Mock()
        self.controller.instance_root_create(
            req, body, instance_id, cluster_instances)
        mock_cluster_root_create.assert_called_with(
            self.context, instance_id, password,
            cluster_instances)