github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/pkg/plugins/client_test.go (about)

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