-
Notifications
You must be signed in to change notification settings - Fork 2k
/
dns_testing.go
58 lines (49 loc) · 1.97 KB
/
dns_testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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))
}
})
}