port_mapping_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package bridge
  2. import (
  3. "os"
  4. "testing"
  5. "github.com/docker/docker/pkg/reexec"
  6. "github.com/docker/libnetwork/netutils"
  7. "github.com/docker/libnetwork/pkg/netlabel"
  8. )
  9. func TestMain(m *testing.M) {
  10. if reexec.Init() {
  11. return
  12. }
  13. os.Exit(m.Run())
  14. }
  15. func TestPortMappingConfig(t *testing.T) {
  16. defer netutils.SetupTestNetNS(t)()
  17. d := newDriver()
  18. binding1 := netutils.PortBinding{Proto: netutils.UDP, Port: uint16(400), HostPort: uint16(54000)}
  19. binding2 := netutils.PortBinding{Proto: netutils.TCP, Port: uint16(500), HostPort: uint16(65000)}
  20. portBindings := []netutils.PortBinding{binding1, binding2}
  21. epOptions := make(map[string]interface{})
  22. epOptions[netlabel.PortMap] = portBindings
  23. netConfig := &NetworkConfiguration{
  24. BridgeName: DefaultBridgeName,
  25. EnableIPTables: true,
  26. }
  27. netOptions := make(map[string]interface{})
  28. netOptions[netlabel.GenericData] = netConfig
  29. err := d.CreateNetwork("dummy", netOptions)
  30. if err != nil {
  31. t.Fatalf("Failed to create bridge: %v", err)
  32. }
  33. _, err = d.CreateEndpoint("dummy", "ep1", epOptions)
  34. if err != nil {
  35. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  36. }
  37. dd := d.(*driver)
  38. ep, _ := dd.network.endpoints["ep1"]
  39. if len(ep.portMapping) != 2 {
  40. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  41. }
  42. if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
  43. ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
  44. t.Fatalf("bridgeEndpoint has incorrect port mapping values")
  45. }
  46. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  47. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
  48. t.Fatalf("operational port mapping data not found on bridgeEndpoint")
  49. }
  50. err = releasePorts(ep)
  51. if err != nil {
  52. t.Fatalf("Failed to release mapped ports: %v", err)
  53. }
  54. }