docker_api_exec_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "os/exec"
  9. "github.com/go-check/check"
  10. )
  11. // Regression test for #9414
  12. func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) {
  13. name := "exec_test"
  14. runCmd := exec.Command(dockerBinary, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  15. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  16. c.Fatal(out, err)
  17. }
  18. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
  19. c.Assert(status, check.Equals, http.StatusInternalServerError)
  20. c.Assert(err, check.IsNil)
  21. if !bytes.Contains(body, []byte("No exec command specified")) {
  22. c.Fatalf("Expected message when creating exec command with no Cmd specified")
  23. }
  24. }
  25. func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) {
  26. name := "exec_test"
  27. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  28. jsonData := bytes.NewBuffer(nil)
  29. if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil {
  30. c.Fatalf("Can not encode data to json %s", err)
  31. }
  32. res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain")
  33. c.Assert(err, check.IsNil)
  34. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  35. b, err := readBody(body)
  36. c.Assert(err, check.IsNil)
  37. if !bytes.Contains(b, []byte("Content-Type specified")) {
  38. c.Fatalf("Expected message when creating exec command with invalid Content-Type specified")
  39. }
  40. }