12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import random
16
+ import string
17
+
15
18
import export
16
19
from gcloud import logging
17
20
from gcp .testing import eventually_consistent
18
21
import pytest
19
22
20
- TEST_SINK_NAME = 'example_sink '
23
+ TEST_SINK_NAME_TMPL = 'example_sink_{} '
21
24
TEST_SINK_FILTER = 'severity>=CRITICAL'
22
25
23
26
24
- @pytest .fixture
27
+ def _random_id ():
28
+ return '' .join (
29
+ random .choice (string .ascii_uppercase + string .digits )
30
+ for _ in range (6 ))
31
+
32
+
33
+ @pytest .yield_fixture
25
34
def example_sink (cloud_config ):
26
35
client = logging .Client ()
27
36
28
37
sink = client .sink (
29
- TEST_SINK_NAME ,
38
+ TEST_SINK_NAME_TMPL . format ( _random_id ()) ,
30
39
TEST_SINK_FILTER ,
31
40
'storage.googleapis.com/{bucket}' .format (
32
41
bucket = cloud_config .storage_bucket ))
33
42
34
- if sink .exists ():
35
- sink .delete ()
36
-
37
43
sink .create ()
38
44
39
- return sink
45
+ yield sink
46
+
47
+ try :
48
+ sink .delete ()
49
+ except :
50
+ pass
40
51
41
52
42
53
def test_list (example_sink , capsys ):
@@ -48,31 +59,32 @@ def _():
48
59
49
60
50
61
def test_create (cloud_config , capsys ):
51
- # Delete the sink if it exists, otherwise the test will fail in conflit.
52
- client = logging .Client ()
53
- sink = client .sink (TEST_SINK_NAME )
54
- if sink .exists ():
55
- sink .delete ()
56
-
57
- export .create_sink (
58
- TEST_SINK_NAME ,
59
- TEST_SINK_FILTER ,
60
- 'storage.googleapis.com/{bucket}' .format (
61
- bucket = cloud_config .storage_bucket ))
62
+ sink_name = TEST_SINK_NAME_TMPL .format (_random_id ())
63
+
64
+ try :
65
+ export .create_sink (
66
+ sink_name ,
67
+ cloud_config .storage_bucket ,
68
+ TEST_SINK_FILTER )
69
+ # Clean-up the temporary sink.
70
+ finally :
71
+ try :
72
+ logging .Client ().sink (sink_name ).delete ()
73
+ except :
74
+ pass
62
75
63
76
out , _ = capsys .readouterr ()
64
- assert TEST_SINK_NAME in out
65
- assert sink .exists ()
77
+ assert sink_name in out
66
78
67
79
68
80
def test_update (example_sink , capsys ):
69
81
updated_filter = 'severity>=INFO'
70
- export .update_sink (TEST_SINK_NAME , updated_filter )
82
+ export .update_sink (example_sink . name , updated_filter )
71
83
72
84
example_sink .reload ()
73
85
assert example_sink .filter_ == updated_filter
74
86
75
87
76
88
def test_delete (example_sink , capsys ):
77
- export .delete_sink (TEST_SINK_NAME )
89
+ export .delete_sink (example_sink . name )
78
90
assert not example_sink .exists ()
0 commit comments