From b41608df5102465331667d4b605969e6012f1be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Date: Thu, 24 Mar 2016 08:47:23 +0100 Subject: [PATCH] client/driver: added more tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- client/driver/exec_test.go | 34 +++++++++++++++++++++++++ client/driver/java_test.go | 36 +++++++++++++++++++++++++++ client/driver/qemu_test.go | 45 ++++++++++++++++++++++++++++++++++ client/driver/raw_exec_test.go | 33 +++++++++++++++++++++++++ client/driver/rkt_test.go | 37 ++++++++++++++++++++++++++++ 5 files changed, 185 insertions(+) diff --git a/client/driver/exec_test.go b/client/driver/exec_test.go index 0eb3d7f97..103267323 100644 --- a/client/driver/exec_test.go +++ b/client/driver/exec_test.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "reflect" + "strings" "syscall" "testing" "time" @@ -291,3 +292,36 @@ func TestExecDriver_Start_Kill_Wait(t *testing.T) { t.Fatalf("timeout") } } + +func TestExecDriverUser(t *testing.T) { + t.Parallel() + ctestutils.ExecCompatible(t) + task := &structs.Task{ + Name: "sleep", + User: "alice", + Config: map[string]interface{}{ + "command": "/bin/sleep", + "args": []string{"100"}, + }, + LogConfig: &structs.LogConfig{ + MaxFiles: 10, + MaxFileSizeMB: 10, + }, + Resources: basicResources, + KillTimeout: 10 * time.Second, + } + + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() + d := NewExecDriver(driverCtx) + + handle, err := d.Start(execCtx, task) + if err == nil { + handle.Kill() + t.Fatalf("Should've failed") + } + msg := "user alice" + if !strings.Contains(err.Error(), msg) { + t.Fatalf("Expecting '%v' in '%v'", msg, err) + } +} diff --git a/client/driver/java_test.go b/client/driver/java_test.go index eeca35b41..e33b47597 100644 --- a/client/driver/java_test.go +++ b/client/driver/java_test.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "testing" "time" @@ -221,3 +222,38 @@ func TestJavaDriver_Start_Kill_Wait(t *testing.T) { } } } + +func TestJavaDriverUser(t *testing.T) { + t.Parallel() + if !javaLocated() { + t.Skip("Java not found; skipping") + } + + ctestutils.JavaCompatible(t) + task := &structs.Task{ + Name: "demo-app", + User: "alice", + Config: map[string]interface{}{ + "jar_path": "demoapp.jar", + }, + LogConfig: &structs.LogConfig{ + MaxFiles: 10, + MaxFileSizeMB: 10, + }, + Resources: basicResources, + } + + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() + d := NewJavaDriver(driverCtx) + + handle, err := d.Start(execCtx, task) + if err == nil { + handle.Kill() + t.Fatalf("Should've failed") + } + msg := "user alice" + if !strings.Contains(err.Error(), msg) { + t.Fatalf("Expecting '%v' in '%v'", msg, err) + } +} diff --git a/client/driver/qemu_test.go b/client/driver/qemu_test.go index c5cd0b31d..23dfcff1c 100644 --- a/client/driver/qemu_test.go +++ b/client/driver/qemu_test.go @@ -3,6 +3,7 @@ package driver import ( "fmt" "path/filepath" + "strings" "testing" "github.com/hashicorp/nomad/client/config" @@ -93,3 +94,47 @@ func TestQemuDriver_StartOpen_Wait(t *testing.T) { fmt.Printf("\nError killing Qemu test: %s", err) } } + +func TestQemuDriverUser(t *testing.T) { + t.Parallel() + ctestutils.QemuCompatible(t) + task := &structs.Task{ + Name: "linux", + User: "alice", + Config: map[string]interface{}{ + "image_path": "linux-0.2.img", + "accelerator": "tcg", + "port_map": []map[string]int{{ + "main": 22, + "web": 8080, + }}, + }, + LogConfig: &structs.LogConfig{ + MaxFiles: 10, + MaxFileSizeMB: 10, + }, + Resources: &structs.Resources{ + CPU: 500, + MemoryMB: 512, + Networks: []*structs.NetworkResource{ + &structs.NetworkResource{ + ReservedPorts: []structs.Port{{"main", 22000}, {"web", 80}}, + }, + }, + }, + } + + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() + d := NewQemuDriver(driverCtx) + + handle, err := d.Start(execCtx, task) + if err == nil { + handle.Kill() + t.Fatalf("Should've failed") + } + msg := "unknown user alice" + if !strings.Contains(err.Error(), msg) { + t.Fatalf("Expecting '%v' in '%v'", msg, err) + } +} diff --git a/client/driver/raw_exec_test.go b/client/driver/raw_exec_test.go index f35be1ef1..9ebfb3315 100644 --- a/client/driver/raw_exec_test.go +++ b/client/driver/raw_exec_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "path/filepath" "reflect" + "strings" "testing" "time" @@ -245,3 +246,35 @@ func TestRawExecDriver_Start_Kill_Wait(t *testing.T) { t.Fatalf("timeout") } } + +func TestRawExecDriverUser(t *testing.T) { + t.Parallel() + task := &structs.Task{ + Name: "sleep", + User: "alice", + Config: map[string]interface{}{ + "command": testtask.Path(), + "args": []string{"sleep", "45s"}, + }, + LogConfig: &structs.LogConfig{ + MaxFiles: 10, + MaxFileSizeMB: 10, + }, + Resources: basicResources, + } + testtask.SetTaskEnv(task) + + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() + d := NewRawExecDriver(driverCtx) + + handle, err := d.Start(execCtx, task) + if err == nil { + handle.Kill() + t.Fatalf("Should've failed") + } + msg := "unknown user alice" + if !strings.Contains(err.Error(), msg) { + t.Fatalf("Expecting '%v' in '%v'", msg, err) + } +} diff --git a/client/driver/rkt_test.go b/client/driver/rkt_test.go index 06650005b..0c5c08850 100644 --- a/client/driver/rkt_test.go +++ b/client/driver/rkt_test.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "path/filepath" "reflect" + "strings" "testing" "time" @@ -259,3 +260,39 @@ func TestRktDriver_Start_Wait_AllocDir(t *testing.T) { t.Fatalf("Command output is %v; expected %v", act, exp) } } + +func TestRktDriverUser(t *testing.T) { + ctestutils.RktCompatible(t) + task := &structs.Task{ + Name: "etcd", + User: "alice", + Config: map[string]interface{}{ + "trust_prefix": "coreos.com/etcd", + "image": "coreos.com/etcd:v2.0.4", + "command": "/etcd", + "args": []string{"--version"}, + }, + LogConfig: &structs.LogConfig{ + MaxFiles: 10, + MaxFileSizeMB: 10, + }, + Resources: &structs.Resources{ + MemoryMB: 128, + CPU: 100, + }, + } + + driverCtx, execCtx := testDriverContexts(task) + defer execCtx.AllocDir.Destroy() + d := NewRktDriver(driverCtx) + + handle, err := d.Start(execCtx, task) + if err == nil { + handle.Kill() + t.Fatalf("Should've failed") + } + msg := "unknown user alice" + if !strings.Contains(err.Error(), msg) { + t.Fatalf("Expecting '%v' in '%v'", msg, err) + } +}