open-nomad/plugins/drivers/testutils/dns_testing.go

59 lines
2.0 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))
}
})
}