1818
1919import unittest
2020import uuid
21- from metrics import *
21+ from hawkular .metrics import *
22+ import os
23+
24+ from tests import base
2225
23- class TestMetricFunctionsBase (unittest .TestCase ):
2426
27+ class TestMetricFunctionsBase (unittest .TestCase ):
2528 def setUp (self ):
2629 self .maxDiff = None
2730 self .test_tenant = str (uuid .uuid4 ())
28- self .client = HawkularMetricsClient (tenant_id = self .test_tenant , port = 8080 )
29-
31+ self .client = HawkularMetricsClient (tenant_id = self .test_tenant , port = 8080 , authtoken = 'secret' )
32+
33+
3034class TenantTestCase (TestMetricFunctionsBase ):
3135 """
3236 Test creating and fetching tenants. Each creation test should also
3337 fetch the tenants to test that functionality also
3438 """
35-
39+
3640 def test_tenant_creation (self ):
3741 tenant = str (uuid .uuid4 ())
3842 self .client .create_tenant (tenant )
3943 tenants = self .client .query_tenants ()
4044
41- expect = { 'id' : tenant }
45+ expect = {'id' : tenant }
4246 self .assertIn (expect , tenants )
4347
4448 def test_tenant_creation_with_retentions (self ):
4549 tenant = str (uuid .uuid4 ())
46- retentions = { 'gauge' : 8 , 'availability' : 30 }
50+ retentions = {'gauge' : 8 , 'availability' : 30 }
4751 self .client .create_tenant (tenant , retentions )
4852
49- expect = { 'id' : tenant , 'retentions' : { 'gauge' : 8 , 'availability' : 30 } }
53+ expect = {'id' : tenant , 'retentions' : {'gauge' : 8 , 'availability' : 30 } }
5054 self .assertIn (expect , self .client .query_tenants ())
51-
55+
56+
5257class MetricsTestCase (TestMetricFunctionsBase ):
5358 """
5459 Test metric functionality, both adding definition and querying for definition,
@@ -62,7 +67,7 @@ def find_metric(self, name, definitions):
6267 for defin in definitions :
6368 if defin ['id' ] == name :
6469 return defin
65-
70+
6671 def test_gauge_creation (self ):
6772 """
6873 Test creating gauge metric definitions with different tags and definition.
@@ -72,7 +77,8 @@ def test_gauge_creation(self):
7277
7378 md1 = self .client .create_metric_definition (MetricType .Gauge , id_name .format ('1' ))
7479 md2 = self .client .create_metric_definition (MetricType .Gauge , id_name .format ('2' ), dataRetention = 90 )
75- md3 = self .client .create_metric_definition (MetricType .Gauge , id_name .format ('3' ), dataRetention = 90 , units = 'bytes' , env = 'qa' )
80+ md3 = self .client .create_metric_definition (MetricType .Gauge , id_name .format ('3' ), dataRetention = 90 ,
81+ units = 'bytes' , env = 'qa' )
7682 self .assertTrue (md1 )
7783 self .assertTrue (md2 )
7884 self .assertTrue (md3 )
@@ -88,8 +94,8 @@ def test_gauge_creation(self):
8894
8995 # This is what the returned dict should look like
9096 expect = [
91- { 'dataRetention' : 7 , 'type' : 'gauge' , 'id' : 'test.create.gauge.1' ,
92- 'tenantId' : self .test_tenant },
97+ {'dataRetention' : 7 , 'type' : 'gauge' , 'id' : 'test.create.gauge.1' ,
98+ 'tenantId' : self .test_tenant },
9399 {'dataRetention' : 90 , 'type' : 'gauge' , 'id' : 'test.create.gauge.2' , 'tenantId' : self .test_tenant },
94100 {'tags' : {'units' : 'bytes' , 'env' : 'qa' },
95101 'id' : 'test.create.gauge.3' , 'dataRetention' : 90 , 'type' : 'gauge' , 'tenantId' : self .test_tenant }]
@@ -109,7 +115,7 @@ def test_availability_creation(self):
109115 self .client .create_metric_definition (MetricType .Availability , id_name .format ('2' ), dataRetention = 90 )
110116 self .client .create_metric_definition (MetricType .Availability , id_name .format ('3' ), dataRetention = 94 , env = 'qa' )
111117 # Fetch metrics and check that it did appear
112- m = self .client .query_metric_definitions (MetricType .Availability )
118+ m = self .client .query_metric_definitions (MetricType .Availability )
113119 self .assertEqual (3 , len (m ))
114120
115121 avail_3 = self .find_metric (id_name .format ('3' ), m )
@@ -135,7 +141,7 @@ def test_tags_modifications(self):
135141 self .assertEqual (0 , len (tags_2 ))
136142
137143 def test_tags_queries (self ):
138- for i in range (1 ,9 ):
144+ for i in range (1 , 9 ):
139145 m_id = 'test.query.tags.{}' .format (i )
140146 hostname = 'host{}' .format (i )
141147 self .client .create_metric_definition (MetricType .Counter , m_id , hostname = hostname , env = 'qa' )
@@ -170,22 +176,24 @@ def test_add_availability_single(self):
170176
171177 up = self .client .query_metric (MetricType .Availability , 'test.avail.1' )
172178 self .assertEqual (up [0 ]['value' ], 'up' )
173-
179+
174180 down = self .client .query_metric (MetricType .Availability , 'test.avail.2' )
175181 self .assertEqual (down [0 ]['value' ], Availability .Down )
176182
183+ @unittest .skipIf (base .version != 'latest' and base .major_version == 0 and base .minor_version <= 15 ,
184+ 'Not supported in ' + base .version + ' version' )
177185 def test_add_string_single (self ):
178186 self .client .push (MetricType .String , 'test.string.1' , "foo" )
179187 data = self .client .query_metric (MetricType .String , 'test.string.1' )
180188 self .assertEqual (data [0 ]["value" ], 'foo' )
181-
189+
182190 def test_add_gauge_multi_datapoint (self ):
183191 metric_1v = create_datapoint (float (1.45 ))
184192 metric_2v = create_datapoint (float (2.00 ), (time_millis () - 2000 ))
185193
186194 metric = create_metric (MetricType .Gauge , 'test.gauge.multi' , [metric_1v , metric_2v ])
187195 self .client .put (metric )
188-
196+
189197 data = self .client .query_metric (MetricType .Gauge , 'test.gauge.multi' )
190198 self .assertEqual (len (data ), 2 )
191199 self .assertEqual (data [0 ]['value' ], float (1.45 ))
@@ -197,7 +205,7 @@ def test_add_availability_multi_datapoint(self):
197205 down = create_datapoint ('down' , t )
198206
199207 m = create_metric (MetricType .Availability , 'test.avail.multi' , [up , down ])
200-
208+
201209 self .client .put (m )
202210 data = self .client .query_metric (MetricType .Availability , 'test.avail.multi' )
203211
@@ -212,7 +220,7 @@ def test_add_mixed_metrics_and_datapoints(self):
212220 metric_multi = create_metric (MetricType .Gauge , 'test.multi.gauge.1' , [metric1 , metric1_2 ])
213221
214222 metric2 = create_datapoint (Availability .Up )
215- metric2_multi = create_metric (MetricType .Availability ,'test.multi.gauge.2' , [metric2 ])
223+ metric2_multi = create_metric (MetricType .Availability , 'test.multi.gauge.2' , [metric2 ])
216224
217225 self .client .put ([metric_multi , metric2_multi ])
218226
@@ -237,7 +245,7 @@ def test_query_options(self):
237245 self .assertEqual (2 , len (d ))
238246
239247 # Query for data which has start time limitation
240- d = self .client .query_metric (MetricType .Gauge , 'test.query.gauge.1' , start = (t - 1000 ))
248+ d = self .client .query_metric (MetricType .Gauge , 'test.query.gauge.1' , start = (t - 1000 ))
241249 self .assertEqual (1 , len (d ))
242250
243251 def test_tenant_changing (self ):
@@ -278,5 +286,6 @@ def test_get_metrics_stats_url(self):
278286 url = self .client ._get_metrics_stats_url ('some.key' )
279287 self .assertEqual ('some.key/stats' , url )
280288
289+
281290if __name__ == '__main__' :
282291 unittest .main ()
0 commit comments