port_mapping_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package bridge
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/docker/docker/pkg/reexec"
  6. "github.com/docker/libnetwork/netlabel"
  7. "github.com/docker/libnetwork/testutils"
  8. "github.com/docker/libnetwork/types"
  9. )
  10. func TestMain(m *testing.M) {
  11. if reexec.Init() {
  12. return
  13. }
  14. os.Exit(m.Run())
  15. }
  16. func TestPortMappingConfig(t *testing.T) {
  17. defer testutils.SetupTestOSContext(t)()
  18. d := newDriver()
  19. config := &configuration{
  20. EnableIPTables: true,
  21. }
  22. genericOption := make(map[string]interface{})
  23. genericOption[netlabel.GenericData] = config
  24. if err := d.configure(genericOption); err != nil {
  25. t.Fatalf("Failed to setup driver config: %v", err)
  26. }
  27. binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}
  28. binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}
  29. portBindings := []types.PortBinding{binding1, binding2}
  30. sbOptions := make(map[string]interface{})
  31. sbOptions[netlabel.PortMap] = portBindings
  32. netConfig := &networkConfiguration{
  33. BridgeName: DefaultBridgeName,
  34. }
  35. netOptions := make(map[string]interface{})
  36. netOptions[netlabel.GenericData] = netConfig
  37. ipdList := getIPv4Data(t)
  38. err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
  39. if err != nil {
  40. t.Fatalf("Failed to create bridge: %v", err)
  41. }
  42. te := newTestEndpoint(ipdList[0].Pool, 11)
  43. err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
  44. if err != nil {
  45. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  46. }
  47. if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
  48. t.Fatalf("Failed to join the endpoint: %v", err)
  49. }
  50. if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
  51. t.Fatalf("Failed to program external connectivity: %v", err)
  52. }
  53. network, ok := d.networks["dummy"]
  54. if !ok {
  55. t.Fatalf("Cannot find network %s inside driver", "dummy")
  56. }
  57. ep, _ := network.endpoints["ep1"]
  58. if len(ep.portMapping) != 2 {
  59. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  60. }
  61. if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
  62. ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
  63. t.Fatalf("bridgeEndpoint has incorrect port mapping values")
  64. }
  65. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  66. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
  67. t.Fatalf("operational port mapping data not found on bridgeEndpoint")
  68. }
  69. // release host mapped ports
  70. err = d.Leave("dummy", "ep1")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. err = d.RevokeExternalConnectivity("dummy", "ep1")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. }