diff --git a/s3/init_test.go b/s3/init_test.go index dc42627c..7cdb6bd7 100644 --- a/s3/init_test.go +++ b/s3/init_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/stretchr/testify/require" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/modules/minio" "github.com/testcontainers/testcontainers-go/wait" @@ -15,8 +16,6 @@ const ( bucket = "testbucket" ) -var testStore *Storage - const ( // minioImage is the default image used for running S3 in tests. minioImage = "docker.io/minio/minio:latest" @@ -25,7 +24,7 @@ const ( minioPass string = "minio-password" ) -func TestMain(m *testing.M) { +func newTestStore(t testing.TB) *Storage { img := minioImage if imgFromEnv := os.Getenv(minioImageEnvVar); imgFromEnv != "" { img = imgFromEnv @@ -51,7 +50,7 @@ func TestMain(m *testing.M) { panic(err) } - testStore = New( + testStore := New( Config{ Bucket: bucket, Endpoint: "http://" + conn, @@ -65,12 +64,9 @@ func TestMain(m *testing.M) { ) // Create test bucket. - _ = testStore.CreateBucket(bucket) + err = testStore.CreateBucket(bucket) + require.NoError(t, err) + // Do not delete test bucket, as the container is disposed after each test. - exitVal := m.Run() - - // Delete test bucket. - _ = testStore.DeleteBucket(bucket) - - os.Exit(exitVal) + return testStore } diff --git a/s3/s3_methods_test.go b/s3/s3_methods_test.go index a888eeca..56a169c2 100644 --- a/s3/s3_methods_test.go +++ b/s3/s3_methods_test.go @@ -11,6 +11,9 @@ import ( func Test_S3_CreateDeleteBucket(t *testing.T) { bkt := "test-new-bucket" + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.CreateBucket(bkt) require.NoError(t, err) @@ -21,6 +24,9 @@ func Test_S3_CreateDeleteBucket(t *testing.T) { func Test_S3_DeleteMany(t *testing.T) { val := []byte("doe") + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set("john1", val, 0) require.NoError(t, err) @@ -61,6 +67,9 @@ func Test_S3_SetWithChecksum(t *testing.T) { types.ChecksumAlgorithmSha256: sha256sum, } + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.SetWithChecksum(key, val, checksum) require.NoError(t, err) diff --git a/s3/s3_test.go b/s3/s3_test.go index 5ed2cc5f..fe2ad8f2 100644 --- a/s3/s3_test.go +++ b/s3/s3_test.go @@ -12,6 +12,9 @@ func Test_S3_Set(t *testing.T) { val = []byte("doe") ) + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set(key, val, 0) require.NoError(t, err) } @@ -22,6 +25,9 @@ func Test_S3_Set_Override(t *testing.T) { val = []byte("doe") ) + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -35,6 +41,9 @@ func Test_S3_Get(t *testing.T) { val = []byte("doe") ) + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -44,6 +53,9 @@ func Test_S3_Get(t *testing.T) { } func Test_S3_Get_NotExist(t *testing.T) { + testStore := newTestStore(t) + defer testStore.Close() + result, err := testStore.Get("notexist") require.NoError(t, err) require.Zero(t, len(result)) @@ -55,6 +67,9 @@ func Test_S3_Delete(t *testing.T) { val = []byte("doe") ) + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set(key, val, 0) require.NoError(t, err) @@ -69,6 +84,9 @@ func Test_S3_Delete(t *testing.T) { func Test_S3_Reset(t *testing.T) { val := []byte("doe") + testStore := newTestStore(t) + defer testStore.Close() + err := testStore.Set("john1", val, 0) require.NoError(t, err) @@ -88,14 +106,20 @@ func Test_S3_Reset(t *testing.T) { } func Test_S3_Close(t *testing.T) { - require.Nil(t, testStore.Close()) + testStore := newTestStore(t) + require.NoError(t, testStore.Close()) } func Test_S3_Conn(t *testing.T) { + testStore := newTestStore(t) + defer testStore.Close() require.True(t, testStore.Conn() != nil) } func Benchmark_S3_Set(b *testing.B) { + testStore := newTestStore(b) + defer testStore.Close() + b.ReportAllocs() b.ResetTimer() @@ -108,6 +132,9 @@ func Benchmark_S3_Set(b *testing.B) { } func Benchmark_S3_Get(b *testing.B) { + testStore := newTestStore(b) + defer testStore.Close() + err := testStore.Set("john", []byte("doe"), 0) require.NoError(b, err) @@ -122,6 +149,9 @@ func Benchmark_S3_Get(b *testing.B) { } func Benchmark_S3_SetAndDelete(b *testing.B) { + testStore := newTestStore(b) + defer testStore.Close() + b.ReportAllocs() b.ResetTimer()