github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/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" 14 "github.com/docker/docker/errdefs" 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 !errdefs.IsSystem(err) { 23 t.Fatalf("expected a Server Error, got %[1]T: %[1]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: io.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 }