From 7ca971fb495e4de4aa4455964625974464d86920 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Mon, 5 Mar 2018 15:29:02 -0500 Subject: [PATCH] Adds a unit test for plugin request timeout Signed-off-by: Brian Goff --- pkg/plugins/client_test.go | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pkg/plugins/client_test.go b/pkg/plugins/client_test.go index 44dd0fa81c..10c8d8fd56 100644 --- a/pkg/plugins/client_test.go +++ b/pkg/plugins/client_test.go @@ -2,6 +2,7 @@ package plugins // import "github.com/docker/docker/pkg/plugins" import ( "bytes" + "context" "encoding/json" "io" "net/http" @@ -13,7 +14,9 @@ import ( "github.com/docker/docker/pkg/plugins/transport" "github.com/docker/go-connections/tlsconfig" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var ( @@ -232,3 +235,43 @@ func TestClientSendFile(t *testing.T) { } assert.Equal(t, m, output) } + +func TestClientWithRequestTimeout(t *testing.T) { + 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)) + require.Error(t, err, "expected error") + + err = errors.Cause(err) + + switch e := err.(type) { + case *url.Error: + err = e.Err + } + require.Equal(t, context.DeadlineExceeded, err) +} + +type testRequestWrapper struct { + *httptest.Server +} + +func (w *testRequestWrapper) NewRequest(path string, data io.Reader) (*http.Request, error) { + req, err := http.NewRequest("POST", path, data) + 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 +}