github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/client/checkpoint_list_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" 13 "golang.org/x/net/context" 14 ) 15 16 func TestCheckpointListError(t *testing.T) { 17 client := &Client{ 18 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 19 } 20 21 _, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{}) 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 TestCheckpointList(t *testing.T) { 28 expectedURL := "/containers/container_id/checkpoints" 29 30 client := &Client{ 31 client: newMockClient(func(req *http.Request) (*http.Response, error) { 32 if !strings.HasPrefix(req.URL.Path, expectedURL) { 33 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 34 } 35 content, err := json.Marshal([]types.Checkpoint{ 36 { 37 Name: "checkpoint", 38 }, 39 }) 40 if err != nil { 41 return nil, err 42 } 43 return &http.Response{ 44 StatusCode: http.StatusOK, 45 Body: ioutil.NopCloser(bytes.NewReader(content)), 46 }, nil 47 }), 48 } 49 50 checkpoints, err := client.CheckpointList(context.Background(), "container_id", types.CheckpointListOptions{}) 51 if err != nil { 52 t.Fatal(err) 53 } 54 if len(checkpoints) != 1 { 55 t.Fatalf("expected 1 checkpoint, got %v", checkpoints) 56 } 57 } 58 59 func TestCheckpointListContainerNotFound(t *testing.T) { 60 client := &Client{ 61 client: newMockClient(errorMock(http.StatusNotFound, "Server error")), 62 } 63 64 _, err := client.CheckpointList(context.Background(), "unknown", types.CheckpointListOptions{}) 65 if err == nil || !IsErrNotFound(err) { 66 t.Fatalf("expected a containerNotFound error, got %v", err) 67 } 68 }