Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def cleanup_old_instances(self, project_id):
if os.getenv(BIGTABLE_EMULATOR):
return

clear_stale_instances(project_id, "python-bigtable-tests", older_than_days=1)
clear_stale_instances(project_id, older_than_days=1)

@pytest.fixture(scope="session")
def init_table_id(self, start_timestamp):
Expand Down
78 changes: 59 additions & 19 deletions packages/google-cloud-bigtable/tests/system/v2_client/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import os

import pytest
from google.api_core import exceptions
from google.cloud.environment_vars import BIGTABLE_EMULATOR
from test_utils.system import unique_resource_id

Expand Down Expand Up @@ -76,6 +77,16 @@ def admin_client():
return Client(admin=True)


@pytest.fixture(scope="session", autouse=True)
def cleanup_old_instances(admin_client, in_emulator):
"""Automatically deletes stale test instances older than 1 day."""
if in_emulator:
return
from tests.system.utils import clear_stale_instances

clear_stale_instances(admin_client.project, older_than_days=1)


@pytest.fixture(scope="session")
def service_account(admin_client):
from google.oauth2.service_account import Credentials
Expand Down Expand Up @@ -131,14 +142,29 @@ def admin_cluster_with_autoscaling(
def admin_instance_populated(admin_instance, admin_cluster, in_emulator):
# Emulator does not support instance admin operations (create / delete).
# See: https://cloud.google.com/bigtable/docs/emulator
if not in_emulator:
operation = admin_instance.create(clusters=[admin_cluster])
operation.result(timeout=240)

yield admin_instance
if in_emulator:
yield admin_instance
return

if not in_emulator:
_helpers.retry_429(admin_instance.delete)()
operation = admin_instance.create(clusters=[admin_cluster])
created = False
try:
operation.result(timeout=240)
created = True
yield admin_instance
finally:
try:
_helpers.retry_429(admin_instance.delete)()
except exceptions.NotFound:
pass
except Exception as exc:
if not created:
print(
f"Failed to delete timed-out instance "
f"{admin_instance.instance_id}: {exc}"
)
else:
raise


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -169,19 +195,33 @@ def data_instance_populated(
instance = admin_client.instance(data_instance_id, labels=instance_labels)
# Emulator does not support instance admin operations (create / delete).
# See: https://cloud.google.com/bigtable/docs/emulator
if not in_emulator:
cluster = instance.cluster(
data_cluster_id,
location_id=location_id,
serve_nodes=serve_nodes,
)
operation = instance.create(clusters=[cluster])
operation.result(timeout=240)

yield instance
if in_emulator:
yield instance
return

if not in_emulator:
_helpers.retry_429(instance.delete)()
cluster = instance.cluster(
data_cluster_id,
location_id=location_id,
serve_nodes=serve_nodes,
)
operation = instance.create(clusters=[cluster])
created = False
try:
operation.result(timeout=240)
created = True
yield instance
finally:
try:
_helpers.retry_429(instance.delete)()
except exceptions.NotFound:
pass
except Exception as exc:
if not created:
print(
f"Failed to delete timed-out instance {instance.instance_id}: {exc}"
)
else:
raise


@pytest.fixture(scope="function")
Expand Down
Loading