github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/drivers/bridge/port_mapping_linux_test.go (about)

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