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