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: replace spurious googleapis exception w/ api_core #636

Merged
merged 1 commit into from
Oct 25, 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
36 changes: 16 additions & 20 deletions samples/snippets/acl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import uuid

import backoff
from google.api_core.exceptions import GoogleAPIError
from google.cloud import storage
from googleapiclient.errors import HttpError
import pytest

import storage_add_bucket_default_owner
Expand All @@ -33,10 +33,7 @@

# Typically we'd use a @example.com address, but GCS requires a real Google
# account. Retrieve a service account email with storage admin permissions.
TEST_EMAIL = (
"py38-storage-test"
"@python-docs-samples-tests.iam.gserviceaccount.com"
)
TEST_EMAIL = "py38-storage-test" "@python-docs-samples-tests.iam.gserviceaccount.com"


@pytest.fixture(scope="module")
Expand All @@ -45,8 +42,8 @@ def test_bucket():

# The new projects have uniform bucket-level access and our tests don't
# pass with those buckets. We need to use the old main project for now.
original_value = os.environ['GOOGLE_CLOUD_PROJECT']
os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['MAIN_GOOGLE_CLOUD_PROJECT']
original_value = os.environ["GOOGLE_CLOUD_PROJECT"]
os.environ["GOOGLE_CLOUD_PROJECT"] = os.environ["MAIN_GOOGLE_CLOUD_PROJECT"]
bucket = None
while bucket is None or bucket.exists():
bucket_name = "acl-test-{}".format(uuid.uuid4())
Expand All @@ -55,7 +52,7 @@ def test_bucket():
yield bucket
bucket.delete(force=True)
# Set the value back.
os.environ['GOOGLE_CLOUD_PROJECT'] = original_value
os.environ["GOOGLE_CLOUD_PROJECT"] = original_value


@pytest.fixture
Expand Down Expand Up @@ -85,27 +82,26 @@ def test_print_bucket_acl_for_user(test_bucket, capsys):
assert "OWNER" in out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_add_bucket_owner(test_bucket):
storage_add_bucket_owner.add_bucket_owner(test_bucket.name, TEST_EMAIL)

test_bucket.acl.reload()
assert "OWNER" in test_bucket.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_remove_bucket_owner(test_bucket):
test_bucket.acl.user(TEST_EMAIL).grant_owner()
test_bucket.acl.save()

storage_remove_bucket_owner.remove_bucket_owner(
test_bucket.name, TEST_EMAIL)
storage_remove_bucket_owner.remove_bucket_owner(test_bucket.name, TEST_EMAIL)

test_bucket.acl.reload()
assert "OWNER" not in test_bucket.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_add_bucket_default_owner(test_bucket):
storage_add_bucket_default_owner.add_bucket_default_owner(
test_bucket.name, TEST_EMAIL
Expand All @@ -116,7 +112,7 @@ def test_add_bucket_default_owner(test_bucket):
assert "OWNER" in roles


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_remove_bucket_default_owner(test_bucket):
test_bucket.acl.user(TEST_EMAIL).grant_owner()
test_bucket.acl.save()
Expand All @@ -131,13 +127,12 @@ def test_remove_bucket_default_owner(test_bucket):


def test_print_blob_acl(test_blob, capsys):
storage_print_file_acl.print_blob_acl(
test_blob.bucket.name, test_blob.name)
storage_print_file_acl.print_blob_acl(test_blob.bucket.name, test_blob.name)
out, _ = capsys.readouterr()
assert out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_print_blob_acl_for_user(test_blob, capsys):
test_blob.acl.user(TEST_EMAIL).grant_owner()
test_blob.acl.save()
Expand All @@ -150,16 +145,17 @@ def test_print_blob_acl_for_user(test_blob, capsys):
assert "OWNER" in out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_add_blob_owner(test_blob):
storage_add_file_owner.add_blob_owner(
test_blob.bucket.name, test_blob.name, TEST_EMAIL)
test_blob.bucket.name, test_blob.name, TEST_EMAIL
)

test_blob.acl.reload()
assert "OWNER" in test_blob.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
def test_remove_blob_owner(test_blob):
test_blob.acl.user(TEST_EMAIL).grant_owner()
test_blob.acl.save()
Expand Down
1 change: 0 additions & 1 deletion samples/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
google-cloud-pubsub==2.8.0
google-cloud-storage==1.42.3
google-api-python-client==2.25.0