-
Notifications
You must be signed in to change notification settings - Fork 6.6k
test(storage): fix S3 SDK tests #6074
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d0ed113
test(storage): fix S3 SDK tests
tritone 787241d
Merge branch 'master' into s3-key
tritone a9d595b
Add noxfile override for env vars
tritone 2d5b15b
add retry
tritone b39889c
Add retry for other test
tritone 84bd69b
lint
tritone ee75529
Merge branch 'master' into s3-key
tritone 6739a9f
Merge branch 'master' into s3-key
leahecole File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,50 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import os | ||
|
||
|
||
# We are reaching maximum number of HMAC keys on the service account. | ||
# We change the service account based on the value of | ||
# RUN_TESTS_SESSION. The reason we can not use multiple project is | ||
# that our new projects are enforced to have | ||
# 'constraints/iam.disableServiceAccountKeyCreation' policy. | ||
def get_service_account_email(): | ||
session = os.environ.get('RUN_TESTS_SESSION') | ||
if session == 'py-3.6': | ||
return ('py36-storage-test@' | ||
'python-docs-samples-tests.iam.gserviceaccount.com') | ||
if session == 'py-3.7': | ||
return ('py37-storage-test@' | ||
'python-docs-samples-tests.iam.gserviceaccount.com') | ||
if session == 'py-3.8': | ||
return ('py38-storage-test@' | ||
'python-docs-samples-tests.iam.gserviceaccount.com') | ||
return os.environ['HMAC_KEY_TEST_SERVICE_ACCOUNT'] | ||
|
||
|
||
TEST_CONFIG_OVERRIDE = { | ||
# A dictionary you want to inject into your test. Don't put any | ||
# secrets here. These values will override predefined values. | ||
'envs': { | ||
'HMAC_KEY_TEST_SERVICE_ACCOUNT': get_service_account_email(), | ||
# Some tests can not use multiple projects because of several reasons: | ||
# 1. The new projects is enforced to have the | ||
# 'constraints/iam.disableServiceAccountKeyCreation' policy. | ||
# 2. The new projects buckets need to have universal permission model. | ||
# For those tests, we'll use the original project. | ||
'MAIN_GOOGLE_CLOUD_PROJECT': 'python-docs-samples-tests' | ||
}, | ||
} |
This file contains hidden or 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 +1,3 @@ | ||
backoff==1.10.0 | ||
pytest==6.2.4 | ||
google-cloud-storage==1.38.0 |
This file contains hidden or 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,93 @@ | ||
# Copyright 2021 Google, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import uuid | ||
|
||
import backoff | ||
from botocore.exceptions import ClientError | ||
from google.cloud import storage | ||
import pytest | ||
|
||
import list_gcs_buckets | ||
import list_gcs_objects | ||
|
||
|
||
PROJECT_ID = os.environ["MAIN_GOOGLE_CLOUD_PROJECT"] | ||
SERVICE_ACCOUNT_EMAIL = os.environ["HMAC_KEY_TEST_SERVICE_ACCOUNT"] | ||
STORAGE_CLIENT = storage.Client(project=PROJECT_ID) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def hmac_fixture(): | ||
""" | ||
Creates an HMAC Key and secret to supply to the S3 SDK tests. The key | ||
will be deleted after the test session. | ||
""" | ||
hmac_key, secret = STORAGE_CLIENT.create_hmac_key( | ||
service_account_email=SERVICE_ACCOUNT_EMAIL, project_id=PROJECT_ID | ||
) | ||
yield hmac_key, secret | ||
hmac_key.state = "INACTIVE" | ||
hmac_key.update() | ||
hmac_key.delete() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def test_bucket(): | ||
"""Yields a bucket that is deleted after the test completes.""" | ||
bucket = None | ||
while bucket is None or bucket.exists(): | ||
bucket_name = "storage-snippets-test-{}".format(uuid.uuid4()) | ||
bucket = storage.Client().bucket(bucket_name) | ||
bucket.create() | ||
yield bucket | ||
bucket.delete(force=True) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def test_blob(test_bucket): | ||
"""Yields a blob that is deleted after the test completes.""" | ||
bucket = test_bucket | ||
blob = bucket.blob("storage_snippets_test_sigil-{}".format(uuid.uuid4())) | ||
blob.upload_from_string("Hello, is it me you're looking for?") | ||
yield blob | ||
|
||
|
||
def test_list_buckets(capsys, hmac_fixture, test_bucket): | ||
# Retry request because the created key may not be fully propagated for up | ||
# to 15s. | ||
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15) | ||
def list_buckets(): | ||
list_gcs_buckets.list_gcs_buckets( | ||
google_access_key_id=hmac_fixture[0].access_id, google_access_key_secret=hmac_fixture[1] | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Buckets:" in out | ||
assert test_bucket.name in out | ||
|
||
|
||
def test_list_blobs(capsys, hmac_fixture, test_bucket, test_blob): | ||
# Retry request because the created key may not be fully propagated for up | ||
# to 15s. | ||
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15) | ||
def list_objects(): | ||
list_gcs_objects.list_gcs_objects( | ||
google_access_key_id=hmac_fixture[0].access_id, | ||
google_access_key_secret=hmac_fixture[1], | ||
bucket_name=test_bucket.name, | ||
) | ||
out, _ = capsys.readouterr() | ||
assert "Objects:" in out | ||
assert test_blob.name in out |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
_, project_id = google.auth.default()
be used to detect the project?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What project will that return? We need to use
MAIN_GOOGLE_CLOUD_PROJECT
rather than the standard one because we need a permissions exception to have HMAC keys.