-
Notifications
You must be signed in to change notification settings - Fork 68
/
backup_snippet_test.py
192 lines (160 loc) · 5.9 KB
/
backup_snippet_test.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Copyright 2024 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
import backup_snippet
import pytest
from google.api_core.exceptions import DeadlineExceeded
from test_utils.retry import RetryErrors
@pytest.fixture(scope="module")
def sample_name():
return "backup"
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]}"
RESTORE_DB_ID = unique_database_id()
BACKUP_ID = unique_backup_id()
CMEK_RESTORE_DB_ID = unique_database_id()
CMEK_BACKUP_ID = unique_backup_id()
RETENTION_DATABASE_ID = unique_database_id()
RETENTION_PERIOD = "7d"
COPY_BACKUP_ID = unique_backup_id()
@pytest.mark.dependency(name="create_backup")
def test_create_backup(capsys, instance_id, sample_database):
with sample_database.snapshot() as snapshot:
results = snapshot.execute_sql("SELECT CURRENT_TIMESTAMP()")
version_time = list(results)[0][0]
backup_snippet.create_backup(
instance_id,
sample_database.database_id,
BACKUP_ID,
version_time,
)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
@pytest.mark.dependency(name="copy_backup", depends=["create_backup"])
def test_copy_backup(capsys, instance_id, spanner_client):
source_backp_path = (
spanner_client.project_name
+ "/instances/"
+ instance_id
+ "/backups/"
+ BACKUP_ID
)
backup_snippet.copy_backup(instance_id, COPY_BACKUP_ID, source_backp_path)
out, _ = capsys.readouterr()
assert COPY_BACKUP_ID in out
@pytest.mark.dependency(name="create_backup_with_encryption_key")
def test_create_backup_with_encryption_key(
capsys,
instance_id,
sample_database,
kms_key_name,
):
backup_snippet.create_backup_with_encryption_key(
instance_id,
sample_database.database_id,
CMEK_BACKUP_ID,
kms_key_name,
)
out, _ = capsys.readouterr()
assert CMEK_BACKUP_ID in out
assert kms_key_name in out
@pytest.mark.dependency(depends=["create_backup"])
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database(capsys, instance_id, sample_database):
backup_snippet.restore_database(instance_id, RESTORE_DB_ID, BACKUP_ID)
out, _ = capsys.readouterr()
assert (sample_database.database_id + " restored to ") in out
assert (RESTORE_DB_ID + " from backup ") in out
assert BACKUP_ID in out
@pytest.mark.dependency(depends=["create_backup_with_encryption_key"])
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_restore_database_with_encryption_key(
capsys,
instance_id,
sample_database,
kms_key_name,
):
backup_snippet.restore_database_with_encryption_key(
instance_id, CMEK_RESTORE_DB_ID, CMEK_BACKUP_ID, kms_key_name
)
out, _ = capsys.readouterr()
assert (sample_database.database_id + " restored to ") in out
assert (CMEK_RESTORE_DB_ID + " from backup ") in out
assert CMEK_BACKUP_ID in out
assert kms_key_name in out
@pytest.mark.dependency(depends=["create_backup", "copy_backup"])
def test_list_backup_operations(capsys, instance_id, sample_database):
backup_snippet.list_backup_operations(
instance_id, sample_database.database_id, BACKUP_ID
)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
assert sample_database.database_id in out
assert COPY_BACKUP_ID in out
print(out)
@pytest.mark.dependency(name="list_backup", depends=["create_backup", "copy_backup"])
def test_list_backups(
capsys,
instance_id,
sample_database,
):
backup_snippet.list_backups(
instance_id,
sample_database.database_id,
BACKUP_ID,
)
out, _ = capsys.readouterr()
id_count = out.count(BACKUP_ID)
assert id_count == 7
@pytest.mark.dependency(depends=["create_backup"])
def test_update_backup(capsys, instance_id):
backup_snippet.update_backup(instance_id, BACKUP_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
@pytest.mark.dependency(depends=["create_backup", "copy_backup", "list_backup"])
def test_delete_backup(capsys, instance_id):
backup_snippet.delete_backup(instance_id, BACKUP_ID)
out, _ = capsys.readouterr()
assert BACKUP_ID in out
backup_snippet.delete_backup(instance_id, COPY_BACKUP_ID)
out, _ = capsys.readouterr()
assert "has been deleted." in out
assert COPY_BACKUP_ID in out
@pytest.mark.dependency(depends=["create_backup"])
def test_cancel_backup(capsys, instance_id, sample_database):
backup_snippet.cancel_backup(
instance_id,
sample_database.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
@RetryErrors(exception=DeadlineExceeded, max_tries=2)
def test_create_database_with_retention_period(capsys, sample_instance):
backup_snippet.create_database_with_version_retention_period(
sample_instance.instance_id,
RETENTION_DATABASE_ID,
RETENTION_PERIOD,
)
out, _ = capsys.readouterr()
assert (RETENTION_DATABASE_ID + " created with ") in out
assert ("retention period " + RETENTION_PERIOD) in out