@@ -483,37 +483,55 @@ async def test_check_status__not_deleted(self):
483483 assert "File was not deleted" in str (result .error )
484484
485485 @pytest .mark .asyncio
486- async def test_check_status__file_not_saved (self ):
487- """Raise ServiceUnavailable when file does not exist after save."""
486+ @pytest .mark .parametrize (
487+ ("exists_result" , "read_result" , "expected_message" ),
488+ [
489+ (False , None , "does not exist" ),
490+ (True , b"wrong content" , "does not match" ),
491+ ],
492+ )
493+ async def test_check_status__failed_validation_cleans_up_saved_file (
494+ self , exists_result , read_result , expected_message
495+ ):
496+ """Delete the saved probe file when validation fails after write."""
488497 with mock .patch ("health_check.checks.storages" ) as mock_storages :
489498 mock_storage = mock .MagicMock ()
490499 mock_storages .__getitem__ .return_value = mock_storage
491500 mock_storage .save .return_value = "test-file.txt"
492- mock_storage .exists .return_value = False
501+ mock_storage .exists .return_value = exists_result
502+ if read_result is not None :
503+ mock_file = mock .MagicMock ()
504+ mock_file .read .return_value = read_result
505+ mock_storage .open .return_value .__enter__ .return_value = mock_file
493506
494507 check = Storage ()
495508 result = await check .get_result ()
496509 assert result .error is not None
497510 assert isinstance (result .error , ServiceUnavailable )
498- assert "does not exist" in str (result .error )
511+ assert expected_message in str (result .error )
512+ mock_storage .delete .assert_called_once_with ("test-file.txt" )
499513
500514 @pytest .mark .asyncio
501- async def test_check_status__file_content_mismatch (self ):
502- """Raise ServiceUnavailable when file content does not match."""
503- with mock .patch ("health_check.checks.storages" ) as mock_storages :
515+ async def test_check_status__cleanup_failure_does_not_mask_validation_error (self ):
516+ """Log cleanup failure and return the original validation error."""
517+ with (
518+ mock .patch ("health_check.checks.storages" ) as mock_storages ,
519+ mock .patch ("health_check.checks.logger" ) as mock_logger ,
520+ ):
504521 mock_storage = mock .MagicMock ()
505522 mock_storages .__getitem__ .return_value = mock_storage
506523 mock_storage .save .return_value = "test-file.txt"
507- mock_storage .exists .return_value = True
508- mock_file = mock .MagicMock ()
509- mock_file .read .return_value = b"wrong content"
510- mock_storage .open .return_value .__enter__ .return_value = mock_file
524+ mock_storage .exists .return_value = False
525+ mock_storage .delete .side_effect = OSError ("delete failed" )
511526
512527 check = Storage ()
513528 result = await check .get_result ()
529+
514530 assert result .error is not None
515531 assert isinstance (result .error , ServiceUnavailable )
516- assert "does not match" in str (result .error )
532+ assert "does not exist" in str (result .error )
533+ mock_storage .delete .assert_called_once_with ("test-file.txt" )
534+ mock_logger .warning .assert_called_once ()
517535
518536 @pytest .mark .asyncio
519537 async def test_check_status__service_unavailable_passthrough (self ):
0 commit comments