endpoint_test.go 1.7 KB

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