mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
common: handle per-candidate ports for external service checks
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user