Browse code

add swarm unlock test in client

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/05/29 01:02:21
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+package client
1
+
2
+import (
3
+	"bytes"
4
+	"fmt"
5
+	"io/ioutil"
6
+	"net/http"
7
+	"strings"
8
+	"testing"
9
+
10
+	"golang.org/x/net/context"
11
+
12
+	"github.com/docker/docker/api/types/swarm"
13
+)
14
+
15
+func TestSwarmUnlockError(t *testing.T) {
16
+	client := &Client{
17
+		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
18
+	}
19
+
20
+	err := client.SwarmUnlock(context.Background(), swarm.UnlockRequest{"SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeU"})
21
+	if err == nil || err.Error() != "Error response from daemon: Server error" {
22
+		t.Fatalf("expected a Server Error, got %v", err)
23
+	}
24
+}
25
+
26
+func TestSwarmUnlock(t *testing.T) {
27
+	expectedURL := "/swarm/unlock"
28
+
29
+	client := &Client{
30
+		client: newMockClient(func(req *http.Request) (*http.Response, error) {
31
+			if !strings.HasPrefix(req.URL.Path, expectedURL) {
32
+				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
33
+			}
34
+			if req.Method != "POST" {
35
+				return nil, fmt.Errorf("expected POST method, got %s", req.Method)
36
+			}
37
+			return &http.Response{
38
+				StatusCode: http.StatusOK,
39
+				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
40
+			}, nil
41
+		}),
42
+	}
43
+
44
+	err := client.SwarmUnlock(context.Background(), swarm.UnlockRequest{"SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeU"})
45
+	if err != nil {
46
+		t.Fatal(err)
47
+	}
48
+}