docker_cli_history_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. )
  9. // This is a heisen-test. Because the created timestamp of images and the behavior of
  10. // sort is not predictable it doesn't always fail.
  11. func TestBuildHistory(t *testing.T) {
  12. buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildHistory")
  13. buildCmd := exec.Command(dockerBinary, "build", "-t", "testbuildhistory", ".")
  14. buildCmd.Dir = buildDirectory
  15. out, exitCode, err := runCommandWithOutput(buildCmd)
  16. errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))
  17. if err != nil || exitCode != 0 {
  18. t.Fatal("failed to build the image")
  19. }
  20. out, exitCode, err = runCommandWithOutput(exec.Command(dockerBinary, "history", "testbuildhistory"))
  21. errorOut(err, t, fmt.Sprintf("image history failed: %v %v", out, err))
  22. if err != nil || exitCode != 0 {
  23. t.Fatal("failed to get image history")
  24. }
  25. actual_values := strings.Split(out, "\n")[1:27]
  26. expected_values := [26]string{"Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A"}
  27. for i := 0; i < 26; i++ {
  28. echo_value := fmt.Sprintf("echo \"%s\"", expected_values[i])
  29. actual_value := actual_values[i]
  30. if !strings.Contains(actual_value, echo_value) {
  31. t.Fatalf("Expected layer \"%s\", but was: %s", expected_values[i], actual_value)
  32. }
  33. }
  34. deleteImages("testbuildhistory")
  35. logDone("history - build history")
  36. }
  37. func TestHistoryExistentImage(t *testing.T) {
  38. historyCmd := exec.Command(dockerBinary, "history", "busybox")
  39. _, exitCode, err := runCommandWithOutput(historyCmd)
  40. if err != nil || exitCode != 0 {
  41. t.Fatal("failed to get image history")
  42. }
  43. logDone("history - history on existent image must not fail")
  44. }
  45. func TestHistoryNonExistentImage(t *testing.T) {
  46. historyCmd := exec.Command(dockerBinary, "history", "testHistoryNonExistentImage")
  47. _, exitCode, err := runCommandWithOutput(historyCmd)
  48. if err == nil || exitCode == 0 {
  49. t.Fatal("history on a non-existent image didn't result in a non-zero exit status")
  50. }
  51. logDone("history - history on non-existent image must fail")
  52. }