test: Valkey test improvements for testcontainers (#1280)

* 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.
This commit is contained in:
Henri Vasserman 2025-11-15 21:32:37 +02:00 committed by GitHub
parent f6bf98fa28
commit 6c8629e3ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,20 +2,14 @@ package valkey
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"testing" "testing"
"github.com/TecharoHQ/anubis/internal"
"github.com/TecharoHQ/anubis/lib/store/storetest" "github.com/TecharoHQ/anubis/lib/store/storetest"
"github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait" "github.com/testcontainers/testcontainers-go/wait"
) )
func init() {
internal.UnbreakDocker()
}
func TestImpl(t *testing.T) { func TestImpl(t *testing.T) {
if os.Getenv("DONT_USE_NETWORK") != "" { if os.Getenv("DONT_USE_NETWORK") != "" {
t.Skip("test requires network egress") t.Skip("test requires network egress")
@ -24,26 +18,26 @@ func TestImpl(t *testing.T) {
testcontainers.SkipIfProviderIsNotHealthy(t) testcontainers.SkipIfProviderIsNotHealthy(t)
req := testcontainers.ContainerRequest{ valkeyC, err := testcontainers.Run(
Image: "valkey/valkey:8", t.Context(), "valkey/valkey:8",
WaitingFor: wait.ForLog("Ready to accept connections"), testcontainers.WithExposedPorts("6379/tcp"),
} testcontainers.WithWaitStrategy(
valkeyC, err := testcontainers.GenericContainer(t.Context(), testcontainers.GenericContainerRequest{ wait.ForListeningPort("6379/tcp"),
ContainerRequest: req, wait.ForLog("Ready to accept connections"),
Started: true, ),
}) )
testcontainers.CleanupContainer(t, valkeyC) testcontainers.CleanupContainer(t, valkeyC)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
containerIP, err := valkeyC.ContainerIP(t.Context()) endpoint, err := valkeyC.PortEndpoint(t.Context(), "6379/tcp", "redis")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
data, err := json.Marshal(Config{ data, err := json.Marshal(Config{
URL: fmt.Sprintf("redis://%s:6379/0", containerIP), URL: endpoint,
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)