-
Notifications
You must be signed in to change notification settings - Fork 2k
/
dns_testing.go
60 lines (50 loc) · 2 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
59
60
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package testutils
import (
"strings"
"testing"
"github.com/docker/docker/libnetwork/resolvconf"
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/shoenig/test/must"
)
// 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()
must.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, "")
must.Zero(t, r.exitCode)
resolvConf := []byte(strings.TrimSpace(r.stdout))
if dns != nil {
if len(dns.Servers) > 0 {
must.SliceContainsAll(t, dns.Servers, resolvconf.GetNameservers(resolvConf, resolvconf.IP))
}
if len(dns.Searches) > 0 {
must.SliceContainsAll(t, dns.Searches, resolvconf.GetSearchDomains(resolvConf))
}
if len(dns.Options) > 0 {
must.SliceContainsAll(t, dns.Options, resolvconf.GetOptions(resolvConf))
}
} else {
systemPath := "/etc/resolv.conf"
if !usesHostNetwork {
systemPath = resolvconf.Path()
}
system, specificErr := resolvconf.GetSpecific(systemPath)
must.NoError(t, specificErr)
must.SliceContainsAll(t, resolvconf.GetNameservers(system.Content, resolvconf.IP), resolvconf.GetNameservers(resolvConf, resolvconf.IP))
must.SliceContainsAll(t, resolvconf.GetSearchDomains(system.Content), resolvconf.GetSearchDomains(resolvConf))
must.SliceContainsAll(t, resolvconf.GetOptions(system.Content), resolvconf.GetOptions(resolvConf))
}
})
}