-
Notifications
You must be signed in to change notification settings - Fork 637
bake: fix override bug with inheritance #259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,17 @@ func TestReadTargets(t *testing.T) { | |
| fp := filepath.Join(tmpdir, "config.hcl") | ||
| err = ioutil.WriteFile(fp, []byte(` | ||
| target "dep" { | ||
| args { | ||
| VAR_INHERITED = "dep" | ||
| VAR_BOTH = "dep" | ||
| } | ||
| } | ||
|
|
||
| target "webapp" { | ||
| dockerfile = "Dockerfile.webapp" | ||
| args { | ||
| VAR_BOTH = "webapp" | ||
| } | ||
| inherits = ["dep"] | ||
| }`), 0600) | ||
| require.NoError(t, err) | ||
|
|
@@ -32,35 +39,61 @@ target "webapp" { | |
| t.Run("NoOverrides", func(t *testing.T) { | ||
| m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 1, len(m)) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test hardening to catch a regression I had during dev |
||
|
|
||
| require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) | ||
| require.Equal(t, ".", *m["webapp"].Context) | ||
| require.Equal(t, "dep", m["webapp"].Args["VAR_INHERITED"]) | ||
| }) | ||
|
|
||
| t.Run("InvalidTargetOverrides", func(t *testing.T) { | ||
| _, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{"nosuchtarget.context=foo"}) | ||
| require.NotNil(t, err) | ||
| require.Equal(t, err.Error(), "unknown target nosuchtarget") | ||
| }) | ||
|
|
||
| t.Run("ArgsOverrides", func(t *testing.T) { | ||
| os.Setenv("VAR_FROMENV"+t.Name(), "fromEnv") | ||
| defer os.Unsetenv("VAR_FROM_ENV" + t.Name()) | ||
|
|
||
| m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{ | ||
| "webapp.args.VAR_UNSET", | ||
| "webapp.args.VAR_EMPTY=", | ||
| "webapp.args.VAR_SET=bananas", | ||
| "webapp.args.VAR_FROMENV" + t.Name(), | ||
| }) | ||
| require.NoError(t, err) | ||
| t.Run("leaf", func(t *testing.T) { | ||
| os.Setenv("VAR_FROMENV"+t.Name(), "fromEnv") | ||
| defer os.Unsetenv("VAR_FROM_ENV" + t.Name()) | ||
|
|
||
| require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) | ||
| require.Equal(t, ".", *m["webapp"].Context) | ||
| m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{ | ||
| "webapp.args.VAR_UNSET", | ||
| "webapp.args.VAR_EMPTY=", | ||
| "webapp.args.VAR_SET=bananas", | ||
| "webapp.args.VAR_FROMENV" + t.Name(), | ||
| "webapp.args.VAR_INHERITED=override", | ||
| // not overriding VAR_BOTH on purpose | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, "Dockerfile.webapp", *m["webapp"].Dockerfile) | ||
| require.Equal(t, ".", *m["webapp"].Context) | ||
|
|
||
| _, isSet := m["webapp"].Args["VAR_UNSET"] | ||
| require.False(t, isSet, m["webapp"].Args["VAR_UNSET"]) | ||
|
|
||
| _, isSet := m["webapp"].Args["VAR_UNSET"] | ||
| require.False(t, isSet, m["webapp"].Args["VAR_UNSET"]) | ||
| _, isSet = m["webapp"].Args["VAR_EMPTY"] | ||
| require.True(t, isSet, m["webapp"].Args["VAR_EMPTY"]) | ||
|
|
||
| _, isSet = m["webapp"].Args["VAR_EMPTY"] | ||
| require.True(t, isSet, m["webapp"].Args["VAR_EMPTY"]) | ||
| require.Equal(t, m["webapp"].Args["VAR_SET"], "bananas") | ||
|
|
||
| require.Equal(t, m["webapp"].Args["VAR_SET"], "bananas") | ||
| require.Equal(t, m["webapp"].Args["VAR_FROMENV"+t.Name()], "fromEnv") | ||
|
|
||
| require.Equal(t, m["webapp"].Args["VAR_FROMENV"+t.Name()], "fromEnv") | ||
| require.Equal(t, m["webapp"].Args["VAR_BOTH"], "webapp") | ||
| require.Equal(t, m["webapp"].Args["VAR_INHERITED"], "override") | ||
| }) | ||
|
|
||
| // building leaf but overriding parent fields | ||
| t.Run("parent", func(t *testing.T) { | ||
| m, err := ReadTargets(ctx, []string{fp}, []string{"webapp"}, []string{ | ||
| "dep.args.VAR_INHERITED=override", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a regression test for a regression I hit during writing the PR |
||
| "dep.args.VAR_BOTH=override", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is not, it's a bug in master. |
||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, m["webapp"].Args["VAR_INHERITED"], "override") | ||
| require.Equal(t, m["webapp"].Args["VAR_BOTH"], "webapp") | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("ContextOverride", func(t *testing.T) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonistiigi I found another bug (not a regression from this PR). See test with VAR_BOTH. I believe this is correct:
tis the child target, andttis the parent, we want to merge the child into the parent, and the overrides on top.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An important invariant I can add a comment in the code for, is that
tshould not have the default overrides (context = "." and dockerfile = "."), which is the case here because target() is called before setting those defaults in ResolveTarget.