swarm_inspect_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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/swarm"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  15. )
  16. func TestSwarmInspectError(t *testing.T) {
  17. client := &Client{
  18. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  19. }
  20. _, err := client.SwarmInspect(context.Background())
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestSwarmInspect(t *testing.T) {
  24. expectedURL := "/swarm"
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  28. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  29. }
  30. content, err := json.Marshal(swarm.Swarm{
  31. ClusterInfo: swarm.ClusterInfo{
  32. ID: "swarm_id",
  33. },
  34. })
  35. if err != nil {
  36. return nil, err
  37. }
  38. return &http.Response{
  39. StatusCode: http.StatusOK,
  40. Body: io.NopCloser(bytes.NewReader(content)),
  41. }, nil
  42. }),
  43. }
  44. swarmInspect, err := client.SwarmInspect(context.Background())
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if swarmInspect.ID != "swarm_id" {
  49. t.Fatalf("expected `swarm_id`, got %s", swarmInspect.ID)
  50. }
  51. }