Skip to content

test: Added cleanup of old sink storage buckets #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions samples/snippets/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import time

import backoff
from google.cloud import logging
from google.cloud import logging, storage
import pytest

import export
Expand All @@ -46,8 +46,8 @@ def _create_sink_name():


@backoff.on_exception(backoff.expo, Exception, max_time=60, raise_on_giveup=False)
def _delete_sink(sink):
sink.delete()
def _delete_object(obj):
obj.delete()


# Runs once for entire test suite
Expand All @@ -62,7 +62,20 @@ def cleanup_old_sinks():
if match:
sink_timestamp = int(match.group(1))
if TIMESTAMP - sink_timestamp > CLEANUP_THRESHOLD:
_delete_sink(sink)
_delete_object(sink)

storage_client = storage.Client()

# See _sink_storage_setup in usage_guide.py for details about how
# sinks are named.
test_bucket_name_regex = r"^sink\-storage\-(\d+)$"
for bucket in storage_client.list_buckets():
match = re.match(test_bucket_name_regex, bucket.name)
if match:
# Bucket timestamp is int(time.time() * 1000)
bucket_timestamp = int(match.group(1))
if TIMESTAMP - bucket_timestamp // 1000 > CLEANUP_THRESHOLD:
_delete_object(bucket)


@pytest.fixture
Expand All @@ -79,7 +92,7 @@ def example_sink(cleanup_old_sinks):

yield sink

_delete_sink(sink)
_delete_object(sink)


def test_list(example_sink, capsys):
Expand All @@ -99,7 +112,7 @@ def test_create(capsys):
export.create_sink(sink_name, BUCKET, TEST_SINK_FILTER)
# Clean-up the temporary sink.
finally:
_delete_sink(logging.Client().sink(sink_name))
_delete_object(logging.Client().sink(sink_name))

out, _ = capsys.readouterr()
assert sink_name in out
Expand Down