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

tests: add unit-tests for calculate_etag() #1015

Merged
Changes from all 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
42 changes: 42 additions & 0 deletions tests/unit/module_utils/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,52 @@
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

from ansible_collections.amazon.aws.tests.unit.compat.mock import MagicMock
from ansible_collections.amazon.aws.plugins.module_utils import s3
from ansible.module_utils.basic import AnsibleModule

import pytest


class FakeAnsibleModule(AnsibleModule):
def __init__(self):
pass


def test_calculate_etag_single_part(tmp_path_factory):
module = FakeAnsibleModule()
my_image = tmp_path_factory.mktemp("data") / "my.txt"
my_image.write_text("Hello World!")

etag = s3.calculate_etag(
module, str(my_image), etag="", s3=None, bucket=None, obj=None
)
assert etag == '"ed076287532e86365e841e92bfc50d8c"'


def test_calculate_etag_multi_part(tmp_path_factory):
module = FakeAnsibleModule()
my_image = tmp_path_factory.mktemp("data") / "my.txt"
my_image.write_text("Hello World!" * 1000)

mocked_s3 = MagicMock()
mocked_s3.head_object.side_effect = [{"ContentLength": "1000"} for _i in range(12)]

etag = s3.calculate_etag(
module,
str(my_image),
etag='"f20e84ac3d0c33cea77b3f29e3323a09-12"',
s3=mocked_s3,
bucket="my-bucket",
obj="my-obj",
)
assert etag == '"f20e84ac3d0c33cea77b3f29e3323a09-12"'
mocked_s3.head_object.assert_called_with(
Bucket="my-bucket", Key="my-obj", PartNumber=12
)


def test_validate_bucket_name():
Expand Down