github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/endpoint_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package libnetwork
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/libnetwork/ipamapi"
    11  	"github.com/docker/docker/libnetwork/osl"
    12  	"github.com/docker/docker/libnetwork/testutils"
    13  )
    14  
    15  func TestHostsEntries(t *testing.T) {
    16  	if !testutils.IsRunningInContainer() {
    17  		defer testutils.SetupTestOSContext(t)()
    18  	}
    19  
    20  	expectedHostsFile := `127.0.0.1	localhost
    21  ::1	localhost ip6-localhost ip6-loopback
    22  fe00::0	ip6-localnet
    23  ff00::0	ip6-mcastprefix
    24  ff02::1	ip6-allnodes
    25  ff02::2	ip6-allrouters
    26  192.168.222.2	somehost.example.com somehost
    27  fe90::2	somehost.example.com somehost
    28  `
    29  
    30  	opts := []NetworkOption{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "",
    31  		[]*IpamConf{{PreferredPool: "192.168.222.0/24", Gateway: "192.168.222.1"}},
    32  		[]*IpamConf{{PreferredPool: "fe90::/64", Gateway: "fe90::1"}},
    33  		nil)}
    34  
    35  	c, nws := getTestEnv(t, opts)
    36  	ctrlr := c.(*controller)
    37  
    38  	hostsFile, err := os.CreateTemp("", "")
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	defer os.Remove(hostsFile.Name())
    43  
    44  	sbx, err := ctrlr.NewSandbox("sandbox1", OptionHostsPath(hostsFile.Name()), OptionHostname("somehost.example.com"))
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	ep1, err := nws[0].CreateEndpoint("ep1")
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  
    54  	if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
    55  		t.Fatal(err)
    56  	}
    57  
    58  	data, err := os.ReadFile(hostsFile.Name())
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	if string(data) != expectedHostsFile {
    64  		t.Fatalf("expected the hosts file to read:\n%q\nbut instead got the following:\n%q\n", expectedHostsFile, string(data))
    65  	}
    66  
    67  	if err := sbx.Delete(); err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	if len(ctrlr.sandboxes) != 0 {
    72  		t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
    73  	}
    74  
    75  	osl.GC()
    76  }