Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtest_instanceadmin.py
More file actions
Latest commit
180 lines (143 loc) · 6.05 KB
/
Copy pathtest_instanceadmin.py
File metadata and controls
180 lines (143 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Copyright 2018 Google Inc.
#
# 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.
importos
importrandom
importtime
importwarnings
importbackoff
fromgoogle.api_coreimportexceptions
fromgoogle.cloudimportbigtable
importpytest
importinstanceadmin
PROJECT=os.environ["GOOGLE_CLOUD_PROJECT"]
INSTANCE_ID_FORMAT="instanceadmin-{:03}-{}"
CLUSTER_ID_FORMAT="instanceadmin-{:03}"
ID_RANGE=1000
INSTANCE=INSTANCE_ID_FORMAT.format(random.randrange(ID_RANGE), int(time.time()))
CLUSTER1=CLUSTER_ID_FORMAT.format(random.randrange(ID_RANGE))
CLUSTER2=CLUSTER_ID_FORMAT.format(random.randrange(ID_RANGE))
@pytest.fixture(scope="module", autouse=True)
defpreclean():
"""In case any test instances weren't cleared out in a previous run.
Deletes any test instances that were created over an hour ago. Newer instances may
be being used by a concurrent test run.
"""
client=bigtable.Client(project=PROJECT, admin=True)
forinstanceinclient.list_instances()[0]:
ifinstance.instance_id.startswith("instanceadmin-"):
timestamp=instance.instance_id.split("-")[-1]
timestamp=int(timestamp)
iftime.time() -timestamp>3600:
warnings.warn(
f"Deleting leftover test instance: {instance.instance_id}"
)
instance.delete()
@pytest.fixture
defdispose_of():
instances= []
defdisposal(instance):
instances.append(instance)
yielddisposal
client=bigtable.Client(project=PROJECT, admin=True)
forinstance_idininstances:
instance=client.instance(instance_id)
ifinstance.exists():
instance.delete()
deftest_run_instance_operations(capsys, dispose_of):
dispose_of(INSTANCE)
instanceadmin.run_instance_operations(PROJECT, INSTANCE, CLUSTER1)
out=capsys.readouterr().out
assertf"Instance {INSTANCE} does not exist."inout
assert"Creating an instance"inout
assertf"Created instance: {INSTANCE}"inout
assert"Listing instances"inout
assertf"\n{INSTANCE}\n"inout
assertf"Name of instance: {INSTANCE}"inout
assert"Labels: {'prod-label': 'prod-label'}"inout
assert"Listing clusters..."inout
assertf"\n{CLUSTER1}\n"inout
instanceadmin.run_instance_operations(PROJECT, INSTANCE, CLUSTER1)
out=capsys.readouterr().out
assertf"Instance {INSTANCE} already exists."inout
assert"Listing instances"inout
assertf"\n{INSTANCE}\n"inout
assertf"Name of instance: {INSTANCE}"inout
assert"Labels: {'prod-label': 'prod-label'}"inout
assert"Listing clusters..."inout
assertf"\n{CLUSTER1}\n"inout
deftest_delete_instance(capsys, dispose_of):
fromconcurrent.futuresimportTimeoutError
@backoff.on_exception(backoff.expo, TimeoutError)
def_set_up_instance():
dispose_of(INSTANCE)
# Can't delete it, it doesn't exist
instanceadmin.delete_instance(PROJECT, INSTANCE)
out=capsys.readouterr().out
assert"Deleting instance"inout
assertf"Instance {INSTANCE} does not exist"inout
# Ok, create it then
instanceadmin.run_instance_operations(PROJECT, INSTANCE, CLUSTER1)
capsys.readouterr() # throw away output
_set_up_instance()
# Now delete it
instanceadmin.delete_instance(PROJECT, INSTANCE)
out=capsys.readouterr().out
assert"Deleting instance"inout
assertf"Deleted instance: {INSTANCE}"inout
deftest_add_and_delete_cluster(capsys, dispose_of):
fromconcurrent.futuresimportTimeoutError
@backoff.on_exception(backoff.expo, TimeoutError)
def_set_up_instance():
dispose_of(INSTANCE)
# This won't work, because the instance isn't created yet
instanceadmin.add_cluster(PROJECT, INSTANCE, CLUSTER2)
out=capsys.readouterr().out
assertf"Instance {INSTANCE} does not exist"inout
# Get the instance created
instanceadmin.run_instance_operations(PROJECT, INSTANCE, CLUSTER1)
capsys.readouterr() # throw away output
_set_up_instance()
# Add a cluster to that instance
# Avoid failing for "instance is currently being changed" by
# applying an exponential backoff
backoff_503=backoff.on_exception(backoff.expo, exceptions.ServiceUnavailable)
backoff_503(instanceadmin.add_cluster)(PROJECT, INSTANCE, CLUSTER2)
out=capsys.readouterr().out
assertf"Adding cluster to instance {INSTANCE}"inout
assert"Listing clusters..."inout
assertf"\n{CLUSTER1}\n"inout
assertf"Cluster created: {CLUSTER2}"inout
# Try to add the same cluster again, won't work
instanceadmin.add_cluster(PROJECT, INSTANCE, CLUSTER2)
out=capsys.readouterr().out
assert"Listing clusters..."inout
assertf"\n{CLUSTER1}\n"inout
assertf"\n{CLUSTER2}\n"inout
assertf"Cluster not created, as {CLUSTER2} already exists."
# Now delete it
instanceadmin.delete_cluster(PROJECT, INSTANCE, CLUSTER2)
out=capsys.readouterr().out
assert"Deleting cluster"inout
assertf"Cluster deleted: {CLUSTER2}"inout
# Verify deletion
instanceadmin.run_instance_operations(PROJECT, INSTANCE, CLUSTER1)
out=capsys.readouterr().out
assert"Listing clusters..."inout
assertf"\n{CLUSTER1}\n"inout
assertf"\n{CLUSTER2}\n"notinout
# Try deleting it again, for fun (and coverage)
instanceadmin.delete_cluster(PROJECT, INSTANCE, CLUSTER2)
out=capsys.readouterr().out
assert"Deleting cluster"inout
assertf"Cluster {CLUSTER2} does not exist"inout