mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
- An opt-in feature has been added to the agent to allow deprioritizing non coder-related processes for CPU by setting their niceness level to 10. - Opting in to the feature requires setting CODER_PROC_PRIO_MGMT to a non-empty value.
50 lines
1023 B
Go
50 lines
1023 B
Go
package agentproctest
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/spf13/afero"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/agent/agentproc"
|
|
"github.com/coder/coder/v2/cryptorand"
|
|
)
|
|
|
|
func GenerateProcess(t *testing.T, fs afero.Fs, muts ...func(*agentproc.Process)) agentproc.Process {
|
|
t.Helper()
|
|
|
|
pid, err := cryptorand.Intn(1<<31 - 1)
|
|
require.NoError(t, err)
|
|
|
|
arg1, err := cryptorand.String(5)
|
|
require.NoError(t, err)
|
|
|
|
arg2, err := cryptorand.String(5)
|
|
require.NoError(t, err)
|
|
|
|
arg3, err := cryptorand.String(5)
|
|
require.NoError(t, err)
|
|
|
|
cmdline := fmt.Sprintf("%s\x00%s\x00%s", arg1, arg2, arg3)
|
|
|
|
process := agentproc.Process{
|
|
CmdLine: cmdline,
|
|
PID: int32(pid),
|
|
}
|
|
|
|
for _, mut := range muts {
|
|
mut(&process)
|
|
}
|
|
|
|
process.Dir = fmt.Sprintf("%s/%d", "/proc", process.PID)
|
|
|
|
err = fs.MkdirAll(process.Dir, 0o555)
|
|
require.NoError(t, err)
|
|
|
|
err = afero.WriteFile(fs, fmt.Sprintf("%s/cmdline", process.Dir), []byte(process.CmdLine), 0o444)
|
|
require.NoError(t, err)
|
|
|
|
return process
|
|
}
|