port_mapping_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/netutils"
  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 netutils.SetupTestNetNS(t)()
  18. d := newDriver()
  19. binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}
  20. binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}
  21. portBindings := []types.PortBinding{binding1, binding2}
  22. epOptions := make(map[string]interface{})
  23. epOptions[netlabel.PortMap] = portBindings
  24. netConfig := &networkConfiguration{
  25. BridgeName: DefaultBridgeName,
  26. EnableIPTables: true,
  27. }
  28. netOptions := make(map[string]interface{})
  29. netOptions[netlabel.GenericData] = netConfig
  30. err := d.CreateNetwork("dummy", netOptions)
  31. if err != nil {
  32. t.Fatalf("Failed to create bridge: %v", err)
  33. }
  34. te := &testEndpoint{ifaces: []*testInterface{}}
  35. err = d.CreateEndpoint("dummy", "ep1", te, epOptions)
  36. if err != nil {
  37. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  38. }
  39. dd := d.(*driver)
  40. network, ok := dd.networks["dummy"]
  41. if !ok {
  42. t.Fatalf("Cannot find network %s inside driver", "dummy")
  43. }
  44. ep, _ := network.endpoints["ep1"]
  45. if len(ep.portMapping) != 2 {
  46. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  47. }
  48. if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
  49. ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
  50. t.Fatalf("bridgeEndpoint has incorrect port mapping values")
  51. }
  52. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  53. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
  54. t.Fatalf("operational port mapping data not found on bridgeEndpoint")
  55. }
  56. err = releasePorts(ep)
  57. if err != nil {
  58. t.Fatalf("Failed to release mapped ports: %v", err)
  59. }
  60. }