github.com/michael-k/docker@v1.7.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  
    12  var (
    13  	mux    *http.ServeMux
    14  	server *httptest.Server
    15  )
    16  
    17  func setupRemotePluginServer() string {
    18  	mux = http.NewServeMux()
    19  	server = httptest.NewServer(mux)
    20  	return server.URL
    21  }
    22  
    23  func teardownRemotePluginServer() {
    24  	if server != nil {
    25  		server.Close()
    26  	}
    27  }
    28  
    29  func TestFailedConnection(t *testing.T) {
    30  	c := NewClient("tcp://127.0.0.1:1")
    31  	err := c.callWithRetry("Service.Method", nil, nil, false)
    32  	if err == nil {
    33  		t.Fatal("Unexpected successful connection")
    34  	}
    35  }
    36  
    37  func TestEchoInputOutput(t *testing.T) {
    38  	addr := setupRemotePluginServer()
    39  	defer teardownRemotePluginServer()
    40  
    41  	m := Manifest{[]string{"VolumeDriver", "NetworkDriver"}}
    42  
    43  	mux.HandleFunc("/Test.Echo", func(w http.ResponseWriter, r *http.Request) {
    44  		if r.Method != "POST" {
    45  			t.Fatalf("Expected POST, got %s\n", r.Method)
    46  		}
    47  
    48  		header := w.Header()
    49  		header.Set("Content-Type", versionMimetype)
    50  
    51  		io.Copy(w, r.Body)
    52  	})
    53  
    54  	c := NewClient(addr)
    55  	var output Manifest
    56  	err := c.Call("Test.Echo", m, &output)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	if !reflect.DeepEqual(output, m) {
    62  		t.Fatalf("Expected %v, was %v\n", m, output)
    63  	}
    64  }
    65  
    66  func TestBackoff(t *testing.T) {
    67  	cases := []struct {
    68  		retries    int
    69  		expTimeOff time.Duration
    70  	}{
    71  		{0, time.Duration(1)},
    72  		{1, time.Duration(2)},
    73  		{2, time.Duration(4)},
    74  		{4, time.Duration(16)},
    75  		{6, time.Duration(30)},
    76  		{10, time.Duration(30)},
    77  	}
    78  
    79  	for _, c := range cases {
    80  		s := c.expTimeOff * time.Second
    81  		if d := backoff(c.retries); d != s {
    82  			t.Fatalf("Retry %v, expected %v, was %v\n", c.retries, s, d)
    83  		}
    84  	}
    85  }
    86  
    87  func TestAbortRetry(t *testing.T) {
    88  	cases := []struct {
    89  		timeOff  time.Duration
    90  		expAbort bool
    91  	}{
    92  		{time.Duration(1), false},
    93  		{time.Duration(2), false},
    94  		{time.Duration(10), false},
    95  		{time.Duration(30), true},
    96  		{time.Duration(40), true},
    97  	}
    98  
    99  	for _, c := range cases {
   100  		s := c.timeOff * time.Second
   101  		if a := abort(time.Now(), s); a != c.expAbort {
   102  			t.Fatalf("Duration %v, expected %v, was %v\n", c.timeOff, s, a)
   103  		}
   104  	}
   105  }