-
Notifications
You must be signed in to change notification settings - Fork 58
fix: substitute devcontainer.json build args into Dockerfile ARG variables #490
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
20e3937
fix: substitute devcontainer.json build args into Dockerfile ARG vari…
kacpersaw bbbbf98
refactor: address PR review comments
kacpersaw 57c80b4
test: remove MissingBuildArgUsesEmpty test
kacpersaw e914847
style: fix gofumpt formatting
kacpersaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,13 +213,71 @@ func TestImageFromDockerfile(t *testing.T) { | |
| tc := tc | ||
| t.Run(tc.image, func(t *testing.T) { | ||
| t.Parallel() | ||
| ref, err := devcontainer.ImageFromDockerfile(tc.content) | ||
| ref, err := devcontainer.ImageFromDockerfile(tc.content, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.image, ref.Name()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestImageFromDockerfile_BuildArgs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // Test that build args override ARG defaults. | ||
| t.Run("OverridesDefault", func(t *testing.T) { | ||
| t.Parallel() | ||
| content := "ARG VARIANT=3.10\nFROM mcr.microsoft.com/devcontainers/python:0-${VARIANT}" | ||
| ref, err := devcontainer.ImageFromDockerfile(content, map[string]string{"VARIANT": "3.11-bookworm"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "mcr.microsoft.com/devcontainers/python:0-3.11-bookworm", ref.Name()) | ||
| }) | ||
|
|
||
| // Test that build args supply values for ARGs without defaults. | ||
| t.Run("SuppliesArgWithoutDefault", func(t *testing.T) { | ||
| t.Parallel() | ||
| content := "ARG VARIANT\nFROM mcr.microsoft.com/devcontainers/python:1-${VARIANT}" | ||
| ref, err := devcontainer.ImageFromDockerfile(content, map[string]string{"VARIANT": "3.11-bookworm"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "mcr.microsoft.com/devcontainers/python:1-3.11-bookworm", ref.Name()) | ||
| }) | ||
|
|
||
| // Test that a missing build arg for an ARG without default results | ||
| // in the variable being substituted as empty string. | ||
| t.Run("MissingBuildArgUsesEmpty", func(t *testing.T) { | ||
| t.Parallel() | ||
| content := "ARG VARIANT\nFROM mcr.microsoft.com/devcontainers/python:1-${VARIANT}" | ||
| ref, err := devcontainer.ImageFromDockerfile(content, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "mcr.microsoft.com/devcontainers/python:1-", ref.Name()) | ||
|
Member
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. One point I'd like to raise here: if you try to build this Dockerfile it will probably error with something like whereas a different Dockerfile would probably not fail e.g. Docker also seems to have some kind of "special-case" warning about this e.g. |
||
| }) | ||
| } | ||
|
|
||
| func TestUserFromDockerfile_BuildArgs(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("SubstitutesARGInFROM", func(t *testing.T) { | ||
| t.Parallel() | ||
| registry := registrytest.New(t) | ||
| image, err := partial.UncompressedToImage(emptyImage{configFile: &v1.ConfigFile{ | ||
| Config: v1.Config{ | ||
| User: "testuser", | ||
| }, | ||
| }}) | ||
| require.NoError(t, err) | ||
| ref := strings.TrimPrefix(registry, "http://") + "/coder/test:latest" | ||
| parsed, err := name.ParseReference(ref) | ||
| require.NoError(t, err) | ||
| err = remote.Write(parsed, image) | ||
| require.NoError(t, err) | ||
|
|
||
| // Dockerfile uses ARG without default for the image ref. | ||
| content := fmt.Sprintf("ARG TAG\nFROM %s/coder/test:${TAG}", strings.TrimPrefix(registry, "http://")) | ||
| user, err := devcontainer.UserFromDockerfile(content, map[string]string{"TAG": "latest"}) | ||
| require.NoError(t, err) | ||
| require.Equal(t, "testuser", user) | ||
| }) | ||
| } | ||
|
|
||
| func TestUserFrom(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
|
|
@@ -287,7 +345,7 @@ func TestUserFrom(t *testing.T) { | |
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| user, err := devcontainer.UserFromDockerfile(tt.content) | ||
| user, err := devcontainer.UserFromDockerfile(tt.content, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.user, user) | ||
| }) | ||
|
|
@@ -364,7 +422,7 @@ FROM a`, | |
|
|
||
| content := strings.ReplaceAll(tt.content, "coder/test", strings.TrimPrefix(registry, "http://")+"/coder/test") | ||
|
|
||
| user, err := devcontainer.UserFromDockerfile(content) | ||
| user, err := devcontainer.UserFromDockerfile(content, nil) | ||
|
kacpersaw marked this conversation as resolved.
|
||
| require.NoError(t, err) | ||
| require.Equal(t, tt.user, user) | ||
| }) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.