2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2014-09-24 08:13:57 +08:00
|
|
|
"math"
|
2014-05-15 06:21:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-09-24 08:13:57 +08:00
|
|
|
"strconv"
|
2014-05-15 06:21:44 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cgroupFile = "cgroup.file"
|
|
|
|
floatValue = 2048.0
|
|
|
|
floatString = "2048"
|
|
|
|
)
|
|
|
|
|
2014-05-23 04:50:51 +08:00
|
|
|
func TestGetCgroupParamsInt(t *testing.T) {
|
2014-05-15 06:21:44 +08:00
|
|
|
// 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)
|
|
|
|
}
|
2014-09-24 08:13:57 +08:00
|
|
|
value, err := getCgroupParamUint(tempDir, cgroupFile)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if value != floatValue {
|
2014-06-12 13:52:31 +08:00
|
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Success with new line.
|
|
|
|
err = ioutil.WriteFile(tempFile, []byte(floatString+"\n"), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 08:13:57 +08:00
|
|
|
value, err = getCgroupParamUint(tempDir, cgroupFile)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if value != floatValue {
|
2014-06-12 13:52:31 +08:00
|
|
|
t.Fatalf("Expected %d to equal %f", value, floatValue)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-09-24 08:13:57 +08:00
|
|
|
// 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 %f", 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 %f", value, 0)
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
// Not a float.
|
|
|
|
err = ioutil.WriteFile(tempFile, []byte("not-a-float"), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 08:13:57 +08:00
|
|
|
_, err = getCgroupParamUint(tempDir, cgroupFile)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expecting error, got none")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown file.
|
|
|
|
err = os.Remove(tempFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-24 08:13:57 +08:00
|
|
|
_, err = getCgroupParamUint(tempDir, cgroupFile)
|
2014-05-15 06:21:44 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expecting error, got none")
|
|
|
|
}
|
|
|
|
}
|