def test_no_environ_tags(): """ Test tags work even if no global tags present as env variable """ timestamp = int(time.time()) test_val = math.pi mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token"}) mock_environ.start() client = Apptuit() dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1}, timestamp=timestamp, value=test_val) dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val) payload = client._create_payload_from_datapoints([dp1, dp2]) assert_equals(len(payload), 2) assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1}) assert_equals(payload[1]["tags"], {"test": 2}) registry = MetricsRegistry() reporter = ApptuitReporter(registry=registry, tags={"host": "reporter", "ip": "2.2.2.2"}) counter = registry.counter("counter") counter.inc(1) payload = reporter.client._create_payload_from_datapoints(reporter._collect_data_points(reporter.registry)) assert_equals(len(payload), 1) assert_equals(payload[0]["tags"], {'host': 'reporter', 'ip': '2.2.2.2'}) mock_environ.stop()
def test_invalid_chars_in_tag_keys(): """ Test for invalid character in tag keys """ metric_name = "node.load_avg.1m" tags = { "ho\\st": "localhost", "region": "us-east-1", "service": "web-server" } ts = int(time.time()) client = Apptuit(sanitize_mode=None, token="test") with assert_raises(ValueError) as ex: dp = DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random()) client.send([dp]) with assert_raises(AttributeError) as ex: dp = DataPoint(metric=metric_name, tags="error", timestamp=ts, value=random.random()) client.send([dp]) dp = DataPoint(metric=metric_name, tags=None, timestamp=ts, value=random.random()) assert_equals(dp.tags, None)
def test_tags_limit_indirect(mock_post): """ Test for failure when too many tags are used indirectly (when combined with global tags) """ gtags_list = [ "gtk-%d:gtv-%d" % (i, i) for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1) ] global_tags = ",".join(gtags_list) tags = { 'tagk-%d' % i: 'tagv-%d' % i for i in range(apptuit_client.MAX_TAGS_LIMIT // 2 + 1) } timestamp = int(time.time()) with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}): client = Apptuit(sanitize_mode=None, token="test_token") point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14) point2 = DataPoint("metric1", tags, timestamp, 3.14) with assert_raises(ValueError): client.send([point1, point2]) with patch.dict(os.environ, {APPTUIT_PY_TAGS: global_tags}): client = Apptuit(sanitize_mode=None, token="test_token") series1 = TimeSeries('metric1', {"tagk1": "tagv1"}) series1.add_point(timestamp, 3.14) series2 = TimeSeries('metric1', tags) series2.add_point(timestamp, 3.14) with assert_raises(ValueError): client.send_timeseries([series1, series2])
def test_none_datapoint_value(): """ Test DataPoint creation with None value """ timestamp = int(time.time()) with assert_raises(ValueError): DataPoint("metric1", None, timestamp, None)
def test_send_413_error(mock_post): """ Test for the case when we get 413 from the PUT API """ mock_post.return_value.status_code = 413 token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() metric_name = "node.load_avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } dps = [] points_sent = 0 while True: ts = int(time.time()) dps.append( DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random())) if len(dps) == 100: with assert_raises(ApptuitSendException): client.send(dps) dps = [] points_sent += 100 if points_sent > 500: break
def test_send_with_retry(mock_post): """ Test for the case when there is an error from the backend for send """ err_response = Response() err_response.status_code = 505 mock_post.return_value.status_code = 500 mock_post.return_value.raise_for_status.side_effect = requests.exceptions.SSLError( response=err_response) client = __get_apptuit_client() metric_name = "node.load_avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } dps = [] ts = int(time.time()) for i in range(100): dps.append( DataPoint(metric=metric_name, tags=tags, timestamp=ts + i, value=random.random())) with assert_raises(ApptuitException): client.send(dps, retry_count=1)
def test_send_positive(mock_post): """ Test that send API is working as expected """ mock_post.return_value.status_code = 204 client = __get_apptuit_client() metric_name = "node.load_avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } dps = [] client.send(dps) points_sent = 0 while True: ts = int(time.time()) dps.append( DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random())) if len(dps) == 100: client.send(dps) dps = [] points_sent += 100 if points_sent > 500: break
def test_apptuit_send_exception_401(mock_post): """ Test for the case when there is an error from the backend for send """ mock_post.return_value.status_code = 401 token = "asdashdsauh_8aeraerf" client = __get_apptuit_client() dp = DataPoint(metric="test", tags={"tk": "tv"}, timestamp=123, value=123) dps = [dp] with assert_raises(ApptuitSendException): client.send(dps)
def test_tags_limit_direct(mock_post): """ Test for failure when too many tags are used for datapoints/series """ tags = { 'tagk-%d' % i: 'tagv-%d' % i for i in range(apptuit_client.MAX_TAGS_LIMIT + 1) } timestamp = int(time.time()) client = Apptuit(sanitize_mode=None, token="test_token") point1 = DataPoint("metric1", {"tagk1": "tagv1"}, timestamp, 3.14) point2 = DataPoint("metric1", tags, timestamp, 3.14) with assert_raises(ValueError): client.send([point1, point2]) series1 = TimeSeries('metric1', {"tagk1": "tagv1"}) series1.add_point(timestamp, 3.14) series2 = TimeSeries('metric1', tags) series2.add_point(timestamp, 3.14) with assert_raises(ValueError): client.send_timeseries([series1, series2])
def test_apptuit_send_exception_400(mock_post): """ Test for the case when there is an error from the backend for send """ mock_post.return_value.status_code = 400 mock_post.return_value.content = '{"success": 0, "failed": 1, ' + \ '"errors": [{"datapoint": "", "error": "test_error"}] }' client = __get_apptuit_client() dp = DataPoint(metric="test", tags={"tk": "tv"}, timestamp=123, value=123) dps = [dp] with assert_raises(ApptuitSendException): client.send(dps)
def test_datapoint_repr(): """ Test __repr__ of DataPoint """ timestamp = int(time.time()) point = DataPoint('metric1', { "tagk1": "tagv1", "tagk2": "tagv2" }, timestamp, 3.14) expected_repr = 'metric1{tagk1:tagv1, tagk2:tagv2, timestamp: %d, value: %f}' % ( timestamp, 3.14) assert_equals(repr(point), expected_repr) assert_equals(str(point), expected_repr)
def test_nonstring_invalid_datapoint_value(): """ Test for a non-str/numeric value for datapoint value """ metric_name = "node.load.avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } ts = int(time.time()) value = object() with assert_raises(ValueError): DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=value)
def test_numeric_string_datapoint_value(): """ Test for a valid DataPoint value which is a string """ metric_name = "node.load.avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } ts = int(time.time()) value = '3.14' point = DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=value) assert_is_not_none(point)
def test_datapoint_tags_and_envtags(): """ Test that datapoint tags take priority when global tags env variable is present """ mock_environ = patch.dict(os.environ, {APPTUIT_PY_TOKEN: "environ_token", APPTUIT_PY_TAGS: 'host: host1, ip: 1.1.1.1'}) mock_environ.start() client = Apptuit(None) timestamp = int(time.time()) test_val = math.pi dp1 = DataPoint(metric="test_metric", tags={"host": "host2", "ip": "2.2.2.2", "test": 1}, timestamp=timestamp, value=test_val) dp2 = DataPoint(metric="test_metric", tags={"test": 2}, timestamp=timestamp, value=test_val) dp3 = DataPoint(metric="test_metric", tags={}, timestamp=timestamp, value=test_val) dp4 = DataPoint(metric="test_metric", tags=None, timestamp=timestamp, value=test_val) payload = client._create_payload_from_datapoints([dp1, dp2, dp3, dp4]) assert_equals(len(payload), 4) assert_equals(payload[0]["tags"], {"host": "host2", "ip": "2.2.2.2", "test": 1}) assert_equals(payload[1]["tags"], {"host": "host1", "ip": "1.1.1.1", "test": 2}) assert_equals(payload[2]["tags"], {"host": "host1", "ip": "1.1.1.1"}) assert_equals(payload[3]["tags"], {"host": "host1", "ip": "1.1.1.1"}) assert_equals(client._global_tags, {"host": "host1", "ip": "1.1.1.1"}) mock_environ.stop()
def test_invalid_chars_in_tag_values(): """ Test for invalid character in tag values """ metric_name = "node.load_avg.1m" tags = { "host": "local:host", "region": "us east 1", "service": "web+server" } ts = int(time.time()) DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random())
def test_datapoint_value_getter(): """ Test that the value used to create the DataPoint matches with the value returned by the object after creation """ metric_name = "node.load.avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } ts = int(time.time()) value = 3.14 point = DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=value) assert_equals(point.value, value)
def test_tags_not_dict(): """ Test to validate that only dict type values are expected for tags """ metric_name = "node.load_avg.1m" tags = [ "host", "localhost", "region", "us-east-1", "service", "web-server" ] ts = int(time.time()) client = Apptuit(sanitize_mode=None, token="test") with assert_raises(AttributeError) as ex: dp = DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random()) client.send([dp])
def test_invalid_metric_name(): """ Test for invalid character in metric name """ metric_name = "node.load+avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } ts = int(time.time()) client = Apptuit(sanitize_mode=None, token="test") with assert_raises(ValueError) as ex: dp = DataPoint(metric=metric_name, tags=tags, timestamp=ts, value=random.random()) client.send([dp])
def test_send_with_retry_con_err(mock_post): """ Test for the case when there is an error from the backend for send """ mock_post.side_effect = requests.exceptions.ConnectionError client = __get_apptuit_client() metric_name = "node.load_avg.1m" tags = { "host": "localhost", "region": "us-east-1", "service": "web-server" } dps = [] ts = int(time.time()) for i in range(100): dps.append( DataPoint(metric=metric_name, tags=tags, timestamp=ts + i, value=random.random())) with assert_raises(requests.exceptions.ConnectionError): client.send(dps, retry_count=1)