2021-08-23 13:14:53 +00:00
|
|
|
//go:build !windows
|
2019-08-30 21:24:43 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package libnetwork
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-06 00:24:47 +00:00
|
|
|
"github.com/docker/docker/libnetwork/ipamapi"
|
|
|
|
"github.com/docker/docker/libnetwork/osl"
|
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
2019-08-30 21:24:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHostsEntries(t *testing.T) {
|
|
|
|
if !testutils.IsRunningInContainer() {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedHostsFile := `127.0.0.1 localhost
|
|
|
|
::1 localhost ip6-localhost ip6-loopback
|
|
|
|
fe00::0 ip6-localnet
|
|
|
|
ff00::0 ip6-mcastprefix
|
|
|
|
ff02::1 ip6-allnodes
|
|
|
|
ff02::2 ip6-allrouters
|
|
|
|
192.168.222.2 somehost.example.com somehost
|
|
|
|
fe90::2 somehost.example.com somehost
|
|
|
|
`
|
|
|
|
|
|
|
|
opts := []NetworkOption{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "",
|
|
|
|
[]*IpamConf{{PreferredPool: "192.168.222.0/24", Gateway: "192.168.222.1"}},
|
|
|
|
[]*IpamConf{{PreferredPool: "fe90::/64", Gateway: "fe90::1"}},
|
|
|
|
nil)}
|
|
|
|
|
|
|
|
c, nws := getTestEnv(t, opts)
|
|
|
|
ctrlr := c.(*controller)
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
hostsFile, err := os.CreateTemp("", "")
|
2019-08-30 21:24:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(hostsFile.Name())
|
|
|
|
|
|
|
|
sbx, err := ctrlr.NewSandbox("sandbox1", OptionHostsPath(hostsFile.Name()), OptionHostname("somehost.example.com"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ep1, err := nws[0].CreateEndpoint("ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:02:30 +00:00
|
|
|
if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
|
2019-08-30 21:24:43 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 10:10:50 +00:00
|
|
|
data, err := os.ReadFile(hostsFile.Name())
|
2019-08-30 21:24:43 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(data) != expectedHostsFile {
|
|
|
|
t.Fatalf("expected the hosts file to read:\n%q\nbut instead got the following:\n%q\n", expectedHostsFile, string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sbx.Delete(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctrlr.sandboxes) != 0 {
|
|
|
|
t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
|
|
|
|
}
|
|
|
|
|
|
|
|
osl.GC()
|
|
|
|
}
|