2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-05-01 20:01:21 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/netlabel"
|
2021-11-14 18:34:40 -05:00
|
|
|
"github.com/docker/docker/libnetwork/ns"
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
|
|
|
"github.com/docker/docker/libnetwork/types"
|
2021-05-27 20:15:56 -04:00
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-05-01 20:01:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if reexec.Init() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPortMappingConfig(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
Make driver packages register themselves via DriverCallback
In the present code, each driver package provides a `New()` method
which constructs a driver of its type, which is then registered with
the controller.
However, this is not suitable for the `drivers/remote` package, since
it does not provide a (singleton) driver, but a mechanism for drivers
to be added dynamically. As a result, the implementation is oddly
dual-purpose, and a spurious `"remote"` driver is added to the
controller's list of available drivers.
Instead, it is better to provide the registration callback to each
package and let it register its own driver or drivers. That way, the
singleton driver packages can construct one and register it, and the
remote package can hook the callback up with whatever the dynamic
driver mechanism turns out to be.
NB there are some method signature changes; in particular to
controller.New, which can return an error if the built-in driver
packages fail to initialise.
Signed-off-by: Michael Bridgen <mikeb@squaremobius.net>
2015-05-11 08:46:29 -04:00
|
|
|
d := newDriver()
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2015-06-11 21:12:00 -04:00
|
|
|
config := &configuration{
|
|
|
|
EnableIPTables: true,
|
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = config
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
if err := d.configure(genericOption); err != nil {
|
2015-06-11 21:12:00 -04:00
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-05-20 16:28:46 -04:00
|
|
|
binding1 := types.PortBinding{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)}
|
|
|
|
binding2 := types.PortBinding{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)}
|
2017-06-13 01:29:56 -04:00
|
|
|
binding3 := types.PortBinding{Proto: types.SCTP, Port: uint16(300), HostPort: uint16(65000)}
|
|
|
|
portBindings := []types.PortBinding{binding1, binding2, binding3}
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
sbOptions := make(map[string]interface{})
|
|
|
|
sbOptions[netlabel.PortMap] = portBindings
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
netConfig := &networkConfiguration{
|
2015-06-11 21:12:00 -04:00
|
|
|
BridgeName: DefaultBridgeName,
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
2015-05-06 01:51:26 -04:00
|
|
|
netOptions := make(map[string]interface{})
|
|
|
|
netOptions[netlabel.GenericData] = netConfig
|
2015-05-01 20:01:21 -04:00
|
|
|
|
2016-06-27 23:42:50 -04:00
|
|
|
ipdList := getIPv4Data(t, "")
|
2016-04-18 22:55:39 -04:00
|
|
|
err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-05 17:53:25 -04:00
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 11)
|
2015-12-07 17:45:51 -05:00
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
|
|
|
|
t.Fatalf("Failed to join the endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
|
|
|
|
t.Fatalf("Failed to program external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-09-18 17:00:36 -04:00
|
|
|
network, ok := d.networks["dummy"]
|
2015-05-21 16:13:19 -04:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot find network %s inside driver", "dummy")
|
|
|
|
}
|
2021-05-27 20:15:56 -04:00
|
|
|
ep := network.endpoints["ep1"]
|
2017-06-13 01:29:56 -04:00
|
|
|
if len(ep.portMapping) != 3 {
|
2015-05-01 20:01:21 -04:00
|
|
|
t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
|
|
|
|
}
|
|
|
|
if ep.portMapping[0].Proto != binding1.Proto || ep.portMapping[0].Port != binding1.Port ||
|
2017-06-13 01:29:56 -04:00
|
|
|
ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port ||
|
|
|
|
ep.portMapping[2].Proto != binding3.Proto || ep.portMapping[2].Port != binding3.Port {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("bridgeEndpoint has incorrect port mapping values")
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
|
2017-06-13 01:29:56 -04:00
|
|
|
ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 ||
|
|
|
|
ep.portMapping[2].HostIP == nil || ep.portMapping[2].HostPort == 0 {
|
2016-11-21 20:29:53 -05:00
|
|
|
t.Fatal("operational port mapping data not found on bridgeEndpoint")
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:45:51 -05:00
|
|
|
// release host mapped ports
|
|
|
|
err = d.Leave("dummy", "ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.RevokeExternalConnectivity("dummy", "ep1")
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
2015-12-07 17:45:51 -05:00
|
|
|
t.Fatal(err)
|
2015-05-01 20:01:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-12-13 18:56:30 -05:00
|
|
|
|
|
|
|
func TestPortMappingV6Config(t *testing.T) {
|
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2021-11-14 18:34:40 -05:00
|
|
|
if err := loopbackUp(); err != nil {
|
|
|
|
t.Fatalf("Could not bring loopback iface up: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:56:30 -05:00
|
|
|
d := newDriver()
|
|
|
|
|
|
|
|
config := &configuration{
|
|
|
|
EnableIPTables: true,
|
|
|
|
EnableIP6Tables: true,
|
|
|
|
}
|
|
|
|
genericOption := make(map[string]interface{})
|
|
|
|
genericOption[netlabel.GenericData] = config
|
|
|
|
|
|
|
|
if err := d.configure(genericOption); err != nil {
|
|
|
|
t.Fatalf("Failed to setup driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
portBindings := []types.PortBinding{
|
|
|
|
{Proto: types.UDP, Port: uint16(400), HostPort: uint16(54000)},
|
|
|
|
{Proto: types.TCP, Port: uint16(500), HostPort: uint16(65000)},
|
|
|
|
{Proto: types.SCTP, Port: uint16(500), HostPort: uint16(65000)},
|
|
|
|
}
|
|
|
|
|
|
|
|
sbOptions := make(map[string]interface{})
|
|
|
|
sbOptions[netlabel.PortMap] = portBindings
|
|
|
|
netConfig := &networkConfiguration{
|
|
|
|
BridgeName: DefaultBridgeName,
|
|
|
|
EnableIPv6: true,
|
|
|
|
}
|
|
|
|
netOptions := make(map[string]interface{})
|
|
|
|
netOptions[netlabel.GenericData] = netConfig
|
|
|
|
|
|
|
|
ipdList := getIPv4Data(t, "")
|
|
|
|
err := d.CreateNetwork("dummy", netOptions, nil, ipdList, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
te := newTestEndpoint(ipdList[0].Pool, 11)
|
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.Join("dummy", "ep1", "sbox", te, sbOptions); err != nil {
|
|
|
|
t.Fatalf("Failed to join the endpoint: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = d.ProgramExternalConnectivity("dummy", "ep1", sbOptions); err != nil {
|
|
|
|
t.Fatalf("Failed to program external connectivity: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
network, ok := d.networks["dummy"]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Cannot find network %s inside driver", "dummy")
|
|
|
|
}
|
2021-05-27 20:15:56 -04:00
|
|
|
ep := network.endpoints["ep1"]
|
2020-12-13 18:56:30 -05:00
|
|
|
if len(ep.portMapping) != 6 {
|
|
|
|
t.Fatalf("Failed to store the port bindings into the sandbox info. Found: %v", ep.portMapping)
|
|
|
|
}
|
|
|
|
|
|
|
|
// release host mapped ports
|
|
|
|
err = d.Leave("dummy", "ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.RevokeExternalConnectivity("dummy", "ep1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 18:34:40 -05:00
|
|
|
|
|
|
|
func loopbackUp() error {
|
|
|
|
nlHandle := ns.NlHandle()
|
|
|
|
iface, err := nlHandle.LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nlHandle.LinkSetUp(iface)
|
|
|
|
}
|