summaryrefslogtreecommitdiff
path: root/libnetwork/endpoint_test.go
blob: be05653d5b05df37b7a5dd7c4a0367a1dfb683a0 (plain)
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
//go:build !windows
// +build !windows

package libnetwork

import (
	"os"
	"testing"

	"github.com/docker/docker/libnetwork/ipamapi"
	"github.com/docker/docker/libnetwork/osl"
	"github.com/docker/docker/libnetwork/testutils"
)

func TestHostsEntries(t *testing.T) {
	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)}

	ctrlr, nws := getTestEnv(t, opts)

	hostsFile, err := os.CreateTemp("", "")
	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)
	}

	if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
		t.Fatal(err)
	}

	data, err := os.ReadFile(hostsFile.Name())
	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()
}