Skip to content

Commit

Permalink
Extend test of 'requester_pays' bucket to include CRUD w/ 'user_proje…
Browse files Browse the repository at this point in the history
…ct' set (#4084)

* Pass through extra posargs for system tests.

* Plumb 'user_project' arg through 'Client.bucket'.
  • Loading branch information
tseaver committed Oct 10, 2017
1 parent aa86fd4 commit f7f08e4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
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
52 changes: 44 additions & 8 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 @@ -95,14 +98,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 @@ -141,6 +136,47 @@ def test_bucket_update_labels(self):
bucket.update()
self.assertEqual(bucket.labels, {})

@unittest.skipUnless(USER_PROJECT, 'USER_PROJECT not set in environment.')
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

0 comments on commit f7f08e4

Please sign in to comment.