port_mapping_linux_test.go 5.0 KB

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