Skip to content

Commit 1701899

Browse files
authored
ci: label and clean up instanes used for testing samples (#384)
Co-authored-by: larkee <larkee@users.noreply.github.com>
1 parent f3362fe commit 1701899

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

samples/samples/autocommit_test.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
66

7+
import time
78
import uuid
89

910
from google.api_core.exceptions import Aborted
@@ -12,6 +13,7 @@
1213
from test_utils.retry import RetryErrors
1314

1415
import autocommit
16+
from snippets_test import cleanup_old_instances
1517

1618

1719
def unique_instance_id():
@@ -31,9 +33,18 @@ def unique_database_id():
3133
@pytest.fixture(scope="module")
3234
def spanner_instance():
3335
spanner_client = spanner.Client()
34-
config_name = f"{spanner_client.project_name}/instanceConfigs/regional-us-central1"
35-
36-
instance = spanner_client.instance(INSTANCE_ID, config_name)
36+
cleanup_old_instances(spanner_client)
37+
instance_config = "{}/instanceConfigs/{}".format(
38+
spanner_client.project_name, "regional-us-central1"
39+
)
40+
instance = spanner_client.instance(
41+
INSTANCE_ID,
42+
instance_config,
43+
labels={
44+
"cloud_spanner_samples": "true",
45+
"created": str(int(time.time()))
46+
}
47+
)
3748
op = instance.create()
3849
op.result(120) # block until completion
3950
yield instance

samples/samples/backup_sample_test.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
import time
1415
import uuid
1516

1617
from google.api_core.exceptions import DeadlineExceeded
@@ -19,6 +20,7 @@
1920
from test_utils.retry import RetryErrors
2021

2122
import backup_sample
23+
from snippets_test import cleanup_old_instances
2224

2325

2426
def unique_instance_id():
@@ -49,10 +51,18 @@ def unique_backup_id():
4951
@pytest.fixture(scope="module")
5052
def spanner_instance():
5153
spanner_client = spanner.Client()
54+
cleanup_old_instances(spanner_client)
5255
instance_config = "{}/instanceConfigs/{}".format(
5356
spanner_client.project_name, "regional-us-central1"
5457
)
55-
instance = spanner_client.instance(INSTANCE_ID, instance_config)
58+
instance = spanner_client.instance(
59+
INSTANCE_ID,
60+
instance_config,
61+
labels={
62+
"cloud_spanner_samples": "true",
63+
"created": str(int(time.time()))
64+
}
65+
)
5666
op = instance.create()
5767
op.result(120) # block until completion
5868
yield instance

samples/samples/snippets.py

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import datetime
2626
import decimal
2727
import logging
28+
import time
2829

2930
from google.cloud import spanner
3031
from google.cloud.spanner_v1 import param_types
@@ -44,6 +45,10 @@ def create_instance(instance_id):
4445
configuration_name=config_name,
4546
display_name="This is a display name.",
4647
node_count=1,
48+
labels={
49+
"cloud_spanner_samples": "true",
50+
"created": str(int(time.time()))
51+
}
4752
)
4853

4954
operation = instance.create()

samples/samples/snippets_test.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import uuid
1717

1818
from google.cloud import spanner
19+
from google.cloud.spanner_v1.instance import Instance
1920
import pytest
2021

2122
import snippets
@@ -31,15 +32,30 @@ def unique_database_id():
3132
return f"test-db-{uuid.uuid4().hex[:10]}"
3233

3334

35+
def cleanup_old_instances(spanner_client):
36+
# Delete test instances that are older than an hour.
37+
cutoff = int(time.time()) - 1 * 60 * 60
38+
instance_pbs = spanner_client.list_instances("labels.cloud_spanner_samples:true")
39+
for instance_pb in instance_pbs:
40+
instance = Instance.from_pb(instance_pb, spanner_client)
41+
if "created" not in instance.labels:
42+
continue
43+
create_time = int(instance.labels["created"])
44+
if create_time > cutoff:
45+
continue
46+
instance.delete()
47+
48+
3449
INSTANCE_ID = unique_instance_id()
3550
DATABASE_ID = unique_database_id()
3651
CMEK_DATABASE_ID = unique_database_id()
3752

3853

3954
@pytest.fixture(scope="module")
4055
def spanner_instance():
41-
snippets.create_instance(INSTANCE_ID)
4256
spanner_client = spanner.Client()
57+
cleanup_old_instances(spanner_client)
58+
snippets.create_instance(INSTANCE_ID)
4359
instance = spanner_client.instance(INSTANCE_ID)
4460
yield instance
4561
instance.delete()

0 commit comments

Comments
 (0)