def test_http_dispatcher_config_options(self): self.CONF.dispatcher_http.target = 'fake' self.CONF.dispatcher_http.timeout = 2 dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual('fake', dispatcher.target) self.assertEqual(2, dispatcher.timeout)
def test_http_dispatcher_batch_event(self): self.CONF.dispatcher_http.event_target = 'fake' self.CONF.dispatcher_http.batch_mode = True dispatcher = http.HttpDispatcher(self.CONF) with mock.patch('requests.post') as post: dispatcher.record_events([self.event, self.event]) self.assertEqual(1, post.call_count)
def test_http_dispatcher_batch(self): self.CONF.dispatcher_http.target = 'fake' self.CONF.dispatcher_http.batch_mode = True dispatcher = http.HttpDispatcher(self.CONF) with mock.patch('requests.post') as post: dispatcher.record_metering_data([self.msg, self.msg, self.msg]) self.assertEqual(1, post.call_count)
def test_http_dispatcher_with_no_metadata(self): self.CONF.dispatcher_http.target = 'fake' dispatcher = http.HttpDispatcher(self.CONF) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) self.assertEqual(1, post.call_count)
def test_http_dispatcher_share_target(self): self.CONF.dispatcher_http.event_target = 'fake' dispatcher = http.HttpDispatcher(self.CONF) with mock.patch.object(requests, 'post') as post: dispatcher.record_events(self.event) self.assertEqual('fake', post.call_args[0][0])
def test_http_dispatcher_with_cert_auth(self): self.CONF.dispatcher_http.target = 'https://example.com' self.CONF.dispatcher_http.target_clientcert = '/path/to/cert.crt' dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual('/path/to/cert.crt', dispatcher.target_auth_cert) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) self.assertEqual('/path/to/cert.crt', post.call_args[1]['cert'])
def test_http_dispatcher_with_ssl_path(self): self.CONF.dispatcher_http.event_target = 'https://example.com' self.CONF.dispatcher_http.verify_ssl = '/path/to/cert.crt' dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual('/path/to/cert.crt', dispatcher.verify_ssl) with mock.patch.object(requests, 'post') as post: dispatcher.record_events(self.event) self.assertEqual('/path/to/cert.crt', post.call_args[1]['verify'])
def test_http_dispatcher_with_ssl_false(self): self.CONF.dispatcher_http.target = 'https://example.com' self.CONF.dispatcher_http.verify_ssl = 'false' dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual(False, dispatcher.verify_ssl) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) self.assertEqual(False, post.call_args[1]['verify'])
def test_http_dispatcher_share_target(self): self.CONF.dispatcher_http.target = 'fake' dispatcher = http.HttpDispatcher(self.CONF) event = event_models.Event(uuid.uuid4(), 'test', datetime.datetime(2012, 7, 2, 13, 53, 40), [], {}).serialize() with mock.patch.object(requests, 'post') as post: dispatcher.record_events(event) self.assertEqual('fake', post.call_args[0][0])
def test_http_dispatcher_bad(self): self.CONF.dispatcher_http.event_target = '' dispatcher = http.HttpDispatcher(self.CONF) event = event_models.Event(uuid.uuid4(), 'test', datetime.datetime(2012, 7, 2, 13, 53, 40), [], {}).serialize() with mock.patch('ceilometer.dispatcher.http.LOG', mock.MagicMock()) as LOG: dispatcher.record_events(event) self.assertTrue(LOG.exception.called)
def test_http_dispatcher_with_basic_auth(self): self.CONF.dispatcher_http.event_target = 'https://example.com' self.CONF.dispatcher_http.event_target_username = '******' self.CONF.dispatcher_http.event_target_password = '******' dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual(('alice', 'W0nd3r!4nd'), dispatcher.event_target_auth) with mock.patch.object(requests, 'post') as post: dispatcher.record_events(self.event) self.assertEqual(('alice', 'W0nd3r!4nd'), post.call_args[1]['auth'])
def test_http_dispatcher_bad_server(self): self.CONF.dispatcher_http.event_target = 'fake' dispatcher = http.HttpDispatcher(self.CONF) with mock.patch.object(requests, 'post') as post: response = requests.Response() response.status_code = 500 post.return_value = response with mock.patch('ceilometer.dispatcher.http.LOG', mock.MagicMock()) as LOG: dispatcher.record_events(self.event) self.assertTrue(LOG.exception.called)
def test_http_dispatcher_with_no_target(self): self.CONF.dispatcher_http.target = '' dispatcher = http.HttpDispatcher(self.CONF) # The target should be None self.assertEqual('', dispatcher.target) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) # Since the target is not set, no http post should occur, thus the # call_count should be zero. self.assertEqual(0, post.call_count)
def test_http_dispatcher_share_target(self): self.CONF.dispatcher_http.target = 'fake' dispatcher = http.HttpDispatcher(self.CONF) event = event_models.Event(uuid.uuid4(), 'test', datetime.datetime(2012, 7, 2, 13, 53, 40), [], {}) event = utils.message_from_event(event, self.CONF.publisher.telemetry_secret) with mock.patch.object(requests, 'post') as post: dispatcher.record_events(event) self.assertEqual('fake', post.call_args[0][0])
def test_http_dispatcher_bad(self): self.CONF.dispatcher_http.event_target = '' dispatcher = http.HttpDispatcher(self.CONF) event = event_models.Event(uuid.uuid4(), 'test', datetime.datetime(2012, 7, 2, 13, 53, 40), [], {}) event = utils.message_from_event(event, self.CONF.publisher.telemetry_secret) with mock.patch('ceilometer.dispatcher.http.LOG', mock.MagicMock()) as LOG: dispatcher.record_events(event) self.assertTrue(LOG.exception.called)
def test_http_dispatcher_with_cert_key_auth(self): self.CONF.dispatcher_http.event_target = 'https://example.com' self.CONF.dispatcher_http.event_target_clientcert = '/path/to/cert.crt' self.CONF.dispatcher_http.event_target_clientkey = '/path/to/cert.key' dispatcher = http.HttpDispatcher(self.CONF) self.assertEqual(('/path/to/cert.crt', '/path/to/cert.key'), dispatcher.event_target_auth_cert) with mock.patch.object(requests, 'post') as post: dispatcher.record_events(self.event) self.assertEqual(('/path/to/cert.crt', '/path/to/cert.key'), post.call_args[1]['cert'])
def test_http_dispatcher_with_cadf_event(self): self.CONF.dispatcher_http.target = 'fake' self.CONF.dispatcher_http.cadf_only = True dispatcher = http.HttpDispatcher(self.CONF) self.msg['resource_metadata'] = {'request': {'CADF_EVENT': { 'q1': 'v1', 'q2': 'v2'}, }, } self.msg['message_signature'] = utils.compute_signature( self.msg, self.CONF.publisher.telemetry_secret, ) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) self.assertEqual(1, post.call_count)
def test_http_dispatcher_with_none_cadf_event(self): self.CONF.dispatcher_http.target = 'fake' self.CONF.dispatcher_http.cadf_only = False dispatcher = http.HttpDispatcher(self.CONF) self.msg['resource_metadata'] = { 'any': { 'thing1': 'v1', 'thing2': 'v2', }, } self.msg['message_signature'] = utils.compute_signature( self.msg, self.CONF.publisher.metering_secret, ) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) self.assertEqual(1, post.call_count)
def test_http_dispatcher_without_cadf_event(self): self.CONF.dispatcher_http.target = 'fake' self.CONF.dispatcher_http.cadf_only = True dispatcher = http.HttpDispatcher(self.CONF) self.msg['resource_metadata'] = { 'request': { 'NONE_CADF_EVENT': { 'q1': 'v1', 'q2': 'v2' }, }, } self.msg['message_signature'] = utils.compute_signature( self.msg, self.CONF.publisher.metering_secret, ) with mock.patch.object(requests, 'post') as post: dispatcher.record_metering_data(self.msg) # Since the meter does not have metadata or CADF_EVENT, the method # call count should be zero self.assertEqual(0, post.call_count)