docker_api_resize_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "net/http"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. )
  8. func TestResizeApiResponse(t *testing.T) {
  9. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  10. out, _, err := runCommandWithOutput(runCmd)
  11. if err != nil {
  12. t.Fatalf(out, err)
  13. }
  14. defer deleteAllContainers()
  15. cleanedContainerID := strings.TrimSpace(out)
  16. endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
  17. _, _, err = sockRequest("POST", endpoint, nil)
  18. if err != nil {
  19. t.Fatalf("resize Request failed %v", err)
  20. }
  21. logDone("container resize - when started")
  22. }
  23. func TestResizeApiHeightWidthNoInt(t *testing.T) {
  24. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  25. out, _, err := runCommandWithOutput(runCmd)
  26. if err != nil {
  27. t.Fatalf(out, err)
  28. }
  29. defer deleteAllContainers()
  30. cleanedContainerID := strings.TrimSpace(out)
  31. endpoint := "/containers/" + cleanedContainerID + "/resize?h=foo&w=bar"
  32. status, _, err := sockRequest("POST", endpoint, nil)
  33. if err == nil {
  34. t.Fatal("Expected resize Request to fail")
  35. }
  36. if status != http.StatusInternalServerError {
  37. t.Fatalf("Status expected %d, got %d", http.StatusInternalServerError, status)
  38. }
  39. logDone("container resize - height, width no int fail")
  40. }
  41. func TestResizeApiResponseWhenContainerNotStarted(t *testing.T) {
  42. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
  43. out, _, err := runCommandWithOutput(runCmd)
  44. if err != nil {
  45. t.Fatalf(out, err)
  46. }
  47. defer deleteAllContainers()
  48. cleanedContainerID := strings.TrimSpace(out)
  49. // make sure the exited container is not running
  50. runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
  51. out, _, err = runCommandWithOutput(runCmd)
  52. if err != nil {
  53. t.Fatalf(out, err)
  54. }
  55. endpoint := "/containers/" + cleanedContainerID + "/resize?h=40&w=40"
  56. _, body, err := sockRequest("POST", endpoint, nil)
  57. if err == nil {
  58. t.Fatalf("resize should fail when container is not started")
  59. }
  60. if !strings.Contains(string(body), "Cannot resize container") && !strings.Contains(string(body), cleanedContainerID) {
  61. t.Fatalf("resize should fail with message 'Cannot resize container' but instead received %s", string(body))
  62. }
  63. logDone("container resize - when not started should not resize")
  64. }