Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raw_exec: don't use cgroups when no_cgroup is set #9328

Merged
merged 3 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions drivers/rawexec/driver_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,62 @@ func TestRawExec_ExecTaskStreaming(t *testing.T) {
dtestutil.ExecTaskStreamingConformanceTests(t, harness, task.ID)

}

func TestRawExecDriver_NoCgroup(t *testing.T) {
t.Parallel()
if runtime.GOOS != "linux" {
t.Skip("Linux only test")
}

expectedBytes, err := ioutil.ReadFile("/proc/self/cgroup")
require.NoError(t, err)
expected := strings.TrimSpace(string(expectedBytes))

d := newEnabledRawExecDriver(t)
d.config.NoCgroups = true
harness := dtestutil.NewDriverHarness(t, d)

task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "nocgroup",
}

cleanup := harness.MkAllocDir(task, true)
defer cleanup()

tc := &TaskConfig{
Command: "/bin/cat",
Args: []string{"/proc/self/cgroup"},
}
require.NoError(t, task.EncodeConcreteDriverConfig(&tc))
testtask.SetTaskConfigEnv(task)

_, _, err = harness.StartTask(task)
require.NoError(t, err)

// Task should terminate quickly
waitCh, err := harness.WaitTask(context.Background(), task.ID)
require.NoError(t, err)
select {
case res := <-waitCh:
require.True(t, res.Successful())
require.Zero(t, res.ExitCode)
case <-time.After(time.Duration(testutil.TestMultiplier()*6) * time.Second):
require.Fail(t, "WaitTask timeout")
}

// Check the log file to see it exited because of the signal
outputFile := filepath.Join(task.TaskDir().LogDir, "nocgroup.stdout.0")
testutil.WaitForResult(func() (bool, error) {
act, err := ioutil.ReadFile(outputFile)
if err != nil {
return false, fmt.Errorf("Couldn't read expected output: %v", err)
}

if strings.TrimSpace(string(act)) != expected {
t.Logf("Read from %v", outputFile)
return false, fmt.Errorf("Command outputted\n%v; want\n%v", string(act), expected)
}
return true, nil
}, func(err error) { require.NoError(t, err) })
}
6 changes: 4 additions & 2 deletions drivers/shared/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,10 @@ func (e *UniversalExecutor) Launch(command *ExecCommand) (*ProcessState, error)
}

// Setup cgroups on linux
if err := e.configureResourceContainer(os.Getpid()); err != nil {
return nil, err
if e.commandCfg.ResourceLimits || e.commandCfg.BasicProcessCgroup {
if err := e.configureResourceContainer(os.Getpid()); err != nil {
return nil, err
}
}

stdout, err := e.commandCfg.Stdout()
Expand Down
43 changes: 43 additions & 0 deletions drivers/shared/executor/executor_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,46 @@ func TestExecutor_cmdMounts(t *testing.T) {

require.EqualValues(t, expected, cmdMounts(input))
}

// TestUniversalExecutor_NoCgroup asserts that commands are executed in the
// same cgroup as parent process
func TestUniversalExecutor_NoCgroup(t *testing.T) {
t.Parallel()
testutil.ExecCompatible(t)

expectedBytes, err := ioutil.ReadFile("/proc/self/cgroup")
require.NoError(t, err)

expected := strings.TrimSpace(string(expectedBytes))

testExecCmd := testExecutorCommand(t)
execCmd, allocDir := testExecCmd.command, testExecCmd.allocDir
execCmd.Cmd = "/bin/cat"
execCmd.Args = []string{"/proc/self/cgroup"}
defer allocDir.Destroy()

execCmd.BasicProcessCgroup = false
execCmd.ResourceLimits = false

executor := NewExecutor(testlog.HCLogger(t))
defer executor.Shutdown("SIGKILL", 0)

_, err = executor.Launch(execCmd)
require.NoError(t, err)

_, err = executor.Wait(context.Background())
require.NoError(t, err)

tu.WaitForResult(func() (bool, error) {
act := strings.TrimSpace(string(testExecCmd.stdout.String()))
if expected != act {
return false, fmt.Errorf("expected:\n%s actual:\n%s", expected, act)
}
return true, nil
}, func(err error) {
stderr := strings.TrimSpace(string(testExecCmd.stderr.String()))
t.Logf("stderr: %v", stderr)
require.NoError(t, err)
})

}