1919
2020from cloudkitty import collector
2121from cloudkitty import tests
22+ from datetime import datetime
23+ from datetime import timedelta
2224
2325
2426class MetricConfigValidationTest (tests .TestCase ):
@@ -44,6 +46,46 @@ class MetricConfigValidationTest(tests.TestCase):
4446 }
4547 }
4648
49+ list_data = {
50+ 'metrics' : {
51+ 'metric_one' : [
52+ {
53+ 'groupby' : ['one' ],
54+ 'metadata' : ['two' ],
55+ 'alt_name' : 'metric_u' ,
56+ 'unit' : 'u' ,
57+ },
58+ {
59+ 'groupby' : ['three' ],
60+ 'metadata' : ['four' ],
61+ 'alt_name' : 'metric_v' ,
62+ 'unit' : 'v' ,
63+ }
64+ ]
65+ }
66+ }
67+
68+ list_output = {
69+ 'metric_one@#metric_u' : {
70+ 'groupby' : ['one' ],
71+ 'metadata' : ['two' ],
72+ 'unit' : 'u' ,
73+ 'alt_name' : 'metric_u' ,
74+ 'factor' : 1 ,
75+ 'offset' : 0 ,
76+ 'mutate' : 'NONE' ,
77+ },
78+ 'metric_one@#metric_v' : {
79+ 'groupby' : ['three' ],
80+ 'metadata' : ['four' ],
81+ 'unit' : 'v' ,
82+ 'alt_name' : 'metric_v' ,
83+ 'factor' : 1 ,
84+ 'offset' : 0 ,
85+ 'mutate' : 'NONE' ,
86+ },
87+ }
88+
4789 def test_base_minimal_config (self ):
4890 data = copy .deepcopy (self .base_data )
4991 expected_output = copy .deepcopy (self .base_output )
@@ -149,6 +191,48 @@ def test_prometheus_minimal_config_minimal_extra_args(self):
149191 expected_output ,
150192 )
151193
194+ def test_prometheus_query_builder (self ):
195+ data = copy .deepcopy (self .base_data )
196+ data ['metrics' ]['metric_one' ]['extra_args' ] = {
197+ 'aggregation_method' : 'max' ,
198+ 'query_function' : 'abs' ,
199+ 'query_prefix' : 'custom_prefix' ,
200+ 'query_suffix' : 'custom_suffix' ,
201+ 'range_function' : 'delta' ,
202+ }
203+
204+ prometheus = collector .prometheus .PrometheusCollector
205+
206+ conf = prometheus .check_configuration (data )
207+ metric_name = list (conf .keys ())[0 ]
208+ start = datetime .now ()
209+ end = start + timedelta (seconds = 60 )
210+ scope_key = "random_key"
211+ scope_id = "random_value"
212+ groupby = conf [metric_name ].get ('groupby' , [])
213+ metadata = conf [metric_name ].get ('metadata' , [])
214+
215+ query = prometheus .build_query (
216+ conf ,
217+ metric_name ,
218+ start ,
219+ end ,
220+ scope_key ,
221+ scope_id ,
222+ groupby ,
223+ metadata
224+ )
225+
226+ expected_output = (
227+ 'custom_prefix max(abs(delta(metric_one{random_key="random_value"}'
228+ '[60s]))) by (one, project_id, two) custom_suffix'
229+ )
230+
231+ self .assertEqual (
232+ query ,
233+ expected_output ,
234+ )
235+
152236 def test_check_duplicates (self ):
153237 data = copy .deepcopy (self .base_data )
154238 for metric_name , metric in data ['metrics' ].items ():
@@ -179,3 +263,29 @@ def test_validate_map_mutator(self):
179263 self .assertRaises (
180264 collector .InvalidConfiguration ,
181265 collector .validate_map_mutator , metric_name , metric )
266+
267+ def test_base_minimal_config_list (self ):
268+ data = copy .deepcopy (self .list_data )
269+ expected_output = copy .deepcopy (self .list_output )
270+
271+ for _ , metric in expected_output .items ():
272+ metric ['groupby' ].append ('project_id' )
273+
274+ self .assertEqual (
275+ collector .BaseCollector .check_configuration (data ),
276+ expected_output ,
277+ )
278+
279+ # submetric with same alt_name should fail
280+ # Because they would overlap in the dict
281+ def test_check_duplicates_list (self ):
282+ data = copy .deepcopy (self .list_data )
283+ data ['metrics' ]['metric_one' ].append ({
284+ 'groupby' : ['five' ],
285+ 'metadata' : ['six' ],
286+ 'alt_name' : 'metric_v' ,
287+ 'unit' : 'w' ,
288+ })
289+ self .assertRaises (
290+ collector .InvalidConfiguration ,
291+ collector .BaseCollector .check_configuration , data )
0 commit comments