container_restart_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package client
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "time"
  10. "golang.org/x/net/context"
  11. )
  12. func TestContainerRestartError(t *testing.T) {
  13. client := &Client{
  14. client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
  15. }
  16. timeout := 0 * time.Second
  17. err := client.ContainerRestart(context.Background(), "nothing", &timeout)
  18. if err == nil || err.Error() != "Error response from daemon: Server error" {
  19. t.Fatalf("expected a Server Error, got %v", err)
  20. }
  21. }
  22. func TestContainerRestart(t *testing.T) {
  23. expectedURL := "/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. t := req.URL.Query().Get("t")
  30. if t != "100" {
  31. return nil, fmt.Errorf("t (timeout) not set in URL query properly. Expected '100', got %s", t)
  32. }
  33. return &http.Response{
  34. StatusCode: http.StatusOK,
  35. Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
  36. }, nil
  37. }),
  38. }
  39. timeout := 100 * time.Second
  40. err := client.ContainerRestart(context.Background(), "container_id", &timeout)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. }