container_restart_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package client // import "github.com/docker/docker/client"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/errdefs"
  12. "gotest.tools/v3/assert"
  13. is "gotest.tools/v3/assert/cmp"
  14. )
  15. func TestContainerRestartError(t *testing.T) {
  16. client := &Client{
  17. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  18. }
  19. err := client.ContainerRestart(context.Background(), "nothing", container.StopOptions{})
  20. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  21. }
  22. func TestContainerRestart(t *testing.T) {
  23. const expectedURL = "/v1.42/containers/container_id/restart"
  24. client := &Client{
  25. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  26. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  27. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  28. }
  29. s := req.URL.Query().Get("signal")
  30. if s != "SIGKILL" {
  31. return nil, fmt.Errorf("signal not set in URL query. Expected 'SIGKILL', got '%s'", s)
  32. }
  33. t := req.URL.Query().Get("t")
  34. if t != "100" {
  35. return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
  36. }
  37. return &http.Response{
  38. StatusCode: http.StatusOK,
  39. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  40. }, nil
  41. }),
  42. version: "1.42",
  43. }
  44. timeout := 100
  45. err := client.ContainerRestart(context.Background(), "container_id", container.StopOptions{
  46. Signal: "SIGKILL",
  47. Timeout: &timeout,
  48. })
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. }