swarm_get_unlock_key_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestSwarmGetUnlockKeyError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.SwarmGetUnlockKey(context.Background())
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestSwarmGetUnlockKey(t *testing.T) {
  24. expectedURL := "/swarm/unlockkey"
  25. unlockKey := "SWMKEY-1-y6guTZNTwpQeTL5RhUfOsdBdXoQjiB2GADHSRJvbXeE"
  26. client := &Client{
  27. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  28. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  29. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  30. }
  31. if req.Method != http.MethodGet {
  32. return nil, fmt.Errorf("expected GET method, got %s", req.Method)
  33. }
  34. key := types.SwarmUnlockKeyResponse{
  35. UnlockKey: unlockKey,
  36. }
  37. b, err := json.Marshal(key)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &http.Response{
  42. StatusCode: http.StatusOK,
  43. Body: io.NopCloser(bytes.NewReader(b)),
  44. }, nil
  45. }),
  46. }
  47. resp, err := client.SwarmGetUnlockKey(context.Background())
  48. assert.NilError(t, err)
  49. assert.Check(t, is.Equal(unlockKey, resp.UnlockKey))
  50. }