github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+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/ioutil"
     9  	"net/http"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/docker/docker/api/types"
    14  	"github.com/docker/docker/errdefs"
    15  )
    16  
    17  func TestCheckpointCreateError(t *testing.T) {
    18  	client := &Client{
    19  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    20  	}
    21  	err := client.CheckpointCreate(context.Background(), "nothing", types.CheckpointCreateOptions{
    22  		CheckpointID: "noting",
    23  		Exit:         true,
    24  	})
    25  
    26  	if !errdefs.IsSystem(err) {
    27  		t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
    28  	}
    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 := &types.CheckpointCreateOptions{}
    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:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    62  			}, nil
    63  		}),
    64  	}
    65  
    66  	err := client.CheckpointCreate(context.Background(), expectedContainerID, types.CheckpointCreateOptions{
    67  		CheckpointID: expectedCheckpointID,
    68  		Exit:         true,
    69  	})
    70  
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  }