github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/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 if !errdefs.IsSystem(err) { 26 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 27 } 28 } 29 30 func TestSwarmGetUnlockKey(t *testing.T) { 31 expectedURL := "/swarm/unlockkey" 32 unlockKey := "SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeE" 33 34 client := &Client{ 35 client: newMockClient(func(req *http.Request) (*http.Response, error) { 36 if !strings.HasPrefix(req.URL.Path, expectedURL) { 37 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 38 } 39 if req.Method != http.MethodGet { 40 return nil, fmt.Errorf("expected GET method, got %s", req.Method) 41 } 42 43 key := types.SwarmUnlockKeyResponse{ 44 UnlockKey: unlockKey, 45 } 46 47 b, err := json.Marshal(key) 48 if err != nil { 49 return nil, err 50 } 51 52 return &http.Response{ 53 StatusCode: http.StatusOK, 54 Body: io.NopCloser(bytes.NewReader(b)), 55 }, nil 56 }), 57 } 58 59 resp, err := client.SwarmGetUnlockKey(context.Background()) 60 assert.NilError(t, err) 61 assert.Check(t, is.Equal(unlockKey, resp.UnlockKey)) 62 }