-
Notifications
You must be signed in to change notification settings - Fork 43
feat: allow custom build context to be configured when using DOCKERFILE_PATH
#139
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 4 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 |
---|---|---|
|
@@ -719,6 +719,91 @@ func TestNoMethodFails(t *testing.T) { | |
require.ErrorContains(t, err, envbuilder.ErrNoFallbackImage.Error()) | ||
} | ||
|
||
func TestDockerfileBuildContext(t *testing.T) { | ||
t.Parallel() | ||
|
||
inclFile := "myfile" | ||
dockerfile := fmt.Sprintf(`FROM %s | ||
COPY %s .`, testImageAlpine, inclFile) | ||
|
||
tests := []struct { | ||
name string | ||
files map[string]string | ||
dockerfilePath string | ||
buildContextPath string | ||
expectedErr string | ||
}{ | ||
{ | ||
// Dockerfile & build context are in the same dir, copying inclFile should work. | ||
name: "same build context (default)", | ||
files: map[string]string{ | ||
"Dockerfile": dockerfile, | ||
inclFile: "...", | ||
}, | ||
dockerfilePath: "Dockerfile", | ||
buildContextPath: "", // use default | ||
expectedErr: "", // expect no errors | ||
}, | ||
{ | ||
// Dockerfile & build context are not in the same dir, build context is still the default; this should fail | ||
// to copy inclFile since it is not in the same dir as the Dockerfile. | ||
name: "different build context (default)", | ||
files: map[string]string{ | ||
"a/Dockerfile": dockerfile, | ||
"a/" + inclFile: "...", | ||
}, | ||
dockerfilePath: "a/Dockerfile", | ||
buildContextPath: "", // use default | ||
expectedErr: inclFile + ": no such file or directory", | ||
}, | ||
{ | ||
// Dockerfile & build context are not in the same dir, but inclFile is in the default build context dir; | ||
// this should allow inclFile to be copied. This is probably not desirable though? | ||
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. Reviewer: should we warn when a Dockerfile is used outside the 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. You could also make an argument for failing the build entirely; but I think warning is fine here. |
||
name: "different build context (default, different content roots)", | ||
files: map[string]string{ | ||
"a/Dockerfile": dockerfile, | ||
inclFile: "...", | ||
}, | ||
dockerfilePath: "a/Dockerfile", | ||
buildContextPath: "", // use default | ||
expectedErr: "", | ||
}, | ||
{ | ||
// Dockerfile is not in the default build context dir, but the build context has been overridden; this should | ||
// allow inclFile to be copied. | ||
name: "different build context (custom)", | ||
files: map[string]string{ | ||
"a/Dockerfile": dockerfile, | ||
"a/" + inclFile: "...", | ||
}, | ||
dockerfilePath: "a/Dockerfile", | ||
buildContextPath: "a/", | ||
expectedErr: "", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
url := createGitServer(t, gitServerOptions{ | ||
files: tc.files, | ||
}) | ||
_, err := runEnvbuilder(t, options{env: []string{ | ||
"GIT_URL=" + url, | ||
"DOCKERFILE_PATH=" + tc.dockerfilePath, | ||
"BUILD_CONTEXT_PATH=" + tc.buildContextPath, | ||
}}) | ||
|
||
if tc.expectedErr == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, tc.expectedErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// TestMain runs before all tests to build the envbuilder image. | ||
func TestMain(m *testing.M) { | ||
cleanOldEnvbuilders() | ||
|
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.
Since
WorkspaceFolder
is optional, what's the end-result if it's not provided, will this be correct?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.
WorkspaceFolder
will be given a default value if none is provided:envbuilder/envbuilder.go
Lines 270 to 276 in 0643d17