Skip to content

Commit

Permalink
initial version of cli integration tests
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Cristian Staretu <[email protected]> (github: unclejack)
  • Loading branch information
unclejack committed Mar 29, 2014
1 parent c705e4a commit 6db32fd
Show file tree
Hide file tree
Showing 22 changed files with 1,117 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: all binary build cross default docs docs-build docs-shell shell test test-integration
.PHONY: all binary build cross default docs docs-build docs-shell shell test test-integration test-integration-cli

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
DOCKER_IMAGE := docker:$(GIT_BRANCH)
Expand All @@ -23,11 +23,14 @@ docs-shell: docs-build
docker run --rm -i -t -p 8000:8000 "$(DOCKER_DOCS_IMAGE)" bash

test: build
$(DOCKER_RUN_DOCKER) hack/make.sh test test-integration
$(DOCKER_RUN_DOCKER) hack/make.sh binary test test-integration test-integration-cli

test-integration: build
$(DOCKER_RUN_DOCKER) hack/make.sh test-integration

test-integration-cli: build
$(DOCKER_RUN_DOCKER) hack/make.sh binary test-integration-cli

shell: build
$(DOCKER_RUN_DOCKER) bash

Expand Down
1 change: 1 addition & 0 deletions hack/make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ find_dirs() {
\( \
-wholename './vendor' \
-o -wholename './integration' \
-o -wholename './integration-cli' \
-o -wholename './contrib' \
-o -wholename './pkg/mflag/example' \
-o -wholename './.git' \
Expand Down
37 changes: 37 additions & 0 deletions hack/make/test-integration-cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

DEST=$1
DOCKERBIN=$DEST/../binary/docker-$VERSION
DYNDOCKERBIN=$DEST/../dynbinary/docker-$VERSION
DOCKERINITBIN=$DEST/../dynbinary/dockerinit-$VERSION

set -e

bundle_test_integration_cli() {
go_test_dir ./integration-cli
}

if [ -x "/usr/bin/docker" ]; then
echo "docker found at /usr/bin/docker"
elif [ -x "$DOCKERBIN" ]; then
ln -s $DOCKERBIN /usr/bin/docker
elif [ -x "$DYNDOCKERBIN" ]; then
ln -s $DYNDOCKERBIN /usr/bin/docker
ln -s $DOCKERINITBIN /usr/bin/dockerinit
else
echo >&2 'error: binary or dynbinary must be run before test-integration-cli'
false
fi


docker -d -D -p $DEST/docker.pid &> $DEST/docker.log &
sleep 2
docker info
DOCKERD_PID=`cat $DEST/docker.pid`

bundle_test_integration_cli 2>&1 \
| tee $DEST/test.log

kill $DOCKERD_PID
wait $DOCKERD_PID

60 changes: 60 additions & 0 deletions integration-cli/build_tests/TestBuildSixtySteps/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
FROM busybox
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
RUN echo "foo"
28 changes: 28 additions & 0 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"fmt"
"os/exec"
"path/filepath"
"testing"
)

func TestBuildSixtySteps(t *testing.T) {
buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildSixtySteps")
buildCmd := exec.Command(dockerBinary, "build", "-t", "foobuildsixtysteps", ".")
buildCmd.Dir = buildDirectory
out, exitCode, err := runCommandWithOutput(buildCmd)
errorOut(err, t, fmt.Sprintf("build failed to complete: %v %v", out, err))

if err != nil || exitCode != 0 {
t.Fatal("failed to build the image")
}

go deleteImages("foobuildsixtysteps")

logDone("build - build an image with sixty build steps")
}

// TODO: TestCaching

// TODO: TestADDCacheInvalidation
34 changes: 34 additions & 0 deletions integration-cli/docker_cli_commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"os/exec"
"testing"
)

func TestCommitAfterContainerIsDone(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-i", "-a", "stdin", "busybox", "echo", "foo")
out, _, _, err := runCommandWithStdoutStderr(runCmd)
errorOut(err, t, fmt.Sprintf("failed to run container: %v %v", out, err))

cleanedContainerID := stripTrailingCharacters(out)

waitCmd := exec.Command(dockerBinary, "wait", cleanedContainerID)
_, _, err = runCommandWithOutput(waitCmd)
errorOut(err, t, fmt.Sprintf("error thrown while waiting for container: %s", out))

commitCmd := exec.Command(dockerBinary, "commit", cleanedContainerID)
out, _, err = runCommandWithOutput(commitCmd)
errorOut(err, t, fmt.Sprintf("failed to commit container to image: %v %v", out, err))

cleanedImageID := stripTrailingCharacters(out)

inspectCmd := exec.Command(dockerBinary, "inspect", cleanedImageID)
out, _, err = runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("failed to inspect image: %v %v", out, err))

go deleteContainer(cleanedContainerID)
go deleteImages(cleanedImageID)

logDone("commit - echo foo and commit the image")
}
66 changes: 66 additions & 0 deletions integration-cli/docker_cli_diff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"os/exec"
"strings"
"testing"
)

// ensure that an added file shows up in docker diff
func TestDiffFilenameShownInOutput(t *testing.T) {
containerCmd := `echo foo > /root/bar`
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
cid, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("failed to start the container: %v", err))

cleanCID := stripTrailingCharacters(cid)

diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
out, _, err := runCommandWithOutput(diffCmd)
errorOut(err, t, fmt.Sprintf("failed to run diff: %v %v", out, err))

found := false
for _, line := range strings.Split(out, "\n") {
if strings.Contains("A /root/bar", line) {
found = true
break
}
}
if !found {
t.Errorf("couldn't find the new file in docker diff's output: %v", out)
}
go deleteContainer(cleanCID)

logDone("diff - check if created file shows up")
}

// test to ensure GH #3840 doesn't occur any more
func TestDiffEnsureDockerinitFilesAreIgnored(t *testing.T) {
// this is a list of files which shouldn't show up in `docker diff`
dockerinitFiles := []string{"/etc/resolv.conf", "/etc/hostname", "/etc/hosts", "/.dockerinit", "/.dockerenv"}

// we might not run into this problem from the first run, so start a few containers
for i := 0; i < 20; i++ {
containerCmd := `echo foo > /root/bar`
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", containerCmd)
cid, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("%s", err))

cleanCID := stripTrailingCharacters(cid)

diffCmd := exec.Command(dockerBinary, "diff", cleanCID)
out, _, err := runCommandWithOutput(diffCmd)
errorOut(err, t, fmt.Sprintf("failed to run diff: %v %v", out, err))

go deleteContainer(cleanCID)

for _, filename := range dockerinitFiles {
if strings.Contains(out, filename) {
t.Errorf("found file which should've been ignored %v in diff output", filename)
}
}
}

logDone("diff - check if ignored files show up in diff")
}
50 changes: 50 additions & 0 deletions integration-cli/docker_cli_export_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"os"
"os/exec"
"testing"
)

// export an image and try to import it into a new one
func TestExportContainerAndImportImage(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
t.Fatal("failed to create a container", out, err)
}

cleanedContainerID := stripTrailingCharacters(out)

inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
out, _, err = runCommandWithOutput(inspectCmd)
if err != nil {
t.Fatalf("output should've been a container id: %s %s ", cleanedContainerID, err)
}

exportCmdTemplate := `%v export %v > /tmp/testexp.tar`
exportCmdFinal := fmt.Sprintf(exportCmdTemplate, dockerBinary, cleanedContainerID)
exportCmd := exec.Command("bash", "-c", exportCmdFinal)
out, _, err = runCommandWithOutput(exportCmd)
errorOut(err, t, fmt.Sprintf("failed to export container: %v %v", out, err))

importCmdFinal := `cat /tmp/testexp.tar | docker import - testexp`
importCmd := exec.Command("bash", "-c", importCmdFinal)
out, _, err = runCommandWithOutput(importCmd)
errorOut(err, t, fmt.Sprintf("failed to import image: %v %v", out, err))

cleanedImageID := stripTrailingCharacters(out)

inspectCmd = exec.Command(dockerBinary, "inspect", cleanedImageID)
out, _, err = runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("output should've been an image id: %v %v", out, err))

go deleteImages("testexp")
go deleteContainer(cleanedContainerID)

os.Remove("/tmp/testexp.tar")

logDone("export - export a container")
logDone("import - import an image")
}
20 changes: 20 additions & 0 deletions integration-cli/docker_cli_images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"os/exec"
"strings"
"testing"
)

func TestImagesEnsureImageIsListed(t *testing.T) {
imagesCmd := exec.Command(dockerBinary, "images")
out, _, err := runCommandWithOutput(imagesCmd)
errorOut(err, t, fmt.Sprintf("listing images failed with errors: %v", err))

if !strings.Contains(out, "busybox") {
t.Fatal("images should've listed busybox")
}

logDone("images - busybox should be listed")
}
29 changes: 29 additions & 0 deletions integration-cli/docker_cli_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
"os/exec"
"strings"
"testing"
)

// ensure docker info succeeds
func TestInfoEnsureSucceeds(t *testing.T) {
versionCmd := exec.Command(dockerBinary, "info")
out, exitCode, err := runCommandWithOutput(versionCmd)
errorOut(err, t, fmt.Sprintf("encountered error while running docker info: %v", err))

if err != nil || exitCode != 0 {
t.Fatal("failed to execute docker info")
}

stringsToCheck := []string{"Containers:", "Execution Driver:", "Kernel Version:"}

for _, linePrefix := range stringsToCheck {
if !strings.Contains(out, linePrefix) {
t.Errorf("couldn't find string %v in output", linePrefix)
}
}

logDone("info - verify that it works")
}
36 changes: 36 additions & 0 deletions integration-cli/docker_cli_kill_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"os/exec"
"strings"
"testing"
)

func TestKillContainer(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "sleep 10")
out, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("run failed with errors: %v", err))

cleanedContainerID := stripTrailingCharacters(out)

inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
inspectOut, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))

killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
out, _, err = runCommandWithOutput(killCmd)
errorOut(err, t, fmt.Sprintf("failed to kill container: %v %v", out, err))

listRunningContainersCmd := exec.Command(dockerBinary, "ps", "-q")
out, _, err = runCommandWithOutput(listRunningContainersCmd)
errorOut(err, t, fmt.Sprintf("failed to list running containers: %v", err))

if strings.Contains(out, cleanedContainerID) {
t.Fatal("killed container is still running")
}

go deleteContainer(cleanedContainerID)

logDone("kill - kill container running sleep 10")
}
Loading

0 comments on commit 6db32fd

Please sign in to comment.