forked from moby/libnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_common_test.go
112 lines (91 loc) · 3.64 KB
/
service_common_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
package libnetwork
import (
"net"
"testing"
"github.com/docker/libnetwork/resolvconf"
"gotest.tools/assert"
is "gotest.tools/assert/cmp"
)
func TestCleanupServiceDiscovery(t *testing.T) {
c, err := New()
assert.NilError(t, err)
defer c.Stop()
n1, err := c.NewNetwork("bridge", "net1", "", nil)
assert.NilError(t, err)
defer n1.Delete()
n2, err := c.NewNetwork("bridge", "net2", "", nil)
assert.NilError(t, err)
defer n2.Delete()
n1.(*network).addSvcRecords("N1ep1", "service_test", "serviceID1", net.ParseIP("192.168.0.1"), net.IP{}, true, "test")
n1.(*network).addSvcRecords("N2ep2", "service_test", "serviceID2", net.ParseIP("192.168.0.2"), net.IP{}, true, "test")
n2.(*network).addSvcRecords("N2ep1", "service_test", "serviceID1", net.ParseIP("192.168.1.1"), net.IP{}, true, "test")
n2.(*network).addSvcRecords("N2ep2", "service_test", "serviceID2", net.ParseIP("192.168.1.2"), net.IP{}, true, "test")
if len(c.(*controller).svcRecords) != 2 {
t.Fatalf("Service record not added correctly:%v", c.(*controller).svcRecords)
}
// cleanup net1
c.(*controller).cleanupServiceDiscovery(n1.ID())
if len(c.(*controller).svcRecords) != 1 {
t.Fatalf("Service record not cleaned correctly:%v", c.(*controller).svcRecords)
}
c.(*controller).cleanupServiceDiscovery("")
if len(c.(*controller).svcRecords) != 0 {
t.Fatalf("Service record not cleaned correctly:%v", c.(*controller).svcRecords)
}
}
func TestDNSOptions(t *testing.T) {
c, err := New()
assert.NilError(t, err)
sb, err := c.(*controller).NewSandbox("cnt1", nil)
assert.NilError(t, err)
defer sb.Delete()
sb.(*sandbox).startResolver(false)
err = sb.(*sandbox).setupDNS()
assert.NilError(t, err)
err = sb.(*sandbox).rebuildDNS()
assert.NilError(t, err)
currRC, err := resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
assert.NilError(t, err)
dnsOptionsList := resolvconf.GetOptions(currRC.Content)
assert.Check(t, is.Len(dnsOptionsList, 1))
assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))
sb.(*sandbox).config.dnsOptionsList = []string{"ndots:5"}
err = sb.(*sandbox).setupDNS()
assert.NilError(t, err)
currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
assert.NilError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Check(t, is.Len(dnsOptionsList, 1))
assert.Check(t, is.Equal("ndots:5", dnsOptionsList[0]))
err = sb.(*sandbox).rebuildDNS()
assert.NilError(t, err)
currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath)
assert.NilError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Check(t, is.Len(dnsOptionsList, 1))
assert.Check(t, is.Equal("ndots:5", dnsOptionsList[0]))
sb2, err := c.(*controller).NewSandbox("cnt2", nil)
assert.NilError(t, err)
defer sb2.Delete()
sb2.(*sandbox).startResolver(false)
sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:0"}
err = sb2.(*sandbox).setupDNS()
assert.NilError(t, err)
err = sb2.(*sandbox).rebuildDNS()
assert.NilError(t, err)
currRC, err = resolvconf.GetSpecific(sb2.(*sandbox).config.resolvConfPath)
assert.NilError(t, err)
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
assert.Check(t, is.Len(dnsOptionsList, 1))
assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))
sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:foobar"}
err = sb2.(*sandbox).setupDNS()
assert.NilError(t, err)
err = sb2.(*sandbox).rebuildDNS()
assert.Error(t, err, "invalid number for ndots option: foobar")
sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:-1"}
err = sb2.(*sandbox).setupDNS()
assert.NilError(t, err)
err = sb2.(*sandbox).rebuildDNS()
assert.Error(t, err, "invalid number for ndots option: -1")
}