FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-spanner/samples/samples/backup_sample_test.py at main · stophobia/python-spanner · GitHub
stophobia
/
python-spanner
Public
forked from
googleapis/python-spanner
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-spanner
/
samples
/
samples
/
backup_sample_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
260 lines (221 loc) · 8.16 KB
Breadcrumbs
python-spanner
/
samples
/
samples
/
backup_sample_test.py
Copy path
File metadata and controls
260 lines (221 loc) · 8.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# 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
.
api_core
.
exceptions
import
DeadlineExceeded
import
pytest
from
test_utils
.
retry
import
RetryErrors
import
backup_sample
@
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
):
version_time
=
None
with
sample_database
.
snapshot
()
as
snapshot
:
results
=
snapshot
.
execute_sql
(
"SELECT CURRENT_TIMESTAMP()"
)
version_time
=
list
(
results
)[
0
][
0
]
backup_sample
.
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_sample
.
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_sample
.
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
.
skip
(
reason
=
"skipped since the KMS keys are not added on test "
"project"
)
@
pytest
.
mark
.
dependency
(
name
=
"create_backup_with_multiple_kms_keys"
)
def
test_create_backup_with_multiple_kms_keys
(
capsys
,
multi_region_instance
,
multi_region_instance_id
,
sample_multi_region_database
,
kms_key_names
,
):
backup_sample
.
create_backup_with_multiple_kms_keys
(
multi_region_instance_id
,
sample_multi_region_database
.
database_id
,
CMEK_BACKUP_ID
,
kms_key_names
,
)
out
,
_
=
capsys
.
readouterr
()
assert
CMEK_BACKUP_ID
in
out
assert
kms_key_names
[
0
]
in
out
assert
kms_key_names
[
1
]
in
out
assert
kms_key_names
[
2
]
in
out
@
pytest
.
mark
.
skip
(
reason
=
"skipped since the KMS keys are not added on test "
"project"
)
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_backup_with_multiple_kms_keys"
])
def
test_copy_backup_with_multiple_kms_keys
(
capsys
,
multi_region_instance_id
,
spanner_client
,
kms_key_names
):
source_backup_path
=
(
spanner_client
.
project_name
+
"/instances/"
+
multi_region_instance_id
+
"/backups/"
+
CMEK_BACKUP_ID
)
backup_sample
.
copy_backup_with_multiple_kms_keys
(
multi_region_instance_id
,
COPY_BACKUP_ID
,
source_backup_path
,
kms_key_names
)
out
,
_
=
capsys
.
readouterr
()
assert
COPY_BACKUP_ID
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_backup"
])
@
RetryErrors
(
exception
=
DeadlineExceeded
,
max_tries
=
2
)
def
test_restore_database
(
capsys
,
instance_id
,
sample_database
):
backup_sample
.
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_sample
.
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
.
skip
(
reason
=
"skipped since the KMS keys are not added on test "
"project"
)
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_backup_with_multiple_kms_keys"
])
@
RetryErrors
(
exception
=
DeadlineExceeded
,
max_tries
=
2
)
def
test_restore_database_with_multiple_kms_keys
(
capsys
,
multi_region_instance_id
,
sample_multi_region_database
,
kms_key_names
,
):
backup_sample
.
restore_database_with_multiple_kms_keys
(
multi_region_instance_id
,
CMEK_RESTORE_DB_ID
,
CMEK_BACKUP_ID
,
kms_key_names
)
out
,
_
=
capsys
.
readouterr
()
assert
(
sample_multi_region_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_names
[
0
]
in
out
assert
kms_key_names
[
1
]
in
out
assert
kms_key_names
[
2
]
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_backup"
,
"copy_backup"
])
def
test_list_backup_operations
(
capsys
,
instance_id
,
sample_database
):
backup_sample
.
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_sample
.
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_sample
.
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_sample
.
delete_backup
(
instance_id
,
BACKUP_ID
)
out
,
_
=
capsys
.
readouterr
()
assert
BACKUP_ID
in
out
backup_sample
.
delete_backup
(
instance_id
,
COPY_BACKUP_ID
)
out
,
_
=
capsys
.
readouterr
()
assert
COPY_BACKUP_ID
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_backup"
])
def
test_cancel_backup
(
capsys
,
instance_id
,
sample_database
):
backup_sample
.
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_sample
.
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
database
=
sample_instance
.
database
(
RETENTION_DATABASE_ID
)
database
.
drop
()
Back
|
FazBrowse Home
|
New Git URL