Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 8 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
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
29 changes: 0 additions & 29 deletions 29 storage/s3-sdk/list_gcs_buckets_test.py

This file was deleted.

31 changes: 0 additions & 31 deletions 31 storage/s3-sdk/list_gcs_objects_test.py

This file was deleted.

50 changes: 50 additions & 0 deletions 50 storage/s3-sdk/noxfile_config.py
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'
},
}
2 changes: 2 additions & 0 deletions 2 storage/s3-sdk/requirements-test.txt
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
93 changes: 93 additions & 0 deletions 93 storage/s3-sdk/s3_sdk_test.py
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"]
Copy link
Contributor

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?

Copy link
Contributor Author

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.

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
5 changes: 0 additions & 5 deletions 5 testing/test-env.tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export SLACK_TEST_SIGNATURE=
export SLACK_SECRET=
export FUNCTIONS_TOPIC=

# HMAC SA Credentials for S3 SDK samples
export GOOGLE_CLOUD_PROJECT_S3_SDK=
export STORAGE_HMAC_ACCESS_KEY_ID=
export STORAGE_HMAC_ACCESS_SECRET_KEY=

# Service account for HMAC samples
export HMAC_KEY_TEST_SERVICE_ACCOUNT=

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.