port_mapping_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //go:build linux
  2. // +build linux
  3. package bridge
  4. import (
  5. "os"
  6. "testing"
  7. "github.com/docker/docker/libnetwork/netlabel"
  8. "github.com/docker/docker/libnetwork/ns"
  9. "github.com/docker/docker/libnetwork/testutils"
  10. "github.com/docker/docker/libnetwork/types"
  11. "github.com/docker/docker/pkg/reexec"
  12. )
  13. func TestMain(m *testing.M) {
  14. if reexec.Init() {
  15. return
  16. }
  17. os.Exit(m.Run())
  18. }
  19. func TestPortMappingConfig(t *testing.T) {
  20. defer testutils.SetupTestOSContext(t)()
  21. d := newDriver()
  22. config := &configuration{
  23. EnableIPTables: true,
  24. }
  25. genericOption := make(map[string]interface{})
  26. genericOption[netlabel.GenericData] = config
  27. if err := d.configure(genericOption); err != nil {
  28. t.Fatalf("Failed to setup driver config: %v", err)
  29. }
  30. binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}
  31. binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}
  32. binding3 := types.PortBinding{Proto: types.SCTP, Port: uint16(300), HostPort: uint16(65000)}
  33. portBindings := []types.PortBinding{binding1, binding2, binding3}
  34. sbOptions := make(map[string]interface{})
  35. sbOptions[netlabel.PortMap] = portBindings
  36. netConfig := &networkConfiguration{
  37. BridgeName: DefaultBridgeName,
  38. }
  39. netOptions := make(map[string]interface{})
  40. netOptions[netlabel.GenericData] = netConfig
  41. ipdList := getIPv4Data(t, "")
  42. err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
  43. if err != nil {
  44. t.Fatalf("Failed to create bridge: %v", err)
  45. }
  46. te := newTestEndpoint(ipdList[0].Pool, 11)
  47. err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
  48. if err != nil {
  49. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  50. }
  51. if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
  52. t.Fatalf("Failed to join the endpoint: %v", err)
  53. }
  54. if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
  55. t.Fatalf("Failed to program external connectivity: %v", err)
  56. }
  57. network, ok := d.networks["dummy"]
  58. if !ok {
  59. t.Fatalf("Cannot find network %s inside driver", "dummy")
  60. }
  61. ep := network.endpoints["ep1"]
  62. if len(ep.portMapping) != 3 {
  63. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  64. }
  65. if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
  66. ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port ||
  67. ep.portMapping[2].Proto != binding3.Proto || ep.portMapping[2].Port != binding3.Port {
  68. t.Fatal("bridgeEndpoint has incorrect port mapping values")
  69. }
  70. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  71. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 ||
  72. ep.portMapping[2].HostIP == nil || ep.portMapping[2].HostPort == 0 {
  73. t.Fatal("operational port mapping data not found on bridgeEndpoint")
  74. }
  75. // release host mapped ports
  76. err = d.Leave("dummy", "ep1")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. err = d.RevokeExternalConnectivity("dummy", "ep1")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. }
  85. func TestPortMappingV6Config(t *testing.T) {
  86. defer testutils.SetupTestOSContext(t)()
  87. if err := loopbackUp(); err != nil {
  88. t.Fatalf("Could not bring loopback iface up: %v", err)
  89. }
  90. d := newDriver()
  91. config := &configuration{
  92. EnableIPTables: true,
  93. EnableIP6Tables: true,
  94. }
  95. genericOption := make(map[string]interface{})
  96. genericOption[netlabel.GenericData] = config
  97. if err := d.configure(genericOption); err != nil {
  98. t.Fatalf("Failed to setup driver config: %v", err)
  99. }
  100. portBindings := []types.PortBinding{
  101. {Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)},
  102. {Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)},
  103. {Proto: types.SCTP, Port: uint16(500), HostPort: uint16(65000)},
  104. }
  105. sbOptions := make(map[string]interface{})
  106. sbOptions[netlabel.PortMap] = portBindings
  107. netConfig := &networkConfiguration{
  108. BridgeName: DefaultBridgeName,
  109. EnableIPv6: true,
  110. }
  111. netOptions := make(map[string]interface{})
  112. netOptions[netlabel.GenericData] = netConfig
  113. ipdList := getIPv4Data(t, "")
  114. err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
  115. if err != nil {
  116. t.Fatalf("Failed to create bridge: %v", err)
  117. }
  118. te := newTestEndpoint(ipdList[0].Pool, 11)
  119. err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
  120. if err != nil {
  121. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  122. }
  123. if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
  124. t.Fatalf("Failed to join the endpoint: %v", err)
  125. }
  126. if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
  127. t.Fatalf("Failed to program external connectivity: %v", err)
  128. }
  129. network, ok := d.networks["dummy"]
  130. if !ok {
  131. t.Fatalf("Cannot find network %s inside driver", "dummy")
  132. }
  133. ep := network.endpoints["ep1"]
  134. if len(ep.portMapping) != 6 {
  135. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  136. }
  137. // release host mapped ports
  138. err = d.Leave("dummy", "ep1")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. err = d.RevokeExternalConnectivity("dummy", "ep1")
  143. if err != nil {
  144. t.Fatal(err)
  145. }
  146. }
  147. func loopbackUp() error {
  148. nlHandle := ns.NlHandle()
  149. iface, err := nlHandle.LinkByName("lo")
  150. if err != nil {
  151. return err
  152. }
  153. return nlHandle.LinkSetUp(iface)
  154. }