github.com/vvnotw/moby@v1.13.1/pkg/plugins/client_test.go (about) 1 package plugins 2 3 import ( 4 "io" 5 "net/http" 6 "net/http/httptest" 7 "net/url" 8 "reflect" 9 "testing" 10 "time" 11 12 "github.com/docker/docker/pkg/plugins/transport" 13 "github.com/docker/go-connections/tlsconfig" 14 ) 15 16 var ( 17 mux *http.ServeMux 18 server *httptest.Server 19 ) 20 21 func setupRemotePluginServer() string { 22 mux = http.NewServeMux() 23 server = httptest.NewServer(mux) 24 return server.URL 25 } 26 27 func teardownRemotePluginServer() { 28 if server != nil { 29 server.Close() 30 } 31 } 32 33 func TestFailedConnection(t *testing.T) { 34 c, _ := NewClient("tcp://127.0.0.1:1", &tlsconfig.Options{InsecureSkipVerify: true}) 35 _, err := c.callWithRetry("Service.Method", nil, false) 36 if err == nil { 37 t.Fatal("Unexpected successful connection") 38 } 39 } 40 41 func TestEchoInputOutput(t *testing.T) { 42 addr := setupRemotePluginServer() 43 defer teardownRemotePluginServer() 44 45 m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}} 46 47 mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) { 48 if r.Method != "POST" { 49 t.Fatalf("Expected POST, got %s\n", r.Method) 50 } 51 52 header := w.Header() 53 header.Set("Content-Type", transport.VersionMimetype) 54 55 io.Copy(w, r.Body) 56 }) 57 58 c, _ := NewClient(addr, &tlsconfig.Options{InsecureSkipVerify: true}) 59 var output Manifest 60 err := c.Call("Test.Echo", m, &output) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 if !reflect.DeepEqual(output, m) { 66 t.Fatalf("Expected %v, was %v\n", m, output) 67 } 68 err = c.Call("Test.Echo", nil, nil) 69 if err != nil { 70 t.Fatal(err) 71 } 72 } 73 74 func TestBackoff(t *testing.T) { 75 cases := []struct { 76 retries int 77 expTimeOff time.Duration 78 }{ 79 {0, time.Duration(1)}, 80 {1, time.Duration(2)}, 81 {2, time.Duration(4)}, 82 {4, time.Duration(16)}, 83 {6, time.Duration(30)}, 84 {10, time.Duration(30)}, 85 } 86 87 for _, c := range cases { 88 s := c.expTimeOff * time.Second 89 if d := backoff(c.retries); d != s { 90 t.Fatalf("Retry %v, expected %v, was %v\n", c.retries, s, d) 91 } 92 } 93 } 94 95 func TestAbortRetry(t *testing.T) { 96 cases := []struct { 97 timeOff time.Duration 98 expAbort bool 99 }{ 100 {time.Duration(1), false}, 101 {time.Duration(2), false}, 102 {time.Duration(10), false}, 103 {time.Duration(30), true}, 104 {time.Duration(40), true}, 105 } 106 107 for _, c := range cases { 108 s := c.timeOff * time.Second 109 if a := abort(time.Now(), s); a != c.expAbort { 110 t.Fatalf("Duration %v, expected %v, was %v\n", c.timeOff, s, a) 111 } 112 } 113 } 114 115 func TestClientScheme(t *testing.T) { 116 cases := map[string]string{ 117 "tcp://127.0.0.1:8080": "http", 118 "unix:///usr/local/plugins/foo": "http", 119 "http://127.0.0.1:8080": "http", 120 "https://127.0.0.1:8080": "https", 121 } 122 123 for addr, scheme := range cases { 124 u, err := url.Parse(addr) 125 if err != nil { 126 t.Fatal(err) 127 } 128 s := httpScheme(u) 129 130 if s != scheme { 131 t.Fatalf("URL scheme mismatch, expected %s, got %s", scheme, s) 132 } 133 } 134 }