diff --git a/tests/bake.go b/tests/bake.go index 62824a6a..a462be70 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -10,10 +10,9 @@ import ( "github.com/stretchr/testify/require" ) -func bakeCmd(sb integration.Sandbox, dir string, args ...string) (string, error) { - args = append([]string{"bake", "--progress=quiet"}, args...) - cmd := buildxCmd(sb, args...) - cmd.Dir = dir +func bakeCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("bake", "--progress=quiet")}, opts...) + cmd := buildxCmd(sb, opts...) out, err := cmd.CombinedOutput() return string(out), err } @@ -49,7 +48,7 @@ EOT gitutil.GitCommit(git, t, "initial commit") addr := gitutil.GitServeHTTP(git, t) - out, err := bakeCmd(sb, dir, addr, "--set", "*.output=type=local,dest="+dirDest) + out, err := bakeCmd(sb, withDir(dir), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest)) require.NoError(t, err, out) require.FileExists(t, filepath.Join(dirDest, "foo")) @@ -83,7 +82,7 @@ EOT gitutil.GitCommit(git, t, "initial commit") addr := gitutil.GitServeHTTP(git, t) - out, err := bakeCmd(sb, dirSrc, addr, "--set", "*.output=type=local,dest="+dirDest) + out, err := bakeCmd(sb, withDir(dirSrc), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest)) require.NoError(t, err, out) require.FileExists(t, filepath.Join(dirDest, "foo")) @@ -123,7 +122,7 @@ EOT gitutil.GitCommit(gitSrc, t, "initial commit") addrSrc := gitutil.GitServeHTTP(gitSrc, t) - out, err := bakeCmd(sb, "/tmp", addrSpec, addrSrc, "--set", "*.output=type=local,dest="+dirDest) + out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addrSpec, addrSrc, "--set", "*.output=type=local,dest="+dirDest)) require.NoError(t, err, out) require.FileExists(t, filepath.Join(dirDest, "foo")) @@ -157,7 +156,7 @@ COPY super-cool.txt / gitutil.GitCommit(git, t, "initial commit") addr := gitutil.GitServeHTTP(git, t) - out, err := bakeCmd(sb, "/tmp", addr, "--set", "*.output=type=local,dest="+dirDest) + out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest)) require.NoError(t, err, out) require.FileExists(t, filepath.Join(dirDest, "super-cool.txt")) diff --git a/tests/build.go b/tests/build.go index 53bc74d2..f82c80b3 100644 --- a/tests/build.go +++ b/tests/build.go @@ -19,9 +19,9 @@ import ( "github.com/stretchr/testify/require" ) -func buildCmd(sb integration.Sandbox, args ...string) (string, error) { - args = append([]string{"build", "--progress=quiet"}, args...) - cmd := buildxCmd(sb, args...) +func buildCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("build", "--progress=quiet")}, opts...) + cmd := buildxCmd(sb, opts...) out, err := cmd.CombinedOutput() return string(out), err } @@ -36,13 +36,13 @@ var buildTests = []func(t *testing.T, sb integration.Sandbox){ func testBuild(t *testing.T, sb integration.Sandbox) { dir := createTestProject(t) - out, err := buildCmd(sb, dir) + out, err := buildCmd(sb, withArgs(dir)) require.NoError(t, err, string(out)) } func testBuildLocalExport(t *testing.T, sb integration.Sandbox) { dir := createTestProject(t) - out, err := buildCmd(sb, fmt.Sprintf("--output=type=local,dest=%s/result", dir), dir) + out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=local,dest=%s/result", dir), dir)) require.NoError(t, err, string(out)) dt, err := os.ReadFile(dir + "/result/bar") @@ -52,7 +52,7 @@ func testBuildLocalExport(t *testing.T, sb integration.Sandbox) { func testBuildTarExport(t *testing.T, sb integration.Sandbox) { dir := createTestProject(t) - out, err := buildCmd(sb, fmt.Sprintf("--output=type=tar,dest=%s/result.tar", dir), dir) + out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=tar,dest=%s/result.tar", dir), dir)) require.NoError(t, err, string(out)) dt, err := os.ReadFile(fmt.Sprintf("%s/result.tar", dir)) @@ -74,7 +74,7 @@ func testBuildRegistryExport(t *testing.T, sb integration.Sandbox) { require.NoError(t, err) target := registry + "/buildx/registry:latest" - out, err := buildCmd(sb, fmt.Sprintf("--output=type=image,name=%s,push=true", target), dir) + out, err := buildCmd(sb, withArgs(fmt.Sprintf("--output=type=image,name=%s,push=true", target), dir)) require.NoError(t, err, string(out)) desc, provider, err := contentutil.ProviderFromRef(target) @@ -92,11 +92,9 @@ func testBuildRegistryExport(t *testing.T, sb integration.Sandbox) { func testImageIDOutput(t *testing.T, sb integration.Sandbox) { dockerfile := []byte(`FROM busybox:latest`) - dir, err := tmpdir(t, + dir := tmpdir(t, fstest.CreateFile("Dockerfile", dockerfile, 0600), ) - require.NoError(t, err) - targetDir := t.TempDir() outFlag := "--output=type=docker" @@ -106,12 +104,14 @@ func testImageIDOutput(t *testing.T, sb integration.Sandbox) { outFlag += ",dest=" + targetDir + "/image.tar" } - cmd := buildxCmd(sb, "build", "-q", outFlag, "--iidfile", filepath.Join(targetDir, "iid.txt"), "--metadata-file", filepath.Join(targetDir, "md.json"), dir) + cmd := buildxCmd( + sb, + withArgs("build", "-q", outFlag, "--iidfile", filepath.Join(targetDir, "iid.txt"), "--metadata-file", filepath.Join(targetDir, "md.json"), dir), + ) stdout := bytes.NewBuffer(nil) cmd.Stdout = stdout cmd.Stderr = os.Stderr - err = cmd.Run() - + err := cmd.Run() require.NoError(t, err) dt, err := os.ReadFile(filepath.Join(targetDir, "iid.txt")) diff --git a/tests/inspect.go b/tests/inspect.go index ec83af13..207d495e 100644 --- a/tests/inspect.go +++ b/tests/inspect.go @@ -8,9 +8,9 @@ import ( "github.com/stretchr/testify/require" ) -func inspectCmd(sb integration.Sandbox, args ...string) (string, error) { - args = append([]string{"inspect"}, args...) - cmd := buildxCmd(sb, args...) +func inspectCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("inspect")}, opts...) + cmd := buildxCmd(sb, opts...) out, err := cmd.CombinedOutput() return string(out), err } diff --git a/tests/integration.go b/tests/integration.go index a1a860f0..9582bc50 100644 --- a/tests/integration.go +++ b/tests/integration.go @@ -18,13 +18,32 @@ func tmpdir(t *testing.T, appliers ...fstest.Applier) string { return tmpdir } -func buildxCmd(sb integration.Sandbox, args ...string) *exec.Cmd { +type cmdOpt func(*exec.Cmd) + +func withArgs(args ...string) cmdOpt { + return func(cmd *exec.Cmd) { + cmd.Args = append(cmd.Args, args...) + } +} + +func withDir(dir string) cmdOpt { + return func(cmd *exec.Cmd) { + cmd.Dir = dir + } +} + +func buildxCmd(sb integration.Sandbox, opts ...cmdOpt) *exec.Cmd { + cmd := exec.Command("buildx") + cmd.Env = append([]string{}, os.Environ()...) + for _, opt := range opts { + opt(cmd) + } + if builder := sb.Address(); builder != "" { - args = append([]string{"--builder=" + builder}, args...) + cmd.Args = append(cmd.Args, "--builder="+builder) } - cmd := exec.Command("buildx", args...) if context := sb.DockerAddress(); context != "" { - cmd.Env = append(os.Environ(), "DOCKER_CONTEXT="+context) + cmd.Env = append(cmd.Env, "DOCKER_CONTEXT="+context) } return cmd diff --git a/tests/ls.go b/tests/ls.go index 87863095..a072d2af 100644 --- a/tests/ls.go +++ b/tests/ls.go @@ -8,9 +8,9 @@ import ( "github.com/stretchr/testify/require" ) -func lsCmd(sb integration.Sandbox, args ...string) (string, error) { - args = append([]string{"ls"}, args...) - cmd := buildxCmd(sb, args...) +func lsCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { + opts = append([]cmdOpt{withArgs("ls")}, opts...) + cmd := buildxCmd(sb, opts...) out, err := cmd.CombinedOutput() return string(out), err }