13
13
# limitations under the License.
14
14
15
15
import os
16
+ import re
16
17
import random
17
18
import string
19
+ import time
18
20
19
21
import backoff
20
22
from google .cloud import logging
24
26
25
27
26
28
BUCKET = os .environ ["CLOUD_STORAGE_BUCKET" ]
27
- TEST_SINK_NAME_TMPL = "example_sink_{}"
29
+ TEST_SINK_NAME_TMPL = "example_sink_{}_{} "
28
30
TEST_SINK_FILTER = "severity>=CRITICAL"
31
+ TIMESTAMP = int (time .time ())
32
+
33
+ # Threshold beyond which the cleanup_old_sinks fixture will delete
34
+ # old sink, in seconds
35
+ CLEANUP_THRESHOLD = 7200 # 2 hours
29
36
30
37
31
38
def _random_id ():
@@ -34,12 +41,36 @@ def _random_id():
34
41
)
35
42
36
43
44
+ def _create_sink_name ():
45
+ return TEST_SINK_NAME_TMPL .format (TIMESTAMP , _random_id ())
46
+
47
+
48
+ @backoff .on_exception (backoff .expo , Exception , max_time = 60 , raise_on_giveup = False )
49
+ def _delete_sink (sink ):
50
+ sink .delete ()
51
+
52
+
53
+ # Runs once for entire test suite
54
+ @pytest .fixture (scope = "module" )
55
+ def cleanup_old_sinks ():
56
+ client = logging .Client ()
57
+ test_sink_name_regex = (
58
+ r"^" + TEST_SINK_NAME_TMPL .format (r"(\d+)" , r"[A-Z0-9]{6}" ) + r"$"
59
+ )
60
+ for sink in client .list_sinks ():
61
+ match = re .match (test_sink_name_regex , sink .name )
62
+ if match :
63
+ sink_timestamp = int (match .group (1 ))
64
+ if TIMESTAMP - sink_timestamp > CLEANUP_THRESHOLD :
65
+ _delete_sink (sink )
66
+
67
+
37
68
@pytest .fixture
38
- def example_sink ():
69
+ def example_sink (cleanup_old_sinks ):
39
70
client = logging .Client ()
40
71
41
72
sink = client .sink (
42
- TEST_SINK_NAME_TMPL . format ( _random_id () ),
73
+ _create_sink_name ( ),
43
74
filter_ = TEST_SINK_FILTER ,
44
75
destination = "storage.googleapis.com/{bucket}" .format (bucket = BUCKET ),
45
76
)
@@ -48,10 +79,7 @@ def example_sink():
48
79
49
80
yield sink
50
81
51
- try :
52
- sink .delete ()
53
- except Exception :
54
- pass
82
+ _delete_sink (sink )
55
83
56
84
57
85
def test_list (example_sink , capsys ):
@@ -65,16 +93,13 @@ def eventually_consistent_test():
65
93
66
94
67
95
def test_create (capsys ):
68
- sink_name = TEST_SINK_NAME_TMPL . format ( _random_id () )
96
+ sink_name = _create_sink_name ( )
69
97
70
98
try :
71
99
export .create_sink (sink_name , BUCKET , TEST_SINK_FILTER )
72
100
# Clean-up the temporary sink.
73
101
finally :
74
- try :
75
- logging .Client ().sink (sink_name ).delete ()
76
- except Exception :
77
- pass
102
+ _delete_sink (logging .Client ().sink (sink_name ))
78
103
79
104
out , _ = capsys .readouterr ()
80
105
assert sink_name in out
0 commit comments