docker_cli_inspect_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "strconv"
  6. "strings"
  7. "github.com/go-check/check"
  8. )
  9. func (s *DockerSuite) TestInspectImage(c *check.C) {
  10. imageTest := "emptyfs"
  11. imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"
  12. imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Id}}'", imageTest)
  13. out, exitCode, err := runCommandWithOutput(imagesCmd)
  14. if exitCode != 0 || err != nil {
  15. c.Fatalf("failed to inspect image: %s, %v", out, err)
  16. }
  17. if id := strings.TrimSuffix(out, "\n"); id != imageTestID {
  18. c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id)
  19. }
  20. }
  21. func (s *DockerSuite) TestInspectInt64(c *check.C) {
  22. runCmd := exec.Command(dockerBinary, "run", "-d", "-m=300M", "busybox", "true")
  23. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  24. if err != nil {
  25. c.Fatalf("failed to run container: %v, output: %q", err, out)
  26. }
  27. out = strings.TrimSpace(out)
  28. inspectCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.Memory}}", out)
  29. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  30. if err != nil {
  31. c.Fatalf("failed to inspect container: %v, output: %q", err, inspectOut)
  32. }
  33. if strings.TrimSpace(inspectOut) != "314572800" {
  34. c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut)
  35. }
  36. }
  37. func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) {
  38. imageTest := "emptyfs"
  39. imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Size}}'", imageTest)
  40. out, exitCode, err := runCommandWithOutput(imagesCmd)
  41. if exitCode != 0 || err != nil {
  42. c.Fatalf("failed to inspect image: %s, %v", out, err)
  43. }
  44. size, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
  45. if err != nil {
  46. c.Fatalf("failed to inspect size of the image: %s, %v", out, err)
  47. }
  48. //now see if the size turns out to be the same
  49. formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
  50. imagesCmd = exec.Command(dockerBinary, "inspect", formatStr, imageTest)
  51. out, exitCode, err = runCommandWithOutput(imagesCmd)
  52. if exitCode != 0 || err != nil {
  53. c.Fatalf("failed to inspect image: %s, %v", out, err)
  54. }
  55. if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
  56. c.Fatalf("Expected size: %d for image: %s but received size: %s", size, imageTest, strings.TrimSuffix(out, "\n"))
  57. }
  58. }
  59. func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) {
  60. runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
  61. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  62. if err != nil {
  63. c.Fatalf("failed to run container: %v, output: %q", err, out)
  64. }
  65. id := strings.TrimSpace(out)
  66. runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.ExitCode}}'", id)
  67. out, _, err = runCommandWithOutput(runCmd)
  68. if err != nil {
  69. c.Fatalf("failed to inspect container: %s, %v", out, err)
  70. }
  71. exitCode, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
  72. if err != nil {
  73. c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err)
  74. }
  75. //now get the exit code to verify
  76. formatStr := fmt.Sprintf("--format='{{eq .State.ExitCode %d}}'", exitCode)
  77. runCmd = exec.Command(dockerBinary, "inspect", formatStr, id)
  78. out, _, err = runCommandWithOutput(runCmd)
  79. if err != nil {
  80. c.Fatalf("failed to inspect container: %s, %v", out, err)
  81. }
  82. if result, err := strconv.ParseBool(strings.TrimSuffix(out, "\n")); err != nil || !result {
  83. c.Fatalf("Expected exitcode: %d for container: %s", exitCode, id)
  84. }
  85. }