def test_backup_volume(self, mock_volume_get, mock_get_connector_properties, mock_file_open, mock_temporary_chown): vol = tests_utils.create_volume(self.context) self.context.user_id = fake.USER_ID self.context.project_id = fake.PROJECT_ID backup_obj = tests_utils.create_backup(self.context, vol['id']) properties = {} attach_info = {'device': {'path': '/dev/null'}} backup_service = mock.Mock() self.volume.driver._detach_volume = mock.MagicMock() self.volume.driver._attach_volume = mock.MagicMock() self.volume.driver.terminate_connection = mock.MagicMock() mock_volume_get.return_value = vol mock_get_connector_properties.return_value = properties f = mock_file_open.return_value = open('/dev/null', 'rb') backup_service.backup(backup_obj, f, None) self.volume.driver._attach_volume.return_value = attach_info self.volume.driver.backup_volume(self.context, backup_obj, backup_service) mock_volume_get.assert_called_with(self.context, vol['id'])
def test_backup_update_name_description_with_leading_trailing_spaces( self, body): backup = test_utils.create_backup( self.ctxt, status=fields.BackupStatus.AVAILABLE) req = self._fake_update_request(fake.BACKUP_ID) expected_body = copy.deepcopy(body) self.controller.update(req, backup.id, body=body) backup.refresh() # backup update call doesn't return 'description' in response so get # the updated backup to assert name and description req = webob.Request.blank('/v3/%s/backups/%s' % ( fake.PROJECT_ID, backup.id)) req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app( fake_auth_context=self.user_context)) res_dict = jsonutils.loads(res.body) self.assertEqual(expected_body['backup']['name'].strip(), res_dict['backup']['name']) self.assertEqual(expected_body['backup']['description'].strip(), res_dict['backup']['description'])
def test_backup_list_with_name(self, version): backup1 = test_utils.create_backup( self.ctxt, display_name='b_test_name', status=fields.BackupStatus.AVAILABLE) backup2 = test_utils.create_backup( self.ctxt, display_name='a_test_name', status=fields.BackupStatus.AVAILABLE) url = '/v3/%s/backups?sort_key=name' % fake.PROJECT_ID req = fakes.HTTPRequest.blank(url, version=version) if version == mv.get_prior_version(mv.BACKUP_SORT_NAME): self.assertRaises(exception.InvalidInput, self.controller.index, req) else: expect = backup_view.ViewBuilder().summary_list(req, [backup1, backup2]) result = self.controller.index(req) self.assertEqual(expect, result)
def create_backup(self, volume_id=fake.VOLUME_ID, key_id=FIXED_KEY_ID): bak = tests_utils.create_backup(self.context, volume_id=volume_id, host=self.conf.host) if key_id: bak.encryption_key_id = key_id bak.save() self.my_baks = objects.BackupList.get_all_by_host( self.context, self.conf.host) bak.refresh() return bak
def create_backup(self, volume_id=fake.VOLUME_ID, key_id=FIXED_KEY_ID): bak = tests_utils.create_backup(self.context, volume_id=volume_id, host=self.conf.host) if key_id: bak.encryption_key_id = key_id bak.save() self.my_baks = objects.BackupList.get_all_by_host(self.context, self.conf.host) bak.refresh() return bak
def test_get_backup_device_available(self): vol = tests_utils.create_volume(self.context) self.context.user_id = fake.USER_ID self.context.project_id = fake.PROJECT_ID backup_obj = tests_utils.create_backup(self.context, vol['id']) (backup_device, is_snapshot) = self.volume.driver.get_backup_device( self.context, backup_obj) volume = objects.Volume.get_by_id(self.context, vol.id) self.assertEqual(volume, backup_device) self.assertFalse(is_snapshot) backup_obj.refresh() self.assertIsNone(backup_obj.temp_volume_id)
def test_backup_show_with_metadata(self, version): backup = test_utils.create_backup(self.ctxt, display_name='test_backup_metadata', status=fields.BackupStatus.AVAILABLE) # show backup metadata url = '/v3/%s/backups/%s' % (fake.PROJECT_ID, backup.id) req = fakes.HTTPRequest.blank(url, version=version) backup_get = self.controller.show(req, backup.id)['backup'] if version == mv.get_prior_version(mv.BACKUP_METADATA): self.assertNotIn('metadata', backup_get) else: self.assertIn('metadata', backup_get)
def _create_multiple_backups_with_different_project(self): test_utils.create_backup( context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True)) test_utils.create_backup( context.RequestContext(fake.USER_ID, fake.PROJECT_ID, True)) test_utils.create_backup( context.RequestContext(fake.USER_ID, fake.PROJECT2_ID, True))
def test_backup_update_with_null_validate(self): backup = test_utils.create_backup(self.ctxt, status=fields.BackupStatus.AVAILABLE) req = self._fake_update_request(fake.BACKUP_ID) updates = { "name": None, } body = {"backup": updates} self.controller.update(req, backup.id, body=body) backup.refresh() self.assertEqual(fields.BackupStatus.AVAILABLE, backup.status)
def test_backup_show_with_encryption_key_id(self, expected_in_details, encryption_key_id, version): backup = test_utils.create_backup(self.ctxt, encryption_key_id=encryption_key_id) self.addCleanup(backup.destroy) url = '/v3/%s/backups/%s' % (fake.PROJECT_ID, backup.id) req = fakes.HTTPRequest.blank(url, version=version) backup_details = self.controller.show(req, backup.id)['backup'] if expected_in_details: self.assertIn('encryption_key_id', backup_details) else: self.assertNotIn('encryption_key_id', backup_details)
def test_backup_update(self): backup = test_utils.create_backup(self.ctxt, status=fields.BackupStatus.AVAILABLE) req = self._fake_update_request(fake.BACKUP_ID) new_name = "updated_test_name" new_description = "Updated Test description." updates = { "name": new_name, "description": new_description, } body = {"backup": updates} self.controller.update(req, backup.id, body=body) backup.refresh() self.assertEqual(new_name, backup.display_name) self.assertEqual(new_description, backup.display_description)
def test_get_backup_device_in_use(self): vol = tests_utils.create_volume(self.context, status='backing-up', previous_status='in-use') temp_vol = tests_utils.create_volume(self.context) self.context.user_id = fake.USER_ID self.context.project_id = fake.PROJECT_ID backup_obj = tests_utils.create_backup(self.context, vol['id']) with mock.patch.object( self.volume.driver, '_create_temp_cloned_volume') as mock_create_temp: mock_create_temp.return_value = temp_vol (backup_device, is_snapshot) = (self.volume.driver.get_backup_device( self.context, backup_obj)) self.assertEqual(temp_vol, backup_device) self.assertFalse(is_snapshot) backup_obj.refresh() self.assertEqual(temp_vol.id, backup_obj.temp_volume_id)
def test_backup_update(self): backup = test_utils.create_backup( self.ctxt, status=fields.BackupStatus.AVAILABLE) req = self._fake_update_request(fake.BACKUP_ID) new_name = "updated_test_name" new_description = "Updated Test description." updates = { "name": new_name, "description": new_description, } body = {"backup": updates} self.controller.update(req, backup.id, body) backup.refresh() self.assertEqual(new_name, backup.display_name) self.assertEqual(new_description, backup.display_description)
def test_get_backup_device_in_use(self): vol = tests_utils.create_volume(self.context, status='backing-up', previous_status='in-use') temp_vol = tests_utils.create_volume(self.context) self.context.user_id = fake.USER_ID self.context.project_id = fake.PROJECT_ID backup_obj = tests_utils.create_backup(self.context, vol['id']) with mock.patch.object( self.volume.driver, '_create_temp_cloned_volume') as mock_create_temp: mock_create_temp.return_value = temp_vol (backup_device, is_snapshot) = ( self.volume.driver.get_backup_device(self.context, backup_obj)) self.assertEqual(temp_vol, backup_device) self.assertFalse(is_snapshot) backup_obj.refresh() self.assertEqual(temp_vol.id, backup_obj.temp_volume_id)
def test_backup_volume_inuse(self, mock_volume_get, mock_get_connector_properties, mock_file_open, mock_temporary_chown): vol = tests_utils.create_volume(self.context, status='backing-up', previous_status='in-use') self.context.user_id = fake.USER_ID self.context.project_id = fake.PROJECT_ID mock_volume_get.return_value = vol temp_snapshot = tests_utils.create_snapshot(self.context, vol['id']) backup = tests_utils.create_backup(self.context, vol['id']) backup_obj = objects.Backup.get_by_id(self.context, backup.id) properties = {} attach_info = {'device': {'path': '/dev/null'}} backup_service = mock.Mock() self.volume.driver._detach_volume = mock.MagicMock() self.volume.driver._attach_volume = mock.MagicMock() self.volume.driver.terminate_connection = mock.MagicMock() self.volume.driver._create_temp_snapshot = mock.MagicMock() self.volume.driver._delete_temp_snapshot = mock.MagicMock() mock_get_connector_properties.return_value = properties f = mock_file_open.return_value = open('/dev/null', 'rb') backup_service.backup(backup_obj, f, None) self.volume.driver._attach_volume.return_value = attach_info self.volume.driver._create_temp_snapshot.return_value = temp_snapshot self.volume.driver.backup_volume(self.context, backup_obj, backup_service) mock_volume_get.assert_called_with(self.context, vol['id']) self.volume.driver._create_temp_snapshot.assert_called_once_with( self.context, vol) self.volume.driver._delete_temp_snapshot.assert_called_once_with( self.context, temp_snapshot)
def _create_backup(self): backup = test_utils.create_backup(self.project_member_context, status=fields.BackupStatus.AVAILABLE, size=1) self.addCleanup(backup.destroy) return backup