Preservation of forwarded environment (#113)

* Tests for omitting unset variables

* Preservation of forwarded environment
This commit is contained in:
Josh Komoroske 2018-02-27 21:44:44 -08:00 committed by Bo-Yi Wu
parent 8bfc58f9d0
commit 60993a71e2
2 changed files with 30 additions and 2 deletions

View File

@ -75,8 +75,9 @@ func (p Plugin) exec(host string, wg *sync.WaitGroup, errChannel chan error) {
env := []string{}
for _, key := range p.Config.Envs {
key = strings.ToUpper(key)
val := os.Getenv(key)
env = append(env, key+"="+escapeArg(val))
if val, found := os.LookupEnv(key); found {
env = append(env, key+"="+escapeArg(val))
}
}
p.Config.Script = append(env, p.Config.Script...)

View File

@ -257,6 +257,33 @@ func TestSetENV(t *testing.T) {
assert.Nil(t, err)
}
func TestSetExistingENV(t *testing.T) {
os.Setenv("FOO", "Value for foo")
os.Setenv("BAR", "")
plugin := Plugin{
Config: Config{
Host: []string{"localhost"},
UserName: "drone-scp",
Port: 22,
KeyPath: "./tests/.ssh/id_rsa",
Secrets: []string{"FOO"},
Envs: []string{"foo", "bar", "baz"},
Debug: true,
Script: []string{"export FOO", "export BAR", "export BAZ", "env | grep -q '^FOO=Value for foo$'", "env | grep -q '^BAR=$'", "if env | grep -q BAZ; then false; else true; fi"},
CommandTimeout: 1,
Proxy: easyssh.DefaultConfig{
Server: "localhost",
User: "drone-scp",
Port: "22",
KeyPath: "./tests/.ssh/id_rsa",
},
},
}
err := plugin.Exec()
assert.Nil(t, err)
}
func TestSyncMode(t *testing.T) {
plugin := Plugin{
Config: Config{