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