container_start_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/errdefs"
  13. "gotest.tools/v3/assert"
  14. is "gotest.tools/v3/assert/cmp"
  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", container.StartOptions{})
  21. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  22. }
  23. func TestContainerStart(t *testing.T) {
  24. expectedURL := "/containers/container_id/start"
  25. client := &Client{
  26. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  27. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  28. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  29. }
  30. // we're not expecting any payload, but if one is supplied, check it is valid.
  31. if req.Header.Get("Content-Type") == "application/json" {
  32. var startConfig interface{}
  33. if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
  34. return nil, fmt.Errorf("Unable to parse json: %s", err)
  35. }
  36. }
  37. checkpoint := req.URL.Query().Get("checkpoint")
  38. if checkpoint != "checkpoint_id" {
  39. return nil, fmt.Errorf("checkpoint not set in URL query properly. Expected 'checkpoint_id', got %s", checkpoint)
  40. }
  41. return &http.Response{
  42. StatusCode: http.StatusOK,
  43. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  44. }, nil
  45. }),
  46. }
  47. err := client.ContainerStart(context.Background(), "container_id", container.StartOptions{CheckpointID: "checkpoint_id"})
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. }