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

Extend test of 'requester_pays' bucket to include RUD w/ 'user_project' set #4084

Merged
merged 3 commits into from
Sep 29, 2017
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
8 changes: 6 additions & 2 deletions storage/google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def current_batch(self):
"""
return self._batch_stack.top

def bucket(self, bucket_name):
def bucket(self, bucket_name, user_project=None):
"""Factory constructor for bucket object.

.. note::
Expand All @@ -131,10 +131,14 @@ def bucket(self, bucket_name):
:type bucket_name: str
:param bucket_name: The name of the bucket to be instantiated.

:type user_project: str
:param user_project: (Optional) the project ID to be billed for API
requests made via this instance.

:rtype: :class:`google.cloud.storage.bucket.Bucket`
:returns: The bucket object created.
"""
return Bucket(client=self, name=bucket_name)
return Bucket(client=self, name=bucket_name, user_project=user_project)

def batch(self):
"""Factory constructor for batch object.
Expand Down
2 changes: 1 addition & 1 deletion storage/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def system_tests(session, python_version):
session.install('.')

# Run py.test against the system tests.
session.run('py.test', '--quiet', 'tests/system.py')
session.run('py.test', '--quiet', 'tests/system.py', *session.posargs)


@nox.session
Expand Down
61 changes: 49 additions & 12 deletions storage/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
from test_utils.system import unique_resource_id


USER_PROJECT = os.environ.get('GOOGLE_CLOUD_TESTS_USER_PROJECT')


def _bad_copy(bad_request):
"""Predicate: pass only exceptions for a failed copyTo."""
err_msg = bad_request.message
Expand Down Expand Up @@ -83,10 +86,11 @@ def setUp(self):
self.case_buckets_to_delete = []

def tearDown(self):
with Config.CLIENT.batch():
for bucket_name in self.case_buckets_to_delete:
bucket = Config.CLIENT.bucket(bucket_name)
retry_429(bucket.delete)()
if self.case_buckets_to_delete:
with Config.CLIENT.batch():
for bucket_name in self.case_buckets_to_delete:
bucket = Config.CLIENT.bucket(bucket_name)
retry_429(bucket.delete)()

def test_create_bucket(self):
new_bucket_name = 'a-new-bucket' + unique_resource_id('-')
Expand All @@ -96,14 +100,6 @@ def test_create_bucket(self):
self.case_buckets_to_delete.append(new_bucket_name)
self.assertEqual(created.name, new_bucket_name)

def test_create_bucket_with_requester_pays(self):
new_bucket_name = 'w-requester-pays' + unique_resource_id('-')
created = Config.CLIENT.create_bucket(
new_bucket_name, requester_pays=True)
self.case_buckets_to_delete.append(new_bucket_name)
self.assertEqual(created.name, new_bucket_name)
self.assertTrue(created.requester_pays)

def test_list_buckets(self):
buckets_to_create = [
'new' + unique_resource_id(),
Expand Down Expand Up @@ -142,6 +138,47 @@ def test_bucket_update_labels(self):
bucket.update()
self.assertEqual(bucket.labels, {})

@unittest.skipUnless(USER_PROJECT, 'USER_PROJECT not set in environment.')

This comment was marked as spam.

This comment was marked as spam.

def test_crud_bucket_with_requester_pays(self):
new_bucket_name = 'w-requester-pays' + unique_resource_id('-')
created = Config.CLIENT.create_bucket(
new_bucket_name, requester_pays=True)
self.case_buckets_to_delete.append(new_bucket_name)
self.assertEqual(created.name, new_bucket_name)
self.assertTrue(created.requester_pays)

with_up = Config.CLIENT.bucket(
new_bucket_name, user_project=USER_PROJECT)

# Bucket will be deleted in-line below.
self.case_buckets_to_delete.remove(new_bucket_name)

try:
# Exercise 'buckets.get' w/ userProject.
self.assertTrue(with_up.exists())
with_up.reload()
self.assertTrue(with_up.requester_pays)

# Exercise 'buckets.patch' w/ userProject.
with_up.configure_website(
main_page_suffix='index.html', not_found_page='404.html')
with_up.patch()
self.assertEqual(
with_up._properties['website'], {
'mainPageSuffix': 'index.html',
'notFoundPage': '404.html',
})

# Exercise 'buckets.update' w/ userProject.
new_labels = {'another-label': 'another-value'}
with_up.labels = new_labels
with_up.update()
self.assertEqual(with_up.labels, new_labels)

finally:
# Exercise 'buckets.delete' w/ userProject.
with_up.delete()


class TestStorageFiles(unittest.TestCase):

Expand Down
16 changes: 16 additions & 0 deletions storage/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ def test_bucket(self):
self.assertIsInstance(bucket, Bucket)
self.assertIs(bucket.client, client)
self.assertEqual(bucket.name, BUCKET_NAME)
self.assertIsNone(bucket.user_project)

def test_bucket_w_user_project(self):
from google.cloud.storage.bucket import Bucket

PROJECT = 'PROJECT'
USER_PROJECT = 'USER_PROJECT'
CREDENTIALS = _make_credentials()
BUCKET_NAME = 'BUCKET_NAME'

client = self._make_one(project=PROJECT, credentials=CREDENTIALS)
bucket = client.bucket(BUCKET_NAME, user_project=USER_PROJECT)
self.assertIsInstance(bucket, Bucket)
self.assertIs(bucket.client, client)
self.assertEqual(bucket.name, BUCKET_NAME)
self.assertEqual(bucket.user_project, USER_PROJECT)

def test_batch(self):
from google.cloud.storage.batch import Batch
Expand Down