open-nomad/plugins/drivers/testutils/dns_testing.go
Mahmood Ali 567597e108 Compare to the correct host network setting
In systemd-resolved hosts with no DNS customizations, the docker driver
DNS setting should be compared to /run/systemd/resolve/resolv.conf while
exec/java drivers should be compared to /etc/resolv.conf.

When system-resolved is enabled, /etc/resolv.conf is a stub that points
to 127.0.0.53. Docker avoids this stub because this address isn't
accessible from the container.  The exec/java drivers that don't create
network isolations use the stub though in the default configuration.
2020-10-01 10:23:14 -04:00

59 lines
2 KiB
Go

package testutils
import (
"strings"
"testing"
dresolvconf "github.com/docker/libnetwork/resolvconf"
dtypes "github.com/docker/libnetwork/types"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/stretchr/testify/require"
)
// TestTaskDNSConfig asserts that a task is running with the given DNSConfig
func TestTaskDNSConfig(t *testing.T, driver *DriverHarness, taskID string, dns *drivers.DNSConfig) {
t.Run("dns_config", func(t *testing.T) {
caps, err := driver.Capabilities()
require.NoError(t, err)
// FS isolation is used here as a proxy for network isolation.
// This is true for the current built-in drivers but it is not necessarily so.
isolated := caps.FSIsolation != drivers.FSIsolationNone
usesHostNetwork := caps.FSIsolation != drivers.FSIsolationImage
if !isolated {
t.Skip("dns config not supported on non isolated drivers")
}
// write to a file and check it presence in host
r := execTask(t, driver, taskID, `cat /etc/resolv.conf`,
false, "")
require.Zero(t, r.exitCode)
resolvConf := []byte(strings.TrimSpace(r.stdout))
if dns != nil {
if len(dns.Servers) > 0 {
require.ElementsMatch(t, dns.Servers, dresolvconf.GetNameservers(resolvConf, dtypes.IP))
}
if len(dns.Searches) > 0 {
require.ElementsMatch(t, dns.Searches, dresolvconf.GetSearchDomains(resolvConf))
}
if len(dns.Options) > 0 {
require.ElementsMatch(t, dns.Options, dresolvconf.GetOptions(resolvConf))
}
} else {
systemPath := "/etc/resolv.conf"
if !usesHostNetwork {
systemPath = dresolvconf.Path()
}
system, err := dresolvconf.GetSpecific(systemPath)
require.NoError(t, err)
require.ElementsMatch(t, dresolvconf.GetNameservers(system.Content, dtypes.IP), dresolvconf.GetNameservers(resolvConf, dtypes.IP))
require.ElementsMatch(t, dresolvconf.GetSearchDomains(system.Content), dresolvconf.GetSearchDomains(resolvConf))
require.ElementsMatch(t, dresolvconf.GetOptions(system.Content), dresolvconf.GetOptions(resolvConf))
}
})
}