Skip to content

Commit

Permalink
Add mock unittest for S3LogStore
Browse files Browse the repository at this point in the history
Add boto3 to test dependencies
  • Loading branch information
manics committed Oct 10, 2020
1 parent 52c732b commit d69ca94
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# update these accordingly

[dev-packages]
boto3 = "*"
pytest=">=4.6"
wheel="*"
pytest-cov="*"
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Note that there is also a Pipfile for this project if you are updating this
# file do not forget to update the Pipfile accordingly
boto3
pyyaml
pytest>=4.6
wheel
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/test_logstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Test S3LogStore
"""
# botocore includes a stub
# https://botocore.amazonaws.com/v1/documentation/api/latest/reference/stubber.html
# but it doesn't work with upload_file so use mock instead
# https://sgillies.net/2017/10/19/mock-is-magic.html
from unittest.mock import patch
from repo2docker import logstore


@patch("repo2docker.logstore.boto3")
def test_s3logstore_upload(boto3):
store = logstore.S3LogStore(
endpoint="http://localhost:9000",
access_key="access",
secret_key="secret",
bucket="bucket",
keyprefix="prefix/",
logname="test/build.log",
)

store.write("hello\n")
r = store.close()

boto3.resource.assert_called_with(
"s3",
config=boto3.session.Config(signature_version="s3v4"),
endpoint_url="http://localhost:9000",
aws_access_key_id="access",
aws_secret_access_key="secret",
region_name="",
)
boto3.resource().Bucket.assert_called_with("bucket")
boto3.resource().Bucket().upload_file.assert_called_with(
store._logfile.name,
"prefix/test/build.log",
ExtraArgs={"ContentType": "text/plain", "ACL": "public-read"},
)


@patch("repo2docker.logstore.boto3")
def test_s3logstore_empty(boto3):
store = logstore.S3LogStore(
endpoint="http://localhost:9000",
access_key="access",
secret_key="secret",
bucket="bucket",
keyprefix="prefix/",
logname="test/build.log",
)

r = store.close()

assert not boto3.resource.called
assert not boto3.resource().Bucket.called
assert not boto3.resource().Bucket().upload_file.called

0 comments on commit d69ca94

Please sign in to comment.