Skip to content

Commit

Permalink
Uses implicit behavior in storage regression.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jan 27, 2015
1 parent 2d87f34 commit 04fc99a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
11 changes: 0 additions & 11 deletions regression/regression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
PROJECT_ID = os.getenv('GCLOUD_TESTS_PROJECT_ID')
DATASET_ID = os.getenv('GCLOUD_TESTS_DATASET_ID')
CREDENTIALS = os.getenv('GOOGLE_APPLICATION_CREDENTIALS')
CACHED_RETURN_VALS = {}

ENVIRON_ERROR_MSG = """\
To run the regression tests, you need to set some environment variables.
Expand All @@ -46,13 +45,3 @@ def get_environ(require_datastore=False, require_storage=False):
'project_id': PROJECT_ID,
'dataset_id': DATASET_ID,
}


def get_storage_connection():
environ = get_environ(require_storage=True)
project_id = environ['project_id']
key = ('get_storage_connection', project_id)
if key not in CACHED_RETURN_VALS:
# Cache return value for the environment.
CACHED_RETURN_VALS[key] = storage.get_connection(project_id)
return CACHED_RETURN_VALS[key]
31 changes: 13 additions & 18 deletions regression/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,25 @@
import unittest2

from gcloud import storage
# This assumes the command is being run via tox hence the
# repository root is the current directory.
from regression import regression_utils
from gcloud.storage import _implicit_environ


HTTP = httplib2.Http()
SHARED_BUCKETS = {}

storage._PROJECT_ENV_VAR_NAME = 'GCLOUD_TESTS_PROJECT_ID'
storage.set_defaults()

CONNECTION = _implicit_environ.CONNECTION


def setUpModule():
if 'test_bucket' not in SHARED_BUCKETS:
connection = regression_utils.get_storage_connection()
# %d rounds milliseconds to nearest integer.
bucket_name = 'new%d' % (1000 * time.time(),)
# In the **very** rare case the bucket name is reserved, this
# fails with a ConnectionError.
SHARED_BUCKETS['test_bucket'] = connection.create_bucket(bucket_name)
SHARED_BUCKETS['test_bucket'] = CONNECTION.create_bucket(bucket_name)


def safe_delete(bucket):
Expand All @@ -55,14 +57,7 @@ def tearDownModule():
safe_delete(bucket)


class TestStorage(unittest2.TestCase):

@classmethod
def setUpClass(cls):
cls.connection = regression_utils.get_storage_connection()


class TestStorageBuckets(TestStorage):
class TestStorageBuckets(unittest2.TestCase):

def setUp(self):
self.case_buckets_to_delete = []
Expand All @@ -74,8 +69,8 @@ def tearDown(self):
def test_create_bucket(self):
new_bucket_name = 'a-new-bucket'
self.assertRaises(storage.exceptions.NotFound,
self.connection.get_bucket, new_bucket_name)
created = self.connection.create_bucket(new_bucket_name)
CONNECTION.get_bucket, new_bucket_name)
created = CONNECTION.create_bucket(new_bucket_name)
self.case_buckets_to_delete.append(created)
self.assertEqual(created.name, new_bucket_name)

Expand All @@ -87,17 +82,17 @@ def test_get_buckets(self):
]
created_buckets = []
for bucket_name in buckets_to_create:
bucket = self.connection.create_bucket(bucket_name)
bucket = CONNECTION.create_bucket(bucket_name)
self.case_buckets_to_delete.append(bucket)

# Retrieve the buckets.
all_buckets = self.connection.get_all_buckets()
all_buckets = CONNECTION.get_all_buckets()
created_buckets = [bucket for bucket in all_buckets
if bucket.name in buckets_to_create]
self.assertEqual(len(created_buckets), len(buckets_to_create))


class TestStorageFiles(TestStorage):
class TestStorageFiles(unittest2.TestCase):

FILES = {
'logo': {
Expand Down

0 comments on commit 04fc99a

Please sign in to comment.