fix: ensure ipv4 is checked, not sometimes ipv6
continuous-integration/drone/push Build is passing Details

See coop-cloud/organising#490
This commit is contained in:
decentral1se 2023-10-04 03:24:30 +02:00 committed by decentral1se
parent 379915587c
commit 422c642949
2 changed files with 35 additions and 4 deletions

View File

@ -7,11 +7,16 @@ import (
// EnsureIPv4 ensures that an ipv4 address is set for a domain name
func EnsureIPv4(domainName string) (string, error) {
ipv4, err := net.ResolveIPAddr("ip", domainName)
ipv4, err := net.ResolveIPAddr("ip4", domainName)
if err != nil {
return "", err
}
// NOTE(d1): e.g. when there is only an ipv6 record available
if ipv4 == nil {
return "", fmt.Errorf("unable to resolve ipv4 address for %s", domainName)
}
return ipv4.String(), nil
}

View File

@ -3,6 +3,8 @@ package dns
import (
"fmt"
"testing"
"gotest.tools/v3/assert"
)
func TestEnsureDomainsResolveSameIPv4(t *testing.T) {
@ -11,9 +13,10 @@ func TestEnsureDomainsResolveSameIPv4(t *testing.T) {
serverName string
shouldValidate bool
}{
// NOTE(d1): DNS records get checked, so use something we control. if
// you're here because of a failing test, try `dig +short <domain>` to
// ensure stuff matches first!
// NOTE(d1): DNS records get checked, so use something that is maintained
// within the federation. if you're here because of a failing test, try
// `dig +short <domain>` to ensure stuff matches first! If flakyness
// becomes an issue we can look into mocking
{"docs.coopcloud.tech", "coopcloud.tech", true},
{"docs.coopcloud.tech", "swarm.autonomic.zone", true},
@ -36,3 +39,26 @@ func TestEnsureDomainsResolveSameIPv4(t *testing.T) {
}
}
}
func TestEnsureIpv4(t *testing.T) {
// NOTE(d1): DNS records get checked, so use something that is maintained
// within the federation. if you're here because of a failing test, try `dig
// +short <domain>` to ensure stuff matches first! If flakyness becomes an
// issue we can look into mocking
domainName := "collabora.ostrom.collective.tools"
serverName := "ostrom.collective.tools"
for i := 0; i < 15; i++ {
domainIpv4, err := EnsureIPv4(domainName)
if err != nil {
t.Fatal(err)
}
serverIpv4, err := EnsureIPv4(serverName)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, domainIpv4, serverIpv4)
}
}