Advertisement
Guest User

Untitled

a guest
May 28th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. class FixtureHelper(object):
  2. """Underlying helper object for a StorageFixture to hold driver methods"""
  3.  
  4. def __init__(self, fixture):
  5. """
  6. :param fixture: The fixture object
  7. """
  8. self.fixture = fixture
  9.  
  10. def setUp(self):
  11. """Runs pr test, typically a db reset or similar"""
  12.  
  13. def pre_migrate(self):
  14. """Run before migrations"""
  15.  
  16. def migrate(self):
  17. """Migrate the storage"""
  18.  
  19. def post_migrate(self):
  20. """This is executed after migrations"""
  21.  
  22. def post_init(self):
  23. """Runs at the end of the object initialization"""
  24.  
  25.  
  26. class SQLAlchemyHelper(FixtureHelper):
  27. def __init__(self, fixture):
  28. super(SQLAlchemyHelper, self).__init__(fixture)
  29.  
  30. self.sqlite_db = fixture.kw.get('sqlite_db')
  31. self.sqlite_clean_db = fixture.kw.get('sqlite_clean_db')
  32. self.testdb = None
  33.  
  34. def setUp(self):
  35. if self.fixture.database_connection == "sqlite://":
  36. conn = self.fixture.connection.engine.connect()
  37. conn.connection.executescript(self._DB)
  38. self.addCleanup(self.fixture.connection.engine.dispose)
  39. else:
  40. shutil.copyfile(paths.state_path_rel(self.sqlite_clean_db),
  41. paths.state_path_rel(self.sqlite_db))
  42.  
  43. def pre_migrate(self):
  44. self.fixture.connection.engine.dispose()
  45. self.fixture.connection.engine.connect()
  46. if self.fixture.database_connection == "sqlite://":
  47. #https://github.com/openstack/nova/blob/master/nova/test.py#L82-L84
  48. pass
  49. else:
  50. testdb = paths.state_path_rel(self.sqlite_db)
  51. if os.path.exists(testdb):
  52. return
  53.  
  54. def migrate(self):
  55. self.fixture.connection.setup_schema()
  56.  
  57. def post_init(self):
  58. if self.fixture.database_connection == "sqlite://":
  59. conn = self.fixture.connection.engine.connect()
  60. self._as_string = "".join(
  61. l for l in conn.iterdump())
  62. self.fixture.connection.engine.dispose()
  63. else:
  64. cleandb = paths.state_path_rel(self.sqlite_clean_db)
  65. shutil.copyfile(self.testdb, cleandb)
  66.  
  67.  
  68. class StorageFixture(fixtures.Fixture):
  69. """
  70. Storage fixture that for now just supports SQLAlchemy
  71. """
  72. def __init__(self, svc, **kw):
  73. self.svc = svc
  74. self.kw = kw
  75.  
  76. self.driver = kw.get('storage_driver', 'sqlalchemy')
  77. self.database_connection = kw.get('database_connection', 'sqlite://')
  78.  
  79. self.svc_group = 'service:%s' % self.svc
  80. self.driver_group = '%s:%s' % (self.svc, self.driver)
  81.  
  82. self.module = self.import_module()
  83.  
  84. self.helper = SQLAlchemyHelper(self)
  85.  
  86. set_config(storage_driver=self.driver, group=self.svc_group)
  87. set_config(database_connection=self.database_connection,
  88. group=self.driver_group)
  89.  
  90. self.connection = self.get_storage_connection(**kw)
  91.  
  92. self.helper.pre_migrate()
  93. self.helper.migrate()
  94. self.helper.post_migrate()
  95. self.helper.post_init()
  96.  
  97. def setUp(self):
  98. super(StorageFixture, self).setUp()
  99. self.helper.setUp()
  100.  
  101. def import_module(self):
  102. """
  103. Setup the storage module on self.
  104. """
  105. return importutils.import_module('billingstack.%s.storage' % self.svc)
  106.  
  107. def get_storage_connection(self, **kw):
  108. """
  109. Import the storage module for the service that we are going to act on,
  110. then return a connection object for that storage module.
  111.  
  112. :param service: The service.
  113. """
  114. engine = self.module.get_engine(self.driver)
  115. return engine.get_connection()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement