github.com/rish1988/moby@v25.0.2+incompatible/client/checkpoint_create_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/checkpoint" 14 "github.com/docker/docker/errdefs" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestCheckpointCreateError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 err := client.CheckpointCreate(context.Background(), "nothing", checkpoint.CreateOptions{ 24 CheckpointID: "noting", 25 Exit: true, 26 }) 27 28 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 29 } 30 31 func TestCheckpointCreate(t *testing.T) { 32 expectedContainerID := "container_id" 33 expectedCheckpointID := "checkpoint_id" 34 expectedURL := "/containers/container_id/checkpoints" 35 36 client := &Client{ 37 client: newMockClient(func(req *http.Request) (*http.Response, error) { 38 if !strings.HasPrefix(req.URL.Path, expectedURL) { 39 return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL) 40 } 41 42 if req.Method != http.MethodPost { 43 return nil, fmt.Errorf("expected POST method, got %s", req.Method) 44 } 45 46 createOptions := &checkpoint.CreateOptions{} 47 if err := json.NewDecoder(req.Body).Decode(createOptions); err != nil { 48 return nil, err 49 } 50 51 if createOptions.CheckpointID != expectedCheckpointID { 52 return nil, fmt.Errorf("expected CheckpointID to be 'checkpoint_id', got %v", createOptions.CheckpointID) 53 } 54 55 if !createOptions.Exit { 56 return nil, fmt.Errorf("expected Exit to be true") 57 } 58 59 return &http.Response{ 60 StatusCode: http.StatusOK, 61 Body: io.NopCloser(bytes.NewReader([]byte(""))), 62 }, nil 63 }), 64 } 65 66 err := client.CheckpointCreate(context.Background(), expectedContainerID, checkpoint.CreateOptions{ 67 CheckpointID: expectedCheckpointID, 68 Exit: true, 69 }) 70 if err != nil { 71 t.Fatal(err) 72 } 73 }