task_inspect_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. "github.com/pkg/errors"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. )
  17. func TestTaskInspectError(t *testing.T) {
  18. client := &Client{
  19. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  20. }
  21. _, _, err := client.TaskInspectWithRaw(context.Background(), "nothing")
  22. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  23. }
  24. func TestTaskInspectWithEmptyID(t *testing.T) {
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. return nil, errors.New("should not make request")
  28. }),
  29. }
  30. _, _, err := client.TaskInspectWithRaw(context.Background(), "")
  31. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  32. }
  33. func TestTaskInspect(t *testing.T) {
  34. expectedURL := "/tasks/task_id"
  35. client := &Client{
  36. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  37. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  38. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  39. }
  40. content, err := json.Marshal(swarm.Task{
  41. ID: "task_id",
  42. })
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &http.Response{
  47. StatusCode: http.StatusOK,
  48. Body: io.NopCloser(bytes.NewReader(content)),
  49. }, nil
  50. }),
  51. }
  52. taskInspect, _, err := client.TaskInspectWithRaw(context.Background(), "task_id")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if taskInspect.ID != "task_id" {
  57. t.Fatalf("expected `task_id`, got %s", taskInspect.ID)
  58. }
  59. }