|
| 1 | +# Copyright 2016 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import time |
| 16 | + |
| 17 | +import unittest2 |
| 18 | + |
| 19 | +from gcloud import _helpers |
| 20 | +from gcloud.bigtable.client import Client |
| 21 | +from gcloud.environment_vars import TESTS_PROJECT |
| 22 | + |
| 23 | + |
| 24 | +_helpers.PROJECT = TESTS_PROJECT |
| 25 | +CENTRAL_1C_ZONE = 'us-central1-c' |
| 26 | +NOW_MILLIS = int(1000 * time.time()) |
| 27 | +CLUSTER_ID = 'gcloud-python-%d' % (NOW_MILLIS,) |
| 28 | +EXISTING_CLUSTERS = [] |
| 29 | +EXPECTED_ZONES = ( |
| 30 | + 'asia-east1-b', |
| 31 | + 'europe-west1-c', |
| 32 | + 'us-central1-b', |
| 33 | + CENTRAL_1C_ZONE, |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +class Config(object): |
| 38 | + """Run-time configuration to be modified at set-up. |
| 39 | +
|
| 40 | + This is a mutable stand-in to allow test set-up to modify |
| 41 | + global state. |
| 42 | + """ |
| 43 | + CLIENT = None |
| 44 | + CLUSTER = None |
| 45 | + |
| 46 | + |
| 47 | +def setUpModule(): |
| 48 | + Config.CLIENT = Client(admin=True) |
| 49 | + Config.CLUSTER = Config.CLIENT.cluster(CENTRAL_1C_ZONE, CLUSTER_ID, |
| 50 | + display_name=CLUSTER_ID) |
| 51 | + Config.CLIENT.start() |
| 52 | + clusters, failed_zones = Config.CLIENT.list_clusters() |
| 53 | + |
| 54 | + if len(failed_zones) != 0: |
| 55 | + raise ValueError('List clusters failed in module set up.') |
| 56 | + |
| 57 | + EXISTING_CLUSTERS[:] = clusters |
| 58 | + |
| 59 | + # After listing, create the test cluster. |
| 60 | + created_op = Config.CLUSTER.create() |
| 61 | + total_sleep = 0 |
| 62 | + while not created_op.finished(): |
| 63 | + if total_sleep > 5: |
| 64 | + raise RuntimeError('Cluster creation exceed 5 seconds.') |
| 65 | + time.sleep(1) |
| 66 | + total_sleep += 1 |
| 67 | + |
| 68 | + |
| 69 | +def tearDownModule(): |
| 70 | + Config.CLUSTER.delete() |
| 71 | + Config.CLIENT.stop() |
| 72 | + |
| 73 | + |
| 74 | +class TestClusterAdminAPI(unittest2.TestCase): |
| 75 | + |
| 76 | + def setUp(self): |
| 77 | + self.clusters_to_delete = [] |
| 78 | + |
| 79 | + def tearDown(self): |
| 80 | + for cluster in self.clusters_to_delete: |
| 81 | + cluster.delete() |
| 82 | + |
| 83 | + def test_list_zones(self): |
| 84 | + zones = Config.CLIENT.list_zones() |
| 85 | + self.assertEqual(sorted(zones), sorted(EXPECTED_ZONES)) |
| 86 | + |
| 87 | + def test_list_clusters(self): |
| 88 | + clusters, failed_zones = Config.CLIENT.list_clusters() |
| 89 | + self.assertEqual(failed_zones, []) |
| 90 | + # We have added one new cluster in `setUpModule`. |
| 91 | + self.assertEqual(len(clusters), len(EXISTING_CLUSTERS) + 1) |
| 92 | + for cluster in clusters: |
| 93 | + cluster_existence = (cluster in EXISTING_CLUSTERS or |
| 94 | + cluster == Config.CLUSTER) |
| 95 | + self.assertTrue(cluster_existence) |
0 commit comments