|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import json |
| 4 | +from unittest import mock |
| 5 | +from source.bridgeLogger import configureLogging |
| 6 | +from source.queryHandler.Topo import Topo |
| 7 | +from source.collector import QueryPolicy, SensorCollector |
| 8 | +from nose2.tools.such import helper as assert_helper |
| 9 | +from nose2.tools.decorators import with_setup |
| 10 | + |
| 11 | + |
| 12 | +def my_setup(): |
| 13 | + global path, topo, logger, prometheus_attrs, pfilters, pquery_filters, wrong_pfilters |
| 14 | + path = os.getcwd() |
| 15 | + topoStrFile = os.path.join(path, "tests", "test_data", 'topoStr.json') |
| 16 | + with open(topoStrFile) as f: |
| 17 | + topoStr = json.load(f) |
| 18 | + topo = Topo(topoStr) |
| 19 | + logger = configureLogging(path, None) |
| 20 | + prometheus_attrs = {'sensor': 'GPFSFilesystem', 'period': 300, |
| 21 | + 'nsamples': 300, 'rawData': True} |
| 22 | + pfilters = {'node': 'scale-16', 'gpfs_fs_name': 'afmCacheFS'} |
| 23 | + pquery_filters = [f"{k}={v}" for k, v in pfilters.items()] |
| 24 | + wrong_pfilters = {'node': 'scale-16', 'gpfs_filesystem_name': 'afmCacheFS'} |
| 25 | + |
| 26 | + |
| 27 | +@with_setup(my_setup) |
| 28 | +def test_case01(): |
| 29 | + with mock.patch('source.collector.QueryPolicy.md') as md: |
| 30 | + md_instance = md.return_value |
| 31 | + md_instance.includeDiskData.return_value = False |
| 32 | + md_instance.logger = logging.getLogger(__name__) |
| 33 | + request = QueryPolicy(**prometheus_attrs) |
| 34 | + query = request.get_zimon_query() |
| 35 | + query.includeDiskData = md_instance.includeDiskData.return_value |
| 36 | + queryString = 'get -j {0} {1} group {2} bucket_size {3} {4}'.format( |
| 37 | + '', '-z', prometheus_attrs.get('sensor'), |
| 38 | + prometheus_attrs.get('period'), |
| 39 | + f"last {prometheus_attrs.get('period')}") |
| 40 | + queryString += '\n' |
| 41 | + assert "group" in query.__str__() |
| 42 | + assert "last" in query.__str__() |
| 43 | + assert "from" not in query.__str__() |
| 44 | + assert queryString == query.__str__() |
| 45 | + |
| 46 | + |
| 47 | +@with_setup(my_setup) |
| 48 | +def test_case02(): |
| 49 | + prometheus_attrs.update({'nsamples': 1, 'rawData': False}) |
| 50 | + with mock.patch('source.collector.QueryPolicy.md') as md: |
| 51 | + md_instance = md.return_value |
| 52 | + md_instance.includeDiskData.return_value = False |
| 53 | + md_instance.logger = logging.getLogger(__name__) |
| 54 | + request = QueryPolicy(**prometheus_attrs) |
| 55 | + query = request.get_zimon_query() |
| 56 | + query.includeDiskData = md_instance.includeDiskData.return_value |
| 57 | + queryString = 'get -j {0} {1} group {2} bucket_size {3} {4}'.format( |
| 58 | + '', '', prometheus_attrs.get('sensor'), |
| 59 | + prometheus_attrs.get('period'), |
| 60 | + f"last {prometheus_attrs.get('nsamples')}") |
| 61 | + queryString += '\n' |
| 62 | + assert "group" in query.__str__() |
| 63 | + assert "last" in query.__str__() |
| 64 | + assert "from" not in query.__str__() |
| 65 | + |
| 66 | + |
| 67 | +@with_setup(my_setup) |
| 68 | +def test_case03(): |
| 69 | + prometheus_attrs.update({'filters': pfilters}) |
| 70 | + with mock.patch('source.collector.QueryPolicy.md') as md: |
| 71 | + md_instance = md.return_value |
| 72 | + md_instance.includeDiskData.return_value = False |
| 73 | + md_instance.logger = logging.getLogger(__name__) |
| 74 | + request = QueryPolicy(**prometheus_attrs) |
| 75 | + query = request.get_zimon_query() |
| 76 | + query.includeDiskData = md_instance.includeDiskData.return_value |
| 77 | + queryString = 'get -j {0} {1} group {2} bucket_size {3} {4}'.format( |
| 78 | + '', '-z', prometheus_attrs.get('sensor'), |
| 79 | + prometheus_attrs.get('period'), |
| 80 | + f"last {prometheus_attrs.get('period')}") |
| 81 | + queryString += ' from ' + ",".join(pquery_filters) |
| 82 | + queryString += '\n' |
| 83 | + assert "group" in query.__str__() |
| 84 | + assert "last" in query.__str__() |
| 85 | + assert "from" in query.__str__() |
| 86 | + assert queryString == query.__str__() |
| 87 | + |
| 88 | + |
| 89 | +@with_setup(my_setup) |
| 90 | +def test_case04(): |
| 91 | + prometheus_attrs.update({'filters': pfilters}) |
| 92 | + prometheus_attrs.update({'nsamples': 1, 'rawData': False}) |
| 93 | + with mock.patch('source.collector.QueryPolicy.md') as md: |
| 94 | + md_instance = md.return_value |
| 95 | + md_instance.includeDiskData.return_value = False |
| 96 | + md_instance.logger = logging.getLogger(__name__) |
| 97 | + request = QueryPolicy(**prometheus_attrs) |
| 98 | + query = request.get_zimon_query() |
| 99 | + query.includeDiskData = md_instance.includeDiskData.return_value |
| 100 | + queryString = 'get -j {0} {1} group {2} bucket_size {3} {4}'.format( |
| 101 | + '', '', prometheus_attrs.get('sensor'), |
| 102 | + prometheus_attrs.get('period'), |
| 103 | + f"last {prometheus_attrs.get('nsamples')}") |
| 104 | + queryString += ' from ' + ",".join(pquery_filters) |
| 105 | + queryString += '\n' |
| 106 | + assert "group" in query.__str__() |
| 107 | + assert "last" in query.__str__() |
| 108 | + assert "from" in query.__str__() |
| 109 | + assert queryString == query.__str__() |
| 110 | + |
| 111 | + |
| 112 | +@with_setup(my_setup) |
| 113 | +@mock.patch('source.collector.QueryPolicy.md') |
| 114 | +@mock.patch('source.collector.SensorTimeSeries.md') |
| 115 | +@mock.patch('source.collector.SensorCollector.md') |
| 116 | +def test_case05(col_md, sts_md, md): |
| 117 | + sensor = prometheus_attrs.get('sensor') |
| 118 | + period = prometheus_attrs.get('period') |
| 119 | + logger = logging.getLogger(__name__) |
| 120 | + prometheus_attrs.update({'filters': pfilters}) |
| 121 | + prometheus_attrs.update({'nsamples': 1, 'rawData': False}) |
| 122 | + |
| 123 | + md_instance = md.return_value |
| 124 | + md_instance.includeDiskData.return_value = False |
| 125 | + md_instance.logger.return_value = logger |
| 126 | + md_instance.metaData = topo |
| 127 | + # md_instance1 = sts_md.return_value |
| 128 | + # md_instance2 = col_md.return_value |
| 129 | + request = QueryPolicy(**prometheus_attrs) |
| 130 | + collector = SensorCollector(sensor, period, logger, request) |
| 131 | + collector.md = md_instance |
| 132 | + collector.labels = collector._get_sensor_labels() |
| 133 | + collector.filtersMap = collector._get_all_filters() |
| 134 | + assert collector.sensor == sensor |
| 135 | + assert collector.period == period |
| 136 | + assert collector.request == request |
| 137 | + assert collector.labels == topo.getSensorLabels(sensor) |
| 138 | + assert len(collector.filtersMap) > 0 |
| 139 | + assert all(i in collector.labels for i in pfilters.keys()) |
| 140 | + |
| 141 | + |
| 142 | +@with_setup(my_setup) |
| 143 | +@mock.patch('source.collector.QueryPolicy.md') |
| 144 | +@mock.patch('source.collector.SensorTimeSeries.md') |
| 145 | +@mock.patch('source.collector.SensorCollector.md') |
| 146 | +def test_case06(col_md, sts_md, md): |
| 147 | + sensor = prometheus_attrs.get('sensor') |
| 148 | + period = prometheus_attrs.get('period') |
| 149 | + logger = logging.getLogger(__name__) |
| 150 | + prometheus_attrs.update({'filters': wrong_pfilters}) |
| 151 | + prometheus_attrs.update({'nsamples': 1, 'rawData': False}) |
| 152 | + |
| 153 | + md_instance = md.return_value |
| 154 | + md_instance.includeDiskData.return_value = False |
| 155 | + md_instance.logger.return_value = logger |
| 156 | + md_instance.metaData = topo |
| 157 | + # md_instance1 = sts_md.return_value |
| 158 | + # md_instance2 = col_md.return_value |
| 159 | + request = QueryPolicy(**prometheus_attrs) |
| 160 | + collector = SensorCollector(sensor, period, logger, request) |
| 161 | + collector.md = md_instance |
| 162 | + collector.labels = collector._get_sensor_labels() |
| 163 | + collector.filtersMap = collector._get_all_filters() |
| 164 | + assert collector.sensor == sensor |
| 165 | + assert collector.period == period |
| 166 | + assert collector.request == request |
| 167 | + assert collector.labels == topo.getSensorLabels(sensor) |
| 168 | + assert len(collector.filtersMap) > 0 |
| 169 | + assert not all(i in collector.labels for i in wrong_pfilters.keys()) |
| 170 | + with assert_helper.assertRaises(Exception): |
| 171 | + assert collector.validate_query_filters() |
0 commit comments