container_kill_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/errdefs"
  11. "gotest.tools/v3/assert"
  12. is "gotest.tools/v3/assert/cmp"
  13. )
  14. func TestContainerKillError(t *testing.T) {
  15. client := &Client{
  16. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  17. }
  18. err := client.ContainerKill(context.Background(), "nothing", "SIGKILL")
  19. assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
  20. }
  21. func TestContainerKill(t *testing.T) {
  22. expectedURL := "/containers/container_id/kill"
  23. client := &Client{
  24. client: newMockClient(func(req *http.Request) (*http.Response, error) {
  25. if !strings.HasPrefix(req.URL.Path, expectedURL) {
  26. return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
  27. }
  28. signal := req.URL.Query().Get("signal")
  29. if signal != "SIGKILL" {
  30. return nil, fmt.Errorf("signal not set in URL query properly. Expected 'SIGKILL', got %s", signal)
  31. }
  32. return &http.Response{
  33. StatusCode: http.StatusOK,
  34. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  35. }, nil
  36. }),
  37. }
  38. err := client.ContainerKill(context.Background(), "container_id", "SIGKILL")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. }