port_mapping_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. epOptions := make(map[string]interface{})
  31. epOptions[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, 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(), epOptions)
  44. if err != nil {
  45. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  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) != 2 {
  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. t.Fatalf("bridgeEndpoint has incorrect port mapping values")
  58. }
  59. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  60. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
  61. t.Fatalf("operational port mapping data not found on bridgeEndpoint")
  62. }
  63. err = network.releasePorts(ep)
  64. if err != nil {
  65. t.Fatalf("Failed to release mapped ports: %v", err)
  66. }
  67. }