@@ -238,3 +238,161 @@ def test_urls_excluded_from_sampling(self):
238238
239239 URLLib3Instrumentor ().uninstrument ()
240240 RequestsInstrumentor ().uninstrument ()
241+
242+ def test_constructor_with_none_endpoint (self ):
243+ """Tests constructor behavior when endpoint is None"""
244+ with self .assertLogs (_sampling_client_logger , level = "ERROR" ) as cm :
245+ # Constructor will log error but then crash on concatenation
246+ with self .assertRaises (TypeError ):
247+ _AwsXRaySamplingClient (endpoint = None )
248+
249+ # Verify error log was called before the crash
250+ self .assertIn ("endpoint must be specified" , cm .output [0 ])
251+
252+ def test_constructor_with_log_level (self ):
253+ """Tests constructor sets log level when specified"""
254+ original_level = _sampling_client_logger .level
255+ try :
256+ _AwsXRaySamplingClient ("http://test.com" , log_level = logging .DEBUG )
257+ self .assertEqual (_sampling_client_logger .level , logging .DEBUG )
258+ finally :
259+ # Reset log level
260+ _sampling_client_logger .setLevel (original_level )
261+
262+ @patch ("requests.Session.post" )
263+ def test_get_sampling_rules_none_response (self , mock_post ):
264+ """Tests get_sampling_rules when response is None"""
265+ mock_post .return_value = None
266+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
267+
268+ with self .assertLogs (_sampling_client_logger , level = "ERROR" ) as cm :
269+ sampling_rules = client .get_sampling_rules ()
270+
271+ # Verify error log and empty result
272+ self .assertIn ("GetSamplingRules response is None" , cm .output [0 ])
273+ self .assertEqual (len (sampling_rules ), 0 )
274+
275+ @patch ("requests.Session.post" )
276+ def test_get_sampling_rules_request_exception (self , mock_post ):
277+ """Tests get_sampling_rules when RequestException occurs"""
278+ mock_post .side_effect = requests .exceptions .RequestException ("Connection error" )
279+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
280+
281+ with self .assertLogs (_sampling_client_logger , level = "ERROR" ) as cm :
282+ sampling_rules = client .get_sampling_rules ()
283+
284+ # Verify error log and empty result
285+ self .assertIn ("Request error occurred" , cm .output [0 ])
286+ self .assertIn ("Connection error" , cm .output [0 ])
287+ self .assertEqual (len (sampling_rules ), 0 )
288+
289+ @patch ("requests.Session.post" )
290+ def test_get_sampling_rules_json_decode_error (self , mock_post ):
291+ """Tests get_sampling_rules when JSON decode error occurs"""
292+ # Mock response that raises JSONDecodeError when .json() is called
293+ mock_response = mock_post .return_value
294+ mock_response .json .side_effect = json .JSONDecodeError ("Invalid JSON" , "doc" , 0 )
295+
296+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
297+
298+ with self .assertLogs (_sampling_client_logger , level = "ERROR" ) as cm :
299+ sampling_rules = client .get_sampling_rules ()
300+
301+ # Verify error log and empty result
302+ self .assertIn ("Error in decoding JSON response" , cm .output [0 ])
303+ self .assertEqual (len (sampling_rules ), 0 )
304+
305+ @patch ("requests.Session.post" )
306+ def test_get_sampling_rules_general_exception (self , mock_post ):
307+ """Tests get_sampling_rules when general exception occurs"""
308+ mock_post .side_effect = Exception ("Unexpected error" )
309+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
310+
311+ with self .assertLogs (_sampling_client_logger , level = "ERROR" ) as cm :
312+ sampling_rules = client .get_sampling_rules ()
313+
314+ # Verify error log and empty result
315+ self .assertIn ("Error occurred when attempting to fetch rules" , cm .output [0 ])
316+ self .assertIn ("Unexpected error" , cm .output [0 ])
317+ self .assertEqual (len (sampling_rules ), 0 )
318+
319+ @patch ("requests.Session.post" )
320+ def test_get_sampling_targets_none_response (self , mock_post ):
321+ """Tests get_sampling_targets when response is None"""
322+ mock_post .return_value = None
323+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
324+
325+ with self .assertLogs (_sampling_client_logger , level = "DEBUG" ) as cm :
326+ response = client .get_sampling_targets ([])
327+
328+ # Verify debug log and default response
329+ self .assertIn ("GetSamplingTargets response is None" , cm .output [0 ])
330+ self .assertEqual (response .SamplingTargetDocuments , [])
331+ self .assertEqual (response .UnprocessedStatistics , [])
332+ self .assertEqual (response .LastRuleModification , 0.0 )
333+
334+ @patch ("requests.Session.post" )
335+ def test_get_sampling_targets_invalid_response_format (self , mock_post ):
336+ """Tests get_sampling_targets when response format is invalid"""
337+ # Missing required fields
338+ mock_post .return_value .configure_mock (** {"json.return_value" : {"InvalidField" : "value" }})
339+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
340+
341+ with self .assertLogs (_sampling_client_logger , level = "DEBUG" ) as cm :
342+ response = client .get_sampling_targets ([])
343+
344+ # Verify debug log and default response
345+ self .assertIn ("getSamplingTargets response is invalid" , cm .output [0 ])
346+ self .assertEqual (response .SamplingTargetDocuments , [])
347+ self .assertEqual (response .UnprocessedStatistics , [])
348+ self .assertEqual (response .LastRuleModification , 0.0 )
349+
350+ @patch ("requests.Session.post" )
351+ def test_get_sampling_targets_request_exception (self , mock_post ):
352+ """Tests get_sampling_targets when RequestException occurs"""
353+ mock_post .side_effect = requests .exceptions .RequestException ("Network error" )
354+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
355+
356+ with self .assertLogs (_sampling_client_logger , level = "DEBUG" ) as cm :
357+ response = client .get_sampling_targets ([])
358+
359+ # Verify debug log and default response
360+ self .assertIn ("Request error occurred" , cm .output [0 ])
361+ self .assertIn ("Network error" , cm .output [0 ])
362+ self .assertEqual (response .SamplingTargetDocuments , [])
363+ self .assertEqual (response .UnprocessedStatistics , [])
364+ self .assertEqual (response .LastRuleModification , 0.0 )
365+
366+ @patch ("requests.Session.post" )
367+ def test_get_sampling_targets_json_decode_error (self , mock_post ):
368+ """Tests get_sampling_targets when JSON decode error occurs"""
369+ # Mock response that raises JSONDecodeError when .json() is called
370+ mock_response = mock_post .return_value
371+ mock_response .json .side_effect = json .JSONDecodeError ("Invalid JSON" , "doc" , 0 )
372+
373+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
374+
375+ with self .assertLogs (_sampling_client_logger , level = "DEBUG" ) as cm :
376+ response = client .get_sampling_targets ([])
377+
378+ # Verify debug log and default response
379+ self .assertIn ("Error in decoding JSON response" , cm .output [0 ])
380+ self .assertEqual (response .SamplingTargetDocuments , [])
381+ self .assertEqual (response .UnprocessedStatistics , [])
382+ self .assertEqual (response .LastRuleModification , 0.0 )
383+
384+ @patch ("requests.Session.post" )
385+ def test_get_sampling_targets_general_exception (self , mock_post ):
386+ """Tests get_sampling_targets when general exception occurs"""
387+ mock_post .side_effect = Exception ("Unexpected error" )
388+ client = _AwsXRaySamplingClient ("http://127.0.0.1:2000" )
389+
390+ with self .assertLogs (_sampling_client_logger , level = "DEBUG" ) as cm :
391+ response = client .get_sampling_targets ([])
392+
393+ # Verify debug log and default response
394+ self .assertIn ("Error occurred when attempting to fetch targets" , cm .output [0 ])
395+ self .assertIn ("Unexpected error" , cm .output [0 ])
396+ self .assertEqual (response .SamplingTargetDocuments , [])
397+ self .assertEqual (response .UnprocessedStatistics , [])
398+ self .assertEqual (response .LastRuleModification , 0.0 )
0 commit comments