forked from getmoto/moto
-
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.
Admin: Enable some tests to run against AWS (getmoto#6855)
- Loading branch information
Showing
4 changed files
with
124 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Execute tests against AWS | ||
on: | ||
schedule: | ||
- cron: '00 6 * * 0' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
id-token: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.11" | ||
- name: Get pip cache dir | ||
id: pip-cache | ||
run: | | ||
echo "dir=$(pip cache dir)" >> $GITHUB_OUTPUT | ||
- name: pip cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: ${{ steps.pip-cache.outputs.dir }} | ||
key: pip-3.11-${{ hashFiles('**/setup.cfg') }} | ||
- name: Update pip | ||
run: | | ||
python -m pip install --upgrade pip | ||
- name: Install project dependencies | ||
run: | | ||
pip install -r requirements-dev.txt | ||
- name: Configure AWS | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
aws-region: us-east-1 | ||
role-to-assume: arn:aws:iam::682283128318:role/GithubActionsRole | ||
- name: Test with pytest | ||
env: | ||
MOTO_TEST_ALLOW_AWS_REQUEST: ${{ true }} | ||
run: | | ||
pytest -sv tests/test_s3 -m aws_verified |
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,69 @@ | ||
import boto3 | ||
import os | ||
from functools import wraps | ||
from moto import mock_s3 | ||
from moto.s3.responses import DEFAULT_REGION_NAME | ||
from uuid import uuid4 | ||
|
||
|
||
def s3_aws_verified(func): | ||
""" | ||
Function that is verified to work against AWS. | ||
Can be run against AWS at any time by setting: | ||
MOTO_TEST_ALLOW_AWS_REQUEST=true | ||
If this environment variable is not set, the function runs in a `mock_s3` context. | ||
This decorator will: | ||
- Create a bucket | ||
- Run the test and pass the bucket_name as an argument | ||
- Delete the objects and the bucket itself | ||
""" | ||
|
||
@wraps(func) | ||
def pagination_wrapper(): | ||
client = boto3.client("s3", region_name=DEFAULT_REGION_NAME) | ||
bucket_name = str(uuid4()) | ||
|
||
allow_aws_request = ( | ||
os.environ.get("MOTO_TEST_ALLOW_AWS_REQUEST", "false").lower() == "true" | ||
) | ||
|
||
if allow_aws_request: | ||
print(f"Test {func} will create {bucket_name}") | ||
resp = create_bucket_and_test(bucket_name, client) | ||
else: | ||
with mock_s3(): | ||
resp = create_bucket_and_test(bucket_name, client) | ||
return resp | ||
|
||
def create_bucket_and_test(bucket_name, client): | ||
client.create_bucket(Bucket=bucket_name) | ||
client.put_bucket_tagging( | ||
Bucket=bucket_name, | ||
Tagging={"TagSet": [{"Key": "environment", "Value": "moto_tests"}]}, | ||
) | ||
try: | ||
resp = func(bucket_name) | ||
finally: | ||
### CLEANUP ### | ||
|
||
versions = client.list_object_versions(Bucket=bucket_name).get( | ||
"Versions", [] | ||
) | ||
for key in versions: | ||
client.delete_object( | ||
Bucket=bucket_name, Key=key["Key"], VersionId=key.get("VersionId") | ||
) | ||
delete_markers = client.list_object_versions(Bucket=bucket_name).get( | ||
"DeleteMarkers", [] | ||
) | ||
for key in delete_markers: | ||
client.delete_object( | ||
Bucket=bucket_name, Key=key["Key"], VersionId=key.get("VersionId") | ||
) | ||
client.delete_bucket(Bucket=bucket_name) | ||
|
||
return resp | ||
|
||
return pagination_wrapper |
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