Skip to content

Commit

Permalink
Merge branch 'master' of github.com:googleapis/python-spanner into lc…
Browse files Browse the repository at this point in the history
…i-sample
  • Loading branch information
zoercai committed Jul 2, 2021
2 parents 2338fce + 1701899 commit 7ddabae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
17 changes: 14 additions & 3 deletions samples/samples/autocommit_test.py
Expand Up @@ -4,6 +4,7 @@
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

import time
import uuid

from google.api_core.exceptions import Aborted
Expand All @@ -12,6 +13,7 @@
from test_utils.retry import RetryErrors

import autocommit
from snippets_test import cleanup_old_instances


def unique_instance_id():
Expand All @@ -31,9 +33,18 @@ def unique_database_id():
@pytest.fixture(scope="module")
def spanner_instance():
spanner_client = spanner.Client()
config_name = f"{spanner_client.project_name}/instanceConfigs/regional-us-central1"

instance = spanner_client.instance(INSTANCE_ID, config_name)
cleanup_old_instances(spanner_client)
instance_config = "{}/instanceConfigs/{}".format(
spanner_client.project_name, "regional-us-central1"
)
instance = spanner_client.instance(
INSTANCE_ID,
instance_config,
labels={
"cloud_spanner_samples": "true",
"created": str(int(time.time()))
}
)
op = instance.create()
op.result(120) # block until completion
yield instance
Expand Down
12 changes: 11 additions & 1 deletion samples/samples/backup_sample_test.py
Expand Up @@ -11,6 +11,7 @@
# 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.
import time
import uuid

from google.api_core.exceptions import DeadlineExceeded
Expand All @@ -19,6 +20,7 @@
from test_utils.retry import RetryErrors

import backup_sample
from snippets_test import cleanup_old_instances


def unique_instance_id():
Expand Down Expand Up @@ -49,10 +51,18 @@ def unique_backup_id():
@pytest.fixture(scope="module")
def spanner_instance():
spanner_client = spanner.Client()
cleanup_old_instances(spanner_client)
instance_config = "{}/instanceConfigs/{}".format(
spanner_client.project_name, "regional-us-central1"
)
instance = spanner_client.instance(INSTANCE_ID, instance_config)
instance = spanner_client.instance(
INSTANCE_ID,
instance_config,
labels={
"cloud_spanner_samples": "true",
"created": str(int(time.time()))
}
)
op = instance.create()
op.result(120) # block until completion
yield instance
Expand Down
5 changes: 5 additions & 0 deletions samples/samples/snippets.py
Expand Up @@ -25,6 +25,7 @@
import datetime
import decimal
import logging
import time

from google.cloud import spanner
from google.cloud.spanner_v1 import param_types
Expand All @@ -44,6 +45,10 @@ def create_instance(instance_id):
configuration_name=config_name,
display_name="This is a display name.",
node_count=1,
labels={
"cloud_spanner_samples": "true",
"created": str(int(time.time()))
}
)

operation = instance.create()
Expand Down
18 changes: 17 additions & 1 deletion samples/samples/snippets_test.py
Expand Up @@ -16,6 +16,7 @@
import uuid

from google.cloud import spanner
from google.cloud.spanner_v1.instance import Instance
import pytest

import snippets
Expand All @@ -31,6 +32,20 @@ def unique_database_id():
return f"test-db-{uuid.uuid4().hex[:10]}"


def cleanup_old_instances(spanner_client):
# Delete test instances that are older than an hour.
cutoff = int(time.time()) - 1 * 60 * 60
instance_pbs = spanner_client.list_instances("labels.cloud_spanner_samples:true")
for instance_pb in instance_pbs:
instance = Instance.from_pb(instance_pb, spanner_client)
if "created" not in instance.labels:
continue
create_time = int(instance.labels["created"])
if create_time > cutoff:
continue
instance.delete()


INSTANCE_ID = unique_instance_id()
LCI_INSTANCE_ID = unique_instance_id()
DATABASE_ID = unique_database_id()
Expand All @@ -39,8 +54,9 @@ def unique_database_id():

@pytest.fixture(scope="module")
def spanner_instance():
snippets.create_instance(INSTANCE_ID)
spanner_client = spanner.Client()
cleanup_old_instances(spanner_client)
snippets.create_instance(INSTANCE_ID)
instance = spanner_client.instance(INSTANCE_ID)
yield instance
instance.delete()
Expand Down

0 comments on commit 7ddabae

Please sign in to comment.