Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add backup/restore timeouts; reduce backup creation count #528

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
6 changes: 6 additions & 0 deletions tests/system/_helpers.py
Expand Up @@ -39,6 +39,12 @@
DATABASE_OPERATION_TIMEOUT_IN_SECONDS = int(
os.getenv("SPANNER_DATABASE_OPERATION_TIMEOUT_IN_SECONDS", 60)
)
DATABASE_RESTORE_OPERATION_IN_SECONDS = int(
os.getenv("SPANNER_RESTORE_OPERATION_IN_SECONDS", 1200)
)
BACKUP_OPERATION_TIMEOUT_IN_SECONDS = int(
os.getenv("SPANNER_BACKUP_CREATE_OPERATION_IN_SECONDS", 600)
)

USE_EMULATOR_ENVVAR = "SPANNER_EMULATOR_HOST"
USE_EMULATOR = os.getenv(USE_EMULATOR_ENVVAR) is not None
Expand Down
10 changes: 10 additions & 0 deletions tests/system/conftest.py
Expand Up @@ -67,6 +67,16 @@ def database_operation_timeout():
return _helpers.DATABASE_OPERATION_TIMEOUT_IN_SECONDS


@pytest.fixture(scope="session")
def database_restore_operation_timeout():
return _helpers.DATABASE_RESTORE_OPERATION_IN_SECONDS


@pytest.fixture(scope="session")
def backup_operation_timeout():
return _helpers.BACKUP_OPERATION_TIMEOUT_IN_SECONDS


