Commit 1701899 1 parent f3362fe commit 1701899 Copy full SHA for 1701899
File tree 4 files changed +47
-5
lines changed
4 files changed +47
-5
lines changed Original file line number Diff line number Diff line change 4
4
# license that can be found in the LICENSE file or at
5
5
# https://developers.google.com/open-source/licenses/bsd
6
6
7
+ import time
7
8
import uuid
8
9
9
10
from google .api_core .exceptions import Aborted
12
13
from test_utils .retry import RetryErrors
13
14
14
15
import autocommit
16
+ from snippets_test import cleanup_old_instances
15
17
16
18
17
19
def unique_instance_id ():
@@ -31,9 +33,18 @@ def unique_database_id():
31
33
@pytest .fixture (scope = "module" )
32
34
def spanner_instance ():
33
35
spanner_client = spanner .Client ()
34
- config_name = f"{ spanner_client .project_name } /instanceConfigs/regional-us-central1"
35
-
36
- instance = spanner_client .instance (INSTANCE_ID , config_name )
36
+ cleanup_old_instances (spanner_client )
37
+ instance_config = "{}/instanceConfigs/{}" .format (
38
+ spanner_client .project_name , "regional-us-central1"
39
+ )
40
+ instance = spanner_client .instance (
41
+ INSTANCE_ID ,
42
+ instance_config ,
43
+ labels = {
44
+ "cloud_spanner_samples" : "true" ,
45
+ "created" : str (int (time .time ()))
46
+ }
47
+ )
37
48
op = instance .create ()
38
49
op .result (120 ) # block until completion
39
50
yield instance
Original file line number Diff line number Diff line change 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
14
15
import uuid
15
16
16
17
from google .api_core .exceptions import DeadlineExceeded
19
20
from test_utils .retry import RetryErrors
20
21
21
22
import backup_sample
23
+ from snippets_test import cleanup_old_instances
22
24
23
25
24
26
def unique_instance_id ():
@@ -49,10 +51,18 @@ def unique_backup_id():
49
51
@pytest .fixture (scope = "module" )
50
52
def spanner_instance ():
51
53
spanner_client = spanner .Client ()
54
+ cleanup_old_instances (spanner_client )
52
55
instance_config = "{}/instanceConfigs/{}" .format (
53
56
spanner_client .project_name , "regional-us-central1"
54
57
)
55
- instance = spanner_client .instance (INSTANCE_ID , instance_config )
58
+ instance = spanner_client .instance (
59
+ INSTANCE_ID ,
60
+ instance_config ,
61
+ labels = {
62
+ "cloud_spanner_samples" : "true" ,
63
+ "created" : str (int (time .time ()))
64
+ }
65
+ )
56
66
op = instance .create ()
57
67
op .result (120 ) # block until completion
58
68
yield instance
Original file line number Diff line number Diff line change 25
25
import datetime
26
26
import decimal
27
27
import logging
28
+ import time
28
29
29
30
from google .cloud import spanner
30
31
from google .cloud .spanner_v1 import param_types
@@ -44,6 +45,10 @@ def create_instance(instance_id):
44
45
configuration_name = config_name ,
45
46
display_name = "This is a display name." ,
46
47
node_count = 1 ,
48
+ labels = {
49
+ "cloud_spanner_samples" : "true" ,
50
+ "created" : str (int (time .time ()))
51
+ }
47
52
)
48
53
49
54
operation = instance .create ()
Original file line number Diff line number Diff line change 16
16
import uuid
17
17
18
18
from google .cloud import spanner
19
+ from google .cloud .spanner_v1 .instance import Instance
19
20
import pytest
20
21
21
22
import snippets
@@ -31,15 +32,30 @@ def unique_database_id():
31
32
return f"test-db-{ uuid .uuid4 ().hex [:10 ]} "
32
33
33
34
35
+ def cleanup_old_instances (spanner_client ):
36
+ # Delete test instances that are older than an hour.
37
+ cutoff = int (time .time ()) - 1 * 60 * 60
38
+ instance_pbs = spanner_client .list_instances ("labels.cloud_spanner_samples:true" )
39
+ for instance_pb in instance_pbs :
40
+ instance = Instance .from_pb (instance_pb , spanner_client )
41
+ if "created" not in instance .labels :
42
+ continue
43
+ create_time = int (instance .labels ["created" ])
44
+ if create_time > cutoff :
45
+ continue
46
+ instance .delete ()
47
+
48
+
34
49
INSTANCE_ID = unique_instance_id ()
35
50
DATABASE_ID = unique_database_id ()
36
51
CMEK_DATABASE_ID = unique_database_id ()
37
52
38
53
39
54
@pytest .fixture (scope = "module" )
40
55
def spanner_instance ():
41
- snippets .create_instance (INSTANCE_ID )
42
56
spanner_client = spanner .Client ()
57
+ cleanup_old_instances (spanner_client )
58
+ snippets .create_instance (INSTANCE_ID )
43
59
instance = spanner_client .instance (INSTANCE_ID )
44
60
yield instance
45
61
instance .delete ()
You can’t perform that action at this time.
0 commit comments