From 39882fd98ead6f76dae3de9bddadfbfef143bc90 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 20 Jan 2024 09:22:09 +0100 Subject: [PATCH] common: handle per-candidate ports for external service checks --- common/clickhousedb/tests.go | 3 ++- common/helpers/tests.go | 18 ++++++++++-------- common/httpserver/cache_test.go | 3 ++- common/kafka/tests.go | 3 ++- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/common/clickhousedb/tests.go b/common/clickhousedb/tests.go index 8ef9d6b1..216d531f 100644 --- a/common/clickhousedb/tests.go +++ b/common/clickhousedb/tests.go @@ -20,7 +20,8 @@ import ( // SetupClickHouse configures a client to use for testing. func SetupClickHouse(t *testing.T, r *reporter.Reporter) *Component { t.Helper() - chServer := helpers.CheckExternalService(t, "ClickHouse", []string{"clickhouse", "127.0.0.1"}, "9000") + chServer := helpers.CheckExternalService(t, "ClickHouse", + []string{"clickhouse:9000", "127.0.0.1:9000"}) config := DefaultConfiguration() config.Servers = []string{chServer} config.DialTimeout = 100 * time.Millisecond diff --git a/common/helpers/tests.go b/common/helpers/tests.go index 2d541dff..4155e942 100644 --- a/common/helpers/tests.go +++ b/common/helpers/tests.go @@ -22,26 +22,29 @@ import ( // but we suppose that either the services are run through // docker compose manually and ready, either through CI and they are // checked for readiness. -func CheckExternalService(t *testing.T, name string, dnsCandidates []string, port string) string { +func CheckExternalService(t *testing.T, name string, candidates []string) string { t.Helper() if testing.Short() { t.Skipf("Skip test with real %s in short mode", name) } mandatory := os.Getenv("CI_AKVORADO_FUNCTIONAL_TESTS") != "" - var err error - found := "" - for _, dnsCandidate := range dnsCandidates { + server := "" + for _, candidate := range candidates { resolv := net.Resolver{PreferGo: true} ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) - _, err = resolv.LookupHost(ctx, dnsCandidate) + hostname, _, err := net.SplitHostPort(candidate) + if err != nil { + t.Fatalf("%s is an invalid candidate", candidate) + } + _, err = resolv.LookupHost(ctx, hostname) cancel() if err == nil { - found = dnsCandidate + server = candidate break } } - if found == "" { + if server == "" { if mandatory { t.Fatalf("%s cannot be resolved (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name) } @@ -49,7 +52,6 @@ func CheckExternalService(t *testing.T, name string, dnsCandidates []string, por } var d net.Dialer - server := net.JoinHostPort(found, port) ctx, cancel := context.WithTimeout(context.Background(), time.Second) for { _, err := d.DialContext(ctx, "tcp", server) diff --git a/common/httpserver/cache_test.go b/common/httpserver/cache_test.go index 616c15b3..81193650 100644 --- a/common/httpserver/cache_test.go +++ b/common/httpserver/cache_test.go @@ -118,7 +118,8 @@ func TestCacheByRequestBody(t *testing.T) { } func TestRedis(t *testing.T) { - server := helpers.CheckExternalService(t, "Redis", []string{"redis", "127.0.0.1"}, "6379") + server := helpers.CheckExternalService(t, "Redis", + []string{"redis:6379", "127.0.0.1:6379"}) client := redis.NewClient(&redis.Options{ Addr: server, DB: 10, diff --git a/common/kafka/tests.go b/common/kafka/tests.go index c230101e..71b88d04 100644 --- a/common/kafka/tests.go +++ b/common/kafka/tests.go @@ -16,7 +16,8 @@ import ( // SetupKafkaBroker configures a client to use for testing. func SetupKafkaBroker(t *testing.T) (sarama.Client, []string) { - broker := helpers.CheckExternalService(t, "Kafka", []string{"kafka", "127.0.0.1"}, "9092") + broker := helpers.CheckExternalService(t, "Kafka", + []string{"kafka:9092", "127.0.0.1:9092"}) // Wait for broker to be ready saramaConfig := sarama.NewConfig()