11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
- import time
15
14
import uuid
16
15
17
16
from google .api_core .exceptions import DeadlineExceeded
18
- from google .cloud import spanner
19
17
import pytest
20
18
from test_utils .retry import RetryErrors
21
19
22
20
import backup_sample
23
- from snippets_test import cleanup_old_instances
24
21
25
22
26
- def unique_instance_id ():
27
- """ Creates a unique id for the database. """
28
- return f"test-instance- { uuid . uuid4 (). hex [: 10 ] } "
23
+ @ pytest . fixture ( scope = "module" )
24
+ def sample_name ():
25
+ return "backup "
29
26
30
27
31
28
def unique_database_id ():
@@ -38,8 +35,6 @@ def unique_backup_id():
38
35
return f"test-backup-{ uuid .uuid4 ().hex [:10 ]} "
39
36
40
37
41
- INSTANCE_ID = unique_instance_id ()
42
- DATABASE_ID = unique_database_id ()
43
38
RESTORE_DB_ID = unique_database_id ()
44
39
BACKUP_ID = unique_backup_id ()
45
40
CMEK_RESTORE_DB_ID = unique_database_id ()
@@ -48,121 +43,100 @@ def unique_backup_id():
48
43
RETENTION_PERIOD = "7d"
49
44
50
45
51
- @pytest .fixture (scope = "module" )
52
- def spanner_instance ():
53
- spanner_client = spanner .Client ()
54
- cleanup_old_instances (spanner_client )
55
- instance_config = "{}/instanceConfigs/{}" .format (
56
- spanner_client .project_name , "regional-us-central1"
57
- )
58
- instance = spanner_client .instance (
59
- INSTANCE_ID ,
60
- instance_config ,
61
- labels = {
62
- "cloud_spanner_samples" : "true" ,
63
- "sample_name" : "backup" ,
64
- "created" : str (int (time .time ()))
65
- }
66
- )
67
- op = instance .create ()
68
- op .result (120 ) # block until completion
69
- yield instance
70
- for database_pb in instance .list_databases ():
71
- database = instance .database (database_pb .name .split ("/" )[- 1 ])
72
- database .drop ()
73
- for backup_pb in instance .list_backups ():
74
- backup = instance .backup (backup_pb .name .split ("/" )[- 1 ])
75
- backup .delete ()
76
- instance .delete ()
77
-
78
-
79
- @pytest .fixture (scope = "module" )
80
- def database (spanner_instance ):
81
- """ Creates a temporary database that is removed after testing. """
82
- db = spanner_instance .database (DATABASE_ID )
83
- db .create ()
84
- yield db
85
- db .drop ()
86
-
87
-
88
- def test_create_backup (capsys , database ):
46
+ @pytest .mark .dependency (name = "create_backup" )
47
+ def test_create_backup (capsys , instance_id , sample_database ):
89
48
version_time = None
90
- with database .snapshot () as snapshot :
49
+ with sample_database .snapshot () as snapshot :
91
50
results = snapshot .execute_sql ("SELECT CURRENT_TIMESTAMP()" )
92
51
version_time = list (results )[0 ][0 ]
93
52
94
- backup_sample .create_backup (INSTANCE_ID , DATABASE_ID , BACKUP_ID , version_time )
53
+ backup_sample .create_backup (
54
+ instance_id ,
55
+ sample_database .database_id ,
56
+ BACKUP_ID ,
57
+ version_time ,
58
+ )
95
59
out , _ = capsys .readouterr ()
96
60
assert BACKUP_ID in out
97
61
98
62
99
- def test_create_backup_with_encryption_key (capsys , spanner_instance , database ):
100
- kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}" .format (
101
- spanner_instance ._client .project , "us-central1" , "spanner-test-keyring" , "spanner-test-cmek"
63
+ @pytest .mark .dependency (name = "create_backup_with_encryption_key" )
64
+ def test_create_backup_with_encryption_key (
65
+ capsys , instance_id , sample_database , kms_key_name ,
66
+ ):
67
+ backup_sample .create_backup_with_encryption_key (
68
+ instance_id ,
69
+ sample_database .database_id ,
70
+ CMEK_BACKUP_ID ,
71
+ kms_key_name ,
102
72
)
103
- backup_sample .create_backup_with_encryption_key (INSTANCE_ID , DATABASE_ID , CMEK_BACKUP_ID , kms_key_name )
104
73
out , _ = capsys .readouterr ()
105
74
assert CMEK_BACKUP_ID in out
106
75
assert kms_key_name in out
107
76
108
77
109
- # Depends on test_create_backup having run first
78
+ @ pytest . mark . dependency ( depends = [ "create_backup" ])
110
79
@RetryErrors (exception = DeadlineExceeded , max_tries = 2 )
111
- def test_restore_database (capsys ):
112
- backup_sample .restore_database (INSTANCE_ID , RESTORE_DB_ID , BACKUP_ID )
80
+ def test_restore_database (capsys , instance_id , sample_database ):
81
+ backup_sample .restore_database (instance_id , RESTORE_DB_ID , BACKUP_ID )
113
82
out , _ = capsys .readouterr ()
114
- assert (DATABASE_ID + " restored to " ) in out
83
+ assert (sample_database . database_id + " restored to " ) in out
115
84
assert (RESTORE_DB_ID + " from backup " ) in out
116
85
assert BACKUP_ID in out
117
86
118
87
119
- # Depends on test_create_backup having run first
88
+ @ pytest . mark . dependency ( depends = [ "create_backup_with_encryption_key" ])
120
89
@RetryErrors (exception = DeadlineExceeded , max_tries = 2 )
121
- def test_restore_database_with_encryption_key (capsys , spanner_instance ):
122
- kms_key_name = "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}" . format (
123
- spanner_instance . _client . project , "us-central1" , "spanner-test-keyring" , "spanner-test-cmek"
124
- )
125
- backup_sample . restore_database_with_encryption_key ( INSTANCE_ID , CMEK_RESTORE_DB_ID , CMEK_BACKUP_ID , kms_key_name )
90
+ def test_restore_database_with_encryption_key (
91
+ capsys , instance_id , sample_database , kms_key_name ,
92
+ ):
93
+ backup_sample . restore_database_with_encryption_key (
94
+ instance_id , CMEK_RESTORE_DB_ID , CMEK_BACKUP_ID , kms_key_name )
126
95
out , _ = capsys .readouterr ()
127
- assert (DATABASE_ID + " restored to " ) in out
96
+ assert (sample_database . database_id + " restored to " ) in out
128
97
assert (CMEK_RESTORE_DB_ID + " from backup " ) in out
129
98
assert CMEK_BACKUP_ID in out
130
99
assert kms_key_name in out
131
100
132
101
133
- # Depends on test_create_backup having run first
134
- def test_list_backup_operations (capsys , spanner_instance ):
135
- backup_sample .list_backup_operations (INSTANCE_ID , DATABASE_ID )
102
+ @pytest .mark .dependency (depends = ["create_backup" ])
103
+ def test_list_backup_operations (capsys , instance_id , sample_database ):
104
+ backup_sample .list_backup_operations (
105
+ instance_id , sample_database .database_id )
136
106
out , _ = capsys .readouterr ()
137
107
assert BACKUP_ID in out
138
- assert DATABASE_ID in out
108
+ assert sample_database . database_id in out
139
109
140
110
141
- # Depends on test_create_backup having run first
142
- def test_list_backups (capsys , spanner_instance ):
143
- backup_sample .list_backups (INSTANCE_ID , DATABASE_ID , BACKUP_ID )
111
+ @pytest .mark .dependency (depends = ["create_backup" ])
112
+ def test_list_backups (capsys , instance_id , sample_database ):
113
+ backup_sample .list_backups (
114
+ instance_id , sample_database .database_id , BACKUP_ID ,
115
+ )
144
116
out , _ = capsys .readouterr ()
145
117
id_count = out .count (BACKUP_ID )
146
118
assert id_count == 7
147
119
148
120
149
- # Depends on test_create_backup having run first
150
- def test_update_backup (capsys ):
151
- backup_sample .update_backup (INSTANCE_ID , BACKUP_ID )
121
+ @ pytest . mark . dependency ( depends = [ "create_backup" ])
122
+ def test_update_backup (capsys , instance_id ):
123
+ backup_sample .update_backup (instance_id , BACKUP_ID )
152
124
out , _ = capsys .readouterr ()
153
125
assert BACKUP_ID in out
154
126
155
127
156
- # Depends on test_create_backup having run first
157
- def test_delete_backup (capsys , spanner_instance ):
158
- backup_sample .delete_backup (INSTANCE_ID , BACKUP_ID )
128
+ @ pytest . mark . dependency ( depends = [ "create_backup" ])
129
+ def test_delete_backup (capsys , instance_id ):
130
+ backup_sample .delete_backup (instance_id , BACKUP_ID )
159
131
out , _ = capsys .readouterr ()
160
132
assert BACKUP_ID in out
161
133
162
134
163
- # Depends on test_create_backup having run first
164
- def test_cancel_backup (capsys ):
165
- backup_sample .cancel_backup (INSTANCE_ID , DATABASE_ID , BACKUP_ID )
135
+ @pytest .mark .dependency (depends = ["create_backup" ])
136
+ def test_cancel_backup (capsys , instance_id , sample_database ):
137
+ backup_sample .cancel_backup (
138
+ instance_id , sample_database .database_id , BACKUP_ID ,
139
+ )
166
140
out , _ = capsys .readouterr ()
167
141
cancel_success = "Backup creation was successfully cancelled." in out
168
142
cancel_failure = ("Backup was created before the cancel completed." in out ) and (
@@ -172,10 +146,12 @@ def test_cancel_backup(capsys):
172
146
173
147
174
148
@RetryErrors (exception = DeadlineExceeded , max_tries = 2 )
175
- def test_create_database_with_retention_period (capsys , spanner_instance ):
176
- backup_sample .create_database_with_version_retention_period (INSTANCE_ID , RETENTION_DATABASE_ID , RETENTION_PERIOD )
149
+ def test_create_database_with_retention_period (capsys , sample_instance ):
150
+ backup_sample .create_database_with_version_retention_period (
151
+ sample_instance .instance_id , RETENTION_DATABASE_ID , RETENTION_PERIOD ,
152
+ )
177
153
out , _ = capsys .readouterr ()
178
154
assert (RETENTION_DATABASE_ID + " created with " ) in out
179
155
assert ("retention period " + RETENTION_PERIOD ) in out
180
- database = spanner_instance .database (RETENTION_DATABASE_ID )
156
+ database = sample_instance .database (RETENTION_DATABASE_ID )
181
157
database .drop ()
0 commit comments