fix(providers): allow S3 versioned tests cleanup to succeed (#5038)

- allow S3 versioned tests cleanup to succeed
- use ContextForCleanup
This commit is contained in:
Julio López
2025-11-24 22:32:51 -08:00
committed by GitHub
parent b9e5279d76
commit e1e1e0d807
6 changed files with 72 additions and 27 deletions

View File

@@ -41,6 +41,18 @@ func Context(t testingT) context.Context {
return ContextWithLevel(t, LevelDebug)
}
// ContextForCleanup returns a context with attached logger that emits all log entries to go testing.T log output.
// This context is not canceled when the test finishes, so it is suitable to be used in cleanup functions.
func ContextForCleanup(t testingT) context.Context {
return contextWithLevelForCleanup(t, LevelDebug)
}
func contextWithLevelForCleanup(t testingT, level Level) context.Context {
return logging.WithLogger(context.WithoutCancel(t.Context()), func(module string) logging.Logger {
return PrintfLevel(t.Logf, "["+module+"] ", level)
})
}
// ContextWithLevel returns a context with attached logger that emits all log entries with given log level or above.
func ContextWithLevel(t testingT, level Level) context.Context {
return logging.WithLogger(t.Context(), func(module string) logging.Logger {

View File

@@ -86,7 +86,9 @@ func TestCleanupOldData(t *testing.T) {
require.NoError(t, err)
defer st.Close(ctx)
t.Cleanup(func() {
st.Close(testlogging.ContextForCleanup(t))
})
blobtesting.CleanupOldData(ctx, t, st, blobtesting.MinCleanupAge)
}
@@ -152,8 +154,12 @@ func TestAzureStorageSASToken(t *testing.T) {
require.NoError(t, err)
cancel()
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)
@@ -190,8 +196,12 @@ func TestAzureStorageClientSecret(t *testing.T) {
require.NoError(t, err)
cancel()
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)
@@ -228,8 +238,12 @@ func TestAzureStorageClientCertificate(t *testing.T) {
require.NoError(t, err)
cancel()
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)
@@ -266,8 +280,12 @@ func TestAzureFederatedIdentity(t *testing.T) {
require.NoError(t, err)
cancel()
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)

View File

@@ -81,8 +81,12 @@ func TestB2Storage(t *testing.T) {
cancel()
require.NoError(t, err)
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)

View File

@@ -53,8 +53,12 @@ func TestGCSStorage(t *testing.T) {
cancel()
require.NoError(t, err)
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, blob.PutOptions{})

View File

@@ -537,10 +537,9 @@ func TestNeedMD5AWS(t *testing.T) {
getOrMakeBucket(t, cli, options, true)
// ensure it is a bucket with object locking enabled
want := "Enabled"
if got, _, _, _, _ := cli.GetObjectLockConfig(ctx, options.BucketName); got != want {
t.Fatalf("object locking is not enabled: got '%s', want '%s'", got, want)
}
got, _, _, _, _ := cli.GetObjectLockConfig(ctx, options.BucketName) //nolint:dogsled
require.Equal(t, "Enabled", got, "object locking is not enabled")
// ensure a locking configuration is in place
lockingMode := minio.Governance
@@ -555,7 +554,7 @@ func TestNeedMD5AWS(t *testing.T) {
require.NoError(t, err, "could not create storage")
t.Cleanup(func() {
blobtesting.CleanupOldData(ctx, t, s, 0)
blobtesting.CleanupOldData(testlogging.ContextForCleanup(t), t, s, 0)
})
err = s.PutBlob(ctx, blob.ID("test-put-blob-0"), gather.FromSlice([]byte("xxyasdf243z")), blob.PutOptions{})
@@ -586,8 +585,12 @@ func testStorage(t *testing.T, options *Options, runValidationTest bool, opts bl
cancel()
require.NoError(t, err)
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
blobtesting.VerifyStorage(ctx, t, st, opts)
blobtesting.AssertConnectionInfoRoundTrips(ctx, t, st)
@@ -608,8 +611,12 @@ func testPutBlobWithInvalidRetention(t *testing.T, options Options, opts blob.Pu
st, err := newStorage(ctx, &options)
require.NoError(t, err)
defer st.Close(ctx)
defer blobtesting.CleanupOldData(ctx, t, st, 0)
t.Cleanup(func() {
ctx := testlogging.ContextForCleanup(t)
blobtesting.CleanupOldData(ctx, t, st, 0)
st.Close(ctx)
})
// Now attempt to add a block and expect to fail
require.Error(t,

View File

@@ -849,25 +849,25 @@ func isRetriable(err error) bool {
func getVersionedTestStore(tb testing.TB, envName string) *s3Storage {
tb.Helper()
ctx := testlogging.Context(tb)
o := getProviderOptions(tb, envName)
o.Prefix = path.Join(tb.Name(), uuid.NewString()) + "/"
s, err := newStorage(ctx, o)
s, err := newStorage(testlogging.Context(tb), o)
require.NoError(tb, err, "error creating versioned store client")
tb.Cleanup(func() {
cleanupVersions(tb, s)
ctx := testlogging.ContextForCleanup(tb)
cleanupVersions(ctx, tb, s)
blobtesting.CleanupOldData(ctx, tb, s, 0)
})
return s
}
func cleanupVersions(tb testing.TB, s *s3Storage) {
func cleanupVersions(ctx context.Context, tb testing.TB, s *s3Storage) {
tb.Helper()
ctx := testlogging.Context(tb)
ch := make(chan minio.ObjectInfo, 4)
errChan := s.cli.RemoveObjects(ctx, s.BucketName, ch, minio.RemoveObjectsOptions{})