github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/cli/e2e/image/build_test.go (about) 1 package image 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 8 "github.com/docker/cli/e2e/internal/fixtures" 9 "github.com/gotestyourself/gotestyourself/fs" 10 "github.com/gotestyourself/gotestyourself/icmd" 11 "github.com/pkg/errors" 12 ) 13 14 func TestBuildFromContextDirectoryWithTag(t *testing.T) { 15 dir := fs.NewDir(t, "test-build-context-dir", 16 fs.WithFile("run", "echo running", fs.WithMode(0755)), 17 fs.WithDir("data", fs.WithFile("one", "1111")), 18 fs.WithFile("Dockerfile", fmt.Sprintf(` 19 FROM %s 20 COPY run /usr/bin/run 21 RUN run 22 COPY data /data 23 `, fixtures.AlpineImage))) 24 defer dir.Remove() 25 26 result := icmd.RunCmd( 27 icmd.Command("docker", "build", "-t", "myimage", "."), 28 withWorkingDir(dir)) 29 30 result.Assert(t, icmd.Expected{Err: icmd.None}) 31 assertBuildOutput(t, result.Stdout(), map[int]lineCompare{ 32 0: prefix("Sending build context to Docker daemon"), 33 1: equals("Step 1/4 : FROM\tregistry:5000/alpine:3.6"), 34 3: equals("Step 2/4 : COPY\trun /usr/bin/run"), 35 5: equals("Step 3/4 : RUN\t\trun"), 36 7: equals("running"), 37 8: prefix("Removing intermediate container "), 38 10: equals("Step 4/4 : COPY\tdata /data"), 39 12: prefix("Successfully built "), 40 13: equals("Successfully tagged myimage:latest"), 41 }) 42 } 43 44 func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) { 45 return func(cmd *icmd.Cmd) { 46 cmd.Dir = dir.Path() 47 } 48 } 49 50 func assertBuildOutput(t *testing.T, actual string, expectedLines map[int]lineCompare) { 51 for i, line := range strings.Split(actual, "\n") { 52 cmp, ok := expectedLines[i] 53 if !ok { 54 continue 55 } 56 if err := cmp(line); err != nil { 57 t.Errorf("line %d: %s", i, err) 58 } 59 } 60 if t.Failed() { 61 t.Log(actual) 62 } 63 } 64 65 type lineCompare func(string) error 66 67 func prefix(expected string) func(string) error { 68 return func(actual string) error { 69 if strings.HasPrefix(actual, expected) { 70 return nil 71 } 72 return errors.Errorf("expected %s to start with %s", actual, expected) 73 } 74 } 75 76 func equals(expected string) func(string) error { 77 return func(actual string) error { 78 if expected == actual { 79 return nil 80 } 81 return errors.Errorf("got %s, expected %s", actual, expected) 82 } 83 }