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