diff --git a/tests/bake.go b/tests/bake.go index f50e6608a314..3f54034d974f 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "strings" "testing" "github.com/containerd/continuity/fs/fstest" @@ -48,6 +49,10 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakeMetadataWarningsDedup, testBakeMultiExporters, testBakeLoadPush, + testListTargets, + testListVariables, + testBakeCallCheck, + testBakeCallCheckFlag, } func testBakeLocal(t *testing.T, sb integration.Sandbox) { @@ -951,3 +956,163 @@ target "default" { // TODO: test metadata file when supported by multi exporters https://github.com/docker/buildx/issues/2181 } + +func testListTargets(t *testing.T, sb integration.Sandbox) { + bakefile := []byte(` +target "foo" { + description = "This builds foo" +} +target "abc" { +} +`) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + ) + + out, err := bakeCmd( + sb, + withDir(dir), + withArgs("--list-targets"), + ) + require.NoError(t, err, out) + + require.Equal(t, "TARGET\tDESCRIPTION\nabc\t\nfoo\tThis builds foo", strings.TrimSpace(out)) +} + +func testListVariables(t *testing.T, sb integration.Sandbox) { + bakefile := []byte(` +variable "foo" { + default = "bar" + description = "This is foo" +} +variable "abc" { + default = null +} +variable "def" { +} +target "default" { +} +`) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + ) + + out, err := bakeCmd( + sb, + withDir(dir), + withArgs("--list-variables"), + ) + require.NoError(t, err, out) + + require.Equal(t, "VARIABLE\tVALUE\tDESCRIPTION\nabc\t\t\t\ndef\t\t\t\nfoo\t\tbar\tThis is foo", strings.TrimSpace(out)) +} + +func testBakeCallCheck(t *testing.T, sb integration.Sandbox) { + dockerfile := []byte(` +FROM scratch +COPy foo /foo + `) + bakefile := []byte(` +target "validate" { + call = "check" +} +`) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + out, err := bakeCmd( + sb, + withDir(dir), + withArgs("validate"), + ) + require.Error(t, err, out) + + require.Contains(t, out, "validate") + require.Contains(t, out, "ConsistentInstructionCasing") +} + +func testBakeCallCheckFlag(t *testing.T, sb integration.Sandbox) { + dockerfile := []byte(` +FROM scratch +COPy foo /foo + `) + dockerfile2 := []byte(` +FROM scratch +COPY foo$BAR /foo + `) + bakefile := []byte(` +target "build" { + dockerfile = "a.Dockerfile" +} + +target "another" { + dockerfile = "b.Dockerfile" +} +`) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("a.Dockerfile", dockerfile, 0600), + fstest.CreateFile("b.Dockerfile", dockerfile2, 0600), + ) + + out, err := bakeCmd( + sb, + withDir(dir), + withArgs("build", "another", "--check"), + ) + require.Error(t, err, out) + + require.Contains(t, out, "build") + require.Contains(t, out, "ConsistentInstructionCasing") + + require.Contains(t, out, "another") + require.Contains(t, out, "UndefinedVar") + + out, err = bakeCmd( + sb, + withDir(dir), + withArgs("build", "another", "--call", "check,format=json"), + ) + require.Error(t, err, out) + + var res map[string]any + err = json.Unmarshal([]byte(out), &res) + require.NoError(t, err) + + targets, ok := res["target"].(map[string]any) + require.True(t, ok) + + build, ok := targets["build"].(map[string]any) + require.True(t, ok) + + _, ok = build["build"] + require.True(t, ok) + + check, ok := build["check"].(map[string]any) + require.True(t, ok) + + warnings, ok := check["warnings"].([]any) + require.True(t, ok) + + require.Len(t, warnings, 1) + + another, ok := targets["another"].(map[string]any) + require.True(t, ok) + + _, ok = another["build"] + require.True(t, ok) + + check, ok = another["check"].(map[string]any) + require.True(t, ok) + + warnings, ok = check["warnings"].([]any) + require.True(t, ok) + + require.Len(t, warnings, 1) +}