def test_channel_multiple_statuses_down(self): channel = yield self.create_channel(self.service, self.redis, id='channel-id') components = {} for i in range(5): status = TransportStatus(status='ok', component=i, type='bar', message='Bar') yield channel.sstore.store_status('channel-id', status) components[str(i)] = api_from_status('channel-id', status) status = TransportStatus(status='degraded', component=5, type='bar', message='Bar') yield channel.sstore.store_status('channel-id', status) components['5'] = api_from_status('channel-id', status) status = TransportStatus(status='down', component=6, type='bar', message='Bar') yield channel.sstore.store_status('channel-id', status) components['6'] = api_from_status('channel-id', status) self.assert_status((yield channel.status())['status'], level='down', components=components)
def test_store_single_status(self): '''The single status is stored under the correct key''' store = yield self.create_store() status = TransportStatus( status='ok', component='foo', type='bar', message='foo') yield store.store_status('channelid', status) status_redis = yield self.redis.hget('channelid:status', 'foo') self.assertEqual(status_redis, status.to_json()) self.assertEqual((yield self.redis.ttl('channelid:status')), None)
def test_store_single_status(self): '''The single status is stored under the correct key''' store = yield self.create_store() status = TransportStatus(status='ok', component='foo', type='bar', message='foo') yield store.store_status('channelid', status) status_redis = yield self.redis.hget('channelid:status', 'foo') self.assertEqual(status_redis, status.to_json()) self.assertEqual((yield self.redis.ttl('channelid:status')), None)
def test_status_stored_in_redis(self): '''The published status gets consumed and stored in redis under the correct key''' status = TransportStatus(component='foo', status='ok', type='bar', message='Bar') yield self.worker.consume_status(status) redis_status = yield self.worker.store.redis.hget( 'testchannel:status', 'foo') self.assertEqual(redis_status, status.to_json())
def test_status_stored_in_redis(self): '''The published status gets consumed and stored in redis under the correct key''' status = TransportStatus( component='foo', status='ok', type='bar', message='Bar') yield self.worker.consume_status(status) redis_status = yield self.worker.store.redis.hget( 'testchannel:status', 'foo') self.assertEqual(redis_status, status.to_json())
def test_defaults(self): msg = TransportStatus(component='foo', status='ok', type='bar', message='baz') self.assertEqual(msg['details'], {})
def get_statuses(self, channel_id): '''Returns the latest status message for each component in a dictionary''' key = self.get_key(channel_id) statuses = yield self.load_all(key) returnValue( dict((k, TransportStatus.from_json(v)) for k, v in statuses.iteritems()))
def test_store_status_overwrite(self): '''New statuses override old statuses with the same component, but do not affect statuses of different components''' store = yield self.create_store() status_old = TransportStatus(status='ok', component='foo', type='bar', message='foo') status_new = TransportStatus(status='down', component='foo', type='bar', message='foo') status_other = TransportStatus(status='ok', component='bar', type='bar', message='foo') yield store.store_status('channelid', status_other) yield store.store_status('channelid', status_old) yield store.store_status('channelid', status_new) status_new_redis = yield self.redis.hget('channelid:status', 'foo') self.assertEqual(status_new_redis, status_new.to_json()) status_other_redis = yield self.redis.hget('channelid:status', 'bar') self.assertEqual(status_other_redis, status_other.to_json())
def get_statuses(self, channel_id): '''Returns the latest status message for each component in a dictionary''' key = self.get_key(channel_id) statuses = yield self.load_all(key) returnValue(dict( (k, TransportStatus.from_json(v)) for k, v in statuses.iteritems() ))
def test_load_one_status(self): store = yield self.create_store() status = TransportStatus(status='ok', component='foo', type='bar', message='foo') yield store.store_status('channelid', status) stored_statuses = yield store.get_statuses('channelid') self.assertEqual(stored_statuses, {'foo': status})
def test_store_status_overwrite(self): '''New statuses override old statuses with the same component, but do not affect statuses of different components''' store = yield self.create_store() status_old = TransportStatus( status='ok', component='foo', type='bar', message='foo') status_new = TransportStatus( status='down', component='foo', type='bar', message='foo') status_other = TransportStatus( status='ok', component='bar', type='bar', message='foo') yield store.store_status('channelid', status_other) yield store.store_status('channelid', status_old) yield store.store_status('channelid', status_new) status_new_redis = yield self.redis.hget('channelid:status', 'foo') self.assertEqual(status_new_redis, status_new.to_json()) status_other_redis = yield self.redis.hget('channelid:status', 'bar') self.assertEqual(status_other_redis, status_other.to_json())
def test_validate_status_field(self): TransportStatus(status='ok', component='foo', type='bar', message='baz') TransportStatus(status='degraded', component='foo', type='bar', message='baz') TransportStatus(status='down', component='foo', type='bar', message='baz') self.assertRaises(InvalidMessageField, TransportStatus, status='amazing', component='foo', type='bar', message='baz')
def test_channel_status_single_status(self): channel = yield self.create_channel( self.service, self.redis, id='channel-id') status = TransportStatus( status='ok', component='foo', type='bar', message='Bar') yield channel.sstore.store_status('channel-id', status) self.assert_status((yield channel.status())['status'], components={ 'foo': api_from_status('channel-id', status), }, level='ok')
def test_load_many_statuses(self): store = yield self.create_store() expected = {} for i in range(5): status = TransportStatus(status='ok', component=i, type='bar', message='foo') yield store.store_status('channelid', status) expected[str(i)] = status stored_statuses = yield store.get_statuses('channelid') self.assertEqual(stored_statuses, expected)
def publish_status(self, **kw): """ Helper method for publishing a status message. """ msg = TransportStatus(**kw) if self._should_publish_status: conn = self.connectors[self.status_connector_name] return conn.publish_status(msg) else: self.log.debug( 'Status publishing disabled for transport %r, ignoring ' 'status %r' % (self.transport_name, msg)) return succeed(msg)
def test_api_from_status(self): status = TransportStatus(component='foo', status='ok', type='bar', message='Bar', details={'baz': 'quux'}) self.assertEqual( api_from_status('channel-23', status), { 'channel_id': 'channel-23', 'status': 'ok', 'component': 'foo', 'type': 'bar', 'message': 'Bar', 'details': { 'baz': 'quux' } })
def test_status_sent_to_status_url(self): '''The published status gets consumed and sent to the configured status_url''' worker = yield self.get_worker({ 'channel_id': 'channel-23', 'status_url': self.logging_api.url, }) status = TransportStatus(component='foo', status='ok', type='bar', message='Bar') yield worker.consume_status(status) [req] = self.logging_api.requests self.assert_request(req, method='POST', headers={'content-type': ['application/json']}, body=api_from_status('channel-23', status))
def test_status_send_to_status_url_bad_response(self): '''If there is an error sending a status to the configured status_url, the error and status should be logged''' self.patch_logger() worker = yield self.get_worker({ 'channel_id': 'channel-23', 'status_url': "%s/bad/" % (self.logging_api.url,), }) status = TransportStatus( component='foo', status='ok', type='bar', message='Bar') yield worker.consume_status(status) self.assert_was_logged('500') self.assert_was_logged('test-error-response') self.assert_was_logged(repr(status))