From ce8893f4d3fae402494328bfbd07ea6642aae836 Mon Sep 17 00:00:00 2001 From: Marc Koderer Date: Thu, 31 Jul 2014 10:10:17 +0200 Subject: [PATCH] Fix unit test test_import_record_with_verify The test case test_import_record_with_verify is intended to call a backup service class with a defined verify function. This verify function wasn't overloaded by the fake service. Change-Id: Ib5738e9c463856dd71a47717b7abb8dff785b3b1 Closes-bug: #1350699 --- .../tests/backup/fake_service_with_verify.py | 28 ++++++++ cinder/tests/test_backup.py | 69 +++++++++++-------- 2 files changed, 68 insertions(+), 29 deletions(-) create mode 100644 cinder/tests/backup/fake_service_with_verify.py diff --git a/cinder/tests/backup/fake_service_with_verify.py b/cinder/tests/backup/fake_service_with_verify.py new file mode 100644 index 000000000..32695c127 --- /dev/null +++ b/cinder/tests/backup/fake_service_with_verify.py @@ -0,0 +1,28 @@ +# Copyright (C) 2014 Deutsche Telekom AG +# 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 cinder.openstack.common import log as logging +from cinder.tests.backup import fake_service + +LOG = logging.getLogger(__name__) + + +class FakeBackupServiceWithVerify(fake_service.FakeBackupService): + def verify(self, backup): + pass + + +def get_backup_driver(context): + return FakeBackupServiceWithVerify(context) diff --git a/cinder/tests/test_backup.py b/cinder/tests/test_backup.py index dd0b604a6..8b6af7abb 100644 --- a/cinder/tests/test_backup.py +++ b/cinder/tests/test_backup.py @@ -40,11 +40,9 @@ class FakeBackupException(Exception): pass -class BackupTestCase(test.TestCase): - """Test Case for backups.""" - +class BaseBackupTest(test.TestCase): def setUp(self): - super(BackupTestCase, self).setUp() + super(BaseBackupTest, self).setUp() vol_tmpdir = tempfile.mkdtemp() self.flags(volumes_dir=vol_tmpdir) self.backup_mgr = \ @@ -124,6 +122,10 @@ class BackupTestCase(test.TestCase): backup['status'] = status return db.backup_create(self.ctxt, backup)['id'] + +class BackupTestCase(BaseBackupTest): + """Test Case for backups.""" + def test_init_host(self): """Make sure stuck volumes and backups are reset to correct states when backup_manager.init_host() is called @@ -484,31 +486,6 @@ class BackupTestCase(test.TestCase): self.assertEqual(backup['status'], 'available') self.assertEqual(backup['size'], vol_size) - def test_import_record_with_verify(self): - """Test normal backup record import. - - Test the case when import succeeds for the case that the - driver implements verify. - """ - vol_size = 1 - export = self._create_exported_record_entry(vol_size=vol_size) - imported_record = self._create_export_record_db_entry() - backup_hosts = [] - backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) - _mock_backup_verify_class = ('%s.%s.%s' % - (backup_driver.__module__, - backup_driver.__class__.__name__, - 'verify')) - with mock.patch(_mock_backup_verify_class): - self.backup_mgr.import_record(self.ctxt, - imported_record, - export['backup_service'], - export['backup_url'], - backup_hosts) - backup = db.backup_get(self.ctxt, imported_record) - self.assertEqual(backup['status'], 'available') - self.assertEqual(backup['size'], vol_size) - def test_import_record_with_bad_service(self): """Test error handling when attempting an import of a backup record with a different service to that used to create the backup. @@ -564,6 +541,40 @@ class BackupTestCase(test.TestCase): backup = db.backup_get(self.ctxt, imported_record) self.assertEqual(backup['status'], 'error') + +class BackupTestCaseWithVerify(BaseBackupTest): + """Test Case for backups.""" + + def setUp(self): + CONF.set_override("backup_driver", + "cinder.tests.backup.fake_service_with_verify") + super(BackupTestCaseWithVerify, self).setUp() + + def test_import_record_with_verify(self): + """Test normal backup record import. + + Test the case when import succeeds for the case that the + driver implements verify. + """ + vol_size = 1 + export = self._create_exported_record_entry(vol_size=vol_size) + imported_record = self._create_export_record_db_entry() + backup_hosts = [] + backup_driver = self.backup_mgr.service.get_backup_driver(self.ctxt) + _mock_backup_verify_class = ('%s.%s.%s' % + (backup_driver.__module__, + backup_driver.__class__.__name__, + 'verify')) + with mock.patch(_mock_backup_verify_class): + self.backup_mgr.import_record(self.ctxt, + imported_record, + export['backup_service'], + export['backup_url'], + backup_hosts) + backup = db.backup_get(self.ctxt, imported_record) + self.assertEqual(backup['status'], 'available') + self.assertEqual(backup['size'], vol_size) + def test_import_record_with_verify_invalid_backup(self): """Test error handling when attempting an import of a backup record where the backup driver returns an exception. -- 2.45.2