-
Notifications
You must be signed in to change notification settings - Fork 28
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
fix: search $DOCKER_CONFIG
if no base64 config is provided
#398
Changes from 4 commits
8c0198f
5fbcf2d
d559c1d
5da2aaa
6459b39
3f6830d
d463de4
86db5a5
296ab91
167af18
8ad40eb
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 |
---|---|---|
|
@@ -552,13 +552,21 @@ func TestBuildFromDockerfile(t *testing.T) { | |
"Dockerfile": "FROM " + testImageAlpine, | ||
}, | ||
}) | ||
ctr, err := runEnvbuilder(t, runOpts{env: []string{ | ||
envbuilderEnv("GIT_URL", srv.URL), | ||
envbuilderEnv("DOCKERFILE_PATH", "Dockerfile"), | ||
envbuilderEnv("DOCKER_CONFIG_BASE64", base64.StdEncoding.EncodeToString([]byte(`{"experimental": "enabled"}`))), | ||
}}) | ||
logbuf := new(bytes.Buffer) | ||
ctr, err := runEnvbuilder(t, runOpts{ | ||
env: []string{ | ||
envbuilderEnv("GIT_URL", srv.URL), | ||
envbuilderEnv("DOCKERFILE_PATH", "Dockerfile"), | ||
envbuilderEnv("DOCKER_CONFIG_BASE64", base64.StdEncoding.EncodeToString([]byte(`{"experimental": "enabled"}`))), | ||
"DOCKER_CONFIG=/config", // Ignored, because we're setting DOCKER_CONFIG_BASE64. | ||
}, | ||
logbuf: logbuf, | ||
}) | ||
require.NoError(t, err) | ||
|
||
require.Contains(t, logbuf.String(), "Set DOCKER_CONFIG to /.envbuilder") | ||
require.NotContains(t, logbuf.String(), "Using DOCKER_CONFIG at ") | ||
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. Do we generally trust log output for test assertions if we aren't testing logs? If this log line ever drifts, this test will become a silent false positive. Is there a practical way to assert against the actual behaviour of the system? Could we copy the written docker config secret file so that it makes its way into the container and then assert against its contents? Would that approach be worth it? 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. We could modify 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. @SasSwart good points, I agree, in general it’s best to test behavior and not output. @johnstcn I wanted to do that here but we can’t do it in the init script since it runs after cleanup. Not sure if it’ll work but we could try to copy it as part of Dockerfile build instructions, will look into that. 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. Went back to the drawing board with the implementation, changed the behavior for user-provided config files and converted the integration tests to primarily testing behavior. |
||
|
||
output := execContainer(t, ctr, "echo hello") | ||
require.Equal(t, "hello", strings.TrimSpace(output)) | ||
|
||
|
@@ -568,6 +576,66 @@ func TestBuildFromDockerfile(t *testing.T) { | |
require.Contains(t, output, "No such file or directory") | ||
} | ||
|
||
func TestBuildDockerConfigPathFromEnv(t *testing.T) { | ||
// Ensures that a Git repository with a Dockerfile is cloned and built. | ||
srv := gittest.CreateGitServer(t, gittest.Options{ | ||
Files: map[string]string{ | ||
"Dockerfile": "FROM " + testImageAlpine, | ||
}, | ||
}) | ||
config, err := json.Marshal(envbuilder.DockerConfig{ | ||
AuthConfigs: map[string]clitypes.AuthConfig{ | ||
"mytestimage": { | ||
Username: "user", | ||
Password: "test", | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
dir := t.TempDir() | ||
err = os.WriteFile(filepath.Join(dir, "config.json"), config, 0o644) | ||
require.NoError(t, err) | ||
|
||
logbuf := new(bytes.Buffer) | ||
_, err = runEnvbuilder(t, runOpts{ | ||
env: []string{ | ||
envbuilderEnv("GIT_URL", srv.URL), | ||
envbuilderEnv("DOCKERFILE_PATH", "Dockerfile"), | ||
"DOCKER_CONFIG=/config", | ||
}, | ||
privileged: true, | ||
binds: []string{fmt.Sprintf("%s:/config:ro", dir)}, | ||
logbuf: logbuf, | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Logs that the DOCKER_CONFIG is used. | ||
require.Contains(t, logbuf.String(), "Using DOCKER_CONFIG at /config") | ||
// Logs registry auth info from existing file. | ||
require.Contains(t, logbuf.String(), "mytestimage") | ||
} | ||
|
||
func TestBuildDockerConfigDefaultPath(t *testing.T) { | ||
// Ensures that a Git repository with a Dockerfile is cloned and built. | ||
srv := gittest.CreateGitServer(t, gittest.Options{ | ||
Files: map[string]string{ | ||
"Dockerfile": "FROM " + testImageAlpine, | ||
}, | ||
}) | ||
logbuf := new(bytes.Buffer) | ||
_, err := runEnvbuilder(t, runOpts{ | ||
env: []string{ | ||
envbuilderEnv("GIT_URL", srv.URL), | ||
envbuilderEnv("DOCKERFILE_PATH", "Dockerfile"), | ||
}, | ||
logbuf: logbuf, | ||
}) | ||
require.NoError(t, err) | ||
|
||
require.Contains(t, logbuf.String(), "Set DOCKER_CONFIG to /.envbuilder") | ||
require.NotContains(t, logbuf.String(), "Using DOCKER_CONFIG at ") | ||
} | ||
|
||
func TestBuildPrintBuildOutput(t *testing.T) { | ||
// Ensures that a Git repository with a Dockerfile is cloned and built. | ||
srv := gittest.CreateGitServer(t, gittest.Options{ | ||
|
@@ -2292,10 +2360,12 @@ func startContainerFromRef(ctx context.Context, t *testing.T, cli *client.Client | |
} | ||
|
||
type runOpts struct { | ||
image string | ||
binds []string | ||
env []string | ||
volumes map[string]string | ||
image string | ||
privileged bool // Required for remounting. | ||
binds []string | ||
env []string | ||
volumes map[string]string | ||
logbuf *bytes.Buffer | ||
} | ||
|
||
// runEnvbuilder starts the envbuilder container with the given environment | ||
|
@@ -2333,17 +2403,22 @@ func runEnvbuilder(t *testing.T, opts runOpts) (string, error) { | |
require.NoError(t, err, "failed to read image pull response") | ||
img = opts.image | ||
} | ||
hostConfig := &container.HostConfig{ | ||
NetworkMode: container.NetworkMode("host"), | ||
Binds: opts.binds, | ||
Mounts: mounts, | ||
} | ||
if opts.privileged { | ||
hostConfig.CapAdd = append(hostConfig.CapAdd, "SYS_ADMIN") | ||
hostConfig.Privileged = true | ||
} | ||
ctr, err := cli.ContainerCreate(ctx, &container.Config{ | ||
Image: img, | ||
Env: opts.env, | ||
Labels: map[string]string{ | ||
testContainerLabel: "true", | ||
}, | ||
}, &container.HostConfig{ | ||
NetworkMode: container.NetworkMode("host"), | ||
Binds: opts.binds, | ||
Mounts: mounts, | ||
}, nil, nil, "") | ||
}, hostConfig, nil, nil, "") | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
_ = cli.ContainerRemove(ctx, ctr.ID, container.RemoveOptions{ | ||
|
@@ -2357,6 +2432,9 @@ func runEnvbuilder(t *testing.T, opts runOpts) (string, error) { | |
logChan, errChan := streamContainerLogs(t, cli, ctr.ID) | ||
go func() { | ||
for log := range logChan { | ||
if opts.logbuf != nil { | ||
opts.logbuf.WriteString(log + "\n") | ||
} | ||
if strings.HasPrefix(log, "=== Running init command") { | ||
errChan <- nil | ||
return | ||
|
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.
Review: Moved this higher up so that when we reset DOCKER_CONFIG, we don't interfere with envbuilder trying to set envs from the build/devcontainer.