def setUp(self): super(TestQpidNotifier, self).setUp() if not qpid: self.skipTest("qpid not installed") self.mocker = mox.Mox() self.mock_connection = None self.mock_session = None self.mock_sender = None self.mock_receiver = None self.orig_connection = qpid.messaging.Connection self.orig_session = qpid.messaging.Session self.orig_sender = qpid.messaging.Sender self.orig_receiver = qpid.messaging.Receiver qpid.messaging.Connection = lambda *_x, **_y: self.mock_connection qpid.messaging.Session = lambda *_x, **_y: self.mock_session qpid.messaging.Sender = lambda *_x, **_y: self.mock_sender qpid.messaging.Receiver = lambda *_x, **_y: self.mock_receiver self.notify_qpid = importutils.import_module("glance.notifier." "notify_qpid") self.addCleanup(self.reset_qpid) self.addCleanup(self.mocker.ResetAll)
def _load_backend(self): with self._lock: if not self._backend: # Import the untranslated name if we don't have a mapping backend_path = self._backend_mapping.get(self._backend_name, self._backend_name) backend_mod = importutils.import_module(backend_path) self._backend = backend_mod.get_backend()
def test_unwrap(self): CONF.set_override('use_tpool', True) CONF.set_override('data_api', 'glance.tests.functional.db.' 'test_db_api') dbapi = db_api.get_api() self.assertEqual(importutils.import_module(CONF.data_api), dbapi.unwrap())
def _load_backend(self): with self._lock: if not self._backend: # Import the untranslated name if we don't have a mapping backend_path = self._backend_mapping.get( self._backend_name, self._backend_name) backend_mod = importutils.import_module(backend_path) self._backend = backend_mod.get_backend()
def _get_drivers(): """Instantiates and returns drivers based on the flag values.""" global drivers if drivers is None: drivers = [] for notification_driver in CONF.list_notifier_drivers: try: drivers.append(importutils.import_module(notification_driver)) except ImportError as e: drivers.append(ImportFailureNotifier(e)) return drivers
def test_thread_pool(self): module = importutils.import_module("glance.tests.functional.db." "test_db_api") CONF.set_override("use_tpool", True) CONF.set_override("data_api", "glance.tests.functional.db." "test_db_api") dbapi = db_api.get_api() from eventlet import tpool tpool.execute = Mock() dbapi.method_for_test_1(1, 2, kwarg="arg") tpool.execute.assert_called_with(method_for_test_1, 1, 2, kwarg="arg")
def notify(context, publisher_id, event_type, priority, payload): """Sends a notification using the specified driver :param publisher_id: the source worker_type.host of the message :param event_type: the literal type of event (ex. Instance Creation) :param priority: patterned after the enumeration of Python logging levels in the set (DEBUG, WARN, INFO, ERROR, CRITICAL) :param payload: A python dictionary of attributes Outgoing message format includes the above parameters, and appends the following: message_id a UUID representing the id for this notification timestamp the GMT timestamp the notification was sent at The composite message will be constructed as a dictionary of the above attributes, which will then be sent via the transport mechanism defined by the driver. Message example:: {'message_id': str(uuid.uuid4()), 'publisher_id': 'compute.host1', 'timestamp': timeutils.utcnow(), 'priority': 'WARN', 'event_type': 'compute.create_instance', 'payload': {'instance_id': 12, ... }} """ if priority not in log_levels: raise BadPriorityException( _('%s not in valid priorities') % priority) # Ensure everything is JSON serializable. payload = jsonutils.to_primitive(payload, convert_instances=True) driver = importutils.import_module(CONF.notification_driver) msg = dict(message_id=str(uuid.uuid4()), publisher_id=publisher_id, event_type=event_type, priority=priority, payload=payload, timestamp=str(timeutils.utcnow())) try: driver.notify(context, msg) except Exception, e: LOG.exception(_("Problem '%(e)s' attempting to " "send to notification system. Payload=%(payload)s") % locals())
def test_thread_pool(self): module = importutils.import_module('glance.tests.functional.db.' 'test_db_api') CONF.set_override('use_tpool', True) CONF.set_override('data_api', 'glance.tests.functional.db.' 'test_db_api') dbapi = db_api.get_api() from eventlet import tpool tpool.execute = Mock() dbapi.method_for_test_1(1, 2, kwarg='arg') tpool.execute.assert_called_with(method_for_test_1, 1, 2, kwarg='arg')
def add_driver(notification_driver): """Add a notification driver at runtime.""" # Make sure the driver list is initialized. _get_drivers() if isinstance(notification_driver, basestring): # Load and add try: drivers.append(importutils.import_module(notification_driver)) except ImportError as e: drivers.append(ImportFailureNotifier(e)) else: # Driver is already loaded; just add the object. drivers.append(notification_driver)
def setUp(self): super(TestRabbitNotifier, self).setUp() def _fake_connect(rabbit_self): rabbit_self.connection_errors = () rabbit_self.connection = "fake_connection" return None self.notify_kombu = importutils.import_module("glance.notifier." "notify_kombu") self.notify_kombu.RabbitStrategy._send_message = self._send_message self.notify_kombu.RabbitStrategy._connect = _fake_connect self.called = False self.config(notifier_strategy="rabbit", rabbit_retry_backoff=0, rabbit_notification_topic="fake_topic") self.notifier = notifier.Notifier()
def add_driver(notification_driver): """Add a notification driver at runtime.""" # Make sure the driver list is initialized. _get_drivers() if isinstance(notification_driver, basestring): # Load and add try: driver = importutils.import_module(notification_driver) _drivers[notification_driver] = driver except ImportError as e: LOG.exception( _("Failed to load notifier %s. " "These notifications will not be sent.") % notification_driver ) else: # Driver is already loaded; just add the object. _drivers[notification_driver] = notification_driver
def add_driver(notification_driver): """Add a notification driver at runtime.""" # Make sure the driver list is initialized. _get_drivers() if isinstance(notification_driver, basestring): # Load and add try: driver = importutils.import_module(notification_driver) _drivers[notification_driver] = driver except ImportError as e: LOG.exception(_("Failed to load notifier %s. " "These notifications will not be sent.") % notification_driver) else: # Driver is already loaded; just add the object. _drivers[notification_driver] = notification_driver
def setUp(self): super(TestRabbitNotifier, self).setUp() def _fake_connect(rabbit_self): rabbit_self.connection_errors = () rabbit_self.connection = 'fake_connection' return None self.notify_kombu = importutils.import_module("glance.notifier." "notify_kombu") self.notify_kombu.RabbitStrategy._send_message = self._send_message self.notify_kombu.RabbitStrategy._connect = _fake_connect self.called = False self.config(notifier_strategy="rabbit", rabbit_retry_backoff=0, rabbit_notification_topic="fake_topic") self.notifier = notifier.Notifier()
def __get_backend(self): """Get the actual backend. May be a module or an instance of a class. Doesn't matter to us. We do this synchronized as it's possible multiple greenthreads started very quickly trying to do DB calls and eventlet can switch threads before self.__backend gets assigned. """ if self.__backend: # Another thread assigned it return self.__backend backend_name = CONF.database.backend self.__use_tpool = CONF.database.use_tpool if self.__use_tpool: from eventlet import tpool self.__tpool = tpool # Import the untranslated name if we don't have a # mapping. backend_path = self.__backend_mapping.get(backend_name, backend_name) backend_mod = importutils.import_module(backend_path) self.__backend = backend_mod.get_backend() return self.__backend
def setUp(self): super(TestQpidNotifier, self).setUp() if not qpid: return self.mocker = mox.Mox() self.mock_connection = None self.mock_session = None self.mock_sender = None self.mock_receiver = None self.orig_connection = qpid.messaging.Connection self.orig_session = qpid.messaging.Session self.orig_sender = qpid.messaging.Sender self.orig_receiver = qpid.messaging.Receiver qpid.messaging.Connection = lambda *_x, **_y: self.mock_connection qpid.messaging.Session = lambda *_x, **_y: self.mock_session qpid.messaging.Sender = lambda *_x, **_y: self.mock_sender qpid.messaging.Receiver = lambda *_x, **_y: self.mock_receiver self.notify_qpid = importutils.import_module("glance.notifier." "notify_qpid")
def get_api(): api = importutils.import_module(CONF.data_api) if hasattr(api, 'configure'): api.configure() return api
def __init__(self, wrapped): self.wrapped = importutils.import_module(wrapped)
def get_api(): if not CONF.use_tpool: return importutils.import_module(CONF.data_api) return ThreadPoolWrapper(CONF.data_api)
def test_unwrap_dbapi_when_db_pool_is_disabled(self): CONF.set_override('use_tpool', True) dbapi = db_api.get_api() self.assertEqual(importutils.import_module(CONF.data_api), glance.db.unwrap(dbapi))
def test_get_dbapi_when_db_pool_is_disabled(self): CONF.set_override('use_tpool', False) dbapi = db_api.get_api() self.assertFalse(isinstance(dbapi, db_api.ThreadPoolWrapper)) self.assertEqual(importutils.import_module(CONF.data_api), dbapi)
def get_api(): return importutils.import_module(CONF.data_api)