|
|
|
@ -353,6 +353,208 @@ func TestOverrideMerge(t *testing.T) {
|
|
|
|
|
require.Equal(t, "type=registry", m["app"].Outputs[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadContexts(t *testing.T) {
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "base" {
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "bar"
|
|
|
|
|
abc: "def"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
inherits = ["base"]
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "baz"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
m, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(m))
|
|
|
|
|
_, ok := m["app"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
bo, err := TargetsToBuildOpt(m, &Input{})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ctxs := bo["app"].Inputs.NamedContexts
|
|
|
|
|
require.Equal(t, 2, len(ctxs))
|
|
|
|
|
|
|
|
|
|
require.Equal(t, "baz", ctxs["foo"])
|
|
|
|
|
require.Equal(t, "def", ctxs["abc"])
|
|
|
|
|
|
|
|
|
|
m, _, err = ReadTargets(ctx, []File{fp}, []string{"app"}, []string{"app.contexts.foo=bay", "base.contexts.ghi=jkl"}, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(m))
|
|
|
|
|
_, ok = m["app"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
bo, err = TargetsToBuildOpt(m, &Input{})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ctxs = bo["app"].Inputs.NamedContexts
|
|
|
|
|
require.Equal(t, 3, len(ctxs))
|
|
|
|
|
|
|
|
|
|
require.Equal(t, "bay", ctxs["foo"])
|
|
|
|
|
require.Equal(t, "def", ctxs["abc"])
|
|
|
|
|
require.Equal(t, "jkl", ctxs["ghi"])
|
|
|
|
|
|
|
|
|
|
// test resetting base values
|
|
|
|
|
m, _, err = ReadTargets(ctx, []File{fp}, []string{"app"}, []string{"app.contexts.foo="}, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, 1, len(m))
|
|
|
|
|
_, ok = m["app"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
bo, err = TargetsToBuildOpt(m, &Input{})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ctxs = bo["app"].Inputs.NamedContexts
|
|
|
|
|
require.Equal(t, 1, len(ctxs))
|
|
|
|
|
require.Equal(t, "def", ctxs["abc"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadContextFromTargetUnknown(t *testing.T) {
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "base" {
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "bar"
|
|
|
|
|
abc: "def"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "baz"
|
|
|
|
|
bar: "target:bar"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Contains(t, err.Error(), "failed to find target bar")
|
|
|
|
|
}
|
|
|
|
|
func TestReadContextFromTargetChain(t *testing.T) {
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "base" {
|
|
|
|
|
}
|
|
|
|
|
target "mid" {
|
|
|
|
|
output = ["foo"]
|
|
|
|
|
contexts = {
|
|
|
|
|
parent: "target:base"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "baz"
|
|
|
|
|
bar: "target:mid"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target "unused" {}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, 3, len(m))
|
|
|
|
|
app, ok := m["app"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
|
|
require.Equal(t, 2, len(app.Contexts))
|
|
|
|
|
|
|
|
|
|
mid, ok := m["mid"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
require.Equal(t, 0, len(mid.Outputs))
|
|
|
|
|
require.Equal(t, 1, len(mid.Contexts))
|
|
|
|
|
|
|
|
|
|
base, ok := m["base"]
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
require.Equal(t, 0, len(base.Contexts))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadContextFromTargetInfiniteLoop(t *testing.T) {
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "mid" {
|
|
|
|
|
output = ["foo"]
|
|
|
|
|
contexts = {
|
|
|
|
|
parent: "target:app"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
contexts = {
|
|
|
|
|
foo: "baz"
|
|
|
|
|
bar: "target:mid"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
_, _, err := ReadTargets(ctx, []File{fp}, []string{"app", "mid"}, []string{}, nil)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Contains(t, err.Error(), "infinite loop from")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadContextFromTargetMultiPlatform(t *testing.T) {
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "mid" {
|
|
|
|
|
output = ["foo"]
|
|
|
|
|
platforms = ["linux/amd64", "linux/arm64"]
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
contexts = {
|
|
|
|
|
bar: "target:mid"
|
|
|
|
|
}
|
|
|
|
|
platforms = ["linux/amd64", "linux/arm64"]
|
|
|
|
|
}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadContextFromTargetInvalidPlatforms(t *testing.T) {
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
fp := File{
|
|
|
|
|
Name: "docker-bake.hcl",
|
|
|
|
|
Data: []byte(`
|
|
|
|
|
target "mid" {
|
|
|
|
|
output = ["foo"]
|
|
|
|
|
platforms = ["linux/amd64", "linux/riscv64"]
|
|
|
|
|
}
|
|
|
|
|
target "app" {
|
|
|
|
|
contexts = {
|
|
|
|
|
bar: "target:mid"
|
|
|
|
|
}
|
|
|
|
|
platforms = ["linux/amd64", "linux/arm64"]
|
|
|
|
|
}
|
|
|
|
|
`),
|
|
|
|
|
}
|
|
|
|
|
_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil)
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
require.Contains(t, err.Error(), "defined for different platforms")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestReadTargetsDefault(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|