port_mapping_test.go 5.1 KB

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