endpoint_unix_test.go 1.7 KB

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