Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add mock s3 clients / mack CI uploads mock #73

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/docker-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ jobs:
echo "${{ secrets.AWS_DEFAULT_REGION }}" > ./secrets/aws_default_region.txt


- name: Run Docker Compose
run: docker-compose --env-file .env up

- name: Tear down Docker Compose
run: docker-compose down
- name: Build and Test
env:
CI: true
run: |
docker-compose up --build
docker-compose down
18 changes: 15 additions & 3 deletions src/sr2silo/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from __future__ import annotations

import bz2
import logging
import os
import shutil
from pathlib import Path

import boto3
from botocore.exceptions import NoCredentialsError
from moto import mock_aws
Copy link
Preview

Copilot AI Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import should be 'from moto import mock_s3' instead of 'from moto import mock_aws'.

Suggested change
from moto import mock_aws
from moto import mock_s3

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options


def compress_bz2(input_fp: Path, output_fp: Path) -> None:
Expand Down Expand Up @@ -62,14 +65,23 @@ def get_s3_client():
return s3_client


def upload_file_to_s3(file_name, bucket, object_name=None):
def upload_file_to_s3(file_name, bucket, object_name=None, client=None):
"""Upload a file to an S3 bucket"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name

# get the s3 client
s3_client = get_s3_client()
# If running in CI, mock the S3 upload
if os.getenv("CI"):
logging.info("Running in CI environment, mocking S3 upload with moto.")
with mock_aws():
s3_client = boto3.client("s3", region_name="us-east-1")
s3_client.create_bucket(Bucket=bucket)
s3_client.upload_file(file_name, bucket, object_name or file_name)
return True

# If client was given, use it; otherwise, get the s3 client
s3_client = client if client else get_s3_client()

try:
s3_client.upload_file(file_name, bucket, object_name)
Expand Down
40 changes: 35 additions & 5 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

from __future__ import annotations

from sr2silo.s3 import compress_bz2
import boto3
import pytest
from moto import mock_aws

from sr2silo.s3 import compress_bz2, upload_file_to_s3


@pytest.fixture
def s3_client():
"""Mocked S3 client."""
with mock_aws():
# Initialize the mock S3 service
s3 = boto3.client("s3", region_name="us-east-1")
# Create a mock bucket
s3.create_bucket(Bucket="test-bucket")
yield s3


def test_compress_bz2(tmp_path):
Expand All @@ -11,10 +26,10 @@ def test_compress_bz2(tmp_path):
test_file = tmp_path / "test.txt"
test_file.write_text(
"""
This is a test file – it should compress well.
This is a test file – it should compress well.
This is a test file – it should compress well.
"""
This is a test file – it should compress well.
This is a test file – it should compress well.
This is a test file – it should compress well.
"""
)

# Compress the test file
Expand All @@ -25,3 +40,18 @@ def test_compress_bz2(tmp_path):
assert compressed_file.exists()
# Check that the compressed file is smaller than the original file
assert compressed_file.stat().st_size < test_file.stat().st_size


def test_upload_file_to_s3(tmp_path, s3_client):
"""Test the upload_file_to_s3 function."""
# Create a test file
test_file = tmp_path / "test.txt"
test_file.write_text("This is a test file.")

# Upload the test file to the mock S3 bucket
upload_file_to_s3(str(test_file), "test-bucket", "test.txt", client=s3_client)

# Check that the file exists in the mock S3 bucket
response = s3_client.list_objects_v2(Bucket="test-bucket")
assert "Contents" in response
assert any(obj["Key"] == "test.txt" for obj in response["Contents"])
Loading