~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/net/dnsclient_unix_test.go

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2014-11-18 15:12:26 UTC
  • mfrom: (14.2.12 vivid-proposed)
  • Revision ID: package-import@ubuntu.com-20141118151226-zug7vn93mn3dtiz3
Tags: 2:1.3.2-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - Support co-installability with gccgo-go tool:
    - d/rules,golang-go.install: Rename bin/go -> bin/golang-go
    - d/golang-go.{postinst,prerm}: Install/remove /usr/bin/go using
      alternatives.
  - d/copyright: Amendments for full compiliance with copyright format.
  - d/control: Demote golang-go.tools to Suggests to support Ubuntu MIR.
  - dropped patches (now upstream):
    - d/p/issue27650045_40001_50001.diff
    - d/p/issue28050043_60001_70001.diff
    - d/p/issue54790044_100001_110001.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
 
5
 
// +build darwin dragonfly freebsd linux netbsd openbsd
 
5
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
6
6
 
7
7
package net
8
8
 
9
9
import (
 
10
        "io"
 
11
        "io/ioutil"
 
12
        "os"
 
13
        "path"
 
14
        "reflect"
10
15
        "testing"
 
16
        "time"
11
17
)
12
18
 
13
19
func TestTCPLookup(t *testing.T) {
25
31
                t.Fatalf("exchange failed: %v", err)
26
32
        }
27
33
}
 
34
 
 
35
type resolvConfTest struct {
 
36
        *testing.T
 
37
        dir     string
 
38
        path    string
 
39
        started bool
 
40
        quitc   chan chan struct{}
 
41
}
 
42
 
 
43
func newResolvConfTest(t *testing.T) *resolvConfTest {
 
44
        dir, err := ioutil.TempDir("", "resolvConfTest")
 
45
        if err != nil {
 
46
                t.Fatalf("could not create temp dir: %v", err)
 
47
        }
 
48
 
 
49
        // Disable the default loadConfig
 
50
        onceLoadConfig.Do(func() {})
 
51
 
 
52
        r := &resolvConfTest{
 
53
                T:     t,
 
54
                dir:   dir,
 
55
                path:  path.Join(dir, "resolv.conf"),
 
56
                quitc: make(chan chan struct{}),
 
57
        }
 
58
 
 
59
        return r
 
60
}
 
61
 
 
62
func (r *resolvConfTest) Start() {
 
63
        loadConfig(r.path, 100*time.Millisecond, r.quitc)
 
64
        r.started = true
 
65
}
 
66
 
 
67
func (r *resolvConfTest) SetConf(s string) {
 
68
        // Make sure the file mtime will be different once we're done here,
 
69
        // even on systems with coarse (1s) mtime resolution.
 
70
        time.Sleep(time.Second)
 
71
 
 
72
        f, err := os.OpenFile(r.path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
 
73
        if err != nil {
 
74
                r.Fatalf("failed to create temp file %s: %v", r.path, err)
 
75
        }
 
76
        if _, err := io.WriteString(f, s); err != nil {
 
77
                f.Close()
 
78
                r.Fatalf("failed to write temp file: %v", err)
 
79
        }
 
80
        f.Close()
 
81
 
 
82
        if r.started {
 
83
                cfg.ch <- struct{}{} // fill buffer
 
84
                cfg.ch <- struct{}{} // wait for reload to begin
 
85
                cfg.ch <- struct{}{} // wait for reload to complete
 
86
        }
 
87
}
 
88
 
 
89
func (r *resolvConfTest) WantServers(want []string) {
 
90
        cfg.mu.RLock()
 
91
        defer cfg.mu.RUnlock()
 
92
        if got := cfg.dnsConfig.servers; !reflect.DeepEqual(got, want) {
 
93
                r.Fatalf("Unexpected dns server loaded, got %v want %v", got, want)
 
94
        }
 
95
}
 
96
 
 
97
func (r *resolvConfTest) Close() {
 
98
        resp := make(chan struct{})
 
99
        r.quitc <- resp
 
100
        <-resp
 
101
        if err := os.RemoveAll(r.dir); err != nil {
 
102
                r.Logf("failed to remove temp dir %s: %v", r.dir, err)
 
103
        }
 
104
}
 
105
 
 
106
func TestReloadResolvConfFail(t *testing.T) {
 
107
        if testing.Short() || !*testExternal {
 
108
                t.Skip("skipping test to avoid external network")
 
109
        }
 
110
 
 
111
        r := newResolvConfTest(t)
 
112
        defer r.Close()
 
113
 
 
114
        // resolv.conf.tmp does not exist yet
 
115
        r.Start()
 
116
        if _, err := goLookupIP("golang.org"); err == nil {
 
117
                t.Fatal("goLookupIP(missing) succeeded")
 
118
        }
 
119
 
 
120
        r.SetConf("nameserver 8.8.8.8")
 
121
        if _, err := goLookupIP("golang.org"); err != nil {
 
122
                t.Fatalf("goLookupIP(missing; good) failed: %v", err)
 
123
        }
 
124
 
 
125
        // Using a bad resolv.conf while we had a good
 
126
        // one before should not update the config
 
127
        r.SetConf("")
 
128
        if _, err := goLookupIP("golang.org"); err != nil {
 
129
                t.Fatalf("goLookupIP(missing; good; bad) failed: %v", err)
 
130
        }
 
131
}
 
132
 
 
133
func TestReloadResolvConfChange(t *testing.T) {
 
134
        if testing.Short() || !*testExternal {
 
135
                t.Skip("skipping test to avoid external network")
 
136
        }
 
137
 
 
138
        r := newResolvConfTest(t)
 
139
        defer r.Close()
 
140
 
 
141
        r.SetConf("nameserver 8.8.8.8")
 
142
        r.Start()
 
143
 
 
144
        if _, err := goLookupIP("golang.org"); err != nil {
 
145
                t.Fatalf("goLookupIP(good) failed: %v", err)
 
146
        }
 
147
        r.WantServers([]string{"[8.8.8.8]"})
 
148
 
 
149
        // Using a bad resolv.conf when we had a good one
 
150
        // before should not update the config
 
151
        r.SetConf("")
 
152
        if _, err := goLookupIP("golang.org"); err != nil {
 
153
                t.Fatalf("goLookupIP(good; bad) failed: %v", err)
 
154
        }
 
155
 
 
156
        // A new good config should get picked up
 
157
        r.SetConf("nameserver 8.8.4.4")
 
158
        r.WantServers([]string{"[8.8.4.4]"})
 
159
}