endpoint_test.go 1.7 KB

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