summaryrefslogtreecommitdiff
path: root/glance/tests/unit/v1/test_upload_utils.py
blob: 40e718decfeb34f1071896cf6cf16ef8f1559b67 (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
# Copyright 2013 OpenStack Foundation
# 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 contextlib import contextmanager
import mock
from mock import patch

import glance_store
import webob.exc

from glance.api.v1 import upload_utils
from glance.common import exception
from glance.common import store_utils
import glance.registry.client.v1.api as registry
from glance.tests.unit import base
import glance.tests.unit.utils as unit_test_utils


class TestUploadUtils(base.StoreClearingUnitTest):
    def setUp(self):
        super(TestUploadUtils, self).setUp()
        self.config(verbose=True, debug=True)

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

    def test_initiate_delete(self):
        req = unit_test_utils.get_fake_request()
        location = {"url": "file://foo/bar",
                    "metadata": {},
                    "status": "active"}
        id = unit_test_utils.UUID1

        with patch.object(store_utils,
                          "safe_delete_from_backend") as mock_store_utils:
            upload_utils.initiate_deletion(req, location, id)
            mock_store_utils.assert_called_once_with(req.context,
                                                     id,
                                                     location)

    def test_initiate_delete_with_delayed_delete(self):
        self.config(delayed_delete=True)
        req = unit_test_utils.get_fake_request()
        location = {"url": "file://foo/bar",
                    "metadata": {},
                    "status": "active"}
        id = unit_test_utils.UUID1

        with patch.object(store_utils, "schedule_delayed_delete_from_backend",
                          return_value=True) as mock_store_utils:
            upload_utils.initiate_deletion(req, location, id)
            mock_store_utils.assert_called_once_with(req.context,
                                                     id,
                                                     location)

    def test_safe_kill(self):
        req = unit_test_utils.get_fake_request()
        id = unit_test_utils.UUID1

        with patch.object(registry, "update_image_metadata") as mock_registry:
            upload_utils.safe_kill(req, id, 'saving')
            mock_registry.assert_called_once_with(req.context, id,
                                                  {'status': 'killed'},
                                                  from_state='saving')

    def test_safe_kill_with_error(self):
        req = unit_test_utils.get_fake_request()
        id = unit_test_utils.UUID1

        with patch.object(registry, "update_image_metadata",
                          side_effect=Exception()) as mock_registry:
            upload_utils.safe_kill(req, id, 'saving')
            mock_registry.assert_called_once_with(req.context, id,
                                                  {'status': 'killed'},
                                                  from_state='saving')

    @contextmanager
    def _get_store_and_notifier(self, image_size=10, ext_update_data=None,
                                ret_checksum="checksum", exc_class=None):
        location = "file://foo/bar"
        checksum = "checksum"
        size = 10
        update_data = {'checksum': checksum}
        if ext_update_data is not None:
            update_data.update(ext_update_data)
        image_meta = {'id': unit_test_utils.UUID1,
                      'size': image_size}
        image_data = "blah"

        store = mock.MagicMock()
        notifier = mock.MagicMock()

        if exc_class is not None:
            store.add.side_effect = exc_class
        else:
            store.add.return_value = (location, size, ret_checksum, {})
        yield (location, checksum, image_meta, image_data, store, notifier,
               update_data)

        store.add.assert_called_once_with(image_meta['id'], mock.ANY,
                                          image_meta['size'], context=mock.ANY)

    def test_upload_data_to_store(self):
        req = unit_test_utils.get_fake_request()
        with self._get_store_and_notifier(
            ext_update_data={'size': 10}) as (location, checksum, image_meta,
                                              image_data, store, notifier,
                                              update_data):
            ret = image_meta.update(update_data)
            with patch.object(registry, 'update_image_metadata',
                              return_value=ret) as mock_update_image_metadata:
                actual_meta, location_data = upload_utils.upload_data_to_store(
                    req, image_meta, image_data, store, notifier)

                self.assertEqual(location_data['url'], location)
                self.assertEqual(actual_meta, image_meta.update(update_data))
                mock_update_image_metadata.assert_called_once_with(
                    req.context, image_meta['id'], update_data,
                    from_state='saving')

    def test_upload_data_to_store_mismatch_size(self):
        req = unit_test_utils.get_fake_request()

        with self._get_store_and_notifier(
            image_size=11) as (location, checksum, image_meta, image_data,
                               store, notifier, update_data):
            ret = image_meta.update(update_data)
            with patch.object(registry, 'update_image_metadata',
                              return_value=ret) as mock_update_image_metadata:
                self.assertRaises(webob.exc.HTTPBadRequest,
                                  upload_utils.upload_data_to_store,
                                  req, image_meta, image_data, store,
                                  notifier)
                mock_update_image_metadata.assert_called_with(
                    req.context, image_meta['id'], {'status': 'killed'},
                    from_state='saving')

    def test_upload_data_to_store_mismatch_checksum(self):
        req = unit_test_utils.get_fake_request()

        with self._get_store_and_notifier(
            ret_checksum='fake') as (location, checksum, image_meta,
                                     image_data, store, notifier, update_data):
            ret = image_meta.update(update_data)
            with patch.object(registry, "update_image_metadata",
                              return_value=ret) as mock_update_image_metadata:
                self.assertRaises(webob.exc.HTTPBadRequest,
                                  upload_utils.upload_data_to_store,
                                  req, image_meta, image_data, store,
                                  notifier)
                mock_update_image_metadata.assert_called_with(
                    req.context, image_meta['id'], {'status': 'killed'},
                    from_state='saving')

    def _test_upload_data_to_store_exception(self, exc_class, expected_class):
        req = unit_test_utils.get_fake_request()

        with self._get_store_and_notifier(
            exc_class=exc_class) as (location, checksum, image_meta,
                                     image_data, store, notifier, update_data):
            with patch.object(upload_utils, 'safe_kill') as mock_safe_kill:
                self.assertRaises(expected_class,
                                  upload_utils.upload_data_to_store,
                                  req, image_meta, image_data, store, notifier)
                mock_safe_kill.assert_called_once_with(
                    req, image_meta['id'], 'saving')

    def _test_upload_data_to_store_exception_with_notify(self,
                                                         exc_class,
                                                         expected_class,
                                                         image_killed=True):
        req = unit_test_utils.get_fake_request()

        with self._get_store_and_notifier(
            exc_class=exc_class) as (location, checksum, image_meta,
                                     image_data, store, notifier, update_data):
            with patch.object(upload_utils, 'safe_kill') as mock_safe_kill:
                self.assertRaises(expected_class,
                                  upload_utils.upload_data_to_store,
                                  req, image_meta, image_data, store,
                                  notifier)
                if image_killed:
                    mock_safe_kill.assert_called_with(req, image_meta['id'],
                                                      'saving')

    def test_upload_data_to_store_raises_store_disabled(self):
        """Test StoreDisabled exception is raised while uploading data"""
        self._test_upload_data_to_store_exception_with_notify(
            glance_store.StoreAddDisabled,
            webob.exc.HTTPGone,
            image_killed=True)

    def test_upload_data_to_store_duplicate(self):
        """See note in glance.api.v1.upload_utils on why we don't want image to
        be deleted in this case.
        """
        self._test_upload_data_to_store_exception_with_notify(
            exception.Duplicate,
            webob.exc.HTTPConflict,
            image_killed=False)

    def test_upload_data_to_store_forbidden(self):
        self._test_upload_data_to_store_exception_with_notify(
            exception.Forbidden,
            webob.exc.HTTPForbidden)

    def test_upload_data_to_store_storage_full(self):
        self._test_upload_data_to_store_exception_with_notify(
            glance_store.StorageFull,
            webob.exc.HTTPRequestEntityTooLarge)

    def test_upload_data_to_store_storage_write_denied(self):
        self._test_upload_data_to_store_exception_with_notify(
            glance_store.StorageWriteDenied,
            webob.exc.HTTPServiceUnavailable)

    def test_upload_data_to_store_size_limit_exceeded(self):
        self._test_upload_data_to_store_exception_with_notify(
            exception.ImageSizeLimitExceeded,
            webob.exc.HTTPRequestEntityTooLarge)

    def test_upload_data_to_store_http_error(self):
        self._test_upload_data_to_store_exception_with_notify(
            webob.exc.HTTPError,
            webob.exc.HTTPError)

    def test_upload_data_to_store_client_disconnect(self):
        self._test_upload_data_to_store_exception(
            ValueError,
            webob.exc.HTTPBadRequest)

    def test_upload_data_to_store_client_disconnect_ioerror(self):
        self._test_upload_data_to_store_exception(
            IOError,
            webob.exc.HTTPBadRequest)

    def test_upload_data_to_store_exception(self):
        self._test_upload_data_to_store_exception_with_notify(
            Exception,
            webob.exc.HTTPInternalServerError)

    def test_upload_data_to_store_not_found_after_upload(self):
        req = unit_test_utils.get_fake_request()

        with self._get_store_and_notifier(
            ext_update_data={'size': 10}) as (location, checksum, image_meta,
                                              image_data, store, notifier,
                                              update_data):
            exc = exception.NotFound
            with patch.object(registry, 'update_image_metadata',
                              side_effect=exc) as mock_update_image_metadata:
                with patch.object(upload_utils,
                                  "initiate_deletion") as mock_initiate_del:
                    with patch.object(upload_utils,
                                      "safe_kill") as mock_safe_kill:
                        self.assertRaises(webob.exc.HTTPPreconditionFailed,
                                          upload_utils.upload_data_to_store,
                                          req, image_meta, image_data, store,
                                          notifier)
                        mock_update_image_metadata.assert_called_once_with(
                            req.context, image_meta['id'], update_data,
                            from_state='saving')
                        mock_initiate_del.assert_called_once_with(
                            req, {'url': location, 'status': 'active',
                                  'metadata': {}}, image_meta['id'])
                        mock_safe_kill.assert_called_once_with(
                            req, image_meta['id'], 'saving')