FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge branch 'master' of github.com:googleapis/python-spanner into lc… · googleapis/python-spanner@7ddabae · GitHub

This repository was archived by the owner on Jun 8, 2026. It is now read-only.
/ python-spanner Public archive

Commit 7ddabae

Browse files
committed
Merge branch 'master' of github.com:googleapis/python-spanner into lci-sample
2 parents 2338fce + 1701899 commit 7ddabae

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎samples/samples/autocommit_test.py‎

Lines changed: 14 additions & 3 deletions
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‎

Lines changed: 11 additions & 1 deletion
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‎

Lines changed: 5 additions & 0 deletions
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‎

Lines changed: 17 additions & 1 deletion
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,6 +32,20 @@ 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
LCI_INSTANCE_ID = unique_instance_id()
3651
DATABASE_ID = unique_database_id()
@@ -39,8 +54,9 @@ def unique_database_id():
3954

4055
@pytest.fixture(scope="module")
4156
def spanner_instance():
42-
snippets.create_instance(INSTANCE_ID)
4357
spanner_client = spanner.Client()
58+
cleanup_old_instances(spanner_client)
59+
snippets.create_instance(INSTANCE_ID)
4460
instance = spanner_client.instance(INSTANCE_ID)
4561
yield instance
4662
instance.delete()

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL