1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import random
16+ import string
17+
1518import export
1619from gcloud import logging
1720from gcp .testing import eventually_consistent
1821import pytest
1922
20- TEST_SINK_NAME = 'example_sink '
23+ TEST_SINK_NAME_TMPL = 'example_sink_{} '
2124TEST_SINK_FILTER = 'severity>=CRITICAL'
2225
2326
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
2534def example_sink (cloud_config ):
2635 client = logging .Client ()
2736
2837 sink = client .sink (
29- TEST_SINK_NAME ,
38+ TEST_SINK_NAME_TMPL . format ( _random_id ()) ,
3039 TEST_SINK_FILTER ,
3140 'storage.googleapis.com/{bucket}' .format (
3241 bucket = cloud_config .storage_bucket ))
3342
34- if sink .exists ():
35- sink .delete ()
36-
3743 sink .create ()
3844
39- return sink
45+ yield sink
46+
47+ try :
48+ sink .delete ()
49+ except :
50+ pass
4051
4152
4253def test_list (example_sink , capsys ):
@@ -48,31 +59,32 @@ def _():
4859
4960
5061def 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
6275
6376 out , _ = capsys .readouterr ()
64- assert TEST_SINK_NAME in out
65- assert sink .exists ()
77+ assert sink_name in out
6678
6779
6880def test_update (example_sink , capsys ):
6981 updated_filter = 'severity>=INFO'
70- export .update_sink (TEST_SINK_NAME , updated_filter )
82+ export .update_sink (example_sink . name , updated_filter )
7183
7284 example_sink .reload ()
7385 assert example_sink .filter_ == updated_filter
7486
7587
7688def test_delete (example_sink , capsys ):
77- export .delete_sink (TEST_SINK_NAME )
89+ export .delete_sink (example_sink . name )
7890 assert not example_sink .exists ()
0 commit comments