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