Added network plugin tests for basic remote API validation

This commit is contained in:
Onur Filiz 2016-03-28 03:36:47 -07:00
Родитель e3445c49ac
Коммит 76ebb437e2
1 изменённых файлов: 119 добавлений и 8 удалений

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

@ -4,16 +4,21 @@
package network
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
remoteApi "github.com/docker/libnetwork/drivers/remote/api"
)
var plugin NetPlugin
var mux *http.ServeMux
// Wraps the test run with plugin setup and teardown.
func TestMain(m *testing.M) {
var err error
@ -41,14 +46,32 @@ func TestMain(m *testing.M) {
os.Exit(exitCode)
}
// Decodes plugin's responses to test requests.
func decodeResponse(w *httptest.ResponseRecorder, response interface{}) error {
if w.Code != http.StatusOK {
return fmt.Errorf("Request failed with HTTP error %s", w.Code)
}
if w.Body == nil {
return fmt.Errorf("Response body is empty")
}
return json.NewDecoder(w.Body).Decode(&response)
}
//
// Libnetwork remote API compliance tests
// github.com/docker/libnetwork/drivers/remote/api
// https://github.com/docker/libnetwork/blob/master/docs/remote.md
//
// Tests Plugin.Activate functionality.
func TestActivate(t *testing.T) {
fmt.Println("Test: Activate")
var resp struct {
Implements []string
}
req, err := http.NewRequest(http.MethodGet, "/Plugin.Activate", nil)
if err != nil {
t.Fatal(err)
@ -57,16 +80,19 @@ func TestActivate(t *testing.T) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("Activate request failed")
}
err = decodeResponse(w, &resp)
//fmt.Printf("%d - %s", w.Code, w.Body.String())
if err != nil || resp.Implements[0] != "NetworkDriver" {
t.Errorf("Activate response is invalid %+v", resp)
}
}
// Tests NetworkDriver.GetCapabilities functionality.
func TestGetCapabilities(t *testing.T) {
fmt.Println("Test: GetCapabilities")
var resp remoteApi.GetCapabilityResponse
req, err := http.NewRequest(http.MethodGet, "/NetworkDriver.GetCapabilities", nil)
if err != nil {
t.Fatal(err)
@ -75,9 +101,94 @@ func TestGetCapabilities(t *testing.T) {
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Error("GetCapabilities request failed")
err = decodeResponse(w, &resp)
if err != nil || resp.Err != "" || resp.Scope != "local" {
t.Errorf("GetCapabilities response is invalid %+v", resp)
}
}
// Tests NetworkDriver.CreateNetwork functionality.
func TestCreateNetwork(t *testing.T) {
fmt.Println("Test: CreateNetwork")
var body bytes.Buffer
var resp remoteApi.CreateNetworkResponse
info := &remoteApi.CreateNetworkRequest{
NetworkID: "N1",
}
//resp api.GetCapabilityResponse
json.NewEncoder(&body).Encode(info)
req, err := http.NewRequest(http.MethodGet, "/NetworkDriver.CreateNetwork", &body)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
err = decodeResponse(w, &resp)
if err != nil || resp.Err != "" {
t.Errorf("CreateNetwork response is invalid %+v", resp)
}
}
// Tests NetworkDriver.DeleteNetwork functionality.
func TestDeleteNetwork(t *testing.T) {
fmt.Println("Test: DeleteNetwork")
var body bytes.Buffer
var resp remoteApi.DeleteNetworkResponse
info := &remoteApi.DeleteNetworkRequest{
NetworkID: "N1",
}
json.NewEncoder(&body).Encode(info)
req, err := http.NewRequest(http.MethodGet, "/NetworkDriver.DeleteNetwork", &body)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
err = decodeResponse(w, &resp)
if err != nil || resp.Err != "" {
t.Errorf("DeleteNetwork response is invalid %+v", resp)
}
}
// Tests NetworkDriver.EndpointOperInfo functionality.
func TestEndpointOperInfo(t *testing.T) {
fmt.Println("Test: EndpointOperInfo")
var body bytes.Buffer
var resp remoteApi.EndpointInfoResponse
info := &remoteApi.EndpointInfoRequest{
NetworkID: "N1",
EndpointID: "E1",
}
json.NewEncoder(&body).Encode(info)
req, err := http.NewRequest(http.MethodGet, "/NetworkDriver.EndpointOperInfo", &body)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
err = decodeResponse(w, &resp)
if err != nil {
t.Errorf("EndpointOperInfo response is invalid %+v", resp)
}
}