Skip to content

Commit

Permalink
Merge pull request #502 from ChrisHines/portable-driver-tests
Browse files Browse the repository at this point in the history
Portable client/driver tests.
  • Loading branch information
dadgar committed Nov 26, 2015
2 parents 7b801bc + ffda9d7 commit 4398632
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 187 deletions.
13 changes: 13 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func dockerSetup(t *testing.T, task *structs.Task) (*docker.Client, DriverHandle
}

func TestDockerDriver_Handle(t *testing.T) {
t.Parallel()
h := &DockerHandle{
imageID: "imageid",
containerID: "containerid",
Expand All @@ -135,6 +136,7 @@ func TestDockerDriver_Handle(t *testing.T) {

// This test should always pass, even if docker daemon is not available
func TestDockerDriver_Fingerprint(t *testing.T) {
t.Parallel()
d := NewDockerDriver(testDockerDriverContext(""))
node := &structs.Node{
Attributes: make(map[string]string),
Expand All @@ -153,6 +155,7 @@ func TestDockerDriver_Fingerprint(t *testing.T) {
}

func TestDockerDriver_StartOpen_Wait(t *testing.T) {
t.Parallel()
if !dockerIsConnected(t) {
t.SkipNow()
}
Expand Down Expand Up @@ -190,6 +193,7 @@ func TestDockerDriver_StartOpen_Wait(t *testing.T) {
}

func TestDockerDriver_Start_Wait(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
Config: map[string]interface{}{
Expand Down Expand Up @@ -223,6 +227,7 @@ func TestDockerDriver_Start_Wait(t *testing.T) {
}

func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
t.Parallel()
// This test requires that the alloc dir be mounted into docker as a volume.
// Because this cannot happen when docker is run remotely, e.g. when running
// docker in a VM, we skip this when we detect Docker is being run remotely.
Expand Down Expand Up @@ -285,6 +290,7 @@ func TestDockerDriver_Start_Wait_AllocDir(t *testing.T) {
}

func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
t.Parallel()
task := &structs.Task{
Name: "redis-demo",
Config: map[string]interface{}{
Expand Down Expand Up @@ -317,6 +323,7 @@ func TestDockerDriver_Start_Kill_Wait(t *testing.T) {
}

func TestDocker_StartN(t *testing.T) {
t.Parallel()
if !dockerIsConnected(t) {
t.SkipNow()
}
Expand Down Expand Up @@ -371,6 +378,7 @@ func TestDocker_StartN(t *testing.T) {
}

func TestDocker_StartNVersions(t *testing.T) {
t.Parallel()
if !dockerIsConnected(t) {
t.SkipNow()
}
Expand Down Expand Up @@ -428,6 +436,7 @@ func TestDocker_StartNVersions(t *testing.T) {
}

func TestDockerHostNet(t *testing.T) {
t.Parallel()
expected := "host"

task := &structs.Task{
Expand Down Expand Up @@ -457,6 +466,7 @@ func TestDockerHostNet(t *testing.T) {
}

func TestDockerLabels(t *testing.T) {
t.Parallel()
task := dockerTask()
task.Config["labels"] = []map[string]string{
map[string]string{
Expand All @@ -483,6 +493,7 @@ func TestDockerLabels(t *testing.T) {
}

func TestDockerDNS(t *testing.T) {
t.Parallel()
task := dockerTask()
task.Config["dns_servers"] = []string{"8.8.8.8", "8.8.4.4"}
task.Config["dns_search_domains"] = []string{"example.com", "example.org", "example.net"}
Expand Down Expand Up @@ -514,6 +525,7 @@ func inSlice(needle string, haystack []string) bool {
}

func TestDockerPortsNoMap(t *testing.T) {
t.Parallel()
task := dockerTask()

client, handle, cleanup := dockerSetup(t, task)
Expand Down Expand Up @@ -564,6 +576,7 @@ func TestDockerPortsNoMap(t *testing.T) {
}

func TestDockerPortsMapping(t *testing.T) {
t.Parallel()
task := dockerTask()
task.Config["port_map"] = []map[string]string{
map[string]string{
Expand Down
10 changes: 10 additions & 0 deletions client/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/helper/testtask"
"github.com/hashicorp/nomad/nomad/structs"
)

Expand All @@ -30,6 +31,12 @@ func init() {
rand.Seed(49875)
}

func TestMain(m *testing.M) {
if !testtask.Run() {
os.Exit(m.Run())
}
}

func testLogger() *log.Logger {
return log.New(os.Stderr, "", log.LstdFlags)
}
Expand All @@ -54,6 +61,7 @@ func testDriverExecContext(task *structs.Task, driverCtx *DriverContext) *ExecCo
}

func TestDriver_TaskEnvironmentVariables(t *testing.T) {
t.Parallel()
ctx := &ExecContext{}
task := &structs.Task{
Env: map[string]string{
Expand Down Expand Up @@ -101,6 +109,7 @@ func TestDriver_TaskEnvironmentVariables(t *testing.T) {
}

func TestMapMergeStrInt(t *testing.T) {
t.Parallel()
a := map[string]int{
"cakes": 5,
"cookies": 3,
Expand All @@ -125,6 +134,7 @@ func TestMapMergeStrInt(t *testing.T) {
}

func TestMapMergeStrStr(t *testing.T) {
t.Parallel()
a := map[string]string{
"cake": "chocolate",
"cookie": "caramel",
Expand Down
7 changes: 7 additions & 0 deletions client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

func TestExecDriver_Fingerprint(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
d := NewExecDriver(testDriverContext(""))
node := &structs.Node{
Expand All @@ -34,6 +35,7 @@ func TestExecDriver_Fingerprint(t *testing.T) {
}

func TestExecDriver_StartOpen_Wait(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "sleep",
Expand Down Expand Up @@ -68,6 +70,7 @@ func TestExecDriver_StartOpen_Wait(t *testing.T) {
}

func TestExecDriver_Start_Wait(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "sleep",
Expand Down Expand Up @@ -109,6 +112,7 @@ func TestExecDriver_Start_Wait(t *testing.T) {
}

func TestExecDriver_Start_Artifact_basic(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
file := "hi_linux_amd64"
checksum := "sha256:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c"
Expand Down Expand Up @@ -153,6 +157,7 @@ func TestExecDriver_Start_Artifact_basic(t *testing.T) {
}

func TestExecDriver_Start_Artifact_expanded(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
file := "hi_linux_amd64"

Expand Down Expand Up @@ -199,6 +204,7 @@ func TestExecDriver_Start_Artifact_expanded(t *testing.T) {
}
}
func TestExecDriver_Start_Wait_AllocDir(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)

exp := []byte{'w', 'i', 'n'}
Expand Down Expand Up @@ -251,6 +257,7 @@ func TestExecDriver_Start_Wait_AllocDir(t *testing.T) {
}

func TestExecDriver_Start_Kill_Wait(t *testing.T) {
t.Parallel()
ctestutils.ExecCompatible(t)
task := &structs.Task{
Name: "sleep",
Expand Down
21 changes: 0 additions & 21 deletions client/driver/executor/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"os/exec"
"path/filepath"
"strings"

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/nomad/structs"
Expand All @@ -34,11 +33,6 @@ import (

var errNoResources = fmt.Errorf("No resources are associated with this task")

// If testModeEnvVar is set in a process's environment variables, the
// LinuxExecutor will detect it and inject the current binary into the chroot.
// This enables using the test binary in tests to provide portability.
var testModeEnvVar = "NOMAD_EXECUTOR_TEST_ONLY_13871827980214"

// Executor is an interface that any platform- or capability-specific exec
// wrapper must implement. You should not need to implement a Java executor.
// Rather, you would implement a cgroups executor that the Java driver will use.
Expand Down Expand Up @@ -112,18 +106,3 @@ func OpenId(id string) (Executor, error) {
}
return executor, nil
}

// isTest returns whether the cmd is a test binary.
func isTest(cmd *exec.Cmd) bool {
if cmd == nil {
return false
}

for _, env := range cmd.Env {
if strings.HasPrefix(env, testModeEnvVar) {
return true
}
}

return false
}
1 change: 1 addition & 0 deletions client/driver/executor/exec_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package executor
import "testing"

func TestExecutorBasic(t *testing.T) {
t.Parallel()
testExecutor(t, NewBasicExecutor, nil)
}
7 changes: 0 additions & 7 deletions client/driver/executor/exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,6 @@ func (e *LinuxExecutor) ConfigureTaskDir(taskName string, alloc *allocdir.AllocD
return err
}

// Embed ourselves if this is a test. This needs to be done so the test
// binary is inside the chroot.
if isTest(&e.cmd) {
bin := e.cmd.Args[0]
alloc.Embed(taskName, map[string]string{bin: bin})
}

if err := alloc.Embed(taskName, chrootEnv); err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions client/driver/executor/exec_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import (
"testing"

ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testtask"
)

func init() {
// Add test binary to chroot during test run.
chrootEnv[testtask.Path()] = testtask.Path()
}

func TestExecutorLinux(t *testing.T) {
t.Parallel()
testExecutor(t, NewLinuxExecutor, ctestutil.ExecCompatible)
}
Loading

0 comments on commit 4398632

Please sign in to comment.