Skip to content
Open
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
10 changes: 5 additions & 5 deletions packages/google-cloud-spanner/samples/samples/backup_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def create_backup(instance_id, database_id, backup_id, version_time):
operation = database_admin_api.create_backup(request)

# Wait for backup operation to complete.
backup = operation.result(2100)
backup = operation.result(3600)

# Verify that the backup is ready.
assert backup.state == backup_pb.Backup.State.READY
Expand Down Expand Up @@ -101,7 +101,7 @@ def create_backup_with_encryption_key(
operation = database_admin_api.create_backup(request)

# Wait for backup operation to complete.
backup = operation.result(2100)
backup = operation.result(3600)

# Verify that the backup is ready.
assert backup.state == backup_pb.Backup.State.READY
Expand Down Expand Up @@ -149,7 +149,7 @@ def create_backup_with_multiple_kms_keys(
operation = database_admin_api.create_backup(request)

# Wait for backup operation to complete.
backup = operation.result(2100)
backup = operation.result(3600)

# Verify that the backup is ready.
assert backup.state == backup_pb.Backup.State.READY
Expand Down Expand Up @@ -681,7 +681,7 @@ def copy_backup(instance_id, backup_id, source_backup_path):
operation = database_admin_api.copy_backup(request)

# Wait for backup operation to complete.
copy_backup = operation.result(2100)
copy_backup = operation.result(3600)

# Verify that the copy backup is ready.
assert copy_backup.state == backup_pb.Backup.State.READY
Expand Down Expand Up @@ -729,7 +729,7 @@ def copy_backup_with_multiple_kms_keys(
operation = database_admin_api.copy_backup(request)

# Wait for backup operation to complete.
copy_backup = operation.result(2100)
copy_backup = operation.result(3600)

# Verify that the copy backup is ready.
assert copy_backup.state == backup_pb.Backup.State.READY
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Copyright 2026 Google LLC All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

from unittest import mock

from google.cloud.spanner_admin_database_v1.types import backup as backup_pb
import pytest

import backup_sample

EXPECTED_BACKUP_LRO_TIMEOUT = 3600


@pytest.mark.parametrize(
("sample_fn", "api_method", "args"),
[
(backup_sample.create_backup, "create_backup", ("inst", "db", "bk", None)),
(
backup_sample.create_backup_with_encryption_key,
"create_backup",
("inst", "db", "bk", "projects/p/locations/l/keyRings/r/cryptoKeys/k"),
),
(
backup_sample.create_backup_with_multiple_kms_keys,
"create_backup",
(
"inst",
"db",
"bk",
["projects/p/locations/l/keyRings/r/cryptoKeys/k1"],
),
),
(
backup_sample.copy_backup,
"copy_backup",
("inst", "bk", "projects/p/instances/inst/backups/src"),
),
(
backup_sample.copy_backup_with_multiple_kms_keys,
"copy_backup",
(
"inst",
"bk",
"projects/p/instances/inst/backups/src",
["projects/p/locations/l/keyRings/r/cryptoKeys/k1"],
),
),
],
)
def test_backup_lro_polling_timeout(capsys, sample_fn, api_method, args):
mock_operation = mock.MagicMock()
mock_operation.result.return_value = backup_pb.Backup(
name="projects/p/instances/inst/backups/bk",
state=backup_pb.Backup.State.READY,
)

mock_admin_api = mock.MagicMock()
mock_admin_api.instance_path.return_value = "projects/p/instances/inst"
mock_admin_api.database_path.return_value = "projects/p/instances/inst/databases/db"
getattr(mock_admin_api, api_method).return_value = mock_operation

mock_client = mock.MagicMock()
mock_client.project = "p"
mock_client.database_admin_api = mock_admin_api

with mock.patch("backup_sample.spanner.Client", return_value=mock_client):
sample_fn(*args)

mock_operation.result.assert_called_once_with(EXPECTED_BACKUP_LRO_TIMEOUT)
out, _ = capsys.readouterr()
assert "projects/p/instances/inst/backups/bk" in out
Loading