@@ -44,6 +44,26 @@ class Config(object):
4444 CLUSTER = None
4545
4646
47+ def _operation_wait (operation , max_attempts = 5 ):
48+ """Wait until an operation has completed.
49+
50+ :type operation: :class:`gcloud.bigtable.cluster.Operation`
51+ :param operation: Operation that has not finished.
52+
53+ :type max_attempts: int
54+ :param max_attempts: (Optional) The maximum number of times to check if
55+ the operation has finished. Defaults to 5.
56+ """
57+ total_sleep = 0
58+ while not operation .finished ():
59+ if total_sleep > max_attempts :
60+ return False
61+ time .sleep (1 )
62+ total_sleep += 1
63+
64+ return True
65+
66+
4767def setUpModule ():
4868 Config .CLIENT = Client (admin = True )
4969 Config .CLUSTER = Config .CLIENT .cluster (CENTRAL_1C_ZONE , CLUSTER_ID ,
@@ -58,12 +78,8 @@ def setUpModule():
5878
5979 # After listing, create the test cluster.
6080 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
81+ if not _operation_wait (created_op ):
82+ raise RuntimeError ('Cluster creation exceed 5 seconds.' )
6783
6884
6985def tearDownModule ():
@@ -93,3 +109,57 @@ def test_list_clusters(self):
93109 cluster_existence = (cluster in EXISTING_CLUSTERS or
94110 cluster == Config .CLUSTER )
95111 self .assertTrue (cluster_existence )
112+
113+ def test_reload (self ):
114+ # Use same arguments as Config.CLUSTER (created in `setUpModule`)
115+ # so we can use reload() on a fresh instance.
116+ cluster = Config .CLIENT .cluster (CENTRAL_1C_ZONE , CLUSTER_ID )
117+ # Make sure metadata unset before reloading.
118+ cluster .display_name = None
119+ cluster .serve_nodes = None
120+
121+ cluster .reload ()
122+ self .assertEqual (cluster .display_name , Config .CLUSTER .display_name )
123+ self .assertEqual (cluster .serve_nodes , Config .CLUSTER .serve_nodes )
124+
125+ def test_create_cluster (self ):
126+ cluster_id = '%s-a' % (CLUSTER_ID ,)
127+ cluster = Config .CLIENT .cluster (CENTRAL_1C_ZONE , cluster_id )
128+ operation = cluster .create ()
129+ # Make sure this cluster gets deleted after the test case.
130+ self .clusters_to_delete .append (cluster )
131+
132+ # We want to make sure the operation completes.
133+ self .assertTrue (_operation_wait (operation ))
134+
135+ # Create a new cluster instance and make sure it is the same.
136+ cluster_alt = Config .CLIENT .cluster (CENTRAL_1C_ZONE , cluster_id )
137+ cluster_alt .reload ()
138+
139+ self .assertEqual (cluster , cluster_alt )
140+ self .assertEqual (cluster .display_name , cluster_alt .display_name )
141+ self .assertEqual (cluster .serve_nodes , cluster_alt .serve_nodes )
142+
143+ def test_update (self ):
144+ curr_display_name = Config .CLUSTER .display_name
145+ Config .CLUSTER .display_name = 'Foo Bar Baz'
146+ operation = Config .CLUSTER .update ()
147+
148+ # We want to make sure the operation completes.
149+ self .assertTrue (_operation_wait (operation ))
150+
151+ # Create a new cluster instance and make sure it is the same.
152+ cluster_alt = Config .CLIENT .cluster (CENTRAL_1C_ZONE , CLUSTER_ID )
153+ self .assertNotEqual (cluster_alt .display_name ,
154+ Config .CLUSTER .display_name )
155+ cluster_alt .reload ()
156+ self .assertEqual (cluster_alt .display_name ,
157+ Config .CLUSTER .display_name )
158+
159+ # Make sure to put the cluster back the way it was for the
160+ # other test cases.
161+ Config .CLUSTER .display_name = curr_display_name
162+ operation = Config .CLUSTER .update ()
163+
164+ # We want to make sure the operation completes.
165+ self .assertTrue (_operation_wait (operation ))
0 commit comments