Skip to content
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 image based drivers having host env vars set #2215

Merged
merged 1 commit into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ IMPROVEMENTS:
BUG FIXES:
* client: Fix issue where allocations weren't pulled for several minutes. This
manifested as slow starts, delayed kills, etc [GH-2177]
* driver: Fix image based drivers (eg docker) having host env vars set [GH-2211]
* driver/docker: Fix Docker auth/logging interprelation [GH-2063, GH-2130]

## 0.5.2 (December 23, 2016)
Expand Down
8 changes: 5 additions & 3 deletions client/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,11 @@ func GetTaskEnv(taskDir *allocdir.TaskDir, node *structs.Node,
env.SetVaultToken(vaultToken, task.Vault.Env)
}

// Set the host environment variables.
filter := strings.Split(conf.ReadDefault("env.blacklist", config.DefaultEnvBlacklist), ",")
env.AppendHostEnvvars(filter)
// Set the host environment variables for non-image based drivers
if drv.FSIsolation() != cstructs.FSIsolationImage {
filter := strings.Split(conf.ReadDefault("env.blacklist", config.DefaultEnvBlacklist), ",")
env.AppendHostEnvvars(filter)
}

return env.Build(), nil
}
Expand Down
81 changes: 78 additions & 3 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/env"
"github.com/hashicorp/nomad/helper/testtask"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
Expand Down Expand Up @@ -127,10 +128,12 @@ func testDriverContexts(t *testing.T, task *structs.Task) *testContext {
return &testContext{allocDir, driverCtx, execCtx}
}

func TestDriver_GetTaskEnv(t *testing.T) {
// setupTaskEnv creates a test env for GetTaskEnv testing. Returns task dir,
// expected env, and actual env.
func setupTaskEnv(t *testing.T, driver string) (*allocdir.TaskDir, map[string]string, map[string]string) {
task := &structs.Task{
Name: "Foo",
Driver: "mock_driver",
Driver: driver,
Env: map[string]string{
"HELLO": "world",
"lorem": "ipsum",
Expand All @@ -157,7 +160,8 @@ func TestDriver_GetTaskEnv(t *testing.T) {
alloc.Name = "Bar"
conf := testConfig()
allocDir := allocdir.NewAllocDir(testLogger(), filepath.Join(conf.AllocDir, alloc.ID))
env, err := GetTaskEnv(allocDir.NewTaskDir(task.Name), nil, task, alloc, conf, "")
taskDir := allocDir.NewTaskDir(task.Name)
env, err := GetTaskEnv(taskDir, nil, task, alloc, conf, "")
if err != nil {
t.Fatalf("GetTaskEnv() failed: %v", err)
}
Expand Down Expand Up @@ -195,6 +199,16 @@ func TestDriver_GetTaskEnv(t *testing.T) {
}

act := env.EnvMap()
return taskDir, exp, act
}

func TestDriver_GetTaskEnv_None(t *testing.T) {
taskDir, exp, act := setupTaskEnv(t, "raw_exec")

// raw_exec should use host alloc dir path
exp[env.AllocDir] = taskDir.SharedAllocDir
exp[env.TaskLocalDir] = taskDir.LocalDir
exp[env.SecretsDir] = taskDir.SecretsDir

// Since host env vars are included only ensure expected env vars are present
for expk, expv := range exp {
Expand All @@ -207,6 +221,67 @@ func TestDriver_GetTaskEnv(t *testing.T) {
t.Errorf("Expected %s=%q but found %q", expk, expv, v)
}
}

// Make sure common host env vars are included.
for _, envvar := range [...]string{"PATH", "HOME", "USER"} {
if exp := os.Getenv(envvar); act[envvar] != exp {
t.Errorf("Expected envvar %s=%q != %q", envvar, exp, act[envvar])
}
}
}

func TestDriver_GetTaskEnv_Chroot(t *testing.T) {
_, exp, act := setupTaskEnv(t, "exec")

exp[env.AllocDir] = allocdir.SharedAllocContainerPath
exp[env.TaskLocalDir] = allocdir.TaskLocalContainerPath
exp[env.SecretsDir] = allocdir.TaskSecretsContainerPath

// Since host env vars are included only ensure expected env vars are present
for expk, expv := range exp {
v, ok := act[expk]
if !ok {
t.Errorf("%q not found in task env", expk)
continue
}
if v != expv {
t.Errorf("Expected %s=%q but found %q", expk, expv, v)
}
}

// Make sure common host env vars are included.
for _, envvar := range [...]string{"PATH", "HOME", "USER"} {
if exp := os.Getenv(envvar); act[envvar] != exp {
t.Errorf("Expected envvar %s=%q != %q", envvar, exp, act[envvar])
}
}
}

// TestDriver_GetTaskEnv_Image ensures host environment variables are not set
// for image based drivers. See #2211
func TestDriver_GetTaskEnv_Image(t *testing.T) {
_, exp, act := setupTaskEnv(t, "docker")

exp[env.AllocDir] = allocdir.SharedAllocContainerPath
exp[env.TaskLocalDir] = allocdir.TaskLocalContainerPath
exp[env.SecretsDir] = allocdir.TaskSecretsContainerPath

// Since host env vars are excluded expected and actual maps should be equal
for expk, expv := range exp {
v, ok := act[expk]
delete(act, expk)
if !ok {
t.Errorf("Env var %s missing. Expected %s=%q", expk, expk, expv)
continue
}
if v != expv {
t.Errorf("Env var %s=%q -- Expected %q", expk, v, expk)
}
}
// Any remaining env vars are unexpected
for actk, actv := range act {
t.Errorf("Env var %s=%q is unexpected", actk, actv)
}
}

func TestMapMergeStrInt(t *testing.T) {
Expand Down