github.com/rawahars/moby@v24.0.4+incompatible/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  	defer testutils.SetupTestOSContext(t)()
    17  
    18  	expectedHostsFile := `127.0.0.1	localhost
    19  ::1	localhost ip6-localhost ip6-loopback
    20  fe00::0	ip6-localnet
    21  ff00::0	ip6-mcastprefix
    22  ff02::1	ip6-allnodes
    23  ff02::2	ip6-allrouters
    24  192.168.222.2	somehost.example.com somehost
    25  fe90::2	somehost.example.com somehost
    26  `
    27  
    28  	opts := []NetworkOption{NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "",
    29  		[]*IpamConf{{PreferredPool: "192.168.222.0/24", Gateway: "192.168.222.1"}},
    30  		[]*IpamConf{{PreferredPool: "fe90::/64", Gateway: "fe90::1"}},
    31  		nil)}
    32  
    33  	ctrlr, nws := getTestEnv(t, opts)
    34  
    35  	hostsFile, err := os.CreateTemp("", "")
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	defer os.Remove(hostsFile.Name())
    40  
    41  	sbx, err := ctrlr.NewSandbox("sandbox1", OptionHostsPath(hostsFile.Name()), OptionHostname("somehost.example.com"))
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	ep1, err := nws[0].CreateEndpoint("ep1")
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	data, err := os.ReadFile(hostsFile.Name())
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	if string(data) != expectedHostsFile {
    61  		t.Fatalf("expected the hosts file to read:\n%q\nbut instead got the following:\n%q\n", expectedHostsFile, string(data))
    62  	}
    63  
    64  	if err := sbx.Delete(); err != nil {
    65  		t.Fatal(err)
    66  	}
    67  
    68  	if len(ctrlr.sandboxes) != 0 {
    69  		t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
    70  	}
    71  
    72  	osl.GC()
    73  }