2015-05-14 17:05:39 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-03-02 01:16:40 +00:00
|
|
|
"net/url"
|
2015-05-14 17:05:39 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-05-19 20:05:25 +00:00
|
|
|
"time"
|
2015-05-27 22:21:18 +00:00
|
|
|
|
2016-03-02 01:16:40 +00:00
|
|
|
"github.com/docker/docker/pkg/plugins/transport"
|
2015-12-30 00:27:12 +00:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2015-05-14 17:05:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mux *http.ServeMux
|
|
|
|
server *httptest.Server
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupRemotePluginServer() string {
|
|
|
|
mux = http.NewServeMux()
|
|
|
|
server = httptest.NewServer(mux)
|
|
|
|
return server.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func teardownRemotePluginServer() {
|
|
|
|
if server != nil {
|
|
|
|
server.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailedConnection(t *testing.T) {
|
2016-05-16 15:50:55 +00:00
|
|
|
c, _ := NewClient("tcp://127.0.0.1:1", &tlsconfig.Options{InsecureSkipVerify: true})
|
2015-06-05 20:09:53 +00:00
|
|
|
_, err := c.callWithRetry("Service.Method", nil, false)
|
2015-05-14 17:05:39 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected successful connection")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoInputOutput(t *testing.T) {
|
|
|
|
addr := setupRemotePluginServer()
|
|
|
|
defer teardownRemotePluginServer()
|
|
|
|
|
|
|
|
m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}}
|
|
|
|
|
|
|
|
mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != "POST" {
|
|
|
|
t.Fatalf("Expected POST, got %s\n", r.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := w.Header()
|
2016-03-02 01:16:40 +00:00
|
|
|
header.Set("Content-Type", transport.VersionMimetype)
|
2015-05-14 17:05:39 +00:00
|
|
|
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
})
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
2015-05-14 17:05:39 +00:00
|
|
|
var output Manifest
|
|
|
|
err := c.Call("Test.Echo", m, &output)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(output, m) {
|
|
|
|
t.Fatalf("Expected %v, was %v\n", m, output)
|
|
|
|
}
|
2015-12-05 07:55:50 +00:00
|
|
|
err = c.Call("Test.Echo", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
|
|
|
func TestBackoff(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
retries int
|
|
|
|
expTimeOff time.Duration
|
|
|
|
}{
|
|
|
|
{0, time.Duration(1)},
|
|
|
|
{1, time.Duration(2)},
|
|
|
|
{2, time.Duration(4)},
|
|
|
|
{4, time.Duration(16)},
|
|
|
|
{6, time.Duration(30)},
|
|
|
|
{10, time.Duration(30)},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
s := c.expTimeOff * time.Second
|
|
|
|
if d := backoff(c.retries); d != s {
|
|
|
|
t.Fatalf("Retry %v, expected %v, was %v\n", c.retries, s, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbortRetry(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
timeOff time.Duration
|
|
|
|
expAbort bool
|
|
|
|
}{
|
|
|
|
{time.Duration(1), false},
|
|
|
|
{time.Duration(2), false},
|
|
|
|
{time.Duration(10), false},
|
|
|
|
{time.Duration(30), true},
|
|
|
|
{time.Duration(40), true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
s := c.timeOff * time.Second
|
|
|
|
if a := abort(time.Now(), s); a != c.expAbort {
|
|
|
|
t.Fatalf("Duration %v, expected %v, was %v\n", c.timeOff, s, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 19:54:29 +00:00
|
|
|
|
|
|
|
func TestClientScheme(t *testing.T) {
|
|
|
|
cases := map[string]string{
|
|
|
|
"tcp://127.0.0.1:8080": "http",
|
|
|
|
"unix:///usr/local/plugins/foo": "http",
|
|
|
|
"http://127.0.0.1:8080": "http",
|
|
|
|
"https://127.0.0.1:8080": "https",
|
|
|
|
}
|
|
|
|
|
|
|
|
for addr, scheme := range cases {
|
2016-03-02 01:16:40 +00:00
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := httpScheme(u)
|
|
|
|
|
|
|
|
if s != scheme {
|
|
|
|
t.Fatalf("URL scheme mismatch, expected %s, got %s", scheme, s)
|
2015-09-22 19:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|