2015-05-01 20:01:21 -04:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/reexec"
|
2015-05-16 19:02:51 -04:00
|
|
|
"github.com/docker/libnetwork/netlabel"
|
2015-09-07 13:33:28 -04:00
|
|
|
"github.com/docker/libnetwork/testutils"
|
2015-05-20 16:28:46 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
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)}
|
|
|
|
portBindings := []types.PortBinding{binding1, binding2}
|
2015-05-01 20:01:21 -04:00
|
|
|
|
|
|
|
epOptions := make(map[string]interface{})
|
2015-05-06 00:19:57 -04:00
|
|
|
epOptions[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
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
err := d.CreateNetwork("dummy", netOptions, nil, nil)
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create bridge: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-10-03 19:11:50 -04:00
|
|
|
te := &testEndpoint{iface: &testInterface{}}
|
|
|
|
err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create the endpoint: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
ep, _ := network.endpoints["ep1"]
|
2015-05-01 20:01:21 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:14:29 -04:00
|
|
|
err = network.releasePorts(ep)
|
2015-05-01 20:01:21 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to release mapped ports: %v", err)
|
|
|
|
}
|
|
|
|
}
|