-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DST-835: Fix the s3 health check (#162)
- Loading branch information
1 parent
52b0d7f
commit 4cbaef6
Showing
3 changed files
with
52 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
import boto3 | ||
import sentry_sdk | ||
from core.document_storage import PermanentDocumentStorage, TemporaryDocumentStorage | ||
from django.conf import settings | ||
|
||
|
||
def s3_check() -> bool: | ||
""" | ||
Performs a check on the S3 connection | ||
Check if the S3 bucket exists and ensure the app can access it. | ||
https://boto3.amazonaws.com/v1/documentation/api/1.35.9/reference/services/s3/client/head_bucket.html | ||
""" | ||
temporary_document_bucket = TemporaryDocumentStorage().bucket | ||
permanent_document_bucket = PermanentDocumentStorage().bucket | ||
client = boto3.client("s3") | ||
|
||
bucket_names = [settings.TEMPORARY_S3_BUCKET_NAME, settings.PERMANENT_S3_BUCKET_NAME] | ||
|
||
try: | ||
assert temporary_document_bucket.creation_date | ||
assert permanent_document_bucket.creation_date | ||
return True | ||
for bucket_name in bucket_names: | ||
client.head_bucket(Bucket=bucket_name) | ||
except Exception as e: | ||
sentry_sdk.capture_exception(e) | ||
return False | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from django.urls import reverse | ||
|
||
from tests.helpers import get_response_content | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup(): | ||
"""Need to fix the Sites context processor as healthcheck views don't have a site.""" | ||
with patch("core.sites.context_processors.sites", return_value={}): | ||
yield | ||
|
||
|
||
@patch("healthcheck.checks.s3.boto3") | ||
def test_successful_healthcheck(mock_s3_client, al_client): | ||
mock_s3_client.head_bucket.return_value = {"test": True} | ||
response = al_client.get(reverse("healthcheck:healthcheck_ping")) | ||
content = get_response_content(response) | ||
assert "OK" in content | ||
assert response.status_code == 200 | ||
|
||
|
||
@patch("healthcheck.views.s3_check", return_value=False) | ||
def test_s3_broken_healthcheck(mock_s3_check, al_client): | ||
response = al_client.get(reverse("healthcheck:healthcheck_ping")) | ||
content = get_response_content(response) | ||
assert "FAIL" in content | ||
assert response.status_code == 200 | ||
|
||
|
||
@patch("healthcheck.views.db_check", return_value=False) | ||
def test_db_broken_healthcheck(mock_db_check, al_client): | ||
response = al_client.get(reverse("healthcheck:healthcheck_ping")) | ||
content = get_response_content(response) | ||
assert "FAIL" in content | ||
assert response.status_code == 200 |