github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/endpoint_test.go (about)

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