sys: add boundary checks to SetOOMScore()
Produce a more user-friendly error in the odd case we would try to set a score out of range Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -18,6 +18,7 @@ package sys
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
@@ -57,6 +58,28 @@ func TestSetNegativeOomScoreAdjustmentWhenUnprivilegedHasNoEffect(t *testing.T)
|
||||
assert.Check(t, is.Equal(adjustment, initial))
|
||||
}
|
||||
|
||||
func TestSetOOMScoreBoundaries(t *testing.T) {
|
||||
err := SetOOMScore(0, OOMScoreAdjMax+1)
|
||||
assert.ErrorContains(t, err, fmt.Sprintf("value out of range (%d): OOM score must be between", OOMScoreAdjMax+1))
|
||||
|
||||
err = SetOOMScore(0, OOMScoreAdjMin-1)
|
||||
assert.ErrorContains(t, err, fmt.Sprintf("value out of range (%d): OOM score must be between", OOMScoreAdjMin-1))
|
||||
|
||||
_, adjustment, err := adjustOom(OOMScoreAdjMax)
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(adjustment, OOMScoreAdjMax))
|
||||
|
||||
score, err := GetOOMScoreAdj(os.Getpid())
|
||||
assert.NilError(t, err)
|
||||
if score == 0 || score == OOMScoreAdjMin {
|
||||
// we won't be able to set the score lower than the parent process,
|
||||
// so only test if parent process does not have a oom-score-adj
|
||||
_, adjustment, err = adjustOom(OOMScoreAdjMin)
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Equal(adjustment, OOMScoreAdjMin))
|
||||
}
|
||||
}
|
||||
|
||||
func adjustOom(adjustment int) (int, int, error) {
|
||||
cmd := exec.Command("sleep", "100")
|
||||
if err := cmd.Start(); err != nil {
|
||||
|
Reference in New Issue
Block a user