@pytest.fixture(scope="session")
def shared_instance_id():
if _helpers.CREATE_INSTANCE:
Expand Down
79 changes: 33 additions & 46 deletions tests/system/test_backup_api.py
Expand Up @@ -115,6 +115,8 @@ def test_backup_workflow(
database_version_time,
backups_to_delete,
databases_to_delete,
backup_operation_timeout,
database_restore_operation_timeout,
):
from google.cloud.spanner_admin_database_v1 import (
CreateBackupEncryptionConfig,
Expand Down Expand Up @@ -146,7 +148,7 @@ def test_backup_workflow(
metadata = operation.metadata
assert backup.name == metadata.name
assert shared_database.name == metadata.database
operation.result() # blocks indefinitely
operation.result(backup_operation_timeout) # blocks until completion

# Check backup object.
backup.reload()
Expand Down Expand Up @@ -177,7 +179,9 @@ def test_backup_workflow(
)
databases_to_delete.append(database)
operation = database.restore(source=backup)
restored_db = operation.result() # blocks indefinitely
restored_db = operation.result(
database_restore_operation_timeout
) # blocks until completion
assert database_version_time == restored_db.restore_info.backup_info.version_time

metadata = operation.metadata
Expand All @@ -192,41 +196,9 @@ def test_backup_workflow(
assert not backup.exists()


def test_backup_create_w_version_time_dflt_to_create_time(
shared_instance,
shared_database,
database_version_time,
backups_to_delete,
databases_to_delete,
def test_backup_create_w_invalid_expire_time(
shared_instance, shared_database, backup_operation_timeout
):
backup_id = _helpers.unique_id("backup_id", separator="_")
expire_time = datetime.datetime.utcnow() + datetime.timedelta(days=3)
expire_time = expire_time.replace(tzinfo=datetime.timezone.utc)

# Create backup.
backup = shared_instance.backup(
backup_id, database=shared_database, expire_time=expire_time,
)
operation = backup.create()
backups_to_delete.append(backup)

# Check metadata.
metadata = operation.metadata
assert backup.name == metadata.name
assert shared_database.name == metadata.database
operation.result() # blocks indefinitely

# Check backup object.
backup.reload()
assert shared_database.name == backup._database
assert backup.create_time is not None
assert backup.create_time == backup.version_time

backup.delete()
assert not backup.exists()


def test_backup_create_w_invalid_expire_time(shared_instance, shared_database):
backup_id = _helpers.unique_id("backup_id", separator="_")
expire_time = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc)

Expand All @@ -236,11 +208,11 @@ def test_backup_create_w_invalid_expire_time(shared_instance, shared_database):

with pytest.raises(exceptions.InvalidArgument):
op = backup.create()
op.result() # blocks indefinitely
op.result(backup_operation_timeout) # blocks until completion


def test_backup_create_w_invalid_version_time_past(
shared_instance, shared_database,
shared_instance, shared_database, backup_operation_timeout,
):
backup_id = _helpers.unique_id("backup_id", separator="_")
expire_time = datetime.datetime.utcnow() + datetime.timedelta(days=3)
Expand All @@ -257,11 +229,11 @@ def test_backup_create_w_invalid_version_time_past(

with pytest.raises(exceptions.InvalidArgument):
op = backup.create()
op.result() # blocks indefinitely
op.result(backup_operation_timeout) # blocks until completion


def test_backup_create_w_invalid_version_time_future(
shared_instance, shared_database,
shared_instance, shared_database, backup_operation_timeout,
):
backup_id = _helpers.unique_id("backup_id", separator="_")
expire_time = datetime.datetime.utcnow() + datetime.timedelta(days=3)
Expand All @@ -278,7 +250,7 @@ def test_backup_create_w_invalid_version_time_future(

with pytest.raises(exceptions.InvalidArgument):
op = backup.create()
op.result() # blocks indefinitely
op.result(backup_operation_timeout) # blocks until completion


def test_database_restore_to_diff_instance(
Expand All @@ -287,6 +259,8 @@ def test_database_restore_to_diff_instance(
backups_to_delete,
same_config_instance,
databases_to_delete,
backup_operation_timeout,
database_restore_operation_timeout,
):
backup_id = _helpers.unique_id("backup_id", separator="_")
expire_time = datetime.datetime.utcnow() + datetime.timedelta(days=3)
Expand All @@ -296,16 +270,27 @@ def test_database_restore_to_diff_instance(
backup = shared_instance.backup(
backup_id, database=shared_database, expire_time=expire_time,
)
op = backup.create()
operation = backup.create()
backups_to_delete.append(backup)
op.result()

# Check metadata.
metadata = operation.metadata
assert backup.name == metadata.name
assert shared_database.name == metadata.database
operation.result(backup_operation_timeout) # blocks until completion

# Check backup object.
backup.reload()
assert shared_database.name == backup._database
assert backup.create_time is not None
assert backup.create_time == backup.version_time

# Restore database to different instance with same config.
restored_id = _helpers.unique_id("restored_db")
database = same_config_instance.database(restored_id)
databases_to_delete.append(database)
operation = database.restore(source=backup)
operation.result() # blocks indefinitely
operation.result(database_restore_operation_timeout) # blocks until completion

database.drop()
backup.delete()
Expand All @@ -319,6 +304,7 @@ def test_multi_create_cancel_update_error_restore_errors(
diff_config_instance,
backups_to_delete,
databases_to_delete,
backup_operation_timeout,
):
backup_id_1 = _helpers.unique_id("backup_id1", separator="_")
backup_id_2 = _helpers.unique_id("backup_id2", separator="_")
Expand Down Expand Up @@ -348,7 +334,7 @@ def test_multi_create_cancel_update_error_restore_errors(
op2.cancel()
assert op2.cancelled()

op1.result() # blocks indefinitely
op1.result(backup_operation_timeout) # blocks until completion
backup1.reload()
assert backup1.is_ready()

Expand Down Expand Up @@ -378,6 +364,7 @@ def test_instance_list_backups(
second_database,
database_version_time,
backups_to_delete,
backup_operation_timeout,
):
# Remove un-scrubbed backups FBO count below.
_helpers.scrub_instance_backups(shared_instance)
Expand Down Expand Up @@ -405,7 +392,7 @@ def test_instance_list_backups(
# Create two backups.
op1 = backup1.create()
backups_to_delete.append(backup1)
op1.result() # blocks indefinitely
op1.result(backup_operation_timeout) # blocks until completion
backup1.reload()

create_time_compare = datetime.datetime.utcnow().replace(
Expand Down