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