github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/task_inspect_test.go (about) 1 package client 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "strings" 10 "testing" 11 12 "github.com/docker/docker/api/types/swarm" 13 "golang.org/x/net/context" 14 ) 15 16 func TestTaskInspectError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 21 _, _, err := client.TaskInspectWithRaw(context.Background(), "nothing") 22 if err == nil || err.Error() != "Error response from daemon: Server error" { 23 t.Fatalf("expected a Server Error, got %v", err) 24 } 25 } 26 27 func TestTaskInspect(t *testing.T) { 28 expectedURL := "/tasks/task_id" 29 client := &Client{ 30 client: newMockClient(func(req *http.Request) (*http.Response, error) { 31 if !strings.HasPrefix(req.URL.Path, expectedURL) { 32 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 33 } 34 content, err := json.Marshal(swarm.Task{ 35 ID: "task_id", 36 }) 37 if err != nil { 38 return nil, err 39 } 40 return &http.Response{ 41 StatusCode: http.StatusOK, 42 Body: ioutil.NopCloser(bytes.NewReader(content)), 43 }, nil 44 }), 45 } 46 47 taskInspect, _, err := client.TaskInspectWithRaw(context.Background(), "task_id") 48 if err != nil { 49 t.Fatal(err) 50 } 51 if taskInspect.ID != "task_id" { 52 t.Fatalf("expected `task_id`, got %s", taskInspect.ID) 53 } 54 }