github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/e2e/image/build_test.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/khulnasoft/cli/e2e/internal/fixtures"
    11  	"github.com/khulnasoft/cli/internal/test/environment"
    12  	"github.com/khulnasoft/cli/internal/test/output"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  	"gotest.tools/v3/fs"
    16  	"gotest.tools/v3/icmd"
    17  	"gotest.tools/v3/skip"
    18  )
    19  
    20  func TestBuildFromContextDirectoryWithTag(t *testing.T) {
    21  	t.Setenv("DOCKER_BUILDKIT", "0")
    22  
    23  	dir := fs.NewDir(t, "test-build-context-dir",
    24  		fs.WithFile("run", "echo running", fs.WithMode(0o755)),
    25  		fs.WithDir("data", fs.WithFile("one", "1111")),
    26  		fs.WithFile("Dockerfile", fmt.Sprintf(`
    27  	FROM %s
    28  	COPY run /usr/bin/run
    29  	RUN run
    30  	COPY data /data
    31  		`, fixtures.AlpineImage)))
    32  	defer dir.Remove()
    33  
    34  	result := icmd.RunCmd(
    35  		icmd.Command("docker", "build", "-t", "myimage", "."),
    36  		withWorkingDir(dir))
    37  	defer icmd.RunCommand("docker", "image", "rm", "myimage")
    38  
    39  	const buildkitDisabledWarning = `DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
    40              BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0
    41              environment-variable.
    42  `
    43  
    44  	result.Assert(t, icmd.Expected{Err: buildkitDisabledWarning})
    45  	output.Assert(t, result.Stdout(), map[int]func(string) error{
    46  		0: output.Prefix("Sending build context to Docker daemon"),
    47  		1: output.Suffix("Step 1/4 : FROM registry:5000/alpine:frozen"),
    48  		3: output.Suffix("Step 2/4 : COPY run /usr/bin/run"),
    49  		5: output.Suffix("Step 3/4 : RUN run"),
    50  		7: output.Suffix("running"),
    51  		// TODO(krissetto): ugly, remove when no longer testing against moby 24. see https://github.com/moby/moby/pull/46270
    52  		8: func(s string) error {
    53  			err := output.Contains("Removed intermediate container")(s) // moby >= v25
    54  			if err == nil {
    55  				return nil
    56  			}
    57  			return output.Contains("Removing intermediate container")(s) // moby < v25
    58  		},
    59  		10: output.Suffix("Step 4/4 : COPY data /data"),
    60  		12: output.Contains("Successfully built "),
    61  		13: output.Suffix("Successfully tagged myimage:latest"),
    62  	})
    63  }
    64  
    65  func TestTrustedBuild(t *testing.T) {
    66  	skip.If(t, environment.RemoteDaemon())
    67  	t.Setenv("DOCKER_BUILDKIT", "0")
    68  
    69  	dir := fixtures.SetupConfigFile(t)
    70  	defer dir.Remove()
    71  	image1 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build1", "latest")
    72  	image2 := fixtures.CreateMaskedTrustedRemoteImage(t, registryPrefix, "trust-build2", "latest")
    73  
    74  	buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
    75  		fs.WithFile("Dockerfile", fmt.Sprintf(`
    76  	FROM %s as build-base
    77  	RUN echo ok > /foo
    78  	FROM %s
    79  	COPY --from=build-base foo bar
    80  		`, image1, image2)))
    81  	defer buildDir.Remove()
    82  
    83  	result := icmd.RunCmd(
    84  		icmd.Command("docker", "build", "-t", "myimage", "."),
    85  		withWorkingDir(buildDir),
    86  		fixtures.WithConfig(dir.Path()),
    87  		fixtures.WithTrust,
    88  		fixtures.WithNotary,
    89  	)
    90  
    91  	result.Assert(t, icmd.Expected{
    92  		Out: fmt.Sprintf("FROM %s@sha", image1[:len(image1)-7]),
    93  		Err: fmt.Sprintf("Tagging %s@sha", image1[:len(image1)-7]),
    94  	})
    95  	result.Assert(t, icmd.Expected{
    96  		Out: fmt.Sprintf("FROM %s@sha", image2[:len(image2)-7]),
    97  	})
    98  }
    99  
   100  func TestTrustedBuildUntrustedImage(t *testing.T) {
   101  	skip.If(t, environment.RemoteDaemon())
   102  	t.Setenv("DOCKER_BUILDKIT", "0")
   103  
   104  	dir := fixtures.SetupConfigFile(t)
   105  	defer dir.Remove()
   106  	buildDir := fs.NewDir(t, "test-trusted-build-context-dir",
   107  		fs.WithFile("Dockerfile", fmt.Sprintf(`
   108  	FROM %s
   109  	RUN []
   110  		`, fixtures.AlpineImage)))
   111  	defer buildDir.Remove()
   112  
   113  	result := icmd.RunCmd(
   114  		icmd.Command("docker", "build", "-t", "myimage", "."),
   115  		withWorkingDir(buildDir),
   116  		fixtures.WithConfig(dir.Path()),
   117  		fixtures.WithTrust,
   118  		fixtures.WithNotary,
   119  	)
   120  
   121  	result.Assert(t, icmd.Expected{
   122  		ExitCode: 1,
   123  		Err:      "does not have trust data for",
   124  	})
   125  }
   126  
   127  func TestBuildIidFileSquash(t *testing.T) {
   128  	environment.SkipIfNotExperimentalDaemon(t)
   129  	t.Setenv("DOCKER_BUILDKIT", "0")
   130  
   131  	dir := fs.NewDir(t, "test-iidfile-squash")
   132  	defer dir.Remove()
   133  	iidfile := filepath.Join(dir.Path(), "idsquash")
   134  	buildDir := fs.NewDir(t, "test-iidfile-squash-build",
   135  		fs.WithFile("Dockerfile", fmt.Sprintf(`
   136  	FROM %s
   137  	ENV FOO=FOO
   138  	ENV BAR=BAR
   139  	RUN touch /fiip
   140  	RUN touch /foop`, fixtures.AlpineImage)),
   141  	)
   142  	defer buildDir.Remove()
   143  
   144  	imageTag := "testbuildiidfilesquash"
   145  	result := icmd.RunCmd(
   146  		icmd.Command("docker", "build", "--iidfile", iidfile, "--squash", "-t", imageTag, "."),
   147  		withWorkingDir(buildDir),
   148  	)
   149  	result.Assert(t, icmd.Success)
   150  	id, err := os.ReadFile(iidfile)
   151  	assert.NilError(t, err)
   152  	result = icmd.RunCommand("docker", "image", "inspect", "-f", "{{.Id}}", imageTag)
   153  	result.Assert(t, icmd.Success)
   154  	assert.Check(t, is.Equal(string(id), strings.TrimSpace(result.Combined())))
   155  }
   156  
   157  func withWorkingDir(dir *fs.Dir) func(*icmd.Cmd) {
   158  	return func(cmd *icmd.Cmd) {
   159  		cmd.Dir = dir.Path()
   160  	}
   161  }