|
| 1 | +import logging |
| 2 | +from unittest import mock |
| 3 | +from nose2.tools.decorators import with_setup |
| 4 | +from source.queryHandler.QueryHandler import ColumnInfo, Key |
| 5 | +from source.collector import MetricTimeSeries, TimeSeries |
| 6 | +from source.opentsdb import OpenTsdbApi |
| 7 | + |
| 8 | + |
| 9 | +def my_setup(): |
| 10 | + global key1, key2, col1, col2, filtersMap, dps1, dps2, ts1, ts2, metricTS, data, jreq |
| 11 | + |
| 12 | + key1 = Key._from_string('scale-11|GPFSFilesystem|scale-cluster-1.vmlocal|localFS|gpfs_fs_bytes_read', '') |
| 13 | + key2 = Key._from_string('scale-11|GPFSFilesystem|scale-cluster-1.vmlocal|localFS|gpfs_fs_bytes_read', '') |
| 14 | + |
| 15 | + col1 = ColumnInfo(name='gpfs_fs_bytes_read', semType=2, |
| 16 | + keys=(key1,), column=6) |
| 17 | + col2 = ColumnInfo(name='gpfs_fs_bytes_read', semType=2, |
| 18 | + keys=(key2,), column=6) |
| 19 | + |
| 20 | + filtersMap = [{'node': 'scale-11', 'gpfs_cluster_name': 'scale-cluster-1.vmlocal', 'gpfs_fs_name': 'localFS'}, |
| 21 | + {'node': 'scale-12', 'gpfs_cluster_name': 'scale-cluster-1.vmlocal', 'gpfs_fs_name': 'localFS'}] |
| 22 | + |
| 23 | + dps1 = {1715963000: 0, 1715963010: 0, 1715963020: 0, 1715963030: 0, 1715963040: 0, 1715963050: 0, |
| 24 | + 1715963060: 0, 1715963070: 0, 1715963080: 0, 1715963090: 0, 1715963100: 0, 1715963110: 0, |
| 25 | + 1715963120: 0, 1715963130: 0, 1715963140: 0, 1715963150: 0, 1715963160: 0, 1715963170: 0} |
| 26 | + |
| 27 | + dps2 = {1715963000: 0, 1715963010: 0, 1715963020: 0, 1715963030: 0, 1715963040: 0, 1715963050: 0, |
| 28 | + 1715963060: 0, 1715963070: 0, 1715963080: 0, 1715963090: 0, 1715963100: 0, 1715963110: 0, |
| 29 | + 1715963120: 0, 1715963130: 0, 1715963140: 0, 1715963150: 0, 1715963160: 0, 1715963170: 0} |
| 30 | + |
| 31 | + ts1 = TimeSeries(col1, dps1, filtersMap) |
| 32 | + ts2 = TimeSeries(col2, dps2, filtersMap) |
| 33 | + |
| 34 | + metricTS = MetricTimeSeries('gpfs_fs_bytes_read', '') |
| 35 | + metricTS.timeseries = [ts1, ts2] |
| 36 | + |
| 37 | + data = {'gpfs_fs_bytes_read': metricTS} |
| 38 | + jreq = {'start': 1715916393902, 'msResolution': False, 'globalAnnotations': True, 'showQuery': True, |
| 39 | + 'inputQuery': {'aggregator': 'noop', 'alias': '$tag_node', 'currentFilterGroupBy': False, |
| 40 | + 'currentFilterKey': '', 'currentFilterType': 'literal_or', 'currentFilterValue': '', |
| 41 | + 'datasource': {'type': 'opentsdb', 'uid': 'a7fc0a73-eac4-4da7-974a-353e200e9f55'}, |
| 42 | + 'downsampleAggregator': 'sum', 'downsampleFillPolicy': 'none', 'downsampleInterval': '15m', |
| 43 | + 'filters': [ |
| 44 | + {'filter': 'localFS', 'groupBy': False, |
| 45 | + 'tagk': 'gpfs_fs_name', 'type': 'pm_filter' |
| 46 | + }, |
| 47 | + {'filter': 'scale-11|scale-12', |
| 48 | + 'groupBy': False, 'tagk': 'node', 'type': 'pm_filter' |
| 49 | + }], |
| 50 | + 'metric': 'gpfs_fs_bytes_read', 'refId': 'A', 'downsample': '15m-sum', 'index': 0 |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | +@with_setup(my_setup) |
| 56 | +def test_case01(): |
| 57 | + with mock.patch('source.metadata.MetadataHandler') as md: |
| 58 | + md_instance = md.return_value |
| 59 | + logger = logging.getLogger(__name__) |
| 60 | + opentsdb = OpenTsdbApi(logger, md_instance, '9999') |
| 61 | + resp = opentsdb.format_response(data, jreq) |
| 62 | + assert isinstance(resp, list) |
| 63 | + assert len(resp) > 0 |
| 64 | + assert len(resp) == 2 |
| 65 | + |
| 66 | + |
| 67 | +@with_setup(my_setup) |
| 68 | +def test_case02(): |
| 69 | + with mock.patch('source.metadata.MetadataHandler') as md: |
| 70 | + md_instance = md.return_value |
| 71 | + logger = logging.getLogger(__name__) |
| 72 | + opentsdb = OpenTsdbApi(logger, md_instance, '9999') |
| 73 | + resp = opentsdb.format_response(data, jreq) |
| 74 | + assert resp[0].get('metric') == "gpfs_fs_bytes_read" |
| 75 | + assert resp[0].get('query') == jreq.get('inputQuery') |
| 76 | + assert 'gpfs_fs_name' in resp[0].get('tags') |
| 77 | + assert 'node' in resp[0].get('tags') |
| 78 | + assert 'gpfs_cluster_name' in resp[0].get('tags') |
0 commit comments