endpoint_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. c, nws := getTestEnv(t, opts)
  27. ctrlr := c.(*controller)
  28. hostsFile, err := os.CreateTemp("", "")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer os.Remove(hostsFile.Name())
  33. sbx, err := ctrlr.NewSandbox("sandbox1", OptionHostsPath(hostsFile.Name()), OptionHostname("somehost.example.com"))
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. ep1, err := nws[0].CreateEndpoint("ep1")
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
  42. t.Fatal(err)
  43. }
  44. data, err := os.ReadFile(hostsFile.Name())
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if string(data) != expectedHostsFile {
  49. t.Fatalf("expected the hosts file to read:\n%q\nbut instead got the following:\n%q\n", expectedHostsFile, string(data))
  50. }
  51. if err := sbx.Delete(); err != nil {
  52. t.Fatal(err)
  53. }
  54. if len(ctrlr.sandboxes) != 0 {
  55. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  56. }
  57. osl.GC()
  58. }