Skip to content

Commit

Permalink
bake: add tests for call and list
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jun 28, 2024
1 parent 5021c44 commit 69ddc10
Showing 1 changed file with 165 additions and 0 deletions.
165 changes: 165 additions & 0 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/containerd/continuity/fs/fstest"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<null>\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)

Check failure on line 1086 in tests/bake.go

View workflow job for this annotation

GitHub Actions / test-integration (v0.12.5, remote, ./tests, experimental)

Failed: tests/TestIntegration/TestBakeCallCheckFlag/worker=remote

=== RUN TestIntegration/TestBakeCallCheckFlag/worker=remote === PAUSE TestIntegration/TestBakeCallCheckFlag/worker=remote === CONT TestIntegration/TestBakeCallCheckFlag/worker=remote bake.go:1086: Error Trace: /src/tests/bake.go:1086 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:96 /src/vendor/github.com/moby/buildkit/util/testutil/integration/run.go:211 Error: Received unexpected error: invalid character '/' after top-level value Test: TestIntegration/TestBakeCallCheckFlag/worker=remote sandbox.go:133: stdout: /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config496629906/buildkitd.toml --root /tmp/bktest_buildkitd1482733977 --addr unix:///tmp/bktest_buildkitd1482733977/buildkitd.sock --debug sandbox.go:133: stderr: /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config496629906/buildkitd.toml --root /tmp/bktest_buildkitd1482733977 --addr unix:///tmp/bktest_buildkitd1482733977/buildkitd.sock --debug sandbox.go:136: > StartCmd 2024-06-28 07:36:06.522419188 +0000 UTC m=+7.424427864 /usr/bin/buildkitd --oci-worker=true --containerd-worker=false --oci-worker-gc=false --oci-worker-labels=org.mobyproject.buildkit.worker.sandbox=true --config=/tmp/bktest_config496629906/buildkitd.toml --root /tmp/bktest_buildkitd1482733977 --addr unix:///tmp/bktest_buildkitd1482733977/buildkitd.sock --debug sandbox.go:136: time="2024-06-28T07:36:06Z" level=info msg="auto snapshotter: using overlayfs" sandbox.go:136: time="2024-06-28T07:36:06Z" level=warning msg="using host network as the default" sandbox.go:136: time="2024-06-28T07:36:06Z" level=info msg="found worker \"2jczuitfxgcxq1x58klq3a93y\", labels=map[org.mobyproject.buildkit.worker.executor:oci org.mobyproject.buildkit.worker.hostname:32e62d75991e org.mobyproject.buildkit.worker.network:host org.mobyproject.buildkit.worker.oci.process-mode:sandbox org.mobyproject.buildkit.worker.sandbox:true org.mobyproject.buildkit.worker.selinux.enabled:false org.mobyproject.buildkit.worker.snapshotter:overlayfs], platforms=[linux/amd64 linux/amd64/v2 linux/amd64/v3 linux/arm64 linux/riscv64 linux/ppc64le linux/s390x linux/386 linux/mips64le linux/mips64 linux/arm/v7 linux/arm/v6]" sandbox.go:136: time="2024-06-28T07:36:06Z" level=info msg="found 1 workers, default=\"2jczuitfxgcxq1x58klq3a93y\"" sandbox.go:136: time="2024-06-28T07:36:06Z" level=warning msg="currently, only the default worker can be used." sandbox.go:136: time="2024-06-28T07:36:06Z" level=info msg="running server on /tmp/bktest_buildkitd1482733977/buildkitd.sock" sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="session started" spanID=2fdefeecdbe4d95f traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="session finished: <nil>" spanID=2fdefeecdbe4d95f traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="session started" spanID=8aa3fd6eff403231 traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="new ref for local: rqzn2uq7ti49v28adpb7huasx" span="[internal] load build definition from a.Dockerfile" spanID=243eaaa38dfe1d07 traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="diffcopy took: 2.655148ms" span="[internal] load build definition from a.Dockerfile" spanID=cdc645b9cc37205c traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="2024-06-28T07:36:06Z" level=debug msg="saved rqzn2uq7ti49v28adpb7huasx as dockerfile:dockerfile:" span="[internal] load build definition from a.Dockerfile" spanID=243eaaa38dfe1d07 traceID=d58905e8657dcb023757dd9691c1e2d1 sandbox.go:136: time="202

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)
}

0 comments on commit 69ddc10

Please sign in to comment.