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