|
1 | 1 | from datetime import timedelta |
2 | 2 | from hashlib import sha1 |
| 3 | +from unittest.mock import patch |
3 | 4 |
|
| 5 | +import sentry_sdk |
4 | 6 | from django.core.files.base import ContentFile |
5 | 7 | from django.utils import timezone |
6 | 8 |
|
@@ -739,6 +741,42 @@ def test_detect_expired_preprod_artifacts_with_expired(self): |
739 | 741 | and "30 minutes" in expired_size_comparison.error_message |
740 | 742 | ) |
741 | 743 |
|
| 744 | + def test_detect_expired_preprod_artifacts_captures_sentry_message(self): |
| 745 | + """Test that Sentry messages are captured for each expired artifact""" |
| 746 | + current_time = timezone.now() |
| 747 | + old_time = current_time - timedelta(minutes=35) |
| 748 | + |
| 749 | + expired_artifact_1 = PreprodArtifact.objects.create( |
| 750 | + project=self.project, |
| 751 | + state=PreprodArtifact.ArtifactState.UPLOADING, |
| 752 | + ) |
| 753 | + PreprodArtifact.objects.filter(id=expired_artifact_1.id).update(date_updated=old_time) |
| 754 | + |
| 755 | + expired_artifact_2 = PreprodArtifact.objects.create( |
| 756 | + project=self.project, |
| 757 | + state=PreprodArtifact.ArtifactState.UPLOADED, |
| 758 | + ) |
| 759 | + PreprodArtifact.objects.filter(id=expired_artifact_2.id).update(date_updated=old_time) |
| 760 | + |
| 761 | + with patch.object(sentry_sdk, "capture_message") as mock_capture_message: |
| 762 | + detect_expired_preprod_artifacts() |
| 763 | + |
| 764 | + # Should have captured a message for each expired artifact |
| 765 | + assert mock_capture_message.call_count == 2 |
| 766 | + |
| 767 | + # Verify the message text and parameters |
| 768 | + call_args_list = mock_capture_message.call_args_list |
| 769 | + captured_artifact_ids = [call[1]["extras"]["artifact_id"] for call in call_args_list] |
| 770 | + |
| 771 | + assert expired_artifact_1.id in captured_artifact_ids |
| 772 | + assert expired_artifact_2.id in captured_artifact_ids |
| 773 | + |
| 774 | + # Verify all calls have the same message text, error level, and contain artifact_id |
| 775 | + for call in call_args_list: |
| 776 | + assert call[0][0] == "PreprodArtifact expired" |
| 777 | + assert call[1]["level"] == "error" |
| 778 | + assert "artifact_id" in call[1]["extras"] |
| 779 | + |
742 | 780 | def test_detect_expired_preprod_artifacts_mixed_states(self): |
743 | 781 | """Test that only artifacts in the right states are considered for expiration""" |
744 | 782 | current_time = timezone.now() |
|
0 commit comments