swarm_get_unlock_key_test.go 1.5 KB

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