This commit is contained in:
Sushant Sharma 2016-03-07 21:22:18 -08:00
Родитель 5dec368fde e2b61617bd
Коммит 8717b8c4ff
4 изменённых файлов: 138 добавлений и 40 удалений

Просмотреть файл

@ -43,7 +43,8 @@ func (listener *Listener) Start(errChan chan error) error {
listener.l, err = net.Listen("unix", listener.socketName)
if err != nil {
log.Printf("Listener: Failed to listen on %s %v", listener.socketName, err)
log.Printf("Listener: Failed to listen %+v", err)
return err
}
log.Printf("Listener: Started listening on %s.", listener.socketName)
@ -67,6 +68,11 @@ func (listener *Listener) Stop() {
log.Printf("Listener: Stopped listening on %s", listener.socketName)
}
// Returns the HTTP mux for the listener.
func (listener *Listener) GetMux() *http.ServeMux {
return listener.mux
}
// Registers a protocol handler.
func (listener *Listener) AddHandler(endpoint string, method string, handler func(http.ResponseWriter, *http.Request)) {
url := fmt.Sprintf("/%s.%s", endpoint, method)

Просмотреть файл

@ -19,7 +19,9 @@ const endpointName = "IpamDriver"
// IpamPlugin object and interface
type ipamPlugin struct {
name string
version string
scope string
listener *core.Listener
sync.Mutex
}
@ -32,7 +34,9 @@ type IpamPlugin interface {
// Creates a new IpamPlugin object.
func NewPlugin(version string) (IpamPlugin, error) {
return &ipamPlugin{
name: pluginName,
version: version,
scope: "local",
}, nil
}
@ -40,7 +44,7 @@ func NewPlugin(version string) (IpamPlugin, error) {
func (plugin *ipamPlugin) Start(errChan chan error) error {
// Create the listener.
listener, err := core.NewListener(pluginName)
listener, err := core.NewListener(plugin.name)
if err != nil {
log.Printf("Failed to create listener %v", err)
return err
@ -63,7 +67,7 @@ func (plugin *ipamPlugin) Start(errChan chan error) error {
return err
}
log.Printf("%s: Plugin started.", pluginName)
log.Printf("%s: Plugin started.", plugin.name)
return nil
}
@ -71,7 +75,7 @@ func (plugin *ipamPlugin) Start(errChan chan error) error {
// Stops the plugin.
func (plugin *ipamPlugin) Stop() {
plugin.listener.Stop()
log.Printf("%s: Plugin stopped.\n", pluginName)
log.Printf("%s: Plugin stopped.\n", plugin.name)
}
type activateResponse struct {
@ -79,21 +83,21 @@ type activateResponse struct {
}
func (plugin *ipamPlugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
log.Request(pluginName, "Activate", nil, nil)
log.Request(plugin.name, "Activate", nil, nil)
resp := &activateResponse{[]string{endpointName}}
err := plugin.listener.Encode(w, resp)
log.Response(pluginName, "Activate", resp, err)
log.Response(plugin.name, "Activate", resp, err)
}
func (plugin *ipamPlugin) getCapabilities(w http.ResponseWriter, r *http.Request) {
log.Request(pluginName, "GetCapabilities", nil, nil)
log.Request(plugin.name, "GetCapabilities", nil, nil)
resp := map[string]string{"Scope": "local"}
resp := map[string]string{"Scope": plugin.scope}
err := plugin.listener.Encode(w, resp)
log.Response(pluginName, "GetCapabilities", resp, err)
log.Response(plugin.name, "GetCapabilities", resp, err)
}
type defaultAddressSpacesResponseFormat struct {
@ -102,7 +106,7 @@ type defaultAddressSpacesResponseFormat struct {
}
func (plugin *ipamPlugin) getDefaultAddressSpaces(w http.ResponseWriter, r *http.Request) {
log.Request(pluginName, "GetDefaultAddressSpaces", nil, nil)
log.Request(plugin.name, "GetDefaultAddressSpaces", nil, nil)
resp := &defaultAddressSpacesResponseFormat{
LocalDefaultAddressSpace: "",
@ -111,7 +115,7 @@ func (plugin *ipamPlugin) getDefaultAddressSpaces(w http.ResponseWriter, r *http
err := plugin.listener.Encode(w, resp)
log.Response(pluginName, "GetDefaultAddressSpaces", resp, err)
log.Response(plugin.name, "GetDefaultAddressSpaces", resp, err)
}
type requestPoolRequestFormat struct {
@ -133,7 +137,7 @@ func (plugin *ipamPlugin) requestPool(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "RequestPool", req, err)
log.Request(plugin.name, "RequestPool", req, err)
if err == nil {
data := make(map[string]string)
@ -141,7 +145,7 @@ func (plugin *ipamPlugin) requestPool(w http.ResponseWriter, r *http.Request) {
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "RequestPool", resp, err)
log.Response(plugin.name, "RequestPool", resp, err)
}
}
@ -157,14 +161,14 @@ func (plugin *ipamPlugin) releasePool(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "ReleasePool", req, err)
log.Request(plugin.name, "ReleasePool", req, err)
if err == nil {
resp := &releasePoolRequestFormat{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "ReleasePool", resp, err)
log.Response(plugin.name, "ReleasePool", resp, err)
}
}
@ -185,14 +189,14 @@ func (plugin *ipamPlugin) requestAddress(w http.ResponseWriter, r *http.Request)
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "RequestAddress", req, err)
log.Request(plugin.name, "RequestAddress", req, err)
if err == nil {
resp := &requestAddressResponseFormat{"", "", make(map[string]string)}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "RequestAddress", resp, err)
log.Response(plugin.name, "RequestAddress", resp, err)
}
}
@ -206,13 +210,13 @@ func (plugin *ipamPlugin) releaseAddress(w http.ResponseWriter, r *http.Request)
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "ReleaseAddress", req, err)
log.Request(plugin.name, "ReleaseAddress", req, err)
if err == nil {
resp := map[string]string{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "ReleaseAddress", resp, err)
log.Response(plugin.name, "ReleaseAddress", resp, err)
}
}

Просмотреть файл

@ -20,6 +20,7 @@ const endpointName = "NetworkDriver"
// NetPlugin object and its interface
type netPlugin struct {
name string
version string
scope string
listener *core.Listener
@ -36,6 +37,7 @@ type NetPlugin interface {
// NewPlugin creates a new NetPlugin object.
func NewPlugin(version string) (NetPlugin, error) {
return &netPlugin{
name: pluginName,
version: version,
scope: "local",
}, nil
@ -45,7 +47,7 @@ func NewPlugin(version string) (NetPlugin, error) {
func (plugin *netPlugin) Start(errChan chan error) error {
// Create the listener.
listener, err := core.NewListener(pluginName)
listener, err := core.NewListener(plugin.name)
if err != nil {
log.Printf("Failed to create listener %v", err)
return err
@ -70,7 +72,7 @@ func (plugin *netPlugin) Start(errChan chan error) error {
return err
}
log.Printf("%s: Plugin started.", pluginName)
log.Printf("%s: Plugin started.", plugin.name)
return nil
}
@ -79,7 +81,7 @@ func (plugin *netPlugin) Start(errChan chan error) error {
func (plugin *netPlugin) Stop() {
plugin.listener.Stop()
core.FreeSlaves()
log.Printf("%s: Plugin stopped.\n", pluginName)
log.Printf("%s: Plugin stopped.\n", plugin.name)
}
func (plugin *netPlugin) networkExists(networkID string) bool {
@ -107,21 +109,21 @@ type activateResponse struct {
}
func (plugin *netPlugin) activatePlugin(w http.ResponseWriter, r *http.Request) {
log.Request(pluginName, "Activate", nil, nil)
log.Request(plugin.name, "Activate", nil, nil)
resp := &activateResponse{[]string{endpointName}}
err := plugin.listener.Encode(w, resp)
log.Response(pluginName, "Activate", resp, err)
log.Response(plugin.name, "Activate", resp, err)
}
func (plugin *netPlugin) getCapabilities(w http.ResponseWriter, r *http.Request) {
log.Request(pluginName, "GetCapabilities", nil, nil)
log.Request(plugin.name, "GetCapabilities", nil, nil)
resp := map[string]string{"Scope": plugin.scope}
err := plugin.listener.Encode(w, resp)
log.Response(pluginName, "GetCapabilities", resp, err)
log.Response(plugin.name, "GetCapabilities", resp, err)
}
// All request and response formats are well known and are published by libnetwork
@ -135,7 +137,7 @@ func (plugin *netPlugin) createNetwork(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "CreateNetwork", req, err)
log.Request(plugin.name, "CreateNetwork", req, err)
if err != nil {
return
@ -165,7 +167,7 @@ func (plugin *netPlugin) createNetwork(w http.ResponseWriter, r *http.Request) {
resp := map[string]string{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "CreateNetwork", resp, err)
log.Response(plugin.name, "CreateNetwork", resp, err)
}
type networkDeleteRequestFormat struct {
@ -177,7 +179,7 @@ func (plugin *netPlugin) deleteNetwork(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "DeleteNetwork", req, err)
log.Request(plugin.name, "DeleteNetwork", req, err)
if err != nil {
return
@ -195,7 +197,7 @@ func (plugin *netPlugin) deleteNetwork(w http.ResponseWriter, r *http.Request) {
resp := map[string]string{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "DeleteNetwork", resp, err)
log.Response(plugin.name, "DeleteNetwork", resp, err)
}
type azInterface struct {
@ -224,7 +226,7 @@ func (plugin *netPlugin) createEndpoint(w http.ResponseWriter, r *http.Request)
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "CreateEndpoint", req, err)
log.Request(plugin.name, "CreateEndpoint", req, err)
if err != nil {
return
@ -340,7 +342,7 @@ func (plugin *netPlugin) createEndpoint(w http.ResponseWriter, r *http.Request)
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "CreateEndpoint", resp, err)
log.Response(plugin.name, "CreateEndpoint", resp, err)
}
type joinRequestFormat struct {
@ -373,7 +375,7 @@ func (plugin *netPlugin) join(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "Join", req, err)
log.Request(plugin.name, "Join", req, err)
if err != nil {
return
@ -407,7 +409,7 @@ func (plugin *netPlugin) join(w http.ResponseWriter, r *http.Request) {
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "Join", resp, err)
log.Response(plugin.name, "Join", resp, err)
fmt.Printf("srcname: %s dstPRefix:%s \n", ifname.SrcName, ifname.DstPrefix)
@ -425,7 +427,7 @@ func (plugin *netPlugin) deleteEndpoint(w http.ResponseWriter, r *http.Request)
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "DeleteEndpoint", req, err)
log.Request(plugin.name, "DeleteEndpoint", req, err)
if err != nil {
return
@ -454,7 +456,7 @@ func (plugin *netPlugin) deleteEndpoint(w http.ResponseWriter, r *http.Request)
resp := &map[string]string{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "DeleteEndpoint", resp, err)
log.Response(plugin.name, "DeleteEndpoint", resp, err)
}
type leaveRequestFormat struct {
@ -470,7 +472,7 @@ func (plugin *netPlugin) leave(w http.ResponseWriter, r *http.Request) {
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "Leave", req, err)
log.Request(plugin.name, "Leave", req, err)
if err != nil {
return
@ -480,7 +482,7 @@ func (plugin *netPlugin) leave(w http.ResponseWriter, r *http.Request) {
resp := &leaveResponse{}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "Leave", resp, err)
log.Response(plugin.name, "Leave", resp, err)
}
type endpointOperInfoRequestFormat struct {
@ -497,7 +499,7 @@ func (plugin *netPlugin) endpointOperInfo(w http.ResponseWriter, r *http.Request
err := plugin.listener.Decode(w, r, &req)
log.Request(pluginName, "EndpointOperInfo", req, err)
log.Request(plugin.name, "EndpointOperInfo", req, err)
if err != nil {
return
@ -510,5 +512,5 @@ func (plugin *netPlugin) endpointOperInfo(w http.ResponseWriter, r *http.Request
resp := &endpointOperInfoResponseFormat{Value: value}
err = plugin.listener.Encode(w, resp)
log.Response(pluginName, "EndpointOperInfo", resp, err)
log.Response(plugin.name, "EndpointOperInfo", resp, err)
}

86
network/plugin_test.go Normal file
Просмотреть файл

@ -0,0 +1,86 @@
// Copyright Microsoft Corp.
// All rights reserved.
package network
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/sharmasushant/penguin/core"
//"github.com/docker/libnetwork/drivers/remote/api"
)
var plugin NetPlugin
var mux *http.ServeMux
func TestMain(m *testing.M) {
// Create the listener.
listener, err := core.NewListener("test")
if err != nil {
fmt.Printf("Failed to create listener %v", err)
return
}
mux = listener.GetMux()
// Create the plugin.
plugin, err = NewPlugin("test", "")
if err != nil {
fmt.Printf("Failed to create network plugin %v\n", err)
return
}
err = plugin.Start(listener)
if err != nil {
fmt.Printf("Failed to start network plugin %v\n", err)
return
}
// Run tests.
exitCode := m.Run()
// Cleanup.
plugin.Stop()
os.Exit(exitCode)
}
func TestActivate(t *testing.T) {
fmt.Println("Test: Activate")
req, err := http.NewRequest(http.MethodGet, "/Plugin.Activate", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("Activate request failed")
}
//fmt.Printf("%d - %s", w.Code, w.Body.String())
}
func TestGetCapabilities(t *testing.T) {
fmt.Println("Test: GetCapabilities")
req, err := http.NewRequest(http.MethodGet, "/NetworkDriver.GetCapabilities", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("GetCapabilities request failed")
}
//resp api.GetCapabilityResponse
}