From 8e564709984104d259b4e257ade2932263dcd7ca Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:04:10 +0100 Subject: [PATCH 1/6] Add files via upload --- testtsdb.py | 567 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 567 insertions(+) create mode 100644 testtsdb.py diff --git a/testtsdb.py b/testtsdb.py new file mode 100644 index 000000000..f54b831f6 --- /dev/null +++ b/testtsdb.py @@ -0,0 +1,567 @@ +#!/usr/bin/python +# coding=utf-8 +########################################################################## + +from test import unittest +from mock import patch +from diamond.metric import Metric +import urllib2 +import configobj +import StringIO +import gzip +import contextlib +import json + +from diamond.handler.tsdb import TSDBHandler + + +@patch('diamond.handler.tsdb.urllib2.urlopen') +@patch('diamond.handler.tsdb.urllib2.Request') +class TestTSDBdHandler(unittest.TestCase): + + def setUp(self): + self.url = 'http://127.0.0.1:4242/api/put' + + def decompress(self, input): + infile = StringIO.StringIO() + infile.write(input) + with contextlib.closing(gzip.GzipFile(fileobj=infile, mode="r")) as f: + f.rewind() + out = f.read() + return out + + def test_HTTPError(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + header = {'Content-Type': 'application/json'} + exception = urllib2.HTTPError(url=self.url, code=404, msg="Error", + hdrs=header, fp=None) + handler.side_effect = exception + handler.process(metric) + + def test_single_metric(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_compression(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['compression'] = 1 + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] + passed_headers = mock_request.call_args[0][2] + passed_body = mock_request.call_args[0][1] + assert passed_headers['Content-Encoding'] == 'gzip' + assert passed_headers['Content-Type'] == 'application/json' + assert json.loads(self.decompress(passed_body)) == body + + def test_user_password(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['user'] = 'John Doe' + config['password'] = '123456789' + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] + header = {'Content-Type': 'application/json', + 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5'} + self.check_request_param(mock_request, body, header ) + + def test_batch(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['batch'] = 2 + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + metric2 = Metric('servers.myhostname.cpu.cpu_time', + 123, raw_value=456, timestamp=5678910, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + handler.process(metric2) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}, + { + "timestamp": 5678910, + "metric": "cpu.cpu_time", + "value": 123, + "tags": {"hostname": "myhostname"} + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_tags(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = 'tag1=tagv1 tag2=tagv2' + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": { + "hostname": "myhostname", + "tag1": "tagv1", + "tag2": "tagv2" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_prefix(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['prefix'] = 'diamond' + metric = Metric('servers.myhostname.cpu.cpu_count', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "diamond.cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"} + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.cpu.cpu0.user', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.user", + "value": 123, + "tags": { + "cpuId": "cpu0", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_cpu_metrics_taghandling_0(self, mock_request, mock_urlopen): + """ + deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.cpu.cpu0.user', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu0.user", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): + """ + aggregate default + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.cpu.total.user', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + assert not mock_urlopen.called, "should not process" + + def test_cpu_metrics_taghandling_1(self, mock_request, mock_urlopen): + """ + aggregate deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.cpu.total.user', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.total.user", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_cpu_metrics_taghandling_2(self, mock_request, mock_urlopen): + """ + aggregate deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = True + config['skipAggregates'] = False + + metric = Metric('servers.myhostname.cpu.total.user', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "cpu.user", + "value": 123, + "tags": { + "cpuId": "total", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_haproxy_metrics_default(self, mock_request, mock_urlopen): + """ + taghandling default + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' + 'bin', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "haproxy.bin", + "value": 123, + "tags": { + "backend": "SOME-BACKEND", + "myFirstTag": "myValue", + "hostname": "myhostname", + "server": "SOME-SERVER" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_haproxy_metrics(self, mock_request, mock_urlopen): + """ + taghandling deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' + 'bin', + 123, raw_value=123, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "haproxy.SOME-BACKEND.SOME-SERVER.bin", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_diskspace_metrics_default(self, mock_request, mock_urlopen): + """ + taghandling default + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.diskspace.MOUNT_POINT.byte_percent' + 'free', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "diskspace.byte_percentfree", + "value": 80, + "tags": { + "mountpoint": "MOUNT_POINT", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_diskspace_metrics(self, mock_request, mock_urlopen): + """ + taghandling deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.diskspace.MOUNT_POINT.byte_' + 'percentfree', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "diskspace.MOUNT_POINT.byte_percentfree", + "value": 80, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_iostat_metrics_default(self, mock_request, mock_urlopen): + """ + taghandling default + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "iostat.io_in_progress", + "value": 80, + "tags": { + "device": "DEV", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_iostat_metrics(self, mock_request, mock_urlopen): + """ + taghandling deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "iostat.DEV.io_in_progress", + "value": 80, + "tags": + { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_network_metrics_default(self,mock_request, mock_urlopen): + """ + taghandling default + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + + metric = Metric('servers.myhostname.network.IF.rx_packets', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "network.rx_packets", + "value": 80, + "tags": { + "interface": "IF", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header ) + + def test_network_metrics(self, mock_request, mock_urlopen): + """ + taghandling deactivate + """ + config = configobj.ConfigObj() + config['host'] = '127.0.0.1' + config['port'] = '4242' + config['tags'] = ['myFirstTag=myValue'] + config['cleanMetrics'] = False + + metric = Metric('servers.myhostname.network.IF.rx_packets', + 80, raw_value=80, timestamp=1234567, + host='myhostname', metric_type='GAUGE') + + handler = TSDBHandler(config) + handler.process(metric) + body = [ + { + "timestamp": 1234567, + "metric": "network.IF.rx_packets", + "value": 80, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] + header = {'Content-Type': 'application/json'} + self.check_request_param(mock_request, body, header) + + def check_request_param(self, mock, body, header): + actual_body = json.loads(mock.call_args[0][1]) + self.assertEqual(actual_body, body) + actual_url = mock.call_args[0][0] + self.assertEqual(actual_url, self.url) + actual_header = mock.call_args[0][2] + self.assertEqual(actual_header, header) + From 8fba9f827f10f56323ee42307f39ad6ce0da6d23 Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:12:25 +0100 Subject: [PATCH 2/6] Delete testtsdb.py --- testtsdb.py | 567 ---------------------------------------------------- 1 file changed, 567 deletions(-) delete mode 100644 testtsdb.py diff --git a/testtsdb.py b/testtsdb.py deleted file mode 100644 index f54b831f6..000000000 --- a/testtsdb.py +++ /dev/null @@ -1,567 +0,0 @@ -#!/usr/bin/python -# coding=utf-8 -########################################################################## - -from test import unittest -from mock import patch -from diamond.metric import Metric -import urllib2 -import configobj -import StringIO -import gzip -import contextlib -import json - -from diamond.handler.tsdb import TSDBHandler - - -@patch('diamond.handler.tsdb.urllib2.urlopen') -@patch('diamond.handler.tsdb.urllib2.Request') -class TestTSDBdHandler(unittest.TestCase): - - def setUp(self): - self.url = 'http://127.0.0.1:4242/api/put' - - def decompress(self, input): - infile = StringIO.StringIO() - infile.write(input) - with contextlib.closing(gzip.GzipFile(fileobj=infile, mode="r")) as f: - f.rewind() - out = f.read() - return out - - def test_HTTPError(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - header = {'Content-Type': 'application/json'} - exception = urllib2.HTTPError(url=self.url, code=404, msg="Error", - hdrs=header, fp=None) - handler.side_effect = exception - handler.process(metric) - - def test_single_metric(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - body = [{"timestamp": 1234567, - "metric": "cpu.cpu_count", - "value": 123, - "tags": {"hostname": "myhostname"}}] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_compression(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['compression'] = 1 - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - body = [{"timestamp": 1234567, - "metric": "cpu.cpu_count", - "value": 123, - "tags": {"hostname": "myhostname"}}] - passed_headers = mock_request.call_args[0][2] - passed_body = mock_request.call_args[0][1] - assert passed_headers['Content-Encoding'] == 'gzip' - assert passed_headers['Content-Type'] == 'application/json' - assert json.loads(self.decompress(passed_body)) == body - - def test_user_password(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['user'] = 'John Doe' - config['password'] = '123456789' - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - body = [{"timestamp": 1234567, - "metric": "cpu.cpu_count", - "value": 123, - "tags": {"hostname": "myhostname"}}] - header = {'Content-Type': 'application/json', - 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5'} - self.check_request_param(mock_request, body, header ) - - def test_batch(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['batch'] = 2 - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - metric2 = Metric('servers.myhostname.cpu.cpu_time', - 123, raw_value=456, timestamp=5678910, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - handler.process(metric2) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.cpu_count", - "value": 123, - "tags": {"hostname": "myhostname"}}, - { - "timestamp": 5678910, - "metric": "cpu.cpu_time", - "value": 123, - "tags": {"hostname": "myhostname"} - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_tags(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = 'tag1=tagv1 tag2=tagv2' - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.cpu_count", - "value": 123, - "tags": { - "hostname": "myhostname", - "tag1": "tagv1", - "tag2": "tagv2" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_prefix(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['prefix'] = 'diamond' - metric = Metric('servers.myhostname.cpu.cpu_count', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "diamond.cpu.cpu_count", - "value": 123, - "tags": {"hostname": "myhostname"} - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.cpu.cpu0.user', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.user", - "value": 123, - "tags": { - "cpuId": "cpu0", - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_cpu_metrics_taghandling_0(self, mock_request, mock_urlopen): - """ - deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.cpu.cpu0.user', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.cpu0.user", - "value": 123, - "tags": { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): - """ - aggregate default - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.cpu.total.user', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - assert not mock_urlopen.called, "should not process" - - def test_cpu_metrics_taghandling_1(self, mock_request, mock_urlopen): - """ - aggregate deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.cpu.total.user', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.total.user", - "value": 123, - "tags": { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_cpu_metrics_taghandling_2(self, mock_request, mock_urlopen): - """ - aggregate deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = True - config['skipAggregates'] = False - - metric = Metric('servers.myhostname.cpu.total.user', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "cpu.user", - "value": 123, - "tags": { - "cpuId": "total", - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_haproxy_metrics_default(self, mock_request, mock_urlopen): - """ - taghandling default - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' - 'bin', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "haproxy.bin", - "value": 123, - "tags": { - "backend": "SOME-BACKEND", - "myFirstTag": "myValue", - "hostname": "myhostname", - "server": "SOME-SERVER" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_haproxy_metrics(self, mock_request, mock_urlopen): - """ - taghandling deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.haproxy.SOME-BACKEND.SOME-SERVER.' - 'bin', - 123, raw_value=123, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "haproxy.SOME-BACKEND.SOME-SERVER.bin", - "value": 123, - "tags": { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_diskspace_metrics_default(self, mock_request, mock_urlopen): - """ - taghandling default - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.diskspace.MOUNT_POINT.byte_percent' - 'free', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "diskspace.byte_percentfree", - "value": 80, - "tags": { - "mountpoint": "MOUNT_POINT", - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_diskspace_metrics(self, mock_request, mock_urlopen): - """ - taghandling deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.diskspace.MOUNT_POINT.byte_' - 'percentfree', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "diskspace.MOUNT_POINT.byte_percentfree", - "value": 80, - "tags": { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_iostat_metrics_default(self, mock_request, mock_urlopen): - """ - taghandling default - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "iostat.io_in_progress", - "value": 80, - "tags": { - "device": "DEV", - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_iostat_metrics(self, mock_request, mock_urlopen): - """ - taghandling deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.iostat.DEV.io_in_progress', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "iostat.DEV.io_in_progress", - "value": 80, - "tags": - { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_network_metrics_default(self,mock_request, mock_urlopen): - """ - taghandling default - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - - metric = Metric('servers.myhostname.network.IF.rx_packets', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "network.rx_packets", - "value": 80, - "tags": { - "interface": "IF", - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) - - def test_network_metrics(self, mock_request, mock_urlopen): - """ - taghandling deactivate - """ - config = configobj.ConfigObj() - config['host'] = '127.0.0.1' - config['port'] = '4242' - config['tags'] = ['myFirstTag=myValue'] - config['cleanMetrics'] = False - - metric = Metric('servers.myhostname.network.IF.rx_packets', - 80, raw_value=80, timestamp=1234567, - host='myhostname', metric_type='GAUGE') - - handler = TSDBHandler(config) - handler.process(metric) - body = [ - { - "timestamp": 1234567, - "metric": "network.IF.rx_packets", - "value": 80, - "tags": { - "myFirstTag": "myValue", - "hostname": "myhostname" - } - } - ] - header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header) - - def check_request_param(self, mock, body, header): - actual_body = json.loads(mock.call_args[0][1]) - self.assertEqual(actual_body, body) - actual_url = mock.call_args[0][0] - self.assertEqual(actual_url, self.url) - actual_header = mock.call_args[0][2] - self.assertEqual(actual_header, header) - From 8bfd2e9876359356b09a96eb8b492d405b3eae8a Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:12:59 +0100 Subject: [PATCH 3/6] Add files via upload --- src/diamond/handler/test/testtsdb.py | 330 +++++++++++++++++++-------- 1 file changed, 236 insertions(+), 94 deletions(-) diff --git a/src/diamond/handler/test/testtsdb.py b/src/diamond/handler/test/testtsdb.py index 91d48fe9d..f54b831f6 100644 --- a/src/diamond/handler/test/testtsdb.py +++ b/src/diamond/handler/test/testtsdb.py @@ -3,13 +3,14 @@ ########################################################################## from test import unittest -from mock import patch, Mock +from mock import patch from diamond.metric import Metric import urllib2 import configobj import StringIO import gzip import contextlib +import json from diamond.handler.tsdb import TSDBHandler @@ -29,7 +30,7 @@ def decompress(self, input): out = f.read() return out - def test_HTTPError(self, mock_urlopen, mock_request): + def test_HTTPError(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -43,7 +44,7 @@ def test_HTTPError(self, mock_urlopen, mock_request): handler.side_effect = exception handler.process(metric) - def test_single_metric(self, mock_urlopen, mock_request): + def test_single_metric(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -52,12 +53,14 @@ def test_single_metric(self, mock_urlopen, mock_request): host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' - '123, "tags": {"hostname": "myhostname"}}]') + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_compression(self, mock_urlopen, mock_request): + def test_compression(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -67,15 +70,17 @@ def test_compression(self, mock_urlopen, mock_request): host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' - '123, "tags": {"hostname": "myhostname"}}]') - passed_headers = mock_urlopen.call_args[0][2] - passed_body = mock_urlopen.call_args[0][1] + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] + passed_headers = mock_request.call_args[0][2] + passed_body = mock_request.call_args[0][1] assert passed_headers['Content-Encoding'] == 'gzip' assert passed_headers['Content-Type'] == 'application/json' - assert self.decompress(passed_body) == body + assert json.loads(self.decompress(passed_body)) == body - def test_user_password(self, mock_urlopen, mock_request): + def test_user_password(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -86,13 +91,15 @@ def test_user_password(self, mock_urlopen, mock_request): host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' - '123, "tags": {"hostname": "myhostname"}}]') + body = [{"timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}] header = {'Content-Type': 'application/json', 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_batch(self, mock_urlopen, mock_request): + def test_batch(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -106,14 +113,23 @@ def test_batch(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) handler.process(metric2) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' - '123, "tags": {"hostname": "myhostname"}}, {"timestamp": 567891' - '0, "metric": "cpu.cpu_time", "value": 123, "tags": {"hostname"' - ': "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"}}, + { + "timestamp": 5678910, + "metric": "cpu.cpu_time", + "value": 123, + "tags": {"hostname": "myhostname"} + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_tags(self, mock_urlopen, mock_request): + def test_tags(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -123,13 +139,22 @@ def test_tags(self, mock_urlopen, mock_request): host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu_count", "value": ' - '123, "tags": {"hostname": "myhostname", "tag1": "tagv1", ' - '"tag2": "tagv2"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu_count", + "value": 123, + "tags": { + "hostname": "myhostname", + "tag1": "tagv1", + "tag2": "tagv2" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_prefix(self, mock_urlopen, mock_request): + def test_prefix(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -139,12 +164,18 @@ def test_prefix(self, mock_urlopen, mock_request): host='myhostname', metric_type='GAUGE') handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "diamond.cpu.cpu_count", ' - '"value": 123, "tags": {"hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "diamond.cpu.cpu_count", + "value": 123, + "tags": {"hostname": "myhostname"} + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_cpu_metrics_taghandling_default(self, mock_urlopen, mock_request): + def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): config = configobj.ConfigObj() config['host'] = '127.0.0.1' config['port'] = '4242' @@ -156,13 +187,22 @@ def test_cpu_metrics_taghandling_default(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.user", "value": ' - '123, "tags": {"cpuId": "cpu0", "myFirstTag": "myValue", ' - '"hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.user", + "value": 123, + "tags": { + "cpuId": "cpu0", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_cpu_metrics_taghandling_0(self, mock_urlopen, mock_request): + def test_cpu_metrics_taghandling_0(self, mock_request, mock_urlopen): """ deactivate """ @@ -178,13 +218,21 @@ def test_cpu_metrics_taghandling_0(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.cpu0.user", "value": ' - '123, "tags": {"myFirstTag": "myValue", "hostname": ' - '"myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.cpu0.user", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_cpu_metrics_taghandling_default(self, mock_urlopen, mock_request): + def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): """ aggregate default """ @@ -201,7 +249,7 @@ def test_cpu_metrics_taghandling_default(self, mock_urlopen, mock_request): handler.process(metric) assert not mock_urlopen.called, "should not process" - def test_cpu_metrics_taghandling_1(self, mock_urlopen, mock_request): + def test_cpu_metrics_taghandling_1(self, mock_request, mock_urlopen): """ aggregate deactivate """ @@ -217,13 +265,21 @@ def test_cpu_metrics_taghandling_1(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.total.user", "value": ' - '123, "tags": {"myFirstTag": "myValue", "hostname": ' - '"myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.total.user", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_cpu_metrics_taghandling_2(self, mock_urlopen, mock_request): + def test_cpu_metrics_taghandling_2(self, mock_request, mock_urlopen): """ aggregate deactivate """ @@ -240,13 +296,22 @@ def test_cpu_metrics_taghandling_2(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "cpu.user", "value": ' - '123, "tags": {"cpuId": "total", "myFirstTag": "myValue", ' - '"hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "cpu.user", + "value": 123, + "tags": { + "cpuId": "total", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_haproxy_metrics_default(self, mock_urlopen, mock_request): + def test_haproxy_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ @@ -262,14 +327,23 @@ def test_haproxy_metrics_default(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "haproxy.bin",' - ' "value": 123, "tags": {"backend": "SOME-BACKEND",' - ' "myFirstTag": "myValue", "hostname": "myhostname", "server": ' - '"SOME-SERVER"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "haproxy.bin", + "value": 123, + "tags": { + "backend": "SOME-BACKEND", + "myFirstTag": "myValue", + "hostname": "myhostname", + "server": "SOME-SERVER" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_haproxy_metrics(self, mock_urlopen, mock_request): + def test_haproxy_metrics(self, mock_request, mock_urlopen): """ taghandling deactivate """ @@ -286,13 +360,21 @@ def test_haproxy_metrics(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "haproxy.SOME-BACKEND.SOME-' - 'SERVER.bin", "value": 123, "tags": {"myFirstTag": "myValue", ' - '"hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "haproxy.SOME-BACKEND.SOME-SERVER.bin", + "value": 123, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_diskspace_metrics_default(self, mock_urlopen, mock_request): + def test_diskspace_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ @@ -308,14 +390,22 @@ def test_diskspace_metrics_default(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "diskspace.' - 'byte_percentfree", "value": 80, "tags": {"mountpoint": ' - '"MOUNT_POINT", "myFirstTag": "myValue", "hostname": ' - '"myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "diskspace.byte_percentfree", + "value": 80, + "tags": { + "mountpoint": "MOUNT_POINT", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_diskspace_metrics(self, mock_urlopen, mock_request): + def test_diskspace_metrics(self, mock_request, mock_urlopen): """ taghandling deactivate """ @@ -332,13 +422,21 @@ def test_diskspace_metrics(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "diskspace.MOUNT_POINT' - '.byte_percentfree", "value": 80, "tags": {"myFirstTag": ' - '"myValue", "hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "diskspace.MOUNT_POINT.byte_percentfree", + "value": 80, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_iostat_metrics_default(self, mock_urlopen, mock_request): + def test_iostat_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ @@ -353,13 +451,22 @@ def test_iostat_metrics_default(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "iostat.io_in_progress", ' - '"value": 80, "tags": {"device": "DEV", "myFirstTag": ' - '"myValue", "hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "iostat.io_in_progress", + "value": 80, + "tags": { + "device": "DEV", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_iostat_metrics(self, mock_urlopen, mock_request): + def test_iostat_metrics(self, mock_request, mock_urlopen): """ taghandling deactivate """ @@ -375,13 +482,22 @@ def test_iostat_metrics(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "iostat.DEV.io_in_progress"' - ', "value": 80, "tags": {"myFirstTag": "myValue", "hostname": ' - '"myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "iostat.DEV.io_in_progress", + "value": 80, + "tags": + { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_network_metrics_default(self, mock_urlopen, mock_request): + def test_network_metrics_default(self,mock_request, mock_urlopen): """ taghandling default """ @@ -396,13 +512,22 @@ def test_network_metrics_default(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "network.rx_packets", ' - '"value": 80, "tags": {"interface": "IF", "myFirstTag": ' - '"myValue", "hostname": "myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "network.rx_packets", + "value": 80, + "tags": { + "interface": "IF", + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header ) - def test_network_metrics(self, mock_urlopen, mock_request): + def test_network_metrics(self, mock_request, mock_urlopen): """ taghandling deactivate """ @@ -418,8 +543,25 @@ def test_network_metrics(self, mock_urlopen, mock_request): handler = TSDBHandler(config) handler.process(metric) - body = ('[{"timestamp": 1234567, "metric": "network.IF.rx_packets", ' - '"value": 80, "tags": {"myFirstTag": "myValue", "hostname": ' - '"myhostname"}}]') + body = [ + { + "timestamp": 1234567, + "metric": "network.IF.rx_packets", + "value": 80, + "tags": { + "myFirstTag": "myValue", + "hostname": "myhostname" + } + } + ] header = {'Content-Type': 'application/json'} - mock_urlopen.assert_called_with(self.url, body, header) + self.check_request_param(mock_request, body, header) + + def check_request_param(self, mock, body, header): + actual_body = json.loads(mock.call_args[0][1]) + self.assertEqual(actual_body, body) + actual_url = mock.call_args[0][0] + self.assertEqual(actual_url, self.url) + actual_header = mock.call_args[0][2] + self.assertEqual(actual_header, header) + From c84164d301ece6982314741150e1b7c4fa481bfe Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:28:21 +0100 Subject: [PATCH 4/6] Add files via upload Fixing pepo rule violation --- src/diamond/handler/test/testtsdb.py | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/diamond/handler/test/testtsdb.py b/src/diamond/handler/test/testtsdb.py index f54b831f6..8121bccdf 100644 --- a/src/diamond/handler/test/testtsdb.py +++ b/src/diamond/handler/test/testtsdb.py @@ -58,7 +58,7 @@ def test_single_metric(self, mock_request, mock_urlopen): "value": 123, "tags": {"hostname": "myhostname"}}] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_compression(self, mock_request, mock_urlopen): config = configobj.ConfigObj() @@ -97,7 +97,7 @@ def test_user_password(self, mock_request, mock_urlopen): "tags": {"hostname": "myhostname"}}] header = {'Content-Type': 'application/json', 'Authorization': 'Basic Sm9obiBEb2U6MTIzNDU2Nzg5'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_batch(self, mock_request, mock_urlopen): config = configobj.ConfigObj() @@ -127,7 +127,7 @@ def test_batch(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_tags(self, mock_request, mock_urlopen): config = configobj.ConfigObj() @@ -152,7 +152,7 @@ def test_tags(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_prefix(self, mock_request, mock_urlopen): config = configobj.ConfigObj() @@ -173,7 +173,7 @@ def test_prefix(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): config = configobj.ConfigObj() @@ -200,7 +200,7 @@ def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_cpu_metrics_taghandling_0(self, mock_request, mock_urlopen): """ @@ -230,7 +230,7 @@ def test_cpu_metrics_taghandling_0(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_cpu_metrics_taghandling_default(self, mock_request, mock_urlopen): """ @@ -277,7 +277,7 @@ def test_cpu_metrics_taghandling_1(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_cpu_metrics_taghandling_2(self, mock_request, mock_urlopen): """ @@ -309,7 +309,7 @@ def test_cpu_metrics_taghandling_2(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_haproxy_metrics_default(self, mock_request, mock_urlopen): """ @@ -341,7 +341,7 @@ def test_haproxy_metrics_default(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_haproxy_metrics(self, mock_request, mock_urlopen): """ @@ -372,7 +372,7 @@ def test_haproxy_metrics(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_diskspace_metrics_default(self, mock_request, mock_urlopen): """ @@ -403,7 +403,7 @@ def test_diskspace_metrics_default(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_diskspace_metrics(self, mock_request, mock_urlopen): """ @@ -434,7 +434,7 @@ def test_diskspace_metrics(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_iostat_metrics_default(self, mock_request, mock_urlopen): """ @@ -464,7 +464,7 @@ def test_iostat_metrics_default(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_iostat_metrics(self, mock_request, mock_urlopen): """ @@ -495,7 +495,7 @@ def test_iostat_metrics(self, mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_network_metrics_default(self,mock_request, mock_urlopen): """ @@ -525,7 +525,7 @@ def test_network_metrics_default(self,mock_request, mock_urlopen): } ] header = {'Content-Type': 'application/json'} - self.check_request_param(mock_request, body, header ) + self.check_request_param(mock_request, body, header) def test_network_metrics(self, mock_request, mock_urlopen): """ From 705d912245cd76cc50ab48579457d5e9b5a95c78 Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:49:11 +0100 Subject: [PATCH 5/6] Add files via upload fixing pep8 violation issues --- src/diamond/handler/test/testtsdb.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/diamond/handler/test/testtsdb.py b/src/diamond/handler/test/testtsdb.py index 8121bccdf..1978e9464 100644 --- a/src/diamond/handler/test/testtsdb.py +++ b/src/diamond/handler/test/testtsdb.py @@ -497,7 +497,7 @@ def test_iostat_metrics(self, mock_request, mock_urlopen): header = {'Content-Type': 'application/json'} self.check_request_param(mock_request, body, header) - def test_network_metrics_default(self,mock_request, mock_urlopen): + def test_network_metrics_default(self, mock_request, mock_urlopen): """ taghandling default """ @@ -563,5 +563,4 @@ def check_request_param(self, mock, body, header): actual_url = mock.call_args[0][0] self.assertEqual(actual_url, self.url) actual_header = mock.call_args[0][2] - self.assertEqual(actual_header, header) - + self.assertEqual(actual_header, header) \ No newline at end of file From 404ef1098338d0c837560d5be8680c8751290414 Mon Sep 17 00:00:00 2001 From: ajkumarnv Date: Mon, 7 Aug 2017 16:50:36 +0100 Subject: [PATCH 6/6] Add files via upload --- src/diamond/handler/test/testtsdb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diamond/handler/test/testtsdb.py b/src/diamond/handler/test/testtsdb.py index 1978e9464..0c697a0db 100644 --- a/src/diamond/handler/test/testtsdb.py +++ b/src/diamond/handler/test/testtsdb.py @@ -563,4 +563,4 @@ def check_request_param(self, mock, body, header): actual_url = mock.call_args[0][0] self.assertEqual(actual_url, self.url) actual_header = mock.call_args[0][2] - self.assertEqual(actual_header, header) \ No newline at end of file + self.assertEqual(actual_header, header)