2017-10-19 11:36:29 +00:00
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2021-05-28 20:59:41 +00:00
|
|
|
"runtime"
|
2017-10-19 11:36:29 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/resolvconf"
|
2022-11-08 21:35:14 +00:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
2020-08-31 15:55:22 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2021-05-28 20:59:41 +00:00
|
|
|
"gotest.tools/v3/skip"
|
2017-10-19 11:36:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCleanupServiceDiscovery(t *testing.T) {
|
2021-05-28 20:59:41 +00:00
|
|
|
skip.If(t, runtime.GOOS == "windows", "test only works on linux")
|
|
|
|
|
2022-11-08 21:35:14 +00:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2017-10-19 11:36:29 +00:00
|
|
|
c, err := New()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2017-10-19 11:36:29 +00:00
|
|
|
defer c.Stop()
|
|
|
|
|
2021-05-28 00:15:56 +00:00
|
|
|
cleanup := func(n Network) {
|
|
|
|
if err := n.Delete(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 11:36:29 +00:00
|
|
|
n1, err := c.NewNetwork("bridge", "net1", "", nil)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2021-05-28 00:15:56 +00:00
|
|
|
defer cleanup(n1)
|
2017-10-19 11:36:29 +00:00
|
|
|
|
|
|
|
n2, err := c.NewNetwork("bridge", "net2", "", nil)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2021-05-28 00:15:56 +00:00
|
|
|
defer cleanup(n2)
|
2017-10-19 11:36:29 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
if len(c.svcRecords) != 2 {
|
|
|
|
t.Fatalf("Service record not added correctly:%v", c.svcRecords)
|
2017-10-19 11:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup net1
|
2023-01-11 22:43:32 +00:00
|
|
|
c.cleanupServiceDiscovery(n1.ID())
|
2017-10-19 11:36:29 +00:00
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
if len(c.svcRecords) != 1 {
|
|
|
|
t.Fatalf("Service record not cleaned correctly:%v", c.svcRecords)
|
2017-10-19 11:36:29 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
c.cleanupServiceDiscovery("")
|
2017-10-19 11:36:29 +00:00
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
if len(c.svcRecords) != 0 {
|
|
|
|
t.Fatalf("Service record not cleaned correctly:%v", c.svcRecords)
|
2017-10-19 11:36:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 08:52:33 +00:00
|
|
|
|
|
|
|
func TestDNSOptions(t *testing.T) {
|
2021-05-28 20:59:41 +00:00
|
|
|
skip.If(t, runtime.GOOS == "windows", "test only works on linux")
|
|
|
|
|
2017-10-23 08:52:33 +00:00
|
|
|
c, err := New()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2017-10-23 08:52:33 +00:00
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
sb, err := c.NewSandbox("cnt1", nil)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2021-05-28 00:15:56 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
cleanup := func(s *Sandbox) {
|
2021-05-28 00:15:56 +00:00
|
|
|
if err := s.Delete(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer cleanup(sb)
|
2023-01-12 01:10:09 +00:00
|
|
|
sb.startResolver(false)
|
2017-10-23 08:52:33 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb.setupDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb.rebuildDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
currRC, err := resolvconf.GetSpecific(sb.config.resolvConfPath)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2017-10-23 08:52:33 +00:00
|
|
|
dnsOptionsList := resolvconf.GetOptions(currRC.Content)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(dnsOptionsList, 1))
|
|
|
|
assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))
|
2018-01-26 18:07:08 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb.config.dnsOptionsList = []string{"ndots:5"}
|
|
|
|
err = sb.setupDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
currRC, err = resolvconf.GetSpecific(sb.config.resolvConfPath)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-26 18:07:08 +00:00
|
|
|
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(dnsOptionsList, 1))
|
|
|
|
assert.Check(t, is.Equal("ndots:5", dnsOptionsList[0]))
|
2018-01-26 18:07:08 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb.rebuildDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
currRC, err = resolvconf.GetSpecific(sb.config.resolvConfPath)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2018-01-26 18:07:08 +00:00
|
|
|
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(dnsOptionsList, 1))
|
|
|
|
assert.Check(t, is.Equal("ndots:5", dnsOptionsList[0]))
|
2018-06-28 22:58:36 +00:00
|
|
|
|
2023-01-11 22:43:32 +00:00
|
|
|
sb2, err := c.NewSandbox("cnt2", nil)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2021-05-28 00:15:56 +00:00
|
|
|
defer cleanup(sb2)
|
2023-01-12 01:10:09 +00:00
|
|
|
sb2.startResolver(false)
|
2018-06-28 22:58:36 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb2.config.dnsOptionsList = []string{"ndots:0"}
|
|
|
|
err = sb2.setupDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb2.rebuildDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
currRC, err = resolvconf.GetSpecific(sb2.config.resolvConfPath)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2018-06-28 22:58:36 +00:00
|
|
|
dnsOptionsList = resolvconf.GetOptions(currRC.Content)
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(dnsOptionsList, 1))
|
|
|
|
assert.Check(t, is.Equal("ndots:0", dnsOptionsList[0]))
|
2018-06-28 23:01:26 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb2.config.dnsOptionsList = []string{"ndots:foobar"}
|
|
|
|
err = sb2.setupDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb2.rebuildDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Error(t, err, "invalid number for ndots option: foobar")
|
2018-06-28 23:22:17 +00:00
|
|
|
|
2023-01-12 01:10:09 +00:00
|
|
|
sb2.config.dnsOptionsList = []string{"ndots:-1"}
|
|
|
|
err = sb2.setupDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.NilError(t, err)
|
2023-01-12 01:10:09 +00:00
|
|
|
err = sb2.rebuildDNS()
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Error(t, err, "invalid number for ndots option: -1")
|
2017-10-23 08:52:33 +00:00
|
|
|
}
|