def test_annotation_error_sending_to_web(): url = "http://localhost:3000/api/annotations" with requests_mock.Mocker() as m: m.register_uri(requests_mock.POST, url, status_code=400) test_annotation = Annotation('event', ['test'], 'testing', 1559332960, 1559332960) with pytest.raises( Exception, match='Received 400 response, sending event failed'): test_annotation.send(url)
def test_annotation_send_to_influxdb(mock_influxdbclient): url = "influx://*****:*****@localhost" test_annotation = Annotation('event', ['test'], 'testing') mock_influxdbclient.write_points.return_value = True assert test_annotation.send(url) == { 'event_data': [{ 'fields': { 'tags': 'test', 'text': 'testing', 'title': 'event' }, 'measurement': 'events' }], 'message': 'Annotation added' }
def test_annotation_send_to_influxdb_fail(mock_write_points): url = "influx://*****:*****@localhost" test_annotation = Annotation('event', ['test'], 'testing') mock_write_points.return_value = False assert test_annotation.send(url, None) == { 'event_data': [{ 'fields': { 'tags': 'test', 'text': 'testing', 'title': 'event' }, 'measurement': 'events' }], 'message': 'Annotation failed' }
def test_annotation_send_to_web(): url = "http://localhost:3000/api/annotations" with requests_mock.Mocker() as m: m.register_uri(requests_mock.POST, url, status_code=200, json={'message': 'Annotation added'}) test_annotation = Annotation('event', ['test'], 'testing', 1559332960, 1559332970) assert test_annotation.send(url) == { 'event_data': { 'isRegion': True, 'tags': ['test'], 'text': '<b>event</b>\n\ntesting', 'time': 1559332960000, 'timeEnd': 1559332970000 }, 'message': 'Annotation added' }
def test_annotation_send_to_web_with_api_key(): url = "http://localhost:3000/api/annotations" api_key = "307c1ac4-4e7c-4eb4-a56f-3547eeff0e4b" with requests_mock.Mocker() as m: m.register_uri( requests_mock.POST, url, request_headers={'Authorization': "Bearer %s" % api_key}, status_code=200, json={'message': 'Annotation added'}) test_annotation = Annotation('event', ['test'], 'testing', 1559332960, 1559332970) assert test_annotation.send(url, api_key) == { 'event_data': { 'isRegion': True, 'tags': ['test'], 'text': '<b>event</b>\n\ntesting', 'time': 1559332960000, 'timeEnd': 1559332970000 }, 'message': 'Annotation added' }
def main(annotate_uri, title, tags, description, start_time, end_time, debug): """ Send Grafana annotations to various endpoints """ log_level = logging.INFO if debug: log_level = logging.DEBUG logging.basicConfig(format=' [%(levelname)s] %(message)s', level=log_level) try: if description is None: if not sys.stdin.isatty(): description = "".join( [line for line in iter(sys.stdin.readline, '')]) else: description = "" this_annotation = Annotation(title, tags, description, start_time, end_time) result = this_annotation.send(annotate_uri) if result['event_data']: logging.debug(result['event_data']) if result['message']: logging.info(result['message']) except Exception as e: logging.exception(e) """ We could exit 1 here but we really don't want to cause a job to fail just because we couldn't send an event. """ sys.exit(0)
def test_annotation_fail_to_send_to_influxdb(): url = "influx://*****:*****@localhost" test_annotation = Annotation('event', ['test'], 'testing') with pytest.raises(Exception, match='Failed to establish a new connection'): test_annotation.send(url)
def test_annotation_fail_to_send_to_web(): url = "http://*****:*****@localhost" test_annotation = Annotation('event', ['test'], 'testing') with pytest.raises(Exception, match='NewConnectionError'): test_annotation.send(url)
def test_annotation_send_bad_url(): url = "s3://user:pass@localhost" test_annotation = Annotation('event', ['test'], 'testing') with pytest.raises(NotImplementedError, match='Scheme s3 not recognised'): test_annotation.send(url)