2018-02-05 16:05:59 -05:00
|
|
|
package plugins // import "github.com/docker/docker/pkg/plugins"
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-22 05:38:23 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-05-14 13:05:39 -04:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-03-01 20:16:40 -05:00
|
|
|
"net/url"
|
2017-05-26 21:02:31 -04:00
|
|
|
"strings"
|
2015-05-14 13:05:39 -04:00
|
|
|
"testing"
|
2015-05-19 16:05:25 -04:00
|
|
|
"time"
|
2015-05-27 18:21:18 -04:00
|
|
|
|
2016-03-01 20:16:40 -05:00
|
|
|
"github.com/docker/docker/pkg/plugins/transport"
|
2015-12-29 19:27:12 -05:00
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2018-03-05 15:29:02 -05:00
|
|
|
"github.com/pkg/errors"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2015-05-14 13:05:39 -04: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 11:50:55 -04:00
|
|
|
c, _ := NewClient("tcp://127.0.0.1:1", &tlsconfig.Options{InsecureSkipVerify: true})
|
2015-06-05 16:09:53 -04:00
|
|
|
_, err := c.callWithRetry("Service.Method", nil, false)
|
2015-05-14 13:05:39 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected successful connection")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 21:02:31 -04:00
|
|
|
func TestFailOnce(t *testing.T) {
|
|
|
|
addr := setupRemotePluginServer()
|
|
|
|
defer teardownRemotePluginServer()
|
|
|
|
|
|
|
|
failed := false
|
|
|
|
mux.HandleFunc("/Test.FailOnce", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !failed {
|
|
|
|
failed = true
|
|
|
|
panic("Plugin not ready")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
|
|
|
b := strings.NewReader("body")
|
|
|
|
_, err := c.callWithRetry("Test.FailOnce", b, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 13:05:39 -04:00
|
|
|
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) {
|
2019-10-12 14:44:07 -04:00
|
|
|
if r.Method != http.MethodPost {
|
2015-05-14 13:05:39 -04:00
|
|
|
t.Fatalf("Expected POST, got %s\n", r.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := w.Header()
|
2016-03-01 20:16:40 -05:00
|
|
|
header.Set("Content-Type", transport.VersionMimetype)
|
2015-05-14 13:05:39 -04:00
|
|
|
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
})
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
2015-05-14 13:05:39 -04:00
|
|
|
var output Manifest
|
|
|
|
err := c.Call("Test.Echo", m, &output)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(m, output))
|
2015-12-05 02:55:50 -05:00
|
|
|
err = c.Call("Test.Echo", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-14 13:05:39 -04:00
|
|
|
}
|
2015-05-19 16:05:25 -04: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 15:54:29 -04: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-01 20:16:40 -05: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 15:54:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-22 05:38:23 -04:00
|
|
|
|
|
|
|
func TestNewClientWithTimeout(t *testing.T) {
|
|
|
|
addr := setupRemotePluginServer()
|
|
|
|
defer teardownRemotePluginServer()
|
|
|
|
|
|
|
|
m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}}
|
|
|
|
|
|
|
|
mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(time.Duration(600) * time.Millisecond)
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
})
|
|
|
|
|
|
|
|
// setting timeout of 500ms
|
|
|
|
timeout := time.Duration(500) * time.Millisecond
|
|
|
|
c, _ := NewClientWithTimeout(addr, &tlsconfig.Options{InsecureSkipVerify: true}, timeout)
|
|
|
|
var output Manifest
|
|
|
|
err := c.Call("Test.Echo", m, &output)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected timeout error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientStream(t *testing.T) {
|
|
|
|
addr := setupRemotePluginServer()
|
|
|
|
defer teardownRemotePluginServer()
|
|
|
|
|
|
|
|
m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}}
|
|
|
|
var output Manifest
|
|
|
|
|
|
|
|
mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) {
|
2019-10-12 14:44:07 -04:00
|
|
|
if r.Method != http.MethodPost {
|
2017-05-22 05:38:23 -04:00
|
|
|
t.Fatalf("Expected POST, got %s", r.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := w.Header()
|
|
|
|
header.Set("Content-Type", transport.VersionMimetype)
|
|
|
|
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
})
|
|
|
|
|
|
|
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
|
|
|
body, err := c.Stream("Test.Echo", m)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer body.Close()
|
|
|
|
if err := json.NewDecoder(body).Decode(&output); err != nil {
|
|
|
|
t.Fatalf("Test.Echo: error reading plugin resp: %v", err)
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(m, output))
|
2017-05-22 05:38:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientSendFile(t *testing.T) {
|
|
|
|
addr := setupRemotePluginServer()
|
|
|
|
defer teardownRemotePluginServer()
|
|
|
|
|
|
|
|
m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}}
|
|
|
|
var output Manifest
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) {
|
2019-10-12 14:44:07 -04:00
|
|
|
if r.Method != http.MethodPost {
|
2017-05-22 05:38:23 -04:00
|
|
|
t.Fatalf("Expected POST, got %s\n", r.Method)
|
|
|
|
}
|
|
|
|
|
|
|
|
header := w.Header()
|
|
|
|
header.Set("Content-Type", transport.VersionMimetype)
|
|
|
|
|
|
|
|
io.Copy(w, r.Body)
|
|
|
|
})
|
|
|
|
|
|
|
|
c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true})
|
|
|
|
if err := c.SendFile("Test.Echo", &buf, &output); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(m, output))
|
2017-05-22 05:38:23 -04:00
|
|
|
}
|
2018-03-05 15:29:02 -05:00
|
|
|
|
|
|
|
func TestClientWithRequestTimeout(t *testing.T) {
|
2019-04-18 05:59:25 -04:00
|
|
|
type timeoutError interface {
|
|
|
|
Timeout() bool
|
|
|
|
}
|
|
|
|
|
2018-03-05 15:29:02 -05:00
|
|
|
timeout := 1 * time.Millisecond
|
|
|
|
testHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
time.Sleep(timeout + 1*time.Millisecond)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(testHandler))
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
client := &Client{http: srv.Client(), requestFactory: &testRequestWrapper{srv}}
|
|
|
|
_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Assert(t, is.ErrorContains(err, ""), "expected error")
|
2018-03-05 15:29:02 -05:00
|
|
|
|
|
|
|
err = errors.Cause(err)
|
2019-04-18 05:59:25 -04:00
|
|
|
assert.ErrorType(t, err, (*timeoutError)(nil))
|
|
|
|
assert.Equal(t, err.(timeoutError).Timeout(), true)
|
2018-03-05 15:29:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type testRequestWrapper struct {
|
|
|
|
*httptest.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *testRequestWrapper) NewRequest(path string, data io.Reader) (*http.Request, error) {
|
2019-10-12 14:44:07 -04:00
|
|
|
req, err := http.NewRequest(http.MethodPost, path, data)
|
2018-03-05 15:29:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u, err := url.Parse(w.Server.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.URL = u
|
|
|
|
return req, nil
|
|
|
|
}
|