github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/drivers/bridge/port_mapping_test.go (about)

     1  package bridge
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/pkg/reexec"
     8  	"github.com/docker/libnetwork/netlabel"
     9  	"github.com/docker/libnetwork/testutils"
    10  	"github.com/docker/libnetwork/types"
    11  )
    12  
    13  func TestMain(m *testing.M) {
    14  	if reexec.Init() {
    15  		return
    16  	}
    17  	os.Exit(m.Run())
    18  }
    19  
    20  func TestPortMappingConfig(t *testing.T) {
    21  	defer testutils.SetupTestOSContext(t)()
    22  	d := newDriver()
    23  
    24  	config := &configuration{
    25  		EnableIPTables: true,
    26  	}
    27  	genericOption := make(map[string]interface{})
    28  	genericOption[netlabel.GenericData] = config
    29  
    30  	if err := d.configure(genericOption); err != nil {
    31  		t.Fatalf("Failed to setup driver config: %v", err)
    32  	}
    33  
    34  	binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}
    35  	binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}
    36  	portBindings := []types.PortBinding{binding1, binding2}
    37  
    38  	sbOptions := make(map[string]interface{})
    39  	sbOptions[netlabel.PortMap] = portBindings
    40  
    41  	netConfig := &networkConfiguration{
    42  		BridgeName: DefaultBridgeName,
    43  	}
    44  	netOptions := make(map[string]interface{})
    45  	netOptions[netlabel.GenericData] = netConfig
    46  
    47  	ipdList := getIPv4Data(t, "")
    48  	err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
    49  	if err != nil {
    50  		t.Fatalf("Failed to create bridge: %v", err)
    51  	}
    52  
    53  	te := newTestEndpoint(ipdList[0].Pool, 11)
    54  	err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
    55  	if err != nil {
    56  		t.Fatalf("Failed to create the endpoint: %s", err.Error())
    57  	}
    58  
    59  	if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
    60  		t.Fatalf("Failed to join the endpoint: %v", err)
    61  	}
    62  
    63  	if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
    64  		t.Fatalf("Failed to program external connectivity: %v", err)
    65  	}
    66  
    67  	network, ok := d.networks["dummy"]
    68  	if !ok {
    69  		t.Fatalf("Cannot find network %s inside driver", "dummy")
    70  	}
    71  	ep, _ := network.endpoints["ep1"]
    72  	if len(ep.portMapping) != 2 {
    73  		t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
    74  	}
    75  	if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
    76  		ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
    77  		t.Fatalf("bridgeEndpoint has incorrect port mapping values")
    78  	}
    79  	if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
    80  		ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
    81  		t.Fatalf("operational port mapping data not found on bridgeEndpoint")
    82  	}
    83  
    84  	// release host mapped ports
    85  	err = d.Leave("dummy", "ep1")
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  
    90  	err = d.RevokeExternalConnectivity("dummy", "ep1")
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  }