github.com/rish1988/moby@v25.0.2+incompatible/libnetwork/endpoint_unix_test.go (about)

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