forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_sample_test.py
More file actions
112 lines (84 loc) · 3.33 KB
/
backup_sample_test.py
File metadata and controls
112 lines (84 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2020 Google Inc. 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.
import uuid
from google.cloud import spanner
import pytest
import backup_sample
def unique_instance_id():
""" Creates a unique id for the database. """
return f'test-instance-{uuid.uuid4().hex[:10]}'
def unique_database_id():
""" Creates a unique id for the database. """
return f'test-db-{uuid.uuid4().hex[:10]}'
def unique_backup_id():
""" Creates a unique id for the backup. """
return f'test-backup-{uuid.uuid4().hex[:10]}'
INSTANCE_ID = unique_instance_id()
DATABASE_ID = unique_database_id()
RESTORE_DB_ID = unique_database_id()
BACKUP_ID = unique_backup_id()
@pytest.fixture(scope='module')
def spanner_instance():
spanner_client = spanner.Client()
instance_config = '{}/instanceConfigs/{}'.format(
spanner_client.project_name, 'regional-us-central1')
instance = spanner_client.instance(INSTANCE_ID, instance_config)
op = instance.create()
op.result(120) # block until completion
yield instance
instance.delete()
@pytest.fixture(scope='module')
def database(spanner_instance):
""" Creates a temporary database that is removed after testing. """
db = spanner_instance.database(DATABASE_ID)
db.create()
yield db
db.drop()
def test_create_backup(capsys, database):
backup_sample.create_backup(INSTANCE_ID, DATABASE_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
def test_restore_database(capsys):
backup_sample.restore_database(INSTANCE_ID, RESTORE_DB_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert (DATABASE_ID + " restored to ") in out
assert (RESTORE_DB_ID + " from backup ") in out
assert BACKUP_ID in out
def test_list_backup_operations(capsys, spanner_instance):
backup_sample.list_backup_operations(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
assert DATABASE_ID in out
def test_list_backups(capsys, spanner_instance):
backup_sample.list_backups(INSTANCE_ID, DATABASE_ID, BACKUP_ID)
out, _ = capsys.readouterr()
id_count = out.count(BACKUP_ID)
assert id_count == 7
def test_update_backup(capsys):
backup_sample.update_backup(INSTANCE_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
def test_delete_backup(capsys, spanner_instance):
backup_sample.delete_backup(INSTANCE_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
def test_cancel_backup(capsys):
backup_sample.cancel_backup(INSTANCE_ID, DATABASE_ID, BACKUP_ID)
out, _ = capsys.readouterr()
cancel_success = "Backup creation was successfully cancelled." in out
cancel_failure = (
("Backup was created before the cancel completed." in out) and
("Backup deleted." in out)
)
assert cancel_success or cancel_failure