def test_client_initialization_with_ssl_version(self, mock_kombu): ssl_versions = (('tlsv1', getattr(ssl_module, 'PROTOCOL_TLSv1', None)), ('tlsv1_1', getattr(ssl_module, 'PROTOCOL_TLSv1_1', None)), ('tlsv1_2', getattr(ssl_module, 'PROTOCOL_TLSv1_2', None)), ('sslv2', getattr(ssl_module, 'PROTOCOL_SSLv2', None)), ('sslv23', getattr(ssl_module, 'PROTOCOL_SSLv23', None)), ('sslv3', getattr(ssl_module, 'PROTOCOL_SSLv3', None))) exception_count = 0 for ssl_name, ssl_version in ssl_versions: ssl_kwargs = { 'login': '******', 'password': '******', 'host': 'test_host', 'port': 'test_port', 'virtual_host': 'test_virtual_host', 'ssl': True, 'ssl_version': ssl_name, 'ca_certs': ['cert1'], 'insecure': False } # If a ssl_version is not valid, a RuntimeError is thrown. # According to the ssl_version docs in config.py, certain versions # of TLS may be available depending on the system. So, just # check that at least 1 ssl_version works. if ssl_version is None: e = self.assertRaises(RuntimeError, mqclient.MqClient, **ssl_kwargs) self.assertEqual( _('Invalid SSL version: %s') % ssl_name, e.__str__()) exception_count += 1 continue self.ssl_client = mqclient.MqClient(**ssl_kwargs) mock_kombu.Connection.assert_called_once_with( 'amqp://{0}:{1}@{2}:{3}/{4}'.format('test_login', 'test_password', 'test_host', 'test_port', 'test_virtual_host'), ssl={ 'ca_certs': ['cert1'], 'cert_reqs': ssl_module.CERT_REQUIRED, 'ssl_version': ssl_version }) self.assertEqual(mock_kombu.Connection(), self.ssl_client._connection) self.assertIsNone(self.ssl_client._channel) self.assertFalse(self.ssl_client._connected) mock_kombu.Connection.reset_mock() # Check that at least one ssl_version worked. self.assertGreater(len(ssl_versions), exception_count)
def test_alternate_client_initializations(self, mock_kombu): for ca_cert in ['cert1', None]: client = mqclient.MqClient(login='******', password='******', host='test_host', port='test_port', virtual_host='test_virtual_host', ssl=True, ca_certs=ca_cert, insecure=True) mock_kombu.Connection.assert_called_once_with( 'amqp://{0}:{1}@{2}:{3}/{4}'.format('test_login', 'test_password', 'test_host', 'test_port', 'test_virtual_host'), ssl={ 'ca_certs': ca_cert, 'cert_reqs': ssl_module.CERT_OPTIONAL if ca_cert else ssl_module.CERT_NONE }) self.assertEqual(mock_kombu.Connection(), client._connection) mock_kombu.Connection.reset_mock() client = mqclient.MqClient(login='******', password='******', host='test_host', port='test_port', virtual_host='test_virtual_host', ssl=False, ca_certs=None, insecure=False) mock_kombu.Connection.assert_called_once_with( 'amqp://{0}:{1}@{2}:{3}/{4}'.format('test_login', 'test_password', 'test_host', 'test_port', 'test_virtual_host'), ssl=None) self.assertEqual(mock_kombu.Connection(), client._connection)
def create_rmq_client(): rabbitmq = CONF.rabbitmq connection_params = { 'login': rabbitmq.login, 'password': rabbitmq.password, 'host': rabbitmq.host, 'port': rabbitmq.port, 'virtual_host': rabbitmq.virtual_host, 'ssl': rabbitmq.ssl, 'ca_certs': rabbitmq.ca_certs.strip() or None } return mqclient.MqClient(**connection_params)
def setUp(self, mock_kombu): super(MQClientTest, self).setUp() self.ssl_client = mqclient.MqClient(login='******', password='******', host='test_host', port='test_port', virtual_host='test_virtual_host', ssl=True, ca_certs=['cert1'], insecure=False) mock_kombu.Connection.assert_called_once_with( 'amqp://{0}:{1}@{2}:{3}/{4}'.format('test_login', 'test_password', 'test_host', 'test_port', 'test_virtual_host'), ssl={ 'ca_certs': ['cert1'], 'cert_reqs': ssl_module.CERT_REQUIRED }) self.assertEqual(mock_kombu.Connection(), self.ssl_client._connection) self.assertIsNone(self.ssl_client._channel) self.assertFalse(self.ssl_client._connected)
def create_rmq_client(region): region_config = region().getConfig() rmq_settings = dict(region_config['agentRabbitMq']) rmq_settings['ca_certs'] = CONF.rabbitmq.ca_certs.strip() or None return mqclient.MqClient(**rmq_settings)