github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/client/task_inspect_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/swarm" 14 "github.com/docker/docker/errdefs" 15 "github.com/pkg/errors" 16 ) 17 18 func TestTaskInspectError(t *testing.T) { 19 client := &Client{ 20 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 21 } 22 23 _, _, err := client.TaskInspectWithRaw(context.Background(), "nothing") 24 if !errdefs.IsSystem(err) { 25 t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err) 26 } 27 } 28 29 func TestTaskInspectWithEmptyID(t *testing.T) { 30 client := &Client{ 31 client: newMockClient(func(req *http.Request) (*http.Response, error) { 32 return nil, errors.New("should not make request") 33 }), 34 } 35 _, _, err := client.TaskInspectWithRaw(context.Background(), "") 36 if !IsErrNotFound(err) { 37 t.Fatalf("Expected NotFoundError, got %v", err) 38 } 39 } 40 41 func TestTaskInspect(t *testing.T) { 42 expectedURL := "/tasks/task_id" 43 client := &Client{ 44 client: newMockClient(func(req *http.Request) (*http.Response, error) { 45 if !strings.HasPrefix(req.URL.Path, expectedURL) { 46 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 47 } 48 content, err := json.Marshal(swarm.Task{ 49 ID: "task_id", 50 }) 51 if err != nil { 52 return nil, err 53 } 54 return &http.Response{ 55 StatusCode: http.StatusOK, 56 Body: io.NopCloser(bytes.NewReader(content)), 57 }, nil 58 }), 59 } 60 61 taskInspect, _, err := client.TaskInspectWithRaw(context.Background(), "task_id") 62 if err != nil { 63 t.Fatal(err) 64 } 65 if taskInspect.ID != "task_id" { 66 t.Fatalf("expected `task_id`, got %s", taskInspect.ID) 67 } 68 }