def test_storehandlermanager__get_handler(self): """ Verify StoreHandlerManager._get_handler returns handlers properly. """ manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) handler = manager._get_handler(BogusModelType()) self.assertIsInstance(handler, StoreHandlerBase)
def test_storehandlermanager__get_handler(self): """ Verify StoreHandlerManager._get_handler returns handlers properly. """ manager = StoreHandlerManager() manager.register_store_handler(PhonyStoreHandler, {}, TestModel) handler = manager._get_handler(TestModel.new()) self.assertIsInstance(handler, PhonyStoreHandler)
def test_register_store_handler(self): """ Test the register_store_handler function. """ def override_parser_error(string): raise sys.exc_info()[0] parser = mock.MagicMock(argparse.ArgumentParser) parser.error = override_parser_error store_manager = mock.MagicMock(StoreHandlerManager) config = {} self.assertRaises( KeyError, script.register_store_handler, parser, store_manager, config) config = {'name': 'commissaire.bogus.module'} self.assertRaises( ImportError, script.register_store_handler, parser, store_manager, config) config = { 'name': 'commissaire.store.etcdstorehandler', 'models': ['Host*'] } script.register_store_handler(parser, store_manager, config) models = store_manager.register_store_handler.call_args[0][2:] self.assertTrue(len(models) > 1) self.assertTrue(all(x.__name__.startswith('Host') for x in models)) store_manager = StoreHandlerManager() # Note, this config is missing required info. config = { 'name': 'commissaire.store.etcdstorehandler', 'certificate-path': '/path/to/cert', 'models': ['*'] } self.assertRaises( ConfigurationError, script.register_store_handler, parser, store_manager, config) store_manager = StoreHandlerManager() # Catch model conflicts (TestModel assigned to both handlers) config = { 'name': 'commissaire.store.etcdstorehandler', 'models': ['*'] } script.register_store_handler(parser, store_manager, config) config = { 'name': 'commissaire.store.kubestorehandler', 'models': ['Host'] } self.assertRaises( ConfigurationError, script.register_store_handler, parser, store_manager, config)
def test_storehandlermanager_register_store_handler_with_one_model(self): """ Verify StoreHandlerManager registers StoreHandlers properly with one model. """ manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) expected = { BogusModelType: (StoreHandlerBase, {}, (BogusModelType, )), } self.assertEqual(expected, manager._registry)
def test_storehandlermanager_list(self): """ Verify the StoreHandlerManager list method works as expected. """ StoreHandlerBase = mock.MagicMock('StoreHandlerBase') manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) key = '/test' manager.list(key) StoreHandlerBase()._list.assert_called_once_with(key)
def test_storehandlermanager_register_store_handler_with_one_model(self): """ Verify StoreHandlerManager registers StoreHandlers properly with one model. """ manager = StoreHandlerManager() manager.register_store_handler(PhonyStoreHandler, {}, TestModel) expected = { TestModel: (PhonyStoreHandler, {}, (TestModel, )), } self.assertEqual(expected, manager._registry)
def test_storehandlermanager_get(self, PhonyStoreHandler): """ Verify the StoreHandlerManager get method works as expected. """ PhonyStoreHandler()._get.return_value = TestModel.new() manager = StoreHandlerManager() manager.register_store_handler(PhonyStoreHandler, {}, TestModel) model_instance = TestModel.new() result = manager.get(model_instance) PhonyStoreHandler()._get.assert_called_once_with(model_instance) self.assertEqual(PhonyStoreHandler()._get.return_value, result)
def test_storehandlermanager_save(self): """ Verify the StoreHandlerManager save method works as expected. """ StoreHandlerBase = mock.MagicMock('StoreHandlerBase') manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) key = '/test' value = {'test': 'data'} model_instance = BogusModelType() manager.save(model_instance) StoreHandlerBase()._save.assert_called_once_with(model_instance)
def test_storehandlermanager_get(self): """ Verify the StoreHandlerManager get method works as expected. """ StoreHandlerBase = mock.MagicMock('StoreHandlerBase') StoreHandlerBase()._get.return_value = BogusModelType() manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) key = '/test' result = manager.get(key) StoreHandlerBase()._get.assert_called_once_with(key) self.assertEqual(StoreHandlerBase()._get.return_value, result)
def test_storehandlermanager_clone(self): """ Verify the StoreHandlerManager clone method works as expected. """ manager = StoreHandlerManager() manager._registry['test'] = BogusModelType clone_result = manager.clone() # The manager and the cloned_result should not be the same self.assertNotEqual(manager, clone_result) # But their content should be self.assertEqual(manager._registry, clone_result._registry) # And the handlers should still be empty self.assertEqual({}, manager._handlers)
def test_storehandlermanager_clone(self): """ Verify the StoreHandlerManager clone method works as expected. """ manager = StoreHandlerManager() manager._registry['test'] = TestModel clone_result = manager.clone() # The manager and the cloned_result should not be the same self.assertNotEqual(manager, clone_result) # But their content should be self.assertEqual(manager._registry, clone_result._registry) # And the handlers should still be empty self.assertEqual({}, manager._handlers)
def test_storehandlermanager_register_store_handler_with_multiple_models( self): """ Verify StoreHandlerManager registers StoreHandlers properly with multiple models. """ manager = StoreHandlerManager() manager.register_store_handler(PhonyStoreHandler, {}, TestModelA) manager.register_store_handler(BogusStoreHandler, {}, TestModelB) expected = { TestModelA: (PhonyStoreHandler, {}, (TestModelA, )), TestModelB: (BogusStoreHandler, {}, (TestModelB, )), } self.assertEqual(expected, manager._registry)
def test_storehandlermanager_initialization(self): """ Verify the creation of a new StoreHandlerManager. """ manager = StoreHandlerManager() # There all internal data should be empty self.assertEqual({}, manager._registry) self.assertEqual({}, manager._handlers)
def __init__(self, bus): """ Creates a new instance of the CherryPyStorePlugin. :param bus: The CherryPy bus. :type bus: cherrypy.process.wspbus.Bus """ plugins.SimplePlugin.__init__(self, bus) self.manager = StoreHandlerManager()
def test_storehandlermanager_register_store_handler_with_multiple_models(self): """ Verify StoreHandlerManager registers StoreHandlers properly with multiple models. """ # Set up a few more bogus classes for testing class BogusStoreHandler(StoreHandlerBase): pass class AnotherBogusModelType(BogusModelType): pass manager = StoreHandlerManager() manager.register_store_handler(StoreHandlerBase, {}, BogusModelType) manager.register_store_handler(BogusStoreHandler, {}, AnotherBogusModelType) expected = { BogusModelType: (StoreHandlerBase, {}, (BogusModelType, )), AnotherBogusModelType: (BogusStoreHandler, {}, (AnotherBogusModelType, )), } self.assertEqual(expected, manager._registry)
def test_storehandlermanager_list_store_handlers(self): """ Verify StoreHandlerManager can list unique store handler instances. """ manager = StoreHandlerManager() # handler_type -> one model_type manager.register_store_handler(PhonyStoreHandler, {}, TestModelA) self.assertEqual(len(manager.list_store_handlers()), 1) # handler_type -> multiple model_types manager.register_store_handler(BogusStoreHandler, {}, TestModelB, TestModelC) self.assertEqual(len(manager.list_store_handlers()), 2)
def test_storehandlermanager_list_container_managers(self): """ Verify StoreHandlerManager correctly creates container managers. """ manager = StoreHandlerManager() manager.register_store_handler(BogusStoreHandler, {}, TestModelA) mgrs = manager.list_container_managers() self.assertEqual(len(mgrs), 1) self.assertIsInstance(mgrs[0], BogusContainerManager) mgrs = manager.list_container_managers(BOGUS_CLUSTER_TYPE) self.assertEqual(len(mgrs), 1) self.assertIsInstance(mgrs[0], BogusContainerManager) manager.register_store_handler(SillyStoreHandler, {}, TestModelB) # XXX We currently only keep the first container manager. mgrs = manager.list_container_managers() self.assertEqual(len(mgrs), 1) mgrs = manager.list_container_managers(SILLY_CLUSTER_TYPE) self.assertEqual(len(mgrs), 0)
def test_storehandlermanager_list(self, PhonyStoreHandler): """ Verify the StoreHandlerManager list method works as expected. """ manager = StoreHandlerManager() manager.register_store_handler(PhonyStoreHandler, {}, TestModel) model_instance = TestModel.new() manager.list(model_instance) PhonyStoreHandler()._list.assert_called_once_with(model_instance)