-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-devdns_test.go
127 lines (93 loc) · 3.05 KB
/
docker-devdns_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"github.com/miekg/dns"
"github.com/rrva/dockerclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type MyMockedDocker struct {
mock.Mock
dockerclient.Callback
}
func (m *MyMockedDocker) StartMonitorEvents(cb dockerclient.Callback, ec chan error, args ...interface{}) {
m.Callback = cb
}
func (m *MyMockedDocker) ListContainers(all bool, size bool, filters string) ([]dockerclient.Container, error) {
args := m.Called(all, size, filters)
return args.Get(0).([]dockerclient.Container), args.Error(1)
}
func (m *MyMockedDocker) InspectContainer(id string) (*dockerclient.ContainerInfo, error) {
args := m.Called(id)
return args.Get(0).(*dockerclient.ContainerInfo), args.Error(1)
}
func (m *MyMockedDocker) Version() (*dockerclient.Version, error) {
return nil, nil
}
var mocker *MyMockedDocker
func init() {
started := make(chan bool)
mocker = &MyMockedDocker{}
containers := make([]dockerclient.Container, 1)
container := dockerclient.Container{}
names := make([]string, 1)
names[0] = "foo1"
container.Names = names
container.Id = "idblah"
containers[0] = container
mocker.On("ListContainers", false, false, "").Return(containers, nil)
containerInfo := dockerclient.ContainerInfo{}
containerInfo.NetworkSettings.IpAddress = "1.2.3.4"
mocker.On("InspectContainer", container.Id).Return(&containerInfo, nil)
containerInfo2 := dockerclient.ContainerInfo{}
containerInfo2.Name = "foo2"
containerInfo2.NetworkSettings.IpAddress = "2.2.3.4"
mocker.On("InspectContainer", "dyn1").Return(&containerInfo2, nil)
go mymain(":5354", true, mocker, "dev", started)
<-started
}
func dnsQuery(t *testing.T, s string, qtype uint16) *dns.Msg {
c := new(dns.Client)
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(s), qtype)
m.RecursionDesired = true
r, _, err := c.Exchange(m, "127.0.0.1:5354")
if err != nil {
t.Error(err)
}
if r.Rcode != dns.RcodeSuccess {
t.Error("Query unsuccessful")
}
if len(r.Answer) == 0 {
t.Error("No answers")
}
return r
}
func Test_dev_container_should_be_resolvable(t *testing.T) {
r := dnsQuery(t, "foo1.dev", dns.TypeA)
aa := r.Answer[0].(*dns.A)
assert.Equal(t, "1.2.3.4", aa.A.String(), "Got wrong reply")
}
func Test_dynamically_added_dev_container_should_be_resolvable(t *testing.T) {
event := dockerclient.Event{}
event.Status = "start"
event.Id = "dyn1"
mocker.Callback(&event, nil, nil)
r := dnsQuery(t, "foo2.dev", dns.TypeA)
aa := r.Answer[0].(*dns.A)
assert.Equal(t, "2.2.3.4", aa.A.String(), "Got wrong reply")
}
func Test_reverse_lookup_should_give_container_name(t *testing.T) {
event := dockerclient.Event{}
event.Status = "start"
event.Id = "dyn1"
mocker.Callback(&event, nil, nil)
r := dnsQuery(t, "4.3.2.1.in-addr.arpa.", dns.TypePTR)
aa := r.Answer[0].(*dns.PTR)
assert.Equal(t, "foo1.dev.", aa.Ptr, "Got wrong reply")
}
func Test_resolve_non_container_name_should_give_address(t *testing.T) {
r := dnsQuery(t, "example.com", dns.TypeA)
aa := r.Answer[0].(*dns.A)
assert.Equal(t, "93.184.216.34", aa.A.String(), "Got wrong reply")
}