8000 test: Added timestamp to sink names + autodelete sinks older than 2 h… · googleapis/python-logging@b86081e · GitHub
[go: up one dir, main page]

Skip to content

Commit b86081e

Browse files
test: Added timestamp to sink names + autodelete sinks older than 2 hours in export_test.py (#925)
* chore: Running this to remove all sinks * readd try block * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * added timestamp to sink name * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Fixed regex string * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 5bc9b44 commit b86081e

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

samples/snippets/export_test.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
# limitations under the License.
1414

1515
import os
16+
import re
1617
import random
1718
import string
19+
import time
1820

1921
import backoff
2022
from google.cloud import logging
@@ -24,8 +26,13 @@
2426

2527

2628
BUCKET = os.environ["CLOUD_STORAGE_BUCKET"]
27-
TEST_SINK_NAME_TMPL = "example_sink_{}"
29+
TEST_SINK_NAME_TMPL = "example_sink_{}_{}"
2830
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
2936

3037

3138
def _random_id():
@@ -34,12 +41,36 @@ def _random_id():
3441
)
3542

3643

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+
3768
@pytest.fixture
38-
def example_sink():
69+
def example_sink(cleanup_old_sinks):
3970
client = logging.Client()
4071

4172
sink = client.sink(
42-
TEST_SINK_NAME_TMPL.format(_random_id()),
73+
_create_sink_name(),
4374
filter_=TEST_SINK_FILTER,
4475
destination="storage.googleapis.com/{bucket}".format(bucket=BUCKET),
4576
)
@@ -48,10 +79,7 @@ def example_sink():
4879

4980
yield sink
5081

51-
try:
52-
sink.delete()
53-
except Exception:
54-
pass
82+
_delete_sink(sink)
5583

5684

5785
def test_list(example_sink, capsys):
@@ -65,16 +93,13 @@ def eventually_consistent_test():
6593

6694

6795
def test_create(capsys):
68-
sink_name = TEST_SINK_NAME_TMPL.format(_random_id())
96+
sink_name = _create_sink_name()
6997

7098
try:
7199
export.create_sink(sink_name, BUCKET, TEST_SINK_FILTER)
72100
# Clean-up the temporary sink.
73101
finally:
74-
try:
75-
logging.Client().sink(sink_name).delete()
76-
except Exception:
77-
pass
102+
_delete_sink(logging.Client().sink(sink_name))
78103

79104
out, _ = capsys.readouterr()
80105
assert sink_name in out

0 commit comments

Comments
 (0)
0