github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/client/container_start_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  )
    15  
    16  func TestContainerStartError(t *testing.T) {
    17  	client := &Client{
    18  		client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
    19  	}
    20  	err := client.ContainerStart(context.Background(), "nothing", types.ContainerStartOptions{})
    21  	if err == nil || err.Error() != "Error response from daemon: Server error" {
    22  		t.Fatalf("expected a Server Error, got %v", err)
    23  	}
    24  }
    25  
    26  func TestContainerStart(t *testing.T) {
    27  	expectedURL := "/containers/container_id/start"
    28  	client := &Client{
    29  		client: newMockClient(func(req *http.Request) (*http.Response, error) {
    30  			if !strings.HasPrefix(req.URL.Path, expectedURL) {
    31  				return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
    32  			}
    33  			// we're not expecting any payload, but if one is supplied, check it is valid.
    34  			if req.Header.Get("Content-Type") == "application/json" {
    35  				var startConfig interface{}
    36  				if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
    37  					return nil, fmt.Errorf("Unable to parse json: %s", err)
    38  				}
    39  			}
    40  
    41  			checkpoint := req.URL.Query().Get("checkpoint")
    42  			if checkpoint != "checkpoint_id" {
    43  				return nil, fmt.Errorf("checkpoint not set in URL query properly. Expected 'checkpoint_id', got %s", checkpoint)
    44  			}
    45  
    46  			return &http.Response{
    47  				StatusCode: http.StatusOK,
    48  				Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
    49  			}, nil
    50  		}),
    51  	}
    52  
    53  	err := client.ContainerStart(context.Background(), "container_id", types.ContainerStartOptions{CheckpointID: "checkpoint_id"})
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  }