port_mapping_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. err := d.CreateNetwork("dummy", netOptions, nil, nil)
  38. if err != nil {
  39. t.Fatalf("Failed to create bridge: %v", err)
  40. }
  41. te := &testEndpoint{iface: &testInterface{}}
  42. err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
  43. if err != nil {
  44. t.Fatalf("Failed to create the endpoint: %s", err.Error())
  45. }
  46. network, ok := d.networks["dummy"]
  47. if !ok {
  48. t.Fatalf("Cannot find network %s inside driver", "dummy")
  49. }
  50. ep, _ := network.endpoints["ep1"]
  51. if len(ep.portMapping) != 2 {
  52. t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
  53. }
  54. if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
  55. ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
  56. t.Fatalf("bridgeEndpoint has incorrect port mapping values")
  57. }
  58. if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
  59. ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
  60. t.Fatalf("operational port mapping data not found on bridgeEndpoint")
  61. }
  62. err = network.releasePorts(ep)
  63. if err != nil {
  64. t.Fatalf("Failed to release mapped ports: %v", err)
  65. }
  66. }