11import unittest
22import uuid
3- # import metrics
4- # from metrics import Availability, MetricType, HawkularMetricsError
53from metrics import *
64
75class TestMetricFunctionsBase (unittest .TestCase ):
@@ -48,12 +46,15 @@ def test_numeric_creation(self):
4846 Test creating numeric metric definitions with different tags and definition.
4947 """
5048 # Create numeric metrics with empty details and added details
51- self .client .create_numeric_definition ('test.create.numeric.1' )
52- self .client .create_numeric_definition ('test.create.numeric.2' , dataRetention = 90 )
53- self .client .create_numeric_definition ('test.create.numeric.3' , dataRetention = 90 , units = 'bytes' , env = 'qa' )
49+ md1 = self .client .create_numeric_definition ('test.create.numeric.1' )
50+ md2 = self .client .create_numeric_definition ('test.create.numeric.2' , dataRetention = 90 )
51+ md3 = self .client .create_numeric_definition ('test.create.numeric.3' , dataRetention = 90 , units = 'bytes' , env = 'qa' )
52+ self .assertTrue (md1 )
53+ self .assertTrue (md2 )
54+ self .assertTrue (md3 )
5455
5556 # Fetch metrics definition and check that the ones we created appeared also
56- m = self .client .query_definitions (MetricType .Numeric )
57+ m = self .client .query_definitions (MetricType .Gauge )
5758 self .assertEqual (3 , len (m ))
5859 self .assertEqual (self .test_tenant , m [0 ]['tenantId' ])
5960 self .assertEqual ('bytes' , m [2 ]['tags' ]['units' ])
@@ -69,13 +70,8 @@ def test_numeric_creation(self):
6970 self .assertEqual (m , expect ) # Did it?
7071
7172 # Lets try creating a duplicate metric
72- try :
73- self .client .create_numeric_definition ('test.create.numeric.1' )
74- self .fail ('Should have received an exception, metric with the same name was already created' )
75- except HawkularMetricsError , e :
76- # Check return code 400 and that the failure message was correctly parsed
77- self .assertEqual (409 , e .code )
78- self .assertEqual ('A metric with name [test.create.numeric.1] already exists' , e .msg )
73+ md4 = self .client .create_numeric_definition ('test.create.numeric.1' )
74+ self .assertFalse (md4 , 'Should have received an exception, metric with the same name was already created' )
7975
8076 def test_availability_creation (self ):
8177 # Create availability metric
@@ -92,19 +88,19 @@ def test_tags_modifications(self):
9288 m = 'test.create.tags.1'
9389 # Create metric without tags
9490 self .client .create_numeric_definition (m )
95- e = self .client .query_metric_tags (MetricType .Numeric , m )
91+ e = self .client .query_metric_tags (MetricType .Gauge , m )
9692 self .assertIsNotNone (e )
9793 self .assertEqual ({}, e )
9894 # Add tags
99- self .client .update_metric_tags (MetricType .Numeric , m , hostname = 'machine1' , a = 'b' )
95+ self .client .update_metric_tags (MetricType .Gauge , m , hostname = 'machine1' , a = 'b' )
10096 # Fetch metric - check for tags
101- tags = self .client .query_metric_tags (MetricType .Numeric , m )
97+ tags = self .client .query_metric_tags (MetricType .Gauge , m )
10298 self .assertEqual (2 , len (tags ))
10399 self .assertEqual ("b" , tags ['a' ])
104100 # Delete some metric tags
105- self .client .delete_metric_tags (MetricType .Numeric , m , a = 'b' , hostname = 'machine1' )
101+ self .client .delete_metric_tags (MetricType .Gauge , m , a = 'b' , hostname = 'machine1' )
106102 # Fetch metric - check that tags were deleted
107- tags_2 = self .client .query_metric_tags (MetricType .Numeric , m )
103+ tags_2 = self .client .query_metric_tags (MetricType .Gauge , m )
108104 self .assertEqual (0 , len (tags_2 ))
109105
110106 # def test_tags_behavior(self):
@@ -120,19 +116,18 @@ def test_tags_modifications(self):
120116 # print 'END: TEST TAGS'
121117
122118 def test_add_numeric_single (self ):
123-
124119 # Normal way
125120 value = float (4.35 )
126121 datapoint = create_datapoint (value , time_millis ())
127- metric = create_metric ('test.numeric./' , datapoint )
128- self .client .put (MetricType . Numeric , metric )
122+ metric = create_metric (MetricType . Gauge , 'test.numeric./' , datapoint )
123+ self .client .put (metric )
129124
130125 # Fetch results
131126 data = self .client .query_single_numeric ('test.numeric./' )
132127 self .assertEqual (float (data [0 ]['value' ]), value )
133128
134129 # Shortcut method with tags
135- self .client .push (MetricType .Numeric , 'test.numeric.single.tags' , value , hostname = 'localhost' )
130+ self .client .push (MetricType .Gauge , 'test.numeric.single.tags' , value , hostname = 'localhost' )
136131
137132 # Fetch results
138133 data = self .client .query_single_numeric ('test.numeric.single.tags' )
@@ -153,41 +148,42 @@ def test_add_numeric_multi_datapoint(self):
153148 metric_1v = create_datapoint (float (1.45 ))
154149 metric_2v = create_datapoint (float (2.00 ), (time_millis () - 2000 ))
155150
156- metric = create_metric ('test.numeric.multi' , [metric_1v , metric_2v ])
157- self .client .put (MetricType . Numeric , metric )
151+ metric = create_metric (MetricType . Gauge , 'test.numeric.multi' , [metric_1v , metric_2v ])
152+ self .client .put (metric )
158153
159154 data = self .client .query_single_numeric ('test.numeric.multi' )
160155 self .assertEqual (len (data ), 2 )
161156 self .assertEqual (data [0 ]['value' ], float (1.45 ))
162157 self .assertEqual (data [1 ]['value' ], float (2.00 ))
163158
164159 def test_add_availability_multi_datapoint (self ):
165- up = create_datapoint ('up' , (time_millis () - 2000 ))
166- down = create_datapoint ('down' )
160+ t = time_millis ()
161+ up = create_datapoint ('up' , (t - 2000 ))
162+ down = create_datapoint ('down' , t )
167163
168- m = create_metric ('test.avail.multi' , [up , down ])
164+ m = create_metric (MetricType . Availability , 'test.avail.multi' , [up , down ])
169165
170- self .client .put (MetricType . Availability , m )
166+ self .client .put (m )
171167 data = self .client .query_single_availability ('test.avail.multi' )
172168
173169 self .assertEqual (len (data ), 2 )
174- self .assertEqual (data [0 ]['value' ], 'down ' )
175- self .assertEqual (data [1 ]['value' ], 'up ' )
170+ self .assertEqual (data [0 ]['value' ], 'up ' )
171+ self .assertEqual (data [1 ]['value' ], 'down ' )
176172
177- def test_add_multi_metrics_and_datapoints (self ):
173+ def test_add_mixed_metrics_and_datapoints (self ):
178174 metric1 = create_datapoint (float (1.45 ))
179175 metric1_2 = create_datapoint (float (2.00 ), (time_millis () - 2000 ))
180176
181- metric_multi = create_metric ('test.multi.numeric.1' , [metric1 , metric1_2 ])
177+ metric_multi = create_metric (MetricType . Gauge , 'test.multi.numeric.1' , [metric1 , metric1_2 ])
182178
183- metric2 = create_datapoint (float ( 1.55 ) )
184- metric2_multi = create_metric ('test.multi.numeric.2' , [metric2 ])
179+ metric2 = create_datapoint (Availability . Up )
180+ metric2_multi = create_metric (MetricType . Availability , 'test.multi.numeric.2' , [metric2 ])
185181
186- self .client .put (MetricType . Numeric , [metric_multi , metric2_multi ])
182+ self .client .put ([metric_multi , metric2_multi ])
187183
188184 # Check that both were added correctly..
189185 metric1_data = self .client .query_single_numeric ('test.multi.numeric.1' )
190- metric2_data = self .client .query_single_numeric ('test.multi.numeric.2' )
186+ metric2_data = self .client .query_single_availability ('test.multi.numeric.2' )
191187
192188 self .assertEqual (2 , len (metric1_data ))
193189 self .assertEqual (1 , len (metric2_data ))
@@ -198,29 +194,29 @@ def test_query_options(self):
198194 v1 = create_datapoint (float (1.45 ), t )
199195 v2 = create_datapoint (float (2.00 ), (t - 2000 ))
200196
201- m = create_metric ('test.query.numeric.1' , [v1 , v2 ])
202- self .client .put (MetricType . Numeric , m )
197+ m = create_metric (MetricType . Gauge , 'test.query.numeric.1' , [v1 , v2 ])
198+ self .client .put (m )
203199
204200 # Query first without limitations
205- d = self .client .query_metric (MetricType .Numeric , 'test.query.numeric.1' )
201+ d = self .client .query_metric (MetricType .Gauge , 'test.query.numeric.1' )
206202 self .assertEqual (2 , len (d ))
207203
208204 # Query for data which has start time limitation
209- d = self .client .query_metric (MetricType .Numeric , 'test.query.numeric.1' , start = (t - 1000 ))
205+ d = self .client .query_metric (MetricType .Gauge , 'test.query.numeric.1' , start = (t - 1000 ))
210206 self .assertEqual (1 , len (d ))
211207
212208 # This feature isn't really ready for prime time in Hawkular-Metrics yet..
213209 # def test_tags_finding(self):
214210 # # Create metrics with tags
215211 # m = 'test.create.data.tags.1'
216- # self.client.create_metric_definition(MetricType.Numeric , m, ab='cd')
212+ # self.client.create_metric_definition(MetricType.Gauge , m, ab='cd')
217213 # # Push some data to them
218214 # t = time_millis()
219215 # v = float(1.4)
220- # self.client.push(MetricType.Numeric , m, v, t)
216+ # self.client.push(MetricType.Gauge , m, v, t)
221217 # # Fetch data with certain tags
222218 # expected = { 'id': m, 'timestamp': t, 'value': v }
223- # d = self.client.query_data_with_tags(MetricType.Numeric , ab='cd')
219+ # d = self.client.query_data_with_tags(MetricType.Gauge , ab='cd')
224220 # self.assertIsNotNone(d)
225221 # self.assertIn(expected, d)
226222
0 commit comments