common/helpers: cache skip decision when requiring external services

This commit is contained in:
Vincent Bernat
2025-07-30 08:19:00 +02:00
parent a70029a4cd
commit 3d01a68bcb

View File

@@ -15,10 +15,16 @@ import (
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
)
var (
externalServiceSkipped = make(map[string]bool)
externalServiceMu sync.RWMutex
)
// CheckExternalService checks an external service, available either
// as a named service or on a specific port on localhost. This applies
// for example for Kafka and ClickHouse. The timeouts are quite short,
@@ -30,8 +36,20 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
if testing.Short() {
t.Skipf("Skip test with real %s in short mode", name)
}
mandatory := os.Getenv("CI_AKVORADO_FUNCTIONAL_TESTS") != ""
externalServiceMu.RLock()
if skipped, cached := externalServiceSkipped[name]; cached && skipped {
externalServiceMu.RUnlock()
if mandatory {
t.Fatalf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
} else {
t.Skipf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is not set)", name)
}
}
externalServiceMu.RUnlock()
server := ""
for _, candidate := range candidates {
resolv := net.Resolver{PreferGo: true}
@@ -48,6 +66,9 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
}
}
if server == "" {
externalServiceMu.Lock()
externalServiceSkipped[name] = true
externalServiceMu.Unlock()
if mandatory {
t.Fatalf("%s cannot be resolved (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
}
@@ -65,6 +86,9 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
t.Logf("DialContext() error:\n%+v", err)
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
externalServiceMu.Lock()
externalServiceSkipped[name] = true
externalServiceMu.Unlock()
if mandatory {
t.Fatalf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
} else {