mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
![Michael Bridgen](/assets/img/avatar_default.png)
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>
65 lines
1.9 KiB
Go
65 lines
1.9 KiB
Go
package bridge
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
|
"github.com/docker/libnetwork/netutils"
|
|
"github.com/docker/libnetwork/pkg/netlabel"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
if reexec.Init() {
|
|
return
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func TestPortMappingConfig(t *testing.T) {
|
|
defer netutils.SetupTestNetNS(t)()
|
|
d := newDriver()
|
|
|
|
binding1 := netutils.PortBinding{Proto: netutils.UDP, Port: uint16(400), HostPort: uint16(54000)}
|
|
binding2 := netutils.PortBinding{Proto: netutils.TCP, Port: uint16(500), HostPort: uint16(65000)}
|
|
portBindings := []netutils.PortBinding{binding1, binding2}
|
|
|
|
epOptions := make(map[string]interface{})
|
|
epOptions[netlabel.PortMap] = portBindings
|
|
|
|
netConfig := &NetworkConfiguration{
|
|
BridgeName: DefaultBridgeName,
|
|
EnableIPTables: true,
|
|
}
|
|
netOptions := make(map[string]interface{})
|
|
netOptions[netlabel.GenericData] = netConfig
|
|
|
|
err := d.CreateNetwork("dummy", netOptions)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
}
|
|
|
|
_, err = d.CreateEndpoint("dummy", "ep1", epOptions)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create the endpoint: %s", err.Error())
|
|
}
|
|
|
|
dd := d.(*driver)
|
|
ep, _ := dd.network.endpoints["ep1"]
|
|
if len(ep.portMapping) != 2 {
|
|
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 ||
|
|
ep.portMapping[1].Proto != binding2.Proto || ep.portMapping[1].Port != binding2.Port {
|
|
t.Fatalf("bridgeEndpoint has incorrect port mapping values")
|
|
}
|
|
if ep.portMapping[0].HostIP == nil || ep.portMapping[0].HostPort == 0 ||
|
|
ep.portMapping[1].HostIP == nil || ep.portMapping[1].HostPort == 0 {
|
|
t.Fatalf("operational port mapping data not found on bridgeEndpoint")
|
|
}
|
|
|
|
err = releasePorts(ep)
|
|
if err != nil {
|
|
t.Fatalf("Failed to release mapped ports: %v", err)
|
|
}
|
|
}
|