/
dnsmasq_test.go
51 lines (43 loc) · 1.05 KB
/
dnsmasq_test.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
package dockertest_test
import (
"fmt"
"github.com/miekg/dns"
"github.com/oov/dockertest"
)
func Example_dnsmasq() {
c, err := dockertest.New(dockertest.Config{
Image: "andyshinn/dnsmasq", // or "andyshinn/dnsmasq:latest"
Name: "dockertest-dnsmasq",
Args: []string{
"--user=root",
"--address=/the-answer-to-life-the-universe-and-everything/42.42.42.42",
},
PortMapping: map[string]string{
"53/udp": "auto",
"53/tcp": "auto",
},
})
if err != nil {
panic(err)
}
defer c.Close()
// wait until the container has started listening
if err = c.Wait(nil); err != nil {
panic(err)
}
var m dns.Msg
m.SetQuestion("the-answer-to-life-the-universe-and-everything.", dns.TypeA)
in, _, err := (&dns.Client{}).Exchange(&m, c.Mapped["53/udp"].String())
if err != nil {
panic(err)
}
fmt.Println("UDP", in.Answer[0].(*dns.A).A)
in, _, err = (&dns.Client{Net: "tcp"}).Exchange(&m, c.Mapped["53/tcp"].String())
if err != nil {
panic(err)
}
fmt.Println("TCP", in.Answer[0].(*dns.A).A)
// Output:
// UDP 42.42.42.42
// TCP 42.42.42.42
}