github.com/rish1988/moby@v25.0.2+incompatible/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" 9 "net/http" 10 "strings" 11 "testing" 12 13 "github.com/docker/docker/api/types/container" 14 "github.com/docker/docker/errdefs" 15 "gotest.tools/v3/assert" 16 is "gotest.tools/v3/assert/cmp" 17 ) 18 19 func TestContainerStartError(t *testing.T) { 20 client := &Client{ 21 client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")), 22 } 23 err := client.ContainerStart(context.Background(), "nothing", container.StartOptions{}) 24 assert.Check(t, is.ErrorType(err, errdefs.IsSystem)) 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: io.NopCloser(bytes.NewReader([]byte(""))), 50 }, nil 51 }), 52 } 53 54 err := client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"}) 55 if err != nil { 56 t.Fatal(err) 57 } 58 }