summaryrefslogtreecommitdiff
path: root/nova/tests/unit/api/openstack/compute/test_create_backup.py
blob: 9728002e885ed6a65b57791831e8c91b35a62c64 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# Copyright 2011 OpenStack Foundation
# Copyright 2013 IBM Corp.
#
#    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 import mock

from oslo_utils import timeutils
import webob

from nova.api.openstack import common
from nova.api.openstack.compute import create_backup \
        as create_backup_v21
from nova.compute import api
from nova.compute import utils as compute_utils
from nova import exception
from nova import test
from nova.tests.unit.api.openstack.compute import admin_only_action_common
from nova.tests.unit.api.openstack import fakes
from nova.tests.unit import fake_instance


class CreateBackupTestsV21(admin_only_action_common.CommonMixin,
                        test.NoDBTestCase):
    create_backup = create_backup_v21
    controller_name = 'CreateBackupController'
    validation_error = exception.ValidationError

    def setUp(self):
        super(CreateBackupTestsV21, self).setUp()
        self.controller = getattr(self.create_backup, self.controller_name)()
        self.compute_api = self.controller.compute_api

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_with_metadata(self, mock_backup, mock_check_image):
        metadata = {'123': 'asdf'}
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
                'metadata': metadata,
            },
        }

        image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1',
                     properties=metadata)

        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.return_value = image

        res = self.controller._create_backup(self.req, instance.uuid,
                                             body=body)

        mock_check_image.assert_called_once_with(self.context, metadata)
        mock_backup.assert_called_once_with(self.context, instance, 'Backup 1',
                                              'daily', 1,
                                              extra_properties=metadata)

        self.assertEqual(202, res.status_int)
        self.assertIn('fake-image-id', res.headers['Location'])

    def test_create_backup_no_name(self):
        # Name is required for backups.
        body = {
            'createBackup': {
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_name_with_leading_trailing_spaces(self):
        body = {
            'createBackup': {
                'name': '  test  ',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_name_with_leading_trailing_spaces_compat_mode(
            self, mock_backup, mock_check_image):
        body = {
            'createBackup': {
                'name': '  test  ',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1',
                     properties={})
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.return_value = image

        self.req.set_legacy_v2()
        self.controller._create_backup(self.req, instance.uuid,
                                       body=body)

        mock_check_image.assert_called_once_with(self.context, {})
        mock_backup.assert_called_once_with(self.context, instance, 'test',
                                              'daily', 1,
                                              extra_properties={})

    def test_create_backup_no_rotation(self):
        # Rotation is required for backup requests.
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_negative_rotation(self):
        """Rotation must be greater than or equal to zero
        for backup requests
        """
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': -1,
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_negative_rotation_with_string_number(self):
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': '-1',
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_rotation_with_empty_string(self):
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': '',
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_no_backup_type(self):
        # Backup Type (daily or weekly) is required for backup requests.
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'rotation': 1,
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_non_dict_metadata(self):
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
                'metadata': 'non_dict',
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    def test_create_backup_bad_entity(self):
        body = {'createBackup': 'go'}
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_rotation_is_zero(self, mock_backup,
                                         mock_check_image):
        # The happy path for creating backups if rotation is zero.
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 0,
            },
        }

        image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1',
                     properties={})
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.return_value = image

        res = self.controller._create_backup(self.req, instance.uuid,
                                             body=body)

        mock_check_image.assert_called_once_with(self.context, {})
        mock_backup.assert_called_once_with(self.context, instance, 'Backup 1',
                                              'daily', 0,
                                              extra_properties={})
        self.assertEqual(202, res.status_int)
        self.assertNotIn('Location', res.headers)

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_rotation_is_positive(self, mock_backup,
                                                mock_check_image):
        # The happy path for creating backups if rotation is positive.
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }

        image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1',
                     properties={})
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.return_value = image

        res = self.controller._create_backup(self.req, instance.uuid,
                                             body=body)

        mock_check_image.assert_called_once_with(self.context, {})
        mock_backup.assert_called_once_with(self.context, instance, 'Backup 1',
                                              'daily', 1,
                                              extra_properties={})

        self.assertEqual(202, res.status_int)
        self.assertIn('fake-image-id', res.headers['Location'])

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_rotation_is_string_number(
                                self, mock_backup, mock_check_image):
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': '1',
            },
        }

        image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1',
                     properties={})
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.return_value = image

        res = self.controller._create_backup(self.req, instance['uuid'],
                                             body=body)

        mock_check_image.assert_called_once_with(self.context, {})
        mock_backup.assert_called_once_with(self.context, instance, 'Backup 1',
                                              'daily', 1,
                                              extra_properties={})
        self.assertEqual(202, res.status_int)
        self.assertIn('fake-image-id', res.headers['Location'])

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup', return_value=dict(
        id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}))
    def test_create_backup_v2_45(self, mock_backup, mock_check_image):
        """Tests the 2.45 microversion to ensure the Location header is not
        in the response.
        """
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': '1',
            },
        }
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        req = fakes.HTTPRequest.blank('', version='2.45')
        res = self.controller._create_backup(req, instance['uuid'], body=body)
        self.assertIsInstance(res, dict)
        self.assertEqual('fake-image-id', res['image_id'])

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(api.API, 'backup')
    def test_create_backup_raises_conflict_on_invalid_state(self,
                                   mock_backup, mock_check_image):
        body_map = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        instance = fake_instance.fake_instance_obj(self.context)
        self.mock_get.return_value = instance
        mock_backup.side_effect = exception.InstanceInvalidState(
                            attr='vm_state', instance_uuid=instance.uuid,
                            state='foo', method='backup')

        ex = self.assertRaises(webob.exc.HTTPConflict,
                          self.controller._create_backup,
                          self.req, instance.uuid,
                          body=body_map)
        self.assertIn("Cannot 'createBackup' instance %(id)s"
                      % {'id': instance.uuid}, ex.explanation)

    def test_create_backup_with_non_existed_instance(self):
        body_map = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        uuid = fakes.FAKE_UUID
        self.mock_get.side_effect = exception.InstanceNotFound(
                                            instance_id=uuid)
        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller._create_backup,
                          self.req, uuid, body=body_map)

    def test_create_backup_with_invalid_create_backup(self):
        body = {
            'createBackupup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
            },
        }
        self.assertRaises(self.validation_error,
                          self.controller._create_backup,
                          self.req, fakes.FAKE_UUID, body=body)

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(compute_utils, 'is_volume_backed_instance',
                       return_value=True)
    def test_backup_volume_backed_instance(self, mock_is_volume_backed,
                                           mock_check_image):
        body = {
            'createBackup': {
                'name': 'BackupMe',
                'backup_type': 'daily',
                'rotation': 3
            },
        }

        updates = {'vm_state': 'active',
                   'task_state': None,
                   'launched_at': timeutils.utcnow()}
        instance = fake_instance.fake_instance_obj(self.context, **updates)
        instance.image_ref = None
        self.mock_get.return_value = instance

        ex = self.assertRaises(webob.exc.HTTPBadRequest,
                               self.controller._create_backup,
                               self.req, instance['uuid'], body=body)
        mock_check_image.assert_called_once_with(self.context, {})
        mock_is_volume_backed.assert_called_once_with(self.context, instance)
        self.assertIn('Backup is not supported for volume-backed instances',
                      str(ex))


class CreateBackupTestsV239(test.NoDBTestCase):

    def setUp(self):
        super(CreateBackupTestsV239, self).setUp()
        self.controller = create_backup_v21.CreateBackupController()
        self.req = fakes.HTTPRequest.blank('', version='2.39')

    @mock.patch.object(common, 'check_img_metadata_properties_quota')
    @mock.patch.object(common, 'get_instance')
    def test_create_backup_no_quota_checks(self, mock_get_instance,
                                                 mock_check_quotas):
        # 'mock_get_instance' helps to skip the whole logic of the action,
        # but to make the test
        mock_get_instance.side_effect = webob.exc.HTTPNotFound
        metadata = {'123': 'asdf'}
        body = {
            'createBackup': {
                'name': 'Backup 1',
                'backup_type': 'daily',
                'rotation': 1,
                'metadata': metadata,
            },
        }
        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller._create_backup, self.req,
                          fakes.FAKE_UUID, body=body)
        # starting from version 2.39 no quota checks on Nova side are performed
        # for 'createBackup' action after removing 'image-metadata' proxy API
        mock_check_quotas.assert_not_called()