2018-07-18 23:51:50 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2018-11-28 15:41:59 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/env"
|
2018-07-18 23:51:50 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskRunner_Validate_UserEnforcement(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
taskEnv := env.NewEmptyBuilder().Build()
|
|
|
|
conf := config.DefaultConfig()
|
|
|
|
|
|
|
|
// Try to run as root with exec.
|
|
|
|
task := &structs.Task{
|
|
|
|
Driver: "exec",
|
|
|
|
User: "root",
|
|
|
|
}
|
|
|
|
if err := validateTask(task, taskEnv, conf); err == nil {
|
|
|
|
t.Fatalf("expected error running as root with exec")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to run a non-blacklisted user with exec.
|
|
|
|
task.User = "foobar"
|
|
|
|
require.NoError(t, validateTask(task, taskEnv, conf))
|
|
|
|
|
|
|
|
// Try to run as root with docker.
|
|
|
|
task.Driver = "docker"
|
|
|
|
task.User = "root"
|
|
|
|
require.NoError(t, validateTask(task, taskEnv, conf))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskRunner_Validate_ServiceName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
builder := env.NewEmptyBuilder()
|
|
|
|
conf := config.DefaultConfig()
|
|
|
|
|
|
|
|
// Create a task with a service for validation
|
|
|
|
task := &structs.Task{
|
|
|
|
Services: []*structs.Service{
|
|
|
|
{
|
|
|
|
Name: "ok",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, validateTask(task, builder.Build(), conf))
|
|
|
|
|
|
|
|
// Add an env var that should validate
|
2018-11-27 19:53:47 +00:00
|
|
|
builder.SetHookEnv("test", map[string]string{"FOO": "bar"})
|
2018-07-18 23:51:50 +00:00
|
|
|
task.Services[0].Name = "${FOO}"
|
|
|
|
require.NoError(t, validateTask(task, builder.Build(), conf))
|
|
|
|
|
|
|
|
// Add an env var that should *not* validate
|
2018-11-27 19:53:47 +00:00
|
|
|
builder.SetHookEnv("test", map[string]string{"BAD": "invalid/in/consul"})
|
2018-07-18 23:51:50 +00:00
|
|
|
task.Services[0].Name = "${BAD}"
|
|
|
|
require.Error(t, validateTask(task, builder.Build(), conf))
|
|
|
|
}
|