From c920d3cbb04e88ff549edf0afba78a0f6ae0adfe Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 25 Jan 2017 20:58:24 -0800 Subject: [PATCH] Fix unreachable function in tests --- client/driver/executor/checks_linux_test.go | 55 +++++++++++++++++++ client/driver/executor/checks_test.go | 46 ---------------- client/driver/executor/executor_linux_test.go | 52 ++++++++++++++++++ client/driver/executor/executor_test.go | 52 ------------------ 4 files changed, 107 insertions(+), 98 deletions(-) create mode 100644 client/driver/executor/checks_linux_test.go diff --git a/client/driver/executor/checks_linux_test.go b/client/driver/executor/checks_linux_test.go new file mode 100644 index 000000000..b825dbc26 --- /dev/null +++ b/client/driver/executor/checks_linux_test.go @@ -0,0 +1,55 @@ +package executor + +import ( + "log" + "os" + "strings" + "testing" + + "github.com/hashicorp/nomad/client/testutil" +) + +func TestExecScriptCheckWithIsolation(t *testing.T) { + testutil.ExecCompatible(t) + + execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}} + ctx, allocDir := testExecutorContextWithChroot(t) + defer allocDir.Destroy() + + execCmd.FSIsolation = true + execCmd.ResourceLimits = true + execCmd.User = dstructs.DefaultUnpriviledgedUser + + executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags)) + + if err := executor.SetContext(ctx); err != nil { + t.Fatalf("Unexpected error") + } + + _, err := executor.LaunchCmd(&execCmd) + if err != nil { + t.Fatalf("error in launching command: %v", err) + } + + check := &ExecScriptCheck{ + id: "foo", + cmd: "/bin/echo", + args: []string{"hello", "world"}, + taskDir: ctx.TaskDir, + FSIsolation: true, + } + + res := check.Run() + expectedOutput := "hello world" + expectedExitCode := 0 + if res.Err != nil { + t.Fatalf("err: %v", res.Err) + } + if strings.TrimSpace(res.Output) != expectedOutput { + t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output) + } + + if res.ExitCode != expectedExitCode { + t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode) + } +} diff --git a/client/driver/executor/checks_test.go b/client/driver/executor/checks_test.go index 2a2cbdb3b..3907191f9 100644 --- a/client/driver/executor/checks_test.go +++ b/client/driver/executor/checks_test.go @@ -9,7 +9,6 @@ import ( docker "github.com/fsouza/go-dockerclient" - dstructs "github.com/hashicorp/nomad/client/driver/structs" "github.com/hashicorp/nomad/client/testutil" ) @@ -37,51 +36,6 @@ func TestExecScriptCheckNoIsolation(t *testing.T) { } } -func TestExecScriptCheckWithIsolation(t *testing.T) { - testutil.ExecCompatible(t) - - execCmd := ExecCommand{Cmd: "/bin/echo", Args: []string{"hello world"}} - ctx, allocDir := testExecutorContextWithChroot(t) - defer allocDir.Destroy() - - execCmd.FSIsolation = true - execCmd.ResourceLimits = true - execCmd.User = dstructs.DefaultUnpriviledgedUser - - executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags)) - - if err := executor.SetContext(ctx); err != nil { - t.Fatalf("Unexpected error") - } - - _, err := executor.LaunchCmd(&execCmd) - if err != nil { - t.Fatalf("error in launching command: %v", err) - } - - check := &ExecScriptCheck{ - id: "foo", - cmd: "/bin/echo", - args: []string{"hello", "world"}, - taskDir: ctx.TaskDir, - FSIsolation: true, - } - - res := check.Run() - expectedOutput := "hello world" - expectedExitCode := 0 - if res.Err != nil { - t.Fatalf("err: %v", res.Err) - } - if strings.TrimSpace(res.Output) != expectedOutput { - t.Fatalf("output expected: %v, actual: %v", expectedOutput, res.Output) - } - - if res.ExitCode != expectedExitCode { - t.Fatalf("exitcode expected: %v, actual: %v", expectedExitCode, res.ExitCode) - } -} - func TestDockerScriptCheck(t *testing.T) { if !testutil.DockerIsConnected(t) { return diff --git a/client/driver/executor/executor_linux_test.go b/client/driver/executor/executor_linux_test.go index 751de540a..e60aa5758 100644 --- a/client/driver/executor/executor_linux_test.go +++ b/client/driver/executor/executor_linux_test.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "testing" + "time" "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/driver/env" @@ -136,3 +137,54 @@ ld.so.conf.d/` t.Fatalf("Command output incorrectly: want %v; got %v", expected, act) } } + +func TestExecutor_ClientCleanup(t *testing.T) { + testutil.ExecCompatible(t) + + ctx, allocDir := testExecutorContextWithChroot(t) + ctx.Task.LogConfig.MaxFiles = 1 + ctx.Task.LogConfig.MaxFileSizeMB = 300 + defer allocDir.Destroy() + + executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags)) + + if err := executor.SetContext(ctx); err != nil { + t.Fatalf("Unexpected error") + } + + // Need to run a command which will produce continuous output but not + // too quickly to ensure executor.Exit() stops the process. + execCmd := ExecCommand{Cmd: "/bin/bash", Args: []string{"-c", "while true; do /bin/echo X; /bin/sleep 1; done"}} + execCmd.FSIsolation = true + execCmd.ResourceLimits = true + execCmd.User = "nobody" + + ps, err := executor.LaunchCmd(&execCmd) + if err != nil { + t.Fatalf("error in launching command: %v", err) + } + if ps.Pid == 0 { + t.Fatalf("expected process to start and have non zero pid") + } + time.Sleep(500 * time.Millisecond) + if err := executor.Exit(); err != nil { + t.Fatalf("err: %v", err) + } + + file := filepath.Join(ctx.LogDir, "web.stdout.0") + finfo, err := os.Stat(file) + if err != nil { + t.Fatalf("error stating stdout file: %v", err) + } + if finfo.Size() == 0 { + t.Fatal("Nothing in stdout; expected at least one byte.") + } + time.Sleep(2 * time.Second) + finfo1, err := os.Stat(file) + if err != nil { + t.Fatalf("error stating stdout file: %v", err) + } + if finfo.Size() != finfo1.Size() { + t.Fatalf("Expected size: %v, actual: %v", finfo.Size(), finfo1.Size()) + } +} diff --git a/client/driver/executor/executor_test.go b/client/driver/executor/executor_test.go index d9af1eb0c..ae77f058e 100644 --- a/client/driver/executor/executor_test.go +++ b/client/driver/executor/executor_test.go @@ -14,7 +14,6 @@ import ( "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/driver/env" cstructs "github.com/hashicorp/nomad/client/structs" - "github.com/hashicorp/nomad/client/testutil" "github.com/hashicorp/nomad/nomad/mock" "github.com/hashicorp/nomad/nomad/structs" tu "github.com/hashicorp/nomad/testutil" @@ -187,57 +186,6 @@ func TestExecutor_WaitExitSignal(t *testing.T) { } } -func TestExecutor_ClientCleanup(t *testing.T) { - testutil.ExecCompatible(t) - - ctx, allocDir := testExecutorContextWithChroot(t) - ctx.Task.LogConfig.MaxFiles = 1 - ctx.Task.LogConfig.MaxFileSizeMB = 300 - defer allocDir.Destroy() - - executor := NewExecutor(log.New(os.Stdout, "", log.LstdFlags)) - - if err := executor.SetContext(ctx); err != nil { - t.Fatalf("Unexpected error") - } - - // Need to run a command which will produce continuous output but not - // too quickly to ensure executor.Exit() stops the process. - execCmd := ExecCommand{Cmd: "/bin/bash", Args: []string{"-c", "while true; do /bin/echo X; /bin/sleep 1; done"}} - execCmd.FSIsolation = true - execCmd.ResourceLimits = true - execCmd.User = "nobody" - - ps, err := executor.LaunchCmd(&execCmd) - if err != nil { - t.Fatalf("error in launching command: %v", err) - } - if ps.Pid == 0 { - t.Fatalf("expected process to start and have non zero pid") - } - time.Sleep(500 * time.Millisecond) - if err := executor.Exit(); err != nil { - t.Fatalf("err: %v", err) - } - - file := filepath.Join(ctx.LogDir, "web.stdout.0") - finfo, err := os.Stat(file) - if err != nil { - t.Fatalf("error stating stdout file: %v", err) - } - if finfo.Size() == 0 { - t.Fatal("Nothing in stdout; expected at least one byte.") - } - time.Sleep(2 * time.Second) - finfo1, err := os.Stat(file) - if err != nil { - t.Fatalf("error stating stdout file: %v", err) - } - if finfo.Size() != finfo1.Size() { - t.Fatalf("Expected size: %v, actual: %v", finfo.Size(), finfo1.Size()) - } -} - func TestExecutor_Start_Kill(t *testing.T) { execCmd := ExecCommand{Cmd: "/bin/sleep", Args: []string{"10 && hello world"}} ctx, allocDir := testExecutorContext(t)