github.com/wulonghui/docker@v1.8.0-rc2/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/docker/pkg/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, 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  }
    67  
    68  func TestBackoff(t *testing.T) {
    69  	cases := []struct {
    70  		retries    int
    71  		expTimeOff time.Duration
    72  	}{
    73  		{0, time.Duration(1)},
    74  		{1, time.Duration(2)},
    75  		{2, time.Duration(4)},
    76  		{4, time.Duration(16)},
    77  		{6, time.Duration(30)},
    78  		{10, time.Duration(30)},
    79  	}
    80  
    81  	for _, c := range cases {
    82  		s := c.expTimeOff * time.Second
    83  		if d := backoff(c.retries); d != s {
    84  			t.Fatalf("Retry %v, expected %v, was %v\n", c.retries, s, d)
    85  		}
    86  	}
    87  }
    88  
    89  func TestAbortRetry(t *testing.T) {
    90  	cases := []struct {
    91  		timeOff  time.Duration
    92  		expAbort bool
    93  	}{
    94  		{time.Duration(1), false},
    95  		{time.Duration(2), false},
    96  		{time.Duration(10), false},
    97  		{time.Duration(30), true},
    98  		{time.Duration(40), true},
    99  	}
   100  
   101  	for _, c := range cases {
   102  		s := c.timeOff * time.Second
   103  		if a := abort(time.Now(), s); a != c.expAbort {
   104  			t.Fatalf("Duration %v, expected %v, was %v\n", c.timeOff, s, a)
   105  		}
   106  	}
   107  }