github.com/rish1988/moby@v25.0.2+incompatible/client/swarm_get_unlock_key_test.go (about) 1 package client // import "github.com/docker/docker/client" 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/json" 7 "fmt" 8 "io" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/errdefs" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 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 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 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 != http.MethodGet { 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: io.NopCloser(bytes.NewReader(b)), 53 }, nil 54 }), 55 } 56 57 resp, err := client.SwarmGetUnlockKey(context.Background()) 58 assert.NilError(t, err) 59 assert.Check(t, is.Equal(unlockKey, resp.UnlockKey)) 60 }