Skip to content

Commit

Permalink
e2e: make sure that os.environ is preserved
Browse files Browse the repository at this point in the history
We updated some of these functions to make sure os.environ was
preserved, but some where not.

This adds a utility to help with this, which also prevents the
os.environ to be added multiple times.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Jun 11, 2021
1 parent 2ebf5d9 commit f3b6ed7
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions e2e/internal/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,54 +53,41 @@ func SetupConfigWithNotaryURL(t *testing.T, path, notaryURL string) fs.Dir {
// WithConfig sets an environment variable for the docker config location
func WithConfig(dir string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) {
env := append(os.Environ(),
"DOCKER_CONFIG="+dir,
)
cmd.Env = append(cmd.Env, env...)
addEnvs(cmd, "DOCKER_CONFIG="+dir)
}
}

// WithPassphrase sets environment variables for passphrases
func WithPassphrase(rootPwd, repositoryPwd string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) {
env := append(os.Environ(),
addEnvs(cmd,
"DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE="+rootPwd,
"DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE="+repositoryPwd,
)
cmd.Env = append(cmd.Env, env...)
}
}

// WithTrust sets DOCKER_CONTENT_TRUST to 1
func WithTrust(cmd *icmd.Cmd) {
env := append(os.Environ(),
"DOCKER_CONTENT_TRUST=1",
)
cmd.Env = append(cmd.Env, env...)
addEnvs(cmd, "DOCKER_CONTENT_TRUST=1")
}

// WithNotary sets the location of the notary server
func WithNotary(cmd *icmd.Cmd) {
env := append(os.Environ(),
"DOCKER_CONTENT_TRUST_SERVER="+NotaryURL,
)
cmd.Env = append(cmd.Env, env...)
addEnvs(cmd, "DOCKER_CONTENT_TRUST_SERVER="+NotaryURL)
}

// WithHome sets the HOME environment variable
func WithHome(path string) func(*icmd.Cmd) {
return func(cmd *icmd.Cmd) {
cmd.Env = append(cmd.Env, "HOME="+path)
addEnvs(cmd, "HOME="+path)
}
}

// WithNotaryServer sets the location of the notary server
func WithNotaryServer(notaryURL string) func(*icmd.Cmd) {
return func(cmd *icmd.Cmd) {
env := append(os.Environ(),
"DOCKER_CONTENT_TRUST_SERVER="+notaryURL,
)
cmd.Env = append(cmd.Env, env...)
addEnvs(cmd, "DOCKER_CONTENT_TRUST_SERVER="+notaryURL)
}
}

Expand Down Expand Up @@ -133,3 +120,12 @@ func createNamedUnsignedImageFromBusyBox(t *testing.T, image string) {
icmd.RunCommand("docker", "image", "push", image).Assert(t, icmd.Success)
icmd.RunCommand("docker", "image", "rm", image).Assert(t, icmd.Success)
}

// addEnvs adds environment variables to cmd, making sure to preserve the
// current os.Environ(), which would otherwise be omitted (for non-empty .Env).
func addEnvs(cmd *icmd.Cmd, envs ...string) {
if len(cmd.Env) == 0 {
cmd.Env = os.Environ()
}
cmd.Env = append(cmd.Env, envs...)
}

0 comments on commit f3b6ed7

Please sign in to comment.