github.com/jmtd/docker@v1.5.0/integration-cli/docker_cli_history_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  // This is a heisen-test.  Because the created timestamp of images and the behavior of
    11  // sort is not predictable it doesn't always fail.
    12  func TestBuildHistory(t *testing.T) {
    13  	name := "testbuildhistory"
    14  	defer deleteImages(name)
    15  	_, err := buildImage(name, `FROM busybox
    16  RUN echo "A"
    17  RUN echo "B"
    18  RUN echo "C"
    19  RUN echo "D"
    20  RUN echo "E"
    21  RUN echo "F"
    22  RUN echo "G"
    23  RUN echo "H"
    24  RUN echo "I"
    25  RUN echo "J"
    26  RUN echo "K"
    27  RUN echo "L"
    28  RUN echo "M"
    29  RUN echo "N"
    30  RUN echo "O"
    31  RUN echo "P"
    32  RUN echo "Q"
    33  RUN echo "R"
    34  RUN echo "S"
    35  RUN echo "T"
    36  RUN echo "U"
    37  RUN echo "V"
    38  RUN echo "W"
    39  RUN echo "X"
    40  RUN echo "Y"
    41  RUN echo "Z"`,
    42  		true)
    43  
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "history", "testbuildhistory"))
    49  	if err != nil || exitCode != 0 {
    50  		t.Fatalf("failed to get image history: %s, %v", out, err)
    51  	}
    52  
    53  	actualValues := strings.Split(out, "\n")[1:27]
    54  	expectedValues := [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"}
    55  
    56  	for i := 0; i < 26; i++ {
    57  		echoValue := fmt.Sprintf("echo \"%s\"", expectedValues[i])
    58  		actualValue := actualValues[i]
    59  
    60  		if !strings.Contains(actualValue, echoValue) {
    61  			t.Fatalf("Expected layer \"%s\", but was: %s", expectedValues[i], actualValue)
    62  		}
    63  	}
    64  
    65  	logDone("history - build history")
    66  }
    67  
    68  func TestHistoryExistentImage(t *testing.T) {
    69  	historyCmd := exec.Command(dockerBinary, "history", "busybox")
    70  	_, exitCode, err := runCommandWithOutput(historyCmd)
    71  	if err != nil || exitCode != 0 {
    72  		t.Fatal("failed to get image history")
    73  	}
    74  	logDone("history - history on existent image must not fail")
    75  }
    76  
    77  func TestHistoryNonExistentImage(t *testing.T) {
    78  	historyCmd := exec.Command(dockerBinary, "history", "testHistoryNonExistentImage")
    79  	_, exitCode, err := runCommandWithOutput(historyCmd)
    80  	if err == nil || exitCode == 0 {
    81  		t.Fatal("history on a non-existent image didn't result in a non-zero exit status")
    82  	}
    83  	logDone("history - history on non-existent image must fail")
    84  }