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