From b3e86185cca91069054ba539e4027b32634020f8 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sat, 20 Feb 2016 00:55:11 -0800 Subject: [PATCH 1/2] Introducing basic system test for Bigtable. --- system_tests/bigtable.py | 85 +++++++++++++++++++++++++++++++++ system_tests/run_system_test.py | 3 ++ 2 files changed, 88 insertions(+) create mode 100644 system_tests/bigtable.py diff --git a/system_tests/bigtable.py b/system_tests/bigtable.py new file mode 100644 index 000000000000..90a5d494e25d --- /dev/null +++ b/system_tests/bigtable.py @@ -0,0 +1,85 @@ +# Copyright 2016 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import time + +import unittest2 + +from gcloud import _helpers +from gcloud.bigtable.client import Client +from gcloud.environment_vars import TESTS_PROJECT + + +_helpers.PROJECT = TESTS_PROJECT +CENTRAL_1C_ZONE = 'us-central1-c' +NOW_MILLIS = int(1000 * time.time()) +CLUSTER_ID = 'gcloud-python-%d' % (NOW_MILLIS,) +EXISTING_CLUSTERS = [] +EXPECTED_ZONES = ( + 'asia-east1-b', + 'europe-west1-c', + 'us-central1-b', + CENTRAL_1C_ZONE, +) +CLIENT = Client(admin=True) +CLUSTER = CLIENT.cluster(CENTRAL_1C_ZONE, CLUSTER_ID, + display_name=CLUSTER_ID) + + +def setUpModule(): + CLIENT.start() + clusters, failed_zones = CLIENT.list_clusters() + + if len(failed_zones) != 0: + raise ValueError('List clusters failed in module set up.') + + EXISTING_CLUSTERS[:] = clusters + + # After listing, create the test cluster. + created_op = CLUSTER.create() + total_sleep = 0 + while not created_op.finished(): + if total_sleep > 5: + raise RuntimeError('Cluster creation exceed 5 seconds.') + time.sleep(1) + total_sleep += 1 + + +def tearDownModule(): + CLUSTER.delete() + CLIENT.stop() + + +class TestClusterAdminAPI(unittest2.TestCase): + + def setUp(self): + self.clusters_to_delete = [] + + def tearDown(self): + for cluster in self.clusters_to_delete: + cluster.delete() + + def test_list_zones(self): + zones = CLIENT.list_zones() + self.assertEqual(sorted(zones), list(EXPECTED_ZONES)) + + def test_list_clusters(self): + clusters, failed_zones = CLIENT.list_clusters() + self.assertEqual(failed_zones, []) + # We have added one new cluster in `setUpModule`. + self.assertEqual(len(clusters), len(EXISTING_CLUSTERS) + 1) + for cluster in clusters: + cluster_existence = (cluster in EXISTING_CLUSTERS or + cluster == CLUSTER) + self.assertTrue(cluster_existence) diff --git a/system_tests/run_system_test.py b/system_tests/run_system_test.py index 9d49c57b0c86..7c42cad88322 100644 --- a/system_tests/run_system_test.py +++ b/system_tests/run_system_test.py @@ -19,6 +19,7 @@ # This assumes the command is being run via tox hence the # repository root is the current directory. import bigquery +import bigtable import datastore import pubsub import storage @@ -30,12 +31,14 @@ 'storage': ['project', 'credentials'], 'pubsub': ['project', 'credentials'], 'bigquery': ['project', 'credentials'], + 'bigtable': ['project', 'credentials'], } TEST_MODULES = { 'datastore': datastore, 'storage': storage, 'pubsub': pubsub, 'bigquery': bigquery, + 'bigtable': bigtable, } From b22331142dcdb042866cd4060d020c7e83c40a70 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Sat, 20 Feb 2016 09:57:02 -0800 Subject: [PATCH 2/2] Review changes (will be squashed). - Using sorted() zone comparison - Creating global CLIENT and CLUSTER at run-time rather than at import-time --- system_tests/bigtable.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/system_tests/bigtable.py b/system_tests/bigtable.py index 90a5d494e25d..29cc70c8e464 100644 --- a/system_tests/bigtable.py +++ b/system_tests/bigtable.py @@ -32,14 +32,24 @@ 'us-central1-b', CENTRAL_1C_ZONE, ) -CLIENT = Client(admin=True) -CLUSTER = CLIENT.cluster(CENTRAL_1C_ZONE, CLUSTER_ID, - display_name=CLUSTER_ID) + + +class Config(object): + """Run-time configuration to be modified at set-up. + + This is a mutable stand-in to allow test set-up to modify + global state. + """ + CLIENT = None + CLUSTER = None def setUpModule(): - CLIENT.start() - clusters, failed_zones = CLIENT.list_clusters() + Config.CLIENT = Client(admin=True) + Config.CLUSTER = Config.CLIENT.cluster(CENTRAL_1C_ZONE, CLUSTER_ID, + display_name=CLUSTER_ID) + Config.CLIENT.start() + clusters, failed_zones = Config.CLIENT.list_clusters() if len(failed_zones) != 0: raise ValueError('List clusters failed in module set up.') @@ -47,7 +57,7 @@ def setUpModule(): EXISTING_CLUSTERS[:] = clusters # After listing, create the test cluster. - created_op = CLUSTER.create() + created_op = Config.CLUSTER.create() total_sleep = 0 while not created_op.finished(): if total_sleep > 5: @@ -57,8 +67,8 @@ def setUpModule(): def tearDownModule(): - CLUSTER.delete() - CLIENT.stop() + Config.CLUSTER.delete() + Config.CLIENT.stop() class TestClusterAdminAPI(unittest2.TestCase): @@ -71,15 +81,15 @@ def tearDown(self): cluster.delete() def test_list_zones(self): - zones = CLIENT.list_zones() - self.assertEqual(sorted(zones), list(EXPECTED_ZONES)) + zones = Config.CLIENT.list_zones() + self.assertEqual(sorted(zones), sorted(EXPECTED_ZONES)) def test_list_clusters(self): - clusters, failed_zones = CLIENT.list_clusters() + clusters, failed_zones = Config.CLIENT.list_clusters() self.assertEqual(failed_zones, []) # We have added one new cluster in `setUpModule`. self.assertEqual(len(clusters), len(EXISTING_CLUSTERS) + 1) for cluster in clusters: cluster_existence = (cluster in EXISTING_CLUSTERS or - cluster == CLUSTER) + cluster == Config.CLUSTER) self.assertTrue(cluster_existence)