Fix cleanup of persistence tests

Cinderlib is designed to only have 1 persitent plugin loaded through
the whole life of the application, so our unit tests have to work around
this by cleaning things up manually, which they are not doing correctly.

Current code runs fine because test_dbms.py goes before test_memory.py,
but if we rename the test_memory.py to test_a.py then we'll start seing
"IndexError: list index out of range" errors.

There are 2 issues with the current code:

- We are not cleaning up properly after the memory persistence test
  because we are changing instance attributes instead of the class
  attributes.  We resolve this calling the clear method instead.

- We are not taking into account that the memory persistency plugin
  changes some of the OVO methods, so we need to restore them after
  the tests.

Change-Id: I29847f7a8de49625882145c3eff7983ff8d01265
This commit is contained in:
Gorka Eguileor 2019-07-24 13:59:00 +02:00
parent a18218ad0d
commit c44afc95af
2 changed files with 20 additions and 4 deletions

View File

@ -16,6 +16,8 @@
from cinder.cmd import volume as volume_cmd
from cinder.db.sqlalchemy import api
from cinder.db.sqlalchemy import models
from cinder import objects
from cinder.objects import base as cinder_base_ovo
from oslo_versionedobjects import fields
import cinderlib
@ -26,6 +28,15 @@ from cinderlib.tests.unit import utils
class BasePersistenceTest(base.BaseTest):
@classmethod
def setUpClass(cls):
# Save OVO methods that some persistence plugins mess up
cls.ovo_methods = {}
for ovo_name in cinder_base_ovo.CinderObjectRegistry.obj_classes():
ovo_cls = getattr(objects, ovo_name)
cls.ovo_methods[ovo_name] = {
'save': getattr(ovo_cls, 'save', None),
'get_by_id': getattr(ovo_cls, 'get_by_id', None),
}
cls.original_impl = volume_cmd.session.IMPL
cinderlib.Backend.global_initialization = False
cinderlib.setup(persistence_config=cls.PERSISTENCE_CFG)
@ -35,6 +46,11 @@ class BasePersistenceTest(base.BaseTest):
volume_cmd.session.IMPL = cls.original_impl
cinderlib.Backend.global_initialization = False
api.main_context_manager = api.enginefacade.transaction_context()
for ovo_name, methods in cls.ovo_methods.items():
ovo_cls = getattr(objects, ovo_name)
for method_name, method in methods.items():
if method:
setattr(ovo_cls, method_name, method)
def setUp(self):
super(BasePersistenceTest, self).setUp()

View File

@ -22,10 +22,10 @@ class TestMemoryPersistence(base.BasePersistenceTest):
def tearDown(self):
# Since this plugin uses class attributes we have to clear them
self.persistence.volumes = {}
self.persistence.snapshots = {}
self.persistence.connections = {}
self.persistence.key_values = {}
self.persistence.volumes.clear()
self.persistence.snapshots.clear()
self.persistence.connections.clear()
self.persistence.key_values.clear()
super(TestMemoryPersistence, self).tearDown()
def test_db(self):