common: handle per-candidate ports for external service checks

This commit is contained in:
Vincent Bernat
2024-01-20 09:22:09 +01:00
parent 621fff521d
commit 39882fd98e
4 changed files with 16 additions and 11 deletions

View File

@@ -20,7 +20,8 @@ import (
// SetupClickHouse configures a client to use for testing. // SetupClickHouse configures a client to use for testing.
func SetupClickHouse(t *testing.T, r *reporter.Reporter) *Component { func SetupClickHouse(t *testing.T, r *reporter.Reporter) *Component {
t.Helper() 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 := DefaultConfiguration()
config.Servers = []string{chServer} config.Servers = []string{chServer}
config.DialTimeout = 100 * time.Millisecond config.DialTimeout = 100 * time.Millisecond

View File

@@ -22,26 +22,29 @@ import (
// but we suppose that either the services are run through // but we suppose that either the services are run through
// docker compose manually and ready, either through CI and they are // docker compose manually and ready, either through CI and they are
// checked for readiness. // 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() t.Helper()
if testing.Short() { if testing.Short() {
t.Skipf("Skip test with real %s in short mode", name) t.Skipf("Skip test with real %s in short mode", name)
} }
mandatory := os.Getenv("CI_AKVORADO_FUNCTIONAL_TESTS") != "" mandatory := os.Getenv("CI_AKVORADO_FUNCTIONAL_TESTS") != ""
var err error
found := "" server := ""
for _, dnsCandidate := range dnsCandidates { for _, candidate := range candidates {
resolv := net.Resolver{PreferGo: true} resolv := net.Resolver{PreferGo: true}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) 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() cancel()
if err == nil { if err == nil {
found = dnsCandidate server = candidate
break break
} }
} }
if found == "" { if server == "" {
if mandatory { if mandatory {
t.Fatalf("%s cannot be resolved (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name) 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 var d net.Dialer
server := net.JoinHostPort(found, port)
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
for { for {
_, err := d.DialContext(ctx, "tcp", server) _, err := d.DialContext(ctx, "tcp", server)

View File

@@ -118,7 +118,8 @@ func TestCacheByRequestBody(t *testing.T) {
} }
func TestRedis(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{ client := redis.NewClient(&redis.Options{
Addr: server, Addr: server,
DB: 10, DB: 10,

View File

@@ -16,7 +16,8 @@ import (
// SetupKafkaBroker configures a client to use for testing. // SetupKafkaBroker configures a client to use for testing.
func SetupKafkaBroker(t *testing.T) (sarama.Client, []string) { 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 // Wait for broker to be ready
saramaConfig := sarama.NewConfig() saramaConfig := sarama.NewConfig()