e2e: java driver isolation tests

This commit is contained in:
Chris Baker 2021-01-27 14:44:07 +00:00
parent aa55df0413
commit 2adf0f12d6
4 changed files with 164 additions and 1 deletions

View File

@ -13,7 +13,7 @@ job "alloc_exec" {
config {
command = "/bin/sleep"
args = ["30s"]
args = ["30s"]
}
resources {

View File

@ -0,0 +1,39 @@
job "java_sleep" {
datacenters = ["dc1"]
type = "batch"
group "java" {
task "build" {
lifecycle {
hook = "prestart"
sidecar = false
}
driver = "exec"
config {
command = "javac"
args = ["-d", "${NOMAD_ALLOC_DIR}", "local/Sleep.java"]
}
template {
destination = "local/Sleep.java"
data = <<EOH
public class Sleep {
public static void main(String... s) throws Exception {
Thread.sleep(30000);
}
}
EOH
}
}
task "sleep" {
driver = "java"
config {
class_path = "${NOMAD_ALLOC_DIR}"
class = "Sleep"
}
}
}
}

View File

@ -0,0 +1,39 @@
job "java_pid" {
datacenters = ["dc1"]
type = "batch"
group "java" {
task "build" {
lifecycle {
hook = "prestart"
sidecar = false
}
driver = "exec"
config {
command = "javac"
args = ["-d", "${NOMAD_ALLOC_DIR}", "local/Pid.java"]
}
template {
destination = "local/Pid.java"
data = <<EOH
public class Pid {
public static void main(String... s) throws Exception {
System.out.println("my pid is " + ProcessHandle.current().pid());
}
}
EOH
}
}
task "pid" {
driver = "java"
config {
class_path = "${NOMAD_ALLOC_DIR}"
class = "Pid"
}
}
}
}

View File

@ -121,6 +121,91 @@ func (tc *IsolationTest) TestIsolation_ExecDriver_PIDNamespacing_AllocExec(f *fr
require.Len(t, lines, 3)
}
func (tc *IsolationTest) TestIsolation_JavaDriver_PIDNamespacing(f *framework.F) {
t := f.T()
clientNodes, err := e2eutil.ListLinuxClientNodes(tc.Nomad())
require.Nil(t, err)
if len(clientNodes) == 0 {
t.Skip("no Linux clients")
}
uuid := uuid.Generate()
jobID := "isolation-pid-namespace-" + uuid[0:8]
file := "isolation/input/java.nomad"
allocs := e2eutil.RegisterAndWaitForAllocs(t, tc.Nomad(), file, jobID, "")
require.Equal(t, len(allocs), 1, fmt.Sprintf("failed to register %s", jobID))
tc.jobIDs = append(tc.jobIDs, jobID)
defer func() {
tc.Nomad().Jobs().Deregister(jobID, true, nil)
}()
allocID := allocs[0].ID
e2eutil.WaitForAllocStopped(t, tc.Nomad(), allocID)
out, err := e2eutil.AllocTaskLogs(allocID, "pid", e2eutil.LogsStdOut)
require.NoError(t, err, fmt.Sprintf("could not get logs for alloc %s", allocID))
require.Contains(t, out, "my pid is 1\n")
}
func (tc *IsolationTest) TestIsolation_JavaDriver_PIDNamespacing_AllocExec(f *framework.F) {
t := f.T()
clientNodes, err := e2eutil.ListLinuxClientNodes(tc.Nomad())
require.Nil(t, err)
if len(clientNodes) == 0 {
t.Skip("no Linux clients")
}
uuid := uuid.Generate()
jobID := "isolation-pid-namespace-" + uuid[0:8]
file := "isolation/input/alloc_exec_java.nomad"
allocs := e2eutil.RegisterAndWaitForAllocs(t, tc.Nomad(), file, jobID, "")
require.Equal(t, len(allocs), 1, fmt.Sprintf("failed to register %s", jobID))
defer func() {
tc.Nomad().Jobs().Deregister(jobID, true, nil)
}()
allocID := allocs[0].ID
e2eutil.WaitForAllocTaskRunning(t, tc.Nomad(), allocID, "sleep")
alloc, _, err := tc.Nomad().Allocations().Info(allocID, nil)
require.NoError(t, err)
require.NotNil(t, alloc)
resizeCh := make(chan api.TerminalSize)
var tty bool
ctx, cancelFn := context.WithTimeout(context.Background(), 15*time.Second)
defer cancelFn()
var stdout, stderr bytes.Buffer
exitCode, err := tc.Nomad().Allocations().Exec(
ctx,
alloc,
"sleep",
tty,
[]string{"ps", "ax"},
bytes.NewReader([]byte("")),
&stdout,
&stderr,
resizeCh,
nil,
)
require.NoError(t, err)
require.Equal(t, 0, exitCode)
lines := strings.Split(strings.TrimSpace(stdout.String()), "\n")
// header, sleep process, ps ax process are the only output lines expected
require.Len(t, lines, 3)
}
func (tc *IsolationTest) TestIsolation_RawExecDriver_NoPIDNamespacing(f *framework.F) {
t := f.T()