github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/e2e/image/build_test.go (about) 1 package image 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/docker/cli/e2e/internal/fixtures" 11 "github.com/docker/cli/internal/test/environment" 12 "github.com/docker/cli/internal/test/output" 13 "gotest.tools/assert" 14 is "gotest.tools/assert/cmp" 15 "gotest.tools/fs" 16 "gotest.tools/icmd" 17 "gotest.tools/skip" 18 ) 19 20 func TestBuildFromContextDirectoryWithTag(t *testing.T) { 21 dir := fs.NewDir(t, "test-build-context-dir", 22 fs.WithFile("run", "echo running", fs.WithMode(0755)), 23 fs.WithDir("data", fs.WithFile("one", "1111")), 24 fs.WithFile("Dockerfile", fmt.Sprintf(` 25 FROM %s 26 COPY run /usr/bin/run 27 RUN run 28 COPY data /data 29 `, fixtures.AlpineImage))) 30 defer dir.Remove() 31 32 result := icmd.RunCmd( 33 icmd.Command("docker", "build", "-t", "myimage", "."), 34 withWorkingDir(dir)) 35 defer icmd.RunCommand("docker", "image", "rm", "myimage") 36 37 result.Assert(t, icmd.Expected{Err: icmd.None}) 38 output.Assert(t, result.Stdout(), map[int]func(string) error{ 39 0: output.Prefix("Sending build context to Docker daemon"), 40 1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:3.6"), 41 3: output.Suffix("Step 2/4 : COPY run /usr/bin/run"), 42 5: output.Suffix("Step 3/4 : RUN run"), 43 7: output.Suffix("running"), 44 8: output.Contains("Removing intermediate container"), 45 10: output.Suffix("Step 4/4 : COPY data /data"), 46 12: output.Contains("Successfully built "), 47 13: output.Suffix("Successfully tagged myimage:latest"), 48 }) 49 } 50 51 func TestTrustedBuild(t *testing.T) { 52 skip.If(t, environment.RemoteDaemon()) 53 54 dir := fixtures.SetupConfigFile(t) 55 defer dir.Remove() 56 image1 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build1", "latest") 57 image2 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build2", "latest") 58 59 buildDir := fs.NewDir(t, "test-trusted-build-context-dir", 60 fs.WithFile("Dockerfile", fmt.Sprintf(` 61 FROM %s as build-base 62 RUN echo ok > /foo 63 FROM %s 64 COPY --from=build-base foo bar 65 `, image1, image2))) 66 defer buildDir.Remove() 67 68 result := icmd.RunCmd( 69 icmd.Command("docker", "build", "-t", "myimage", "."), 70 withWorkingDir(buildDir), 71 fixtures.WithConfig(dir.Path()), 72 fixtures.WithTrust, 73 fixtures.WithNotary, 74 ) 75 76 result.Assert(t, icmd.Expected{ 77 Out: fmt.Sprintf("FROM %s@sha", image1[:len(image1)-7]), 78 Err: fmt.Sprintf("Tagging %s@sha", image1[:len(image1)-7]), 79 }) 80 result.Assert(t, icmd.Expected{ 81 Out: fmt.Sprintf("FROM %s@sha", image2[:len(image2)-7]), 82 }) 83 } 84 85 func TestTrustedBuildUntrustedImage(t *testing.T) { 86 skip.If(t, environment.RemoteDaemon()) 87 88 dir := fixtures.SetupConfigFile(t) 89 defer dir.Remove() 90 buildDir := fs.NewDir(t, "test-trusted-build-context-dir", 91 fs.WithFile("Dockerfile", fmt.Sprintf(` 92 FROM %s 93 RUN [] 94 `, fixtures.AlpineImage))) 95 defer buildDir.Remove() 96 97 result := icmd.RunCmd( 98 icmd.Command("docker", "build", "-t", "myimage", "."), 99 withWorkingDir(buildDir), 100 fixtures.WithConfig(dir.Path()), 101 fixtures.WithTrust, 102 fixtures.WithNotary, 103 ) 104 105 result.Assert(t, icmd.Expected{ 106 ExitCode: 1, 107 Err: "does not have trust data for", 108 }) 109 } 110 111 func TestBuildIidFileSquash(t *testing.T) { 112 environment.SkipIfNotExperimentalDaemon(t) 113 dir := fs.NewDir(t, "test-iidfile-squash") 114 defer dir.Remove() 115 iidfile := filepath.Join(dir.Path(), "idsquash") 116 buildDir := fs.NewDir(t, "test-iidfile-squash-build", 117 fs.WithFile("Dockerfile", fmt.Sprintf(` 118 FROM %s 119 ENV FOO FOO 120 ENV BAR BAR 121 RUN touch /fiip 122 RUN touch /foop`, fixtures.AlpineImage)), 123 ) 124 defer buildDir.Remove() 125 126 imageTag := "testbuildiidfilesquash" 127 result := icmd.RunCmd( 128 icmd.Command("docker", "build", "--iidfile", iidfile, "--squash", "-t", imageTag, "."), 129 withWorkingDir(buildDir), 130 ) 131 result.Assert(t, icmd.Success) 132 id, err := ioutil.ReadFile(iidfile) 133 assert.NilError(t, err) 134 result = icmd.RunCommand("docker", "image", "inspect", "-f", "{{.Id}}", imageTag) 135 result.Assert(t, icmd.Success) 136 assert.Check(t, is.Equal(string(id), strings.TrimSpace(result.Combined()))) 137 } 138 139 func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) { 140 return func(cmd *icmd.Cmd) { 141 cmd.Dir = dir.Path() 142 } 143 }