Skip to content

Commit e16ff67

Browse files
authored
test(storage): fix S3 SDK tests (GoogleCloudPlatform#6074)
These tests are currently failing because credentials have expired. This change rewrites the tests to create their own HMAC Key rather than relying on keys in a separate project. Inspired by similar approach in GoogleCloudPlatform/golang-samples#1217 Fixes GoogleCloudPlatform#5906 Fixes GoogleCloudPlatform#5907 ## Checklist - [x] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md) - [x] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#readme-file) - [x] **Tests** pass: `nox -s py-3.6` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [x] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/AUTHORING_GUIDE.md#test-environment-setup)) - [ ] These samples need a new **API enabled** in testing projects to pass (let us know which ones) - [x] These samples need a new/updated **env vars** in testing projects set to pass (let us know which ones) - [x] Please **merge** this PR for me once it is approved. - [ ] This sample adds a new sample directory, and I updated the [CODEOWNERS file](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/.github/CODEOWNERS) with the codeowners for this sample
1 parent c60df89 commit e16ff67

File tree

6 files changed

+145
-65
lines changed

6 files changed

+145
-65
lines changed

storage/s3-sdk/list_gcs_buckets_test.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

storage/s3-sdk/list_gcs_objects_test.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

storage/s3-sdk/noxfile_config.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import os
17+
18+
19+
# We are reaching maximum number of HMAC keys on the service account.
20+
# We change the service account based on the value of
21+
# RUN_TESTS_SESSION. The reason we can not use multiple project is
22+
# that our new projects are enforced to have
23+
# 'constraints/iam.disableServiceAccountKeyCreation' policy.
24+
def get_service_account_email():
25+
session = os.environ.get('RUN_TESTS_SESSION')
26+
if session == 'py-3.6':
27+
return ('py36-storage-test@'
28+
'python-docs-samples-tests.iam.gserviceaccount.com')
29+
if session == 'py-3.7':
30+
return ('py37-storage-test@'
31+
'python-docs-samples-tests.iam.gserviceaccount.com')
32+
if session == 'py-3.8':
33+
return ('py38-storage-test@'
34+
'python-docs-samples-tests.iam.gserviceaccount.com')
35+
return os.environ['HMAC_KEY_TEST_SERVICE_ACCOUNT']
36+
37+
38+
TEST_CONFIG_OVERRIDE = {
39+
# A dictionary you want to inject into your test. Don't put any
40+
# secrets here. These values will override predefined values.
41+
'envs': {
42+
'HMAC_KEY_TEST_SERVICE_ACCOUNT': get_service_account_email(),
43+
# Some tests can not use multiple projects because of several reasons:
44+
# 1. The new projects is enforced to have the
45+
# 'constraints/iam.disableServiceAccountKeyCreation' policy.
46+
# 2. The new projects buckets need to have universal permission model.
47+
# For those tests, we'll use the original project.
48+
'MAIN_GOOGLE_CLOUD_PROJECT': 'python-docs-samples-tests'
49+
},
50+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
backoff==1.10.0
12
pytest==6.2.4
3+
google-cloud-storage==1.38.0

storage/s3-sdk/s3_sdk_test.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Copyright 2021 Google, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
import backoff
19+
from botocore.exceptions import ClientError
20+
from google.cloud import storage
21+
import pytest
22+
23+
import list_gcs_buckets
24+
import list_gcs_objects
25+
26+
27+
PROJECT_ID = os.environ["MAIN_GOOGLE_CLOUD_PROJECT"]
28+
SERVICE_ACCOUNT_EMAIL = os.environ["HMAC_KEY_TEST_SERVICE_ACCOUNT"]
29+
STORAGE_CLIENT = storage.Client(project=PROJECT_ID)
30+
31+
32+
@pytest.fixture(scope="module")
33+
def hmac_fixture():
34+
"""
35+
Creates an HMAC Key and secret to supply to the S3 SDK tests. The key
36+
will be deleted after the test session.
37+
"""
38+
hmac_key, secret = STORAGE_CLIENT.create_hmac_key(
39+
service_account_email=SERVICE_ACCOUNT_EMAIL, project_id=PROJECT_ID
40+
)
41+
yield hmac_key, secret
42+
hmac_key.state = "INACTIVE"
43+
hmac_key.update()
44+
hmac_key.delete()
45+
46+
47+
@pytest.fixture(scope="module")
48+
def test_bucket():
49+
"""Yields a bucket that is deleted after the test completes."""
50+
bucket = None
51+
while bucket is None or bucket.exists():
52+
bucket_name = "storage-snippets-test-{}".format(uuid.uuid4())
53+
bucket = storage.Client().bucket(bucket_name)
54+
bucket.create()
55+
yield bucket
56+
bucket.delete(force=True)
57+
58+
59+
@pytest.fixture(scope="module")
60+
def test_blob(test_bucket):
61+
"""Yields a blob that is deleted after the test completes."""
62+
bucket = test_bucket
63+
blob = bucket.blob("storage_snippets_test_sigil-{}".format(uuid.uuid4()))
64+
blob.upload_from_string("Hello, is it me you're looking for?")
65+
yield blob
66+
67+
68+
def test_list_buckets(capsys, hmac_fixture, test_bucket):
69+
# Retry request because the created key may not be fully propagated for up
70+
# to 15s.
71+
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
72+
def list_buckets():
73+
list_gcs_buckets.list_gcs_buckets(
74+
google_access_key_id=hmac_fixture[0].access_id, google_access_key_secret=hmac_fixture[1]
75+
)
76+
out, _ = capsys.readouterr()
77+
assert "Buckets:" in out
78+
assert test_bucket.name in out
79+
80+
81+
def test_list_blobs(capsys, hmac_fixture, test_bucket, test_blob):
82+
# Retry request because the created key may not be fully propagated for up
83+
# to 15s.
84+
@backoff.on_exception(backoff.constant, ClientError, interval=1, max_time=15)
85+
def list_objects():
86+
list_gcs_objects.list_gcs_objects(
87+
google_access_key_id=hmac_fixture[0].access_id,
88+
google_access_key_secret=hmac_fixture[1],
89+
bucket_name=test_bucket.name,
90+
)
91+
out, _ = capsys.readouterr()
92+
assert "Objects:" in out
93+
assert test_blob.name in out

testing/test-env.tmpl.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@ export SLACK_TEST_SIGNATURE=
5050
export SLACK_SECRET=
5151
export FUNCTIONS_TOPIC=
5252

53-
# HMAC SA Credentials for S3 SDK samples
54-
export GOOGLE_CLOUD_PROJECT_S3_SDK=
55-
export STORAGE_HMAC_ACCESS_KEY_ID=
56-
export STORAGE_HMAC_ACCESS_SECRET_KEY=
57-
5853
# Service account for HMAC samples
5954
export HMAC_KEY_TEST_SERVICE_ACCOUNT=
6055

0 commit comments

Comments
 (0)