You've already forked runc
mirror of
https://github.com/opencontainers/runc.git
synced 2025-07-30 17:43:06 +03:00
split fs2 package from fs, as mixing up fs and fs2 is very likely to result in unmaintainable code. Inspired by containerd/cgroups#109 Fix #2157 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
98 lines
2.0 KiB
Go
98 lines
2.0 KiB
Go
// +build linux
|
|
|
|
package fscommon
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
cgroupFile = "cgroup.file"
|
|
floatValue = 2048.0
|
|
floatString = "2048"
|
|
)
|
|
|
|
func TestGetCgroupParamsInt(t *testing.T) {
|
|
// Setup tempdir.
|
|
tempDir, err := ioutil.TempDir("", "cgroup_utils_test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
tempFile := filepath.Join(tempDir, cgroupFile)
|
|
|
|
// Success.
|
|
err = ioutil.WriteFile(tempFile, []byte(floatString), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err := GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != floatValue {
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
|
}
|
|
|
|
// Success with new line.
|
|
err = ioutil.WriteFile(tempFile, []byte(floatString+"\n"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != floatValue {
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
|
}
|
|
|
|
// Success with negative values
|
|
err = ioutil.WriteFile(tempFile, []byte("-12345"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != 0 {
|
|
t.Fatalf("Expected %d to equal %d", value, 0)
|
|
}
|
|
|
|
// Success with negative values lesser than min int64
|
|
s := strconv.FormatFloat(math.MinInt64, 'f', -1, 64)
|
|
err = ioutil.WriteFile(tempFile, []byte(s), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
} else if value != 0 {
|
|
t.Fatalf("Expected %d to equal %d", value, 0)
|
|
}
|
|
|
|
// Not a float.
|
|
err = ioutil.WriteFile(tempFile, []byte("not-a-float"), 0755)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err == nil {
|
|
t.Fatal("Expecting error, got none")
|
|
}
|
|
|
|
// Unknown file.
|
|
err = os.Remove(tempFile)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = GetCgroupParamUint(tempDir, cgroupFile)
|
|
if err == nil {
|
|
t.Fatal("Expecting error, got none")
|
|
}
|
|
}
|