* test: testcontainers improvements Use the endpoint feature to get the connection URL for the container. There are cases where localhost is not the correct one, for example when DOCKER_HOST is set to another machine. Also, don't specify the external port for the mapping so a random unused port is used, in cases when there is already Valkey/Redis running as a container and port mapped externally on 6379. * also remove this hack, doesn't seem necessary.
47 lines
971 B
Go
47 lines
971 B
Go
package valkey
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/TecharoHQ/anubis/lib/store/storetest"
|
|
"github.com/testcontainers/testcontainers-go"
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
|
)
|
|
|
|
func TestImpl(t *testing.T) {
|
|
if os.Getenv("DONT_USE_NETWORK") != "" {
|
|
t.Skip("test requires network egress")
|
|
return
|
|
}
|
|
|
|
testcontainers.SkipIfProviderIsNotHealthy(t)
|
|
|
|
valkeyC, err := testcontainers.Run(
|
|
t.Context(), "valkey/valkey:8",
|
|
testcontainers.WithExposedPorts("6379/tcp"),
|
|
testcontainers.WithWaitStrategy(
|
|
wait.ForListeningPort("6379/tcp"),
|
|
wait.ForLog("Ready to accept connections"),
|
|
),
|
|
)
|
|
testcontainers.CleanupContainer(t, valkeyC)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
endpoint, err := valkeyC.PortEndpoint(t.Context(), "6379/tcp", "redis")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
data, err := json.Marshal(Config{
|
|
URL: endpoint,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
storetest.Common(t, Factory{}, json.RawMessage(data))
|
|
}
|