aboutsummaryrefslogtreecommitdiff
path: root/src/net/dnsclient_unix_test.go
diff options
context:
space:
mode:
authorDan Peterson <dpiddy@gmail.com>2016-09-15 17:24:42 -0300
committerMatthew Dempsky <mdempsky@google.com>2016-09-17 00:34:19 +0000
commita1bf203b57ab854f2c1b7668ae96bc34e60e02fc (patch)
tree662a0e5b8a3be9c43a31b07fd03c75620621cc5f /src/net/dnsclient_unix_test.go
parentf7e49f6644bde3f17b4a795218a35876347455a1 (diff)
downloadgo-a1bf203b57ab854f2c1b7668ae96bc34e60e02fc.tar.gz
go-a1bf203b57ab854f2c1b7668ae96bc34e60e02fc.zip
net: respect resolv.conf rotate option
Instead of ranging over servers in the config, grab an offset from the config that is used to determine indices. When the rotate option is enabled, the offset increases which rotates queries through servers. Otherwise, it is always 0 which uses servers in config order. Fixes #17126 Change-Id: If233f6de7bfa42f88570055b9ab631be08a76b3e Reviewed-on: https://go-review.googlesource.com/29233 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/net/dnsclient_unix_test.go')
-rw-r--r--src/net/dnsclient_unix_test.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/net/dnsclient_unix_test.go b/src/net/dnsclient_unix_test.go
index f185642feb..8ee64d407c 100644
--- a/src/net/dnsclient_unix_test.go
+++ b/src/net/dnsclient_unix_test.go
@@ -797,3 +797,73 @@ func TestRetryTimeout(t *testing.T) {
t.Error("deadline0 still zero", deadline0)
}
}
+
+func TestRotate(t *testing.T) {
+ // without rotation, always uses the first server
+ testRotate(t, false, []string{"192.0.2.1", "192.0.2.2"}, []string{"192.0.2.1:53", "192.0.2.1:53", "192.0.2.1:53"})
+
+ // with rotation, rotates through back to first
+ testRotate(t, true, []string{"192.0.2.1", "192.0.2.2"}, []string{"192.0.2.1:53", "192.0.2.2:53", "192.0.2.1:53"})
+}
+
+func testRotate(t *testing.T, rotate bool, nameservers, wantServers []string) {
+ origTestHookDNSDialer := testHookDNSDialer
+ defer func() { testHookDNSDialer = origTestHookDNSDialer }()
+
+ conf, err := newResolvConfTest()
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer conf.teardown()
+
+ var confLines []string
+ for _, ns := range nameservers {
+ confLines = append(confLines, "nameserver "+ns)
+ }
+ if rotate {
+ confLines = append(confLines, "options rotate")
+ }
+
+ if err := conf.writeAndUpdate(confLines); err != nil {
+ t.Fatal(err)
+ }
+
+ d := &fakeDNSDialer{}
+ testHookDNSDialer = func() dnsDialer { return d }
+
+ var usedServers []string
+ d.rh = func(s string, q *dnsMsg, _ time.Time) (*dnsMsg, error) {
+ usedServers = append(usedServers, s)
+
+ r := &dnsMsg{
+ dnsMsgHdr: dnsMsgHdr{
+ id: q.id,
+ response: true,
+ recursion_available: true,
+ },
+ question: q.question,
+ answer: []dnsRR{
+ &dnsRR_CNAME{
+ Hdr: dnsRR_Header{
+ Name: q.question[0].Name,
+ Rrtype: dnsTypeCNAME,
+ Class: dnsClassINET,
+ },
+ Cname: "golang.org",
+ },
+ },
+ }
+ return r, nil
+ }
+
+ // len(nameservers) + 1 to allow rotation to get back to start
+ for i := 0; i < len(nameservers)+1; i++ {
+ if _, err := goLookupCNAME(context.Background(), "www.golang.org"); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ if !reflect.DeepEqual(usedServers, wantServers) {
+ t.Fatalf("rotate=%t got used servers:\n%v\nwant:\n%v", rotate, usedServers, wantServers)
+ }
+}