mirror of
https://github.com/gofiber/storage.git
synced 2025-10-24 00:43:12 +08:00
Updated Testcontainer with generic one
This commit is contained in:
@@ -2,35 +2,73 @@ package aerospike
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/aerospike/aerospike-client-go/v8"
|
"github.com/aerospike/aerospike-client-go/v8"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/testcontainers/testcontainers-go"
|
"github.com/testcontainers/testcontainers-go"
|
||||||
tcaerospike "github.com/testcontainers/testcontainers-go/modules/aerospike"
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
aerospikeImage = "aerospike/aerospike-server:latest"
|
// aerospikeImage is the default image used for running Aerospike in tests.
|
||||||
aerospikePort = "3000/tcp"
|
aerospikeImage = "aerospike/aerospike-server:latest"
|
||||||
aerospikeNamespace = "test"
|
aerospikeImageEnvVar string = "TEST_AEROSPIKE_IMAGE"
|
||||||
|
aerospikePort = "3000/tcp"
|
||||||
|
febricPort = "3001/tcp"
|
||||||
|
heartbeatPort = "3002/tcp"
|
||||||
|
infoPort = "3003/tcp"
|
||||||
|
aerospikeReadyLog = "migrations: complete"
|
||||||
|
aerospikeNamespace = "test"
|
||||||
)
|
)
|
||||||
|
|
||||||
// setupAerospikeTestClient sets up a test Aerospike client
|
// startAerospikeContainer starts an Aerospike container for testing
|
||||||
// using testcontainers-go. It starts an Aerospike container and returns
|
func startAerospikeContainer(ctx context.Context) (testcontainers.Container, error) {
|
||||||
// a Storage instance configured to connect to the container.
|
// Get custom image from env if specified
|
||||||
// The container is cleaned up after the test completes.
|
image := aerospikeImage
|
||||||
func setupAerospikeTestClient(t *testing.T) *Storage {
|
if envImage := os.Getenv(aerospikeImageEnvVar); envImage != "" {
|
||||||
t.Helper()
|
image = envImage
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
c, err := tcaerospike.Run(ctx, aerospikeImage)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to start aerospike container: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
testcontainers.CleanupContainer(t, c)
|
// Container config
|
||||||
|
req := testcontainers.ContainerRequest{
|
||||||
|
Image: image,
|
||||||
|
ExposedPorts: []string{aerospikePort, febricPort, heartbeatPort, infoPort},
|
||||||
|
WaitingFor: wait.ForAll(
|
||||||
|
wait.ForLog(aerospikeReadyLog),
|
||||||
|
wait.ForListeningPort(aerospikePort).WithStartupTimeout(5*time.Second),
|
||||||
|
wait.ForListeningPort(febricPort).WithStartupTimeout(5*time.Second),
|
||||||
|
wait.ForListeningPort(heartbeatPort).WithStartupTimeout(5*time.Second),
|
||||||
|
),
|
||||||
|
Cmd: []string{
|
||||||
|
"--config-file",
|
||||||
|
"/etc/aerospike/aerospike.conf",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start container
|
||||||
|
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
|
||||||
|
ContainerRequest: req,
|
||||||
|
Started: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to start aerospike container: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return container, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupTestClient creates a client connected to the test container
|
||||||
|
func setupTestClient(t *testing.T) *Storage {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
c, err := startAerospikeContainer(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to start Aerospike container: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Extract host and port
|
// Extract host and port
|
||||||
host, err := c.Host(context.TODO())
|
host, err := c.Host(context.TODO())
|
||||||
@@ -43,12 +81,16 @@ func setupAerospikeTestClient(t *testing.T) *Storage {
|
|||||||
t.Fatalf("Failed to get container port: %v", err)
|
t.Fatalf("Failed to get container port: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testcontainers.CleanupContainer(t, c)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to cleanup Aerospike container: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
return New(Config{
|
return New(Config{
|
||||||
Hosts: []*aerospike.Host{aerospike.NewHost(host, port.Int())},
|
Hosts: []*aerospike.Host{aerospike.NewHost(host, port.Int())},
|
||||||
Reset: true,
|
Reset: true,
|
||||||
Namespace: aerospikeNamespace,
|
Namespace: aerospikeNamespace,
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test_AeroSpikeDB_Get tests the Get method
|
// Test_AeroSpikeDB_Get tests the Get method
|
||||||
@@ -58,7 +100,7 @@ func Test_AeroSpikeDB_Get(t *testing.T) {
|
|||||||
val = []byte("doe")
|
val = []byte("doe")
|
||||||
)
|
)
|
||||||
|
|
||||||
testStore := setupAerospikeTestClient(t)
|
testStore := setupTestClient(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
// Set a value
|
// Set a value
|
||||||
@@ -79,7 +121,7 @@ func Test_AeroSpikeDB_Delete(t *testing.T) {
|
|||||||
val = []byte("doe")
|
val = []byte("doe")
|
||||||
)
|
)
|
||||||
|
|
||||||
testStore := setupAerospikeTestClient(t)
|
testStore := setupTestClient(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
// Set a value
|
// Set a value
|
||||||
@@ -106,7 +148,7 @@ func Test_AeroSpikeDB_Reset(t *testing.T) {
|
|||||||
val2 = []byte("smith")
|
val2 = []byte("smith")
|
||||||
)
|
)
|
||||||
|
|
||||||
testStore := setupAerospikeTestClient(t)
|
testStore := setupTestClient(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
// Set multiple values
|
// Set multiple values
|
||||||
@@ -132,7 +174,7 @@ func Test_AeroSpikeDB_Reset(t *testing.T) {
|
|||||||
// Test_AeroSpikeDB_GetSchemaInfo tests the GetSchemaInfo method
|
// Test_AeroSpikeDB_GetSchemaInfo tests the GetSchemaInfo method
|
||||||
func Test_AeroSpikeDB_GetSchemaInfo(t *testing.T) {
|
func Test_AeroSpikeDB_GetSchemaInfo(t *testing.T) {
|
||||||
|
|
||||||
testStore := setupAerospikeTestClient(t)
|
testStore := setupTestClient(t)
|
||||||
defer testStore.Close()
|
defer testStore.Close()
|
||||||
|
|
||||||
// Get schema info
|
// Get schema info
|
||||||
|
Reference in New Issue
Block a user