e2e: make sure that os.environ is preserved

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>
This commit is contained in:
Sebastiaan van Stijn 2021-06-11 17:46:30 +02:00
parent 2ebf5d92aa
commit f3b6ed744f
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 15 additions and 19 deletions

View File

@ -53,54 +53,41 @@ func SetupConfigWithNotaryURL(t *testing.T, path, notaryURL string) fs.Dir {
// WithConfig sets an environment variable for the docker config location // WithConfig sets an environment variable for the docker config location
func WithConfig(dir string) func(cmd *icmd.Cmd) { func WithConfig(dir string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) { return func(cmd *icmd.Cmd) {
env := append(os.Environ(), addEnvs(cmd, "DOCKER_CONFIG="+dir)
"DOCKER_CONFIG="+dir,
)
cmd.Env = append(cmd.Env, env...)
} }
} }
// WithPassphrase sets environment variables for passphrases // WithPassphrase sets environment variables for passphrases
func WithPassphrase(rootPwd, repositoryPwd string) func(cmd *icmd.Cmd) { func WithPassphrase(rootPwd, repositoryPwd string) func(cmd *icmd.Cmd) {
return func(cmd *icmd.Cmd) { return func(cmd *icmd.Cmd) {
env := append(os.Environ(), addEnvs(cmd,
"DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE="+rootPwd, "DOCKER_CONTENT_TRUST_ROOT_PASSPHRASE="+rootPwd,
"DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE="+repositoryPwd, "DOCKER_CONTENT_TRUST_REPOSITORY_PASSPHRASE="+repositoryPwd,
) )
cmd.Env = append(cmd.Env, env...)
} }
} }
// WithTrust sets DOCKER_CONTENT_TRUST to 1 // WithTrust sets DOCKER_CONTENT_TRUST to 1
func WithTrust(cmd *icmd.Cmd) { func WithTrust(cmd *icmd.Cmd) {
env := append(os.Environ(), addEnvs(cmd, "DOCKER_CONTENT_TRUST=1")
"DOCKER_CONTENT_TRUST=1",
)
cmd.Env = append(cmd.Env, env...)
} }
// WithNotary sets the location of the notary server // WithNotary sets the location of the notary server
func WithNotary(cmd *icmd.Cmd) { func WithNotary(cmd *icmd.Cmd) {
env := append(os.Environ(), addEnvs(cmd, "DOCKER_CONTENT_TRUST_SERVER="+NotaryURL)
"DOCKER_CONTENT_TRUST_SERVER="+NotaryURL,
)
cmd.Env = append(cmd.Env, env...)
} }
// WithHome sets the HOME environment variable // WithHome sets the HOME environment variable
func WithHome(path string) func(*icmd.Cmd) { func WithHome(path string) func(*icmd.Cmd) {
return func(cmd *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 // WithNotaryServer sets the location of the notary server
func WithNotaryServer(notaryURL string) func(*icmd.Cmd) { func WithNotaryServer(notaryURL string) func(*icmd.Cmd) {
return func(cmd *icmd.Cmd) { return func(cmd *icmd.Cmd) {
env := append(os.Environ(), addEnvs(cmd, "DOCKER_CONTENT_TRUST_SERVER="+notaryURL)
"DOCKER_CONTENT_TRUST_SERVER="+notaryURL,
)
cmd.Env = append(cmd.Env, env...)
} }
} }
@ -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", "push", image).Assert(t, icmd.Success)
icmd.RunCommand("docker", "image", "rm", 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...)
}