summaryrefslogtreecommitdiff
path: root/trove/tests/unittests/mgmt/test_datastores.py
blob: c3b5ad720a01f9ee64cc542b105d0e00e61fe8ea (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
# Copyright [2015] Hewlett-Packard Development Company, L.P.
#
# 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, patch
from glanceclient import exc as glance_exceptions

from trove.common import clients
from trove.common import exception
from trove.datastore import models
from trove.extensions.mgmt.datastores.service import DatastoreVersionController
from trove.tests.unittests import trove_testtools
from trove.tests.unittests.util import util


class TestDatastoreVersion(trove_testtools.TestCase):

    def setUp(self):
        super(TestDatastoreVersion, self).setUp()
        util.init_db()
        models.update_datastore(name='test_ds', default_version=None)
        models.update_datastore_version(
            'test_ds', 'test_vr1', 'mysql',
            '154b350d-4d86-4214-9067-9c54b230c0da', 'pkg-1', 1)
        models.update_datastore_version(
            'test_ds', 'test_vr2', 'mysql',
            '154b350d-4d86-4214-9067-9c54b230c0da', 'pkg-1', 1)
        self.ds = models.Datastore.load('test_ds')
        self.ds_version2 = models.DatastoreVersion.load(self.ds, 'test_vr2')

        self.context = trove_testtools.TroveTestContext(self)
        self.req = Mock()
        self.req.environ = Mock()
        self.req.environ.__getitem__ = Mock(return_value=self.context)
        self.tenant_id = Mock()
        self.version_controller = DatastoreVersionController()

    def tearDown(self):
        super(TestDatastoreVersion, self).tearDown()

    @patch.object(clients, 'create_glance_client')
    def test_version_create(self, mock_glance_client):
        body = {"version": {
            "datastore_name": "test_ds",
            "name": "test_version",
            "datastore_manager": "mysql",
            "image": "image-id",
            "packages": "test-pkg",
            "active": True,
            "default": True}}
        output = self.version_controller.create(
            self.req, body, self.tenant_id)
        self.assertEqual(202, output.status)

    @patch.object(clients, 'create_glance_client')
    @patch.object(models.DatastoreVersion, 'load')
    def test_fail_already_exists_version_create(self, mock_load,
                                                mock_glance_client):
        body = {"version": {
            "datastore_name": "test_ds",
            "name": "test_new_vr",
            "datastore_manager": "mysql",
            "image": "image-id",
            "packages": "test-pkg",
            "active": True,
            "default": True}}
        self.assertRaisesRegex(
            exception.DatastoreVersionAlreadyExists,
            "A datastore version with the name 'test_new_vr' already exists",
            self.version_controller.create, self.req, body, self.tenant_id)

    @patch.object(clients, 'create_glance_client')
    def test_fail_image_not_found_version_create(self, mock_glance_client):
        mock_glance_client.return_value.images.get = Mock(
            side_effect=glance_exceptions.HTTPNotFound())
        body = {"version": {
            "datastore_name": "test_ds",
            "name": "test_vr",
            "datastore_manager": "mysql",
            "image": "image-id",
            "packages": "test-pkg",
            "active": True,
            "default": True}}
        self.assertRaisesRegex(
            exception.ImageNotFound,
            "Image image-id cannot be found.",
            self.version_controller.create, self.req, body, self.tenant_id)

    def test_version_delete(self):
        ds_version1 = models.DatastoreVersion.load(self.ds, 'test_vr1')

        output = self.version_controller.delete(self.req,
                                                self.tenant_id,
                                                ds_version1.id)
        err_msg = ("Datastore version '%s' cannot be found." %
                   ds_version1.id)

        self.assertEqual(202, output.status)

        # Try to find deleted version, this should raise exception.
        self.assertRaisesRegex(
            exception.DatastoreVersionNotFound,
            err_msg, models.DatastoreVersion.load_by_uuid, ds_version1.id)

    @patch.object(clients, 'create_glance_client')
    def test_version_update(self, mock_client):
        body = {"image": "c022f4dc-76ed-4e3f-a25e-33e031f43f8b"}
        output = self.version_controller.edit(self.req, body,
                                              self.tenant_id,
                                              self.ds_version2.id)
        self.assertEqual(202, output.status)

        # Find the details of version updated and match the updated attribute.
        test_ds_version = models.DatastoreVersion.load_by_uuid(
            self.ds_version2.id)
        self.assertEqual(body['image'], test_ds_version.image_id)

    @patch.object(clients, 'create_glance_client')
    def test_version_update_fail_image_not_found(self, mock_glance_client):
        mock_glance_client.return_value.images.get = Mock(
            side_effect=glance_exceptions.HTTPNotFound())
        body = {"image": "non-existent-image-id"}

        self.assertRaisesRegex(
            exception.ImageNotFound,
            "Image non-existent-image-id cannot be found.",
            self.version_controller.edit, self.req, body,
            self.tenant_id, self.ds_version2.id)

    @patch.object(models.DatastoreVersion, 'load_by_uuid')
    def test_version_index(self, mock_load):
        output = self.version_controller.index(
            self.req, self.tenant_id)
        self.assertEqual(200, output.status)

    def test_version_show(self):
        output = self.version_controller.show(
            self.req, self.tenant_id, self.ds_version2.id)
        self.assertEqual(200, output.status)
        self.assertEqual(self.ds_version2.id,
                         output._data['version']['id'])
        self.assertEqual(self.ds_version2.name,
                         output._data['version']['name'])
        self.assertEqual(self.ds_version2.datastore_id,
                         output._data['version']['datastore_id'])
        self.assertEqual(self.ds_version2.datastore_name,
                         output._data['version']['datastore_name'])
        self.assertEqual(self.ds_version2.manager,
                         output._data['version']['datastore_manager'])
        self.assertEqual(self.ds_version2.image_id,
                         output._data['version']['image'])
        self.assertEqual(self.ds_version2.packages.split(','),
                         output._data['version']['packages'])
        self.assertEqual(self.ds_version2.active,
                         output._data['version']['active'])