github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/libnetwork/drivers/bridge/port_mapping_test.go (about) 1 // +build linux 2 3 package bridge 4 5 import ( 6 "os" 7 "testing" 8 9 "github.com/docker/docker/libnetwork/netlabel" 10 "github.com/docker/docker/libnetwork/testutils" 11 "github.com/docker/docker/libnetwork/types" 12 "github.com/docker/docker/pkg/reexec" 13 ) 14 15 func TestMain(m *testing.M) { 16 if reexec.Init() { 17 return 18 } 19 os.Exit(m.Run()) 20 } 21 22 func TestPortMappingConfig(t *testing.T) { 23 defer testutils.SetupTestOSContext(t)() 24 d := newDriver() 25 26 config := &configuration{ 27 EnableIPTables: true, 28 } 29 genericOption := make(map[string]interface{}) 30 genericOption[netlabel.GenericData] = config 31 32 if err := d.configure(genericOption); err != nil { 33 t.Fatalf("Failed to setup driver config: %v", err) 34 } 35 36 binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)} 37 binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)} 38 binding3 := types.PortBinding{Proto: types.SCTP, Port: uint16(300), HostPort: uint16(65000)} 39 portBindings := []types.PortBinding{binding1, binding2, binding3} 40 41 sbOptions := make(map[string]interface{}) 42 sbOptions[netlabel.PortMap] = portBindings 43 44 netConfig := &networkConfiguration{ 45 BridgeName: DefaultBridgeName, 46 } 47 netOptions := make(map[string]interface{}) 48 netOptions[netlabel.GenericData] = netConfig 49 50 ipdList := getIPv4Data(t, "") 51 err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil) 52 if err != nil { 53 t.Fatalf("Failed to create bridge: %v", err) 54 } 55 56 te := newTestEndpoint(ipdList[0].Pool, 11) 57 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil) 58 if err != nil { 59 t.Fatalf("Failed to create the endpoint: %s", err.Error()) 60 } 61 62 if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil { 63 t.Fatalf("Failed to join the endpoint: %v", err) 64 } 65 66 if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil { 67 t.Fatalf("Failed to program external connectivity: %v", err) 68 } 69 70 network, ok := d.networks["dummy"] 71 if !ok { 72 t.Fatalf("Cannot find network %s inside driver", "dummy") 73 } 74 ep := network.endpoints["ep1"] 75 if len(ep.portMapping) != 3 { 76 t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) 77 } 78 if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port || 79 ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port || 80 ep.portMapping[2].Proto != binding3.Proto || ep.portMapping[2].Port != binding3.Port { 81 t.Fatal("bridgeEndpoint has incorrect port mapping values") 82 } 83 if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 || 84 ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 || 85 ep.portMapping[2].HostIP == nil || ep.portMapping[2].HostPort == 0 { 86 t.Fatal("operational port mapping data not found on bridgeEndpoint") 87 } 88 89 // release host mapped ports 90 err = d.Leave("dummy", "ep1") 91 if err != nil { 92 t.Fatal(err) 93 } 94 95 err = d.RevokeExternalConnectivity("dummy", "ep1") 96 if err != nil { 97 t.Fatal(err) 98 } 99 } 100 101 func TestPortMappingV6Config(t *testing.T) { 102 defer testutils.SetupTestOSContext(t)() 103 d := newDriver() 104 105 config := &configuration{ 106 EnableIPTables: true, 107 EnableIP6Tables: true, 108 } 109 genericOption := make(map[string]interface{}) 110 genericOption[netlabel.GenericData] = config 111 112 if err := d.configure(genericOption); err != nil { 113 t.Fatalf("Failed to setup driver config: %v", err) 114 } 115 116 portBindings := []types.PortBinding{ 117 {Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}, 118 {Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}, 119 {Proto: types.SCTP, Port: uint16(500), HostPort: uint16(65000)}, 120 } 121 122 sbOptions := make(map[string]interface{}) 123 sbOptions[netlabel.PortMap] = portBindings 124 netConfig := &networkConfiguration{ 125 BridgeName: DefaultBridgeName, 126 EnableIPv6: true, 127 } 128 netOptions := make(map[string]interface{}) 129 netOptions[netlabel.GenericData] = netConfig 130 131 ipdList := getIPv4Data(t, "") 132 err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil) 133 if err != nil { 134 t.Fatalf("Failed to create bridge: %v", err) 135 } 136 137 te := newTestEndpoint(ipdList[0].Pool, 11) 138 err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil) 139 if err != nil { 140 t.Fatalf("Failed to create the endpoint: %s", err.Error()) 141 } 142 143 if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil { 144 t.Fatalf("Failed to join the endpoint: %v", err) 145 } 146 147 if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil { 148 t.Fatalf("Failed to program external connectivity: %v", err) 149 } 150 151 network, ok := d.networks["dummy"] 152 if !ok { 153 t.Fatalf("Cannot find network %s inside driver", "dummy") 154 } 155 ep := network.endpoints["ep1"] 156 if len(ep.portMapping) != 6 { 157 t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping) 158 } 159 160 // release host mapped ports 161 err = d.Leave("dummy", "ep1") 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 err = d.RevokeExternalConnectivity("dummy", "ep1") 167 if err != nil { 168 t.Fatal(err) 169 } 170 }