From c8062e4ad4d688bee9d97cbb3e20a96818ef2f89 Mon Sep 17 00:00:00 2001 From: Jana Radhakrishnan Date: Sun, 3 May 2015 20:37:22 +0000 Subject: [PATCH] Added "host" driver and test code. Signed-off-by: Jana Radhakrishnan --- drivers.go | 7 +++++- drivers/host/host.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ libnetwork_test.go | 33 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 drivers/host/host.go diff --git a/drivers.go b/drivers.go index 135eba4..da1da77 100644 --- a/drivers.go +++ b/drivers.go @@ -3,6 +3,7 @@ package libnetwork import ( "github.com/docker/libnetwork/driverapi" "github.com/docker/libnetwork/drivers/bridge" + "github.com/docker/libnetwork/drivers/host" "github.com/docker/libnetwork/drivers/null" ) @@ -11,7 +12,11 @@ type driverTable map[string]driverapi.Driver func enumerateDrivers() driverTable { drivers := make(driverTable) - for _, fn := range [](func() (string, driverapi.Driver)){bridge.New, null.New} { + for _, fn := range [](func() (string, driverapi.Driver)){ + bridge.New, + host.New, + null.New, + } { name, driver := fn() drivers[name] = driver } diff --git a/drivers/host/host.go b/drivers/host/host.go new file mode 100644 index 0000000..0c7f00f --- /dev/null +++ b/drivers/host/host.go @@ -0,0 +1,60 @@ +package host + +import ( + "github.com/docker/libnetwork/driverapi" + "github.com/docker/libnetwork/sandbox" + "github.com/docker/libnetwork/types" +) + +const networkType = "host" + +type driver struct{} + +// New provides a new instance of host driver +func New() (string, driverapi.Driver) { + return networkType, &driver{} +} + +func (d *driver) Config(option map[string]interface{}) error { + return nil +} + +func (d *driver) CreateNetwork(id types.UUID, option map[string]interface{}) error { + return nil +} + +func (d *driver) DeleteNetwork(nid types.UUID) error { + return nil +} + +func (d *driver) CreateEndpoint(nid, eid types.UUID, epOptions map[string]interface{}) (*sandbox.Info, error) { + return nil, nil +} + +func (d *driver) DeleteEndpoint(nid, eid types.UUID) error { + return nil +} + +func (d *driver) EndpointInfo(nid, eid types.UUID) (map[string]interface{}, error) { + return make(map[string]interface{}, 0), nil +} + +// Join method is invoked when a Sandbox is attached to an endpoint. +func (d *driver) Join(nid, eid types.UUID, sboxKey string, options map[string]interface{}) (*driverapi.JoinInfo, error) { + jInfo := &driverapi.JoinInfo{ + SandboxKey: sandbox.GenerateKey("host"), + NoSandboxCreate: true, + HostsPath: "/etc/hosts", + } + + return jInfo, nil +} + +// Leave method is invoked when a Sandbox detaches from an endpoint. +func (d *driver) Leave(nid, eid types.UUID, options map[string]interface{}) error { + return nil +} + +func (d *driver) Type() string { + return networkType +} diff --git a/libnetwork_test.go b/libnetwork_test.go index d9c0ab1..a66f2c8 100644 --- a/libnetwork_test.go +++ b/libnetwork_test.go @@ -89,6 +89,39 @@ func TestNull(t *testing.T) { } } +func TestHost(t *testing.T) { + network, err := createTestNetwork("host", "testnetwork", options.Generic{}) + if err != nil { + t.Fatal(err) + } + + ep, err := network.CreateEndpoint("testep") + if err != nil { + t.Fatal(err) + } + + _, err = ep.Join("host_container", + libnetwork.JoinOptionHostname("test"), + libnetwork.JoinOptionDomainname("docker.io"), + libnetwork.JoinOptionExtraHost("web", "192.168.0.1")) + if err != nil { + t.Fatal(err) + } + + err = ep.Leave("host_container") + if err != nil { + t.Fatal(err) + } + + if err := ep.Delete(); err != nil { + t.Fatal(err) + } + + if err := network.Delete(); err != nil { + t.Fatal(err) + } +} + func TestBridge(t *testing.T) { defer netutils.SetupTestNetNS(t)() ip, subnet, err := net.ParseCIDR("192.168.100.1/24")