Browse code

Adds a unit test for plugin request timeout

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2018/03/06 05:29:02
Showing 1 changed files
... ...
@@ -2,6 +2,7 @@ package plugins // import "github.com/docker/docker/pkg/plugins"
2 2
 
3 3
 import (
4 4
 	"bytes"
5
+	"context"
5 6
 	"encoding/json"
6 7
 	"io"
7 8
 	"net/http"
... ...
@@ -13,7 +14,9 @@ import (
13 13
 
14 14
 	"github.com/docker/docker/pkg/plugins/transport"
15 15
 	"github.com/docker/go-connections/tlsconfig"
16
+	"github.com/pkg/errors"
16 17
 	"github.com/stretchr/testify/assert"
18
+	"github.com/stretchr/testify/require"
17 19
 )
18 20
 
19 21
 var (
... ...
@@ -232,3 +235,43 @@ func TestClientSendFile(t *testing.T) {
232 232
 	}
233 233
 	assert.Equal(t, m, output)
234 234
 }
235
+
236
+func TestClientWithRequestTimeout(t *testing.T) {
237
+	timeout := 1 * time.Millisecond
238
+	testHandler := func(w http.ResponseWriter, r *http.Request) {
239
+		time.Sleep(timeout + 1*time.Millisecond)
240
+		w.WriteHeader(http.StatusOK)
241
+	}
242
+
243
+	srv := httptest.NewServer(http.HandlerFunc(testHandler))
244
+	defer srv.Close()
245
+
246
+	client := &Client{http: srv.Client(), requestFactory: &testRequestWrapper{srv}}
247
+	_, err := client.callWithRetry("/Plugin.Hello", nil, false, WithRequestTimeout(timeout))
248
+	require.Error(t, err, "expected error")
249
+
250
+	err = errors.Cause(err)
251
+
252
+	switch e := err.(type) {
253
+	case *url.Error:
254
+		err = e.Err
255
+	}
256
+	require.Equal(t, context.DeadlineExceeded, err)
257
+}
258
+
259
+type testRequestWrapper struct {
260
+	*httptest.Server
261
+}
262
+
263
+func (w *testRequestWrapper) NewRequest(path string, data io.Reader) (*http.Request, error) {
264
+	req, err := http.NewRequest("POST", path, data)
265
+	if err != nil {
266
+		return nil, err
267
+	}
268
+	u, err := url.Parse(w.Server.URL)
269
+	if err != nil {
270
+		return nil, err
271
+	}
272
+	req.URL = u
273
+	return req, nil
274
+}