mirror of
https://github.com/gofiber/storage.git
synced 2025-10-16 13:41:26 +08:00
chore(s3): use require in tests, running a container per test function
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
|
||||
|
@@ -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()
|
||||
|
||||
|
Reference in New Issue
Block a user