From 1d4862b7a2d0c3b0641ce6d539eabecee686c794 Mon Sep 17 00:00:00 2001 From: LK4D4 Date: Sun, 6 Jul 2014 15:36:34 +0400 Subject: [PATCH] Moved last build tests to integration-cli Also removed skipped tests on "viz" and "tree" because they blocked integration/buildfile_test.go removing. Docker-DCO-1.1-Signed-off-by: Alexandr Morozov (github: LK4D4) --- integration-cli/docker_cli_build_test.go | 167 +++++++++ integration/buildfile_test.go | 414 ----------------------- integration/commands_test.go | 128 ------- 3 files changed, 167 insertions(+), 542 deletions(-) delete mode 100644 integration/buildfile_test.go diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 2f2f2b6e0c..df0cfcc88e 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -1564,3 +1564,170 @@ func TestDockerignoringDockerfile(t *testing.T) { } logDone("build - test .dockerignore of Dockerfile") } + +func TestBuildLineBreak(t *testing.T) { + name := "testbuildlinebreak" + defer deleteImages(name) + _, err := buildImage(name, + `FROM busybox +RUN sh -c 'echo root:testpass \ + > /tmp/passwd' +RUN mkdir -p /var/run/sshd +RUN [ "$(cat /tmp/passwd)" = "root:testpass" ] +RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`, + true) + if err != nil { + t.Fatal(err) + } + logDone("build - line break with \\") +} + +func TestBuildEOLInLine(t *testing.T) { + name := "testbuildeolinline" + defer deleteImages(name) + _, err := buildImage(name, + `FROM busybox +RUN sh -c 'echo root:testpass > /tmp/passwd' +RUN echo "foo \n bar"; echo "baz" +RUN mkdir -p /var/run/sshd +RUN [ "$(cat /tmp/passwd)" = "root:testpass" ] +RUN [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ]`, + true) + if err != nil { + t.Fatal(err) + } + logDone("build - end of line in dockerfile instruction") +} + +func TestBuildCommentsShebangs(t *testing.T) { + name := "testbuildcomments" + defer deleteImages(name) + _, err := buildImage(name, + `FROM busybox +# This is an ordinary comment. +RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh +RUN [ ! -x /hello.sh ] +# comment with line break \ +RUN chmod +x /hello.sh +RUN [ -x /hello.sh ] +RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ] +RUN [ "$(/hello.sh)" = "hello world" ]`, + true) + if err != nil { + t.Fatal(err) + } + logDone("build - comments and shebangs") +} + +func TestBuildUsersAndGroups(t *testing.T) { + name := "testbuildusers" + defer deleteImages(name) + _, err := buildImage(name, + `FROM busybox + +# Make sure our defaults work +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ] + +# TODO decide if "args.user = strconv.Itoa(syscall.Getuid())" is acceptable behavior for changeUser in sysvinit instead of "return nil" when "USER" isn't specified (so that we get the proper group list even if that is the empty list, even in the default case of not supplying an explicit USER to run as, which implies USER 0) +USER root +RUN [ "$(id -G):$(id -Gn)" = '0 10:root wheel' ] + +# Setup dockerio user and group +RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd +RUN echo 'dockerio:x:1001:' >> /etc/group + +# Make sure we can switch to our user and all the information is exactly as we expect it to be +USER dockerio +RUN id -G +RUN id -Gn +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ] + +# Switch back to root and double check that worked exactly as we might expect it to +USER root +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '0:0/root:root/0 10:root wheel' ] + +# Add a "supplementary" group for our dockerio user +RUN echo 'supplementary:x:1002:dockerio' >> /etc/group + +# ... and then go verify that we get it like we expect +USER dockerio +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ] +USER 1001 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001 1002:dockerio supplementary' ] + +# super test the new "user:group" syntax +USER dockerio:dockerio +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ] +USER 1001:dockerio +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ] +USER dockerio:1001 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ] +USER 1001:1001 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1001/dockerio:dockerio/1001:dockerio' ] +USER dockerio:supplementary +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ] +USER dockerio:1002 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ] +USER 1001:supplementary +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ] +USER 1001:1002 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1001:1002/dockerio:supplementary/1002:supplementary' ] + +# make sure unknown uid/gid still works properly +USER 1042:1043 +RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ]`, + true) + if err != nil { + t.Fatal(err) + } + logDone("build - users and groups") +} + +func TestBuildEnvUsage(t *testing.T) { + name := "testbuildenvusage" + defer deleteImages(name) + dockerfile := `FROM busybox +ENV FOO /foo/baz +ENV BAR /bar +ENV BAZ $BAR +ENV FOOPATH $PATH:$FOO +RUN [ "$BAR" = "$BAZ" ] +RUN [ "$FOOPATH" = "$PATH:/foo/baz" ] +ENV FROM hello/docker/world +ENV TO /docker/world/hello +ADD $FROM $TO +RUN [ "$(cat $TO)" = "hello" ]` + ctx, err := fakeContext(dockerfile, map[string]string{ + "hello/docker/world": "hello", + }) + if err != nil { + t.Fatal(err) + } + _, err = buildImageFromContext(name, ctx, true) + if err != nil { + t.Fatal(err) + } + logDone("build - environment variables usage") +} + +func TestBuildAddScript(t *testing.T) { + name := "testbuildaddscript" + defer deleteImages(name) + dockerfile := ` +FROM busybox +ADD test /test +RUN ["chmod","+x","/test"] +RUN ["/test"] +RUN [ "$(cat /testfile)" = 'test!' ]` + ctx, err := fakeContext(dockerfile, map[string]string{ + "test": "#!/bin/sh\necho 'test!' > /testfile", + }) + if err != nil { + t.Fatal(err) + } + _, err = buildImageFromContext(name, ctx, true) + if err != nil { + t.Fatal(err) + } + logDone("build - add and run script") +} diff --git a/integration/buildfile_test.go b/integration/buildfile_test.go deleted file mode 100644 index 147ae353a2..0000000000 --- a/integration/buildfile_test.go +++ /dev/null @@ -1,414 +0,0 @@ -package docker - -import ( - "bytes" - "encoding/json" - "fmt" - "io/ioutil" - "net" - "net/http" - "net/http/httptest" - "strings" - "testing" - - "github.com/dotcloud/docker/archive" - "github.com/dotcloud/docker/engine" - "github.com/dotcloud/docker/image" - "github.com/dotcloud/docker/server" - "github.com/dotcloud/docker/utils" -) - -// A testContextTemplate describes a build context and how to test it -type testContextTemplate struct { - // Contents of the Dockerfile - dockerfile string - // Additional files in the context, eg [][2]string{"./passwd", "gordon"} - files [][2]string - // Additional remote files to host on a local HTTP server. - remoteFiles [][2]string -} - -func (context testContextTemplate) Archive(dockerfile string, t *testing.T) archive.Archive { - input := []string{"Dockerfile", dockerfile} - for _, pair := range context.files { - input = append(input, pair[0], pair[1]) - } - a, err := archive.Generate(input...) - if err != nil { - t.Fatal(err) - } - return a -} - -// A table of all the contexts to build and test. -// A new docker runtime will be created and torn down for each context. -var testContexts = []testContextTemplate{ - { - ` -from {IMAGE} -run sh -c 'echo root:testpass > /tmp/passwd' -run mkdir -p /var/run/sshd -run [ "$(cat /tmp/passwd)" = "root:testpass" ] -run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ] -`, - nil, - nil, - }, - - // Exactly the same as above, except uses a line split with a \ to test - // multiline support. - { - ` -from {IMAGE} -run sh -c 'echo root:testpass \ - > /tmp/passwd' -run mkdir -p /var/run/sshd -run [ "$(cat /tmp/passwd)" = "root:testpass" ] -run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ] -`, - nil, - nil, - }, - - // Line containing literal "\n" - { - ` -from {IMAGE} -run sh -c 'echo root:testpass > /tmp/passwd' -run echo "foo \n bar"; echo "baz" -run mkdir -p /var/run/sshd -run [ "$(cat /tmp/passwd)" = "root:testpass" ] -run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ] -`, - nil, - nil, - }, - { - ` -from {IMAGE} -add foo /usr/lib/bla/bar -run [ "$(cat /usr/lib/bla/bar)" = 'hello' ] -add http://{SERVERADDR}/baz /usr/lib/baz/quux -run [ "$(cat /usr/lib/baz/quux)" = 'world!' ] -`, - [][2]string{{"foo", "hello"}}, - [][2]string{{"/baz", "world!"}}, - }, - - { - ` -from {IMAGE} -add f / -run [ "$(cat /f)" = "hello" ] -add f /abc -run [ "$(cat /abc)" = "hello" ] -add f /x/y/z -run [ "$(cat /x/y/z)" = "hello" ] -add f /x/y/d/ -run [ "$(cat /x/y/d/f)" = "hello" ] -add d / -run [ "$(cat /ga)" = "bu" ] -add d /somewhere -run [ "$(cat /somewhere/ga)" = "bu" ] -add d /anotherplace/ -run [ "$(cat /anotherplace/ga)" = "bu" ] -add d /somewheeeere/over/the/rainbooow -run [ "$(cat /somewheeeere/over/the/rainbooow/ga)" = "bu" ] -`, - [][2]string{ - {"f", "hello"}, - {"d/ga", "bu"}, - }, - nil, - }, - - { - ` -from {IMAGE} -add http://{SERVERADDR}/x /a/b/c -run [ "$(cat /a/b/c)" = "hello" ] -add http://{SERVERADDR}/x?foo=bar / -run [ "$(cat /x)" = "hello" ] -add http://{SERVERADDR}/x /d/ -run [ "$(cat /d/x)" = "hello" ] -add http://{SERVERADDR} /e -run [ "$(cat /e)" = "blah" ] -`, - nil, - [][2]string{{"/x", "hello"}, {"/", "blah"}}, - }, - - // Comments, shebangs, and executability, oh my! - { - ` -FROM {IMAGE} -# This is an ordinary comment. -RUN { echo '#!/bin/sh'; echo 'echo hello world'; } > /hello.sh -RUN [ ! -x /hello.sh ] -RUN chmod +x /hello.sh -RUN [ -x /hello.sh ] -RUN [ "$(cat /hello.sh)" = $'#!/bin/sh\necho hello world' ] -RUN [ "$(/hello.sh)" = "hello world" ] -`, - nil, - nil, - }, - - // Users and groups - { - ` -FROM {IMAGE} - -# Make sure our defaults work -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)" = '0:0/root:root' ] - -# TODO decide if "args.user = strconv.Itoa(syscall.Getuid())" is acceptable behavior for changeUser in sysvinit instead of "return nil" when "USER" isn't specified (so that we get the proper group list even if that is the empty list, even in the default case of not supplying an explicit USER to run as, which implies USER 0) -USER root -RUN [ "$(id -G):$(id -Gn)" = '0:root' ] - -# Setup dockerio user and group -RUN echo 'dockerio:x:1000:1000::/bin:/bin/false' >> /etc/passwd -RUN echo 'dockerio:x:1000:' >> /etc/group - -# Make sure we can switch to our user and all the information is exactly as we expect it to be -USER dockerio -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000:dockerio' ] - -# Switch back to root and double check that worked exactly as we might expect it to -USER root -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '0:0/root:root/0:root' ] - -# Add a "supplementary" group for our dockerio user -RUN echo 'supplementary:x:1001:dockerio' >> /etc/group - -# ... and then go verify that we get it like we expect -USER dockerio -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000 1001:dockerio supplementary' ] -USER 1000 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000 1001:dockerio supplementary' ] - -# super test the new "user:group" syntax -USER dockerio:dockerio -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000:dockerio' ] -USER 1000:dockerio -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000:dockerio' ] -USER dockerio:1000 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000:dockerio' ] -USER 1000:1000 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1000/dockerio:dockerio/1000:dockerio' ] -USER dockerio:supplementary -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1001/dockerio:supplementary/1001:supplementary' ] -USER dockerio:1001 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1001/dockerio:supplementary/1001:supplementary' ] -USER 1000:supplementary -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1001/dockerio:supplementary/1001:supplementary' ] -USER 1000:1001 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1000:1001/dockerio:supplementary/1001:supplementary' ] - -# make sure unknown uid/gid still works properly -USER 1042:1043 -RUN [ "$(id -u):$(id -g)/$(id -un):$(id -gn)/$(id -G):$(id -Gn)" = '1042:1043/1042:1043/1043:1043' ] -`, - nil, - nil, - }, - - // Environment variable - { - ` -from {IMAGE} -env FOO BAR -run [ "$FOO" = "BAR" ] -`, - nil, - nil, - }, - - // Environment overwriting - { - ` -from {IMAGE} -env FOO BAR -run [ "$FOO" = "BAR" ] -env FOO BAZ -run [ "$FOO" = "BAZ" ] -`, - nil, - nil, - }, - - { - ` -from {IMAGE} -ENTRYPOINT /bin/echo -CMD Hello world -`, - nil, - nil, - }, - - { - ` -from {IMAGE} -VOLUME /test -CMD Hello world -`, - nil, - nil, - }, - - { - ` -from {IMAGE} -env FOO /foo/baz -env BAR /bar -env BAZ $BAR -env FOOPATH $PATH:$FOO -run [ "$BAR" = "$BAZ" ] -run [ "$FOOPATH" = "$PATH:/foo/baz" ] -`, - nil, - nil, - }, - - { - ` -from {IMAGE} -env FOO /bar -env TEST testdir -env BAZ /foobar -add testfile $BAZ/ -add $TEST $FOO -run [ "$(cat /foobar/testfile)" = "test1" ] -run [ "$(cat /bar/withfile)" = "test2" ] -`, - [][2]string{ - {"testfile", "test1"}, - {"testdir/withfile", "test2"}, - }, - nil, - }, - - // JSON! - { - ` -FROM {IMAGE} -RUN ["/bin/echo","hello","world"] -CMD ["/bin/true"] -ENTRYPOINT ["/bin/echo","your command -->"] -`, - nil, - nil, - }, - { - ` -FROM {IMAGE} -ADD test /test -RUN ["chmod","+x","/test"] -RUN ["/test"] -RUN [ "$(cat /testfile)" = 'test!' ] -`, - [][2]string{ - {"test", "#!/bin/sh\necho 'test!' > /testfile"}, - }, - nil, - }, - { - ` -FROM {IMAGE} -# what \ -RUN mkdir /testing -RUN touch /testing/other -`, - nil, - nil, - }, -} - -// FIXME: test building with 2 successive overlapping ADD commands - -func constructDockerfile(template string, ip net.IP, port string) string { - serverAddr := fmt.Sprintf("%s:%s", ip, port) - replacer := strings.NewReplacer("{IMAGE}", unitTestImageID, "{SERVERADDR}", serverAddr) - return replacer.Replace(template) -} - -func mkTestingFileServer(files [][2]string) (*httptest.Server, error) { - mux := http.NewServeMux() - for _, file := range files { - name, contents := file[0], file[1] - mux.HandleFunc(name, func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(contents)) - }) - } - - // This is how httptest.NewServer sets up a net.Listener, except that our listener must accept remote - // connections (from the container). - listener, err := net.Listen("tcp", ":0") - if err != nil { - return nil, err - } - - s := httptest.NewUnstartedServer(mux) - s.Listener = listener - s.Start() - return s, nil -} - -func TestBuild(t *testing.T) { - for _, ctx := range testContexts { - _, err := buildImage(ctx, t, nil, true) - if err != nil { - t.Fatal(err) - } - } -} - -func buildImage(context testContextTemplate, t *testing.T, eng *engine.Engine, useCache bool) (*image.Image, error) { - if eng == nil { - eng = NewTestEngine(t) - runtime := mkDaemonFromEngine(eng, t) - // FIXME: we might not need runtime, why not simply nuke - // the engine? - defer nuke(runtime) - } - srv := mkServerFromEngine(eng, t) - - httpServer, err := mkTestingFileServer(context.remoteFiles) - if err != nil { - t.Fatal(err) - } - defer httpServer.Close() - - idx := strings.LastIndex(httpServer.URL, ":") - if idx < 0 { - t.Fatalf("could not get port from test http server address %s", httpServer.URL) - } - port := httpServer.URL[idx+1:] - - iIP := eng.Hack_GetGlobalVar("httpapi.bridgeIP") - if iIP == nil { - t.Fatal("Legacy bridgeIP field not set in engine") - } - ip, ok := iIP.(net.IP) - if !ok { - panic("Legacy bridgeIP field in engine does not cast to net.IP") - } - dockerfile := constructDockerfile(context.dockerfile, ip, port) - - buildfile := server.NewBuildFile(srv, ioutil.Discard, ioutil.Discard, false, useCache, false, false, ioutil.Discard, utils.NewStreamFormatter(false), nil, nil) - id, err := buildfile.Build(context.Archive(dockerfile, t)) - if err != nil { - return nil, err - } - - job := eng.Job("image_inspect", id) - buffer := bytes.NewBuffer(nil) - image := &image.Image{} - job.Stdout.Add(buffer) - if err := job.Run(); err != nil { - return nil, err - } - err = json.NewDecoder(buffer).Decode(image) - return image, err -} diff --git a/integration/commands_test.go b/integration/commands_test.go index 47e9860052..28210e7536 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "os" "path" - "regexp" "strings" "testing" "time" @@ -15,7 +14,6 @@ import ( "github.com/dotcloud/docker/api/client" "github.com/dotcloud/docker/daemon" "github.com/dotcloud/docker/engine" - "github.com/dotcloud/docker/image" "github.com/dotcloud/docker/pkg/term" "github.com/dotcloud/docker/utils" ) @@ -604,132 +602,6 @@ func TestRunErrorBindNonExistingSource(t *testing.T) { }) } -func TestImagesViz(t *testing.T) { - t.Skip("Image viz is deprecated") - stdout, stdoutPipe := io.Pipe() - - cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil) - defer cleanup(globalEngine, t) - - image := buildTestImages(t, globalEngine) - - c := make(chan struct{}) - go func() { - defer close(c) - if err := cli.CmdImages("--viz"); err != nil { - t.Fatal(err) - } - stdoutPipe.Close() - }() - - setTimeout(t, "Reading command output time out", 2*time.Second, func() { - cmdOutputBytes, err := ioutil.ReadAll(bufio.NewReader(stdout)) - if err != nil { - t.Fatal(err) - } - cmdOutput := string(cmdOutputBytes) - - regexpStrings := []string{ - "digraph docker {", - fmt.Sprintf("base -> \"%s\" \\[style=invis]", unitTestImageIDShort), - fmt.Sprintf("label=\"%s\\\\n%s:latest\"", unitTestImageIDShort, unitTestImageName), - fmt.Sprintf("label=\"%s\\\\n%s:%s\"", utils.TruncateID(image.ID), "test", "latest"), - "base \\[style=invisible]", - } - - compiledRegexps := []*regexp.Regexp{} - for _, regexpString := range regexpStrings { - regexp, err := regexp.Compile(regexpString) - if err != nil { - fmt.Println("Error in regex string: ", err) - return - } - compiledRegexps = append(compiledRegexps, regexp) - } - - for _, regexp := range compiledRegexps { - if !regexp.MatchString(cmdOutput) { - t.Fatalf("images --viz content '%s' did not match regexp '%s'", cmdOutput, regexp) - } - } - }) -} - -func TestImagesTree(t *testing.T) { - t.Skip("Image tree is deprecated") - stdout, stdoutPipe := io.Pipe() - - cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil) - defer cleanup(globalEngine, t) - - image := buildTestImages(t, globalEngine) - - c := make(chan struct{}) - go func() { - defer close(c) - if err := cli.CmdImages("--tree"); err != nil { - t.Fatal(err) - } - stdoutPipe.Close() - }() - - setTimeout(t, "Reading command output time out", 2*time.Second, func() { - cmdOutputBytes, err := ioutil.ReadAll(bufio.NewReader(stdout)) - if err != nil { - t.Fatal(err) - } - cmdOutput := string(cmdOutputBytes) - regexpStrings := []string{ - fmt.Sprintf("└─%s Virtual Size: \\d+.\\d+ MB Tags: %s:latest", unitTestImageIDShort, unitTestImageName), - "(?m) └─[0-9a-f]+.*", - "(?m) └─[0-9a-f]+.*", - "(?m) └─[0-9a-f]+.*", - fmt.Sprintf("(?m)^ └─%s Virtual Size: \\d+.\\d+ MB Tags: test:latest", utils.TruncateID(image.ID)), - } - - compiledRegexps := []*regexp.Regexp{} - for _, regexpString := range regexpStrings { - regexp, err := regexp.Compile(regexpString) - if err != nil { - fmt.Println("Error in regex string: ", err) - return - } - compiledRegexps = append(compiledRegexps, regexp) - } - - for _, regexp := range compiledRegexps { - if !regexp.MatchString(cmdOutput) { - t.Fatalf("images --tree content '%s' did not match regexp '%s'", cmdOutput, regexp) - } - } - }) -} - -func buildTestImages(t *testing.T, eng *engine.Engine) *image.Image { - - var testBuilder = testContextTemplate{ - ` -from {IMAGE} -run sh -c 'echo root:testpass > /tmp/passwd' -run mkdir -p /var/run/sshd -run [ "$(cat /tmp/passwd)" = "root:testpass" ] -run [ "$(ls -d /var/run/sshd)" = "/var/run/sshd" ] -`, - nil, - nil, - } - image, err := buildImage(testBuilder, t, eng, true) - if err != nil { - t.Fatal(err) - } - - if err := eng.Job("tag", image.ID, "test").Run(); err != nil { - t.Fatal(err) - } - - return image -} - // #2098 - Docker cidFiles only contain short version of the containerId //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test" // TestRunCidFile tests that run --cidfile returns the longid