Signed-off-by: allencloud <allen.sun@daocloud.io>
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,60 @@ |
| 0 |
+package client |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "bytes" |
|
| 4 |
+ "encoding/json" |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "io/ioutil" |
|
| 7 |
+ "net/http" |
|
| 8 |
+ "strings" |
|
| 9 |
+ "testing" |
|
| 10 |
+ |
|
| 11 |
+ "github.com/docker/docker/api/types" |
|
| 12 |
+ "github.com/docker/docker/pkg/testutil" |
|
| 13 |
+ "github.com/stretchr/testify/assert" |
|
| 14 |
+ "github.com/stretchr/testify/require" |
|
| 15 |
+ "golang.org/x/net/context" |
|
| 16 |
+) |
|
| 17 |
+ |
|
| 18 |
+func TestSwarmGetUnlockKeyError(t *testing.T) {
|
|
| 19 |
+ client := &Client{
|
|
| 20 |
+ client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ _, err := client.SwarmGetUnlockKey(context.Background()) |
|
| 24 |
+ testutil.ErrorContains(t, err, "Error response from daemon: Server error") |
|
| 25 |
+} |
|
| 26 |
+ |
|
| 27 |
+func TestSwarmGetUnlockKey(t *testing.T) {
|
|
| 28 |
+ expectedURL := "/swarm/unlockkey" |
|
| 29 |
+ unlockKey := "SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeE" |
|
| 30 |
+ |
|
| 31 |
+ client := &Client{
|
|
| 32 |
+ client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
| 33 |
+ if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
| 34 |
+ return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
| 35 |
+ } |
|
| 36 |
+ if req.Method != "GET" {
|
|
| 37 |
+ return nil, fmt.Errorf("expected GET method, got %s", req.Method)
|
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ key := types.SwarmUnlockKeyResponse{
|
|
| 41 |
+ UnlockKey: unlockKey, |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ b, err := json.Marshal(key) |
|
| 45 |
+ if err != nil {
|
|
| 46 |
+ return nil, err |
|
| 47 |
+ } |
|
| 48 |
+ |
|
| 49 |
+ return &http.Response{
|
|
| 50 |
+ StatusCode: http.StatusOK, |
|
| 51 |
+ Body: ioutil.NopCloser(bytes.NewReader(b)), |
|
| 52 |
+ }, nil |
|
| 53 |
+ }), |
|
| 54 |
+ } |
|
| 55 |
+ |
|
| 56 |
+ resp, err := client.SwarmGetUnlockKey(context.Background()) |
|
| 57 |
+ require.NoError(t, err) |
|
| 58 |
+ assert.Equal(t, unlockKey, resp.UnlockKey) |
|
| 59 |
+} |