@@ -127,7 +127,6 @@ def test_get_metric_aggregation(self):  # noqa D102
127127    def  test_get_metric_aggregation_with_incorrect_input_types (self ):  # noqa D102 
128128        with  self .assertRaises (TypeError , msg = "operations accepted invalid value type" ):
129129            _  =  self .pc .get_metric_aggregation (query = "up" , operations = "sum" )
130- 
131130    def  test_retry_on_error (self ):  # noqa D102 
132131        retry  =  Retry (total = 3 , backoff_factor = 0.1 , status_forcelist = [400 ])
133132        pc  =  PrometheusConnect (url = self .prometheus_host , disable_ssl = True , retry = retry )
@@ -140,6 +139,76 @@ def test_get_label_names_method(self):  # noqa D102
140139        self .assertEqual (len (labels ), 4 )
141140        self .assertEqual (labels , ["__name__" , "env" , "instance" , "job" ])
142141
142+     def  test_get_scrape_pools (self ):  # noqa D102 
143+         scrape_pools  =  self .pc .get_scrape_pools ()
144+         self .assertIsInstance (scrape_pools , list )
145+         self .assertTrue (len (scrape_pools ) >  0 , "no scrape pools found" )
146+         self .assertIsInstance (scrape_pools [0 ], str )
147+ 
148+     def  test_get_targets (self ):  # noqa D102 
149+         targets  =  self .pc .get_targets ()
150+         self .assertIsInstance (targets , dict )
151+         self .assertIn ('activeTargets' , targets )
152+         self .assertIsInstance (targets ['activeTargets' ], list )
153+ 
154+         # Test with state filter 
155+         active_targets  =  self .pc .get_targets (state = 'active' )
156+         self .assertIsInstance (active_targets , dict )
157+         self .assertIn ('activeTargets' , active_targets )
158+ 
159+         # Test with scrape_pool filter 
160+         if  len (scrape_pools  :=  self .pc .get_scrape_pools ()) >  0 :
161+             pool_targets  =  self .pc .get_targets (scrape_pool = scrape_pools [0 ])
162+             self .assertIsInstance (pool_targets , dict )
163+             self .assertIn ('activeTargets' , pool_targets )
164+ 
165+     def  test_get_target_metadata (self ):  # noqa D102 
166+         # Get a target to test with 
167+         targets  =  self .pc .get_targets ()
168+         if  len (targets ['activeTargets' ]) >  0 :
169+             target  =  {
170+                 'job' : targets ['activeTargets' ][0 ]['labels' ]['job' ]
171+             }
172+             metadata  =  self .pc .get_target_metadata (target )
173+             self .assertIsInstance (metadata , list )
174+             
175+             # Test with metric filter 
176+             if  len (metadata ) >  0 :
177+                 metric_name  =  metadata [0 ]['metric' ]
178+                 filtered_metadata  =  self .pc .get_target_metadata (target , metric = metric_name )
179+                 self .assertIsInstance (filtered_metadata , list )
180+                 self .assertTrue (all (item ['metric' ] ==  metric_name  for  item  in  filtered_metadata ))
181+             
182+             # Test with limit 
183+             limited_metadata  =  self .pc .get_target_metadata (target , limit = 1 )
184+             self .assertLessEqual (len (limited_metadata ), 1 )
185+ 
186+     def  test_get_metric_metadata (self ):  # noqa D102 
187+         metadata  =  self .pc .get_metric_metadata (metric = None )
188+         self .assertIsInstance (metadata , list )
189+         self .assertTrue (len (metadata ) >  0 , "no metric metadata found" )
190+         
191+         # Check structure of metadata 
192+         self .assertIn ('metric_name' , metadata [0 ])
193+         self .assertIn ('type' , metadata [0 ])
194+         self .assertIn ('help' , metadata [0 ])
195+         self .assertIn ('unit' , metadata [0 ])
196+         
197+         # Test with specific metric 
198+         if  len (metadata ) >  0 :
199+             metric_name  =  metadata [0 ]['metric_name' ]
200+             filtered_metadata  =  self .pc .get_metric_metadata (metric = metric_name )
201+             self .assertIsInstance (filtered_metadata , list )
202+             self .assertTrue (all (item ['metric_name' ] ==  metric_name  for  item  in  filtered_metadata ))
203+         
204+         # Test with limit 
205+         limited_metadata  =  self .pc .get_metric_metadata (limit = 1 )
206+         self .assertLessEqual (len (limited_metadata ), 1 )
207+         
208+         # Test with limit_per_metric 
209+         limited_per_metric  =  self .pc .get_metric_metadata (limit_per_metric = 1 )
210+         self .assertIsInstance (limited_per_metric , list )
211+ 
143212
144213class  TestPrometheusConnectWithMockedNetwork (BaseMockedNetworkTestcase ):
145214    """Network is blocked in this testcase, see base class.""" 
@@ -233,3 +302,6 @@ def test_get_label_values_method(self):  # noqa D102
233302            self .assertEqual (handler .call_count , 1 )
234303            request  =  handler .requests [0 ]
235304            self .assertEqual (request .path_url , "/api/v1/label/label_name/values" )
305+ 
306+ if  __name__  ==  "__main__" :
307+     unittest .main ()
0 commit comments