container_stop_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. "time"
  11. "github.com/docker/docker/errdefs"
  12. )
  13. func TestContainerStopError(t *testing.T) {
  14. client := &Client{
  15. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  16. }
  17. timeout := 0 * time.Second
  18. err := client.ContainerStop(context.Background(), "nothing", &timeout)
  19. if !errdefs.IsSystem(err) {
  20. t.Fatalf("expected a Server Error, got %[1]T: %[1]v", err)
  21. }
  22. }
  23. func TestContainerStop(t *testing.T) {
  24. expectedURL := "/containers/container_id/stop"
  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. t := req.URL.Query().Get("t")
  31. if t != "100" {
  32. return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
  33. }
  34. return &http.Response{
  35. StatusCode: http.StatusOK,
  36. Body: io.NopCloser(bytes.NewReader([]byte(""))),
  37. }, nil
  38. }),
  39. }
  40. timeout := 100 * time.Second
  41. err := client.ContainerStop(context.Background(), "container_id", &timeout)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. }