sysinfo_linux_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package sysinfo
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "testing"
  8. )
  9. func TestReadProcBool(t *testing.T) {
  10. tmpDir, err := ioutil.TempDir("", "test-sysinfo-proc")
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. defer os.RemoveAll(tmpDir)
  15. procFile := filepath.Join(tmpDir, "read-proc-bool")
  16. if err := ioutil.WriteFile(procFile, []byte("1"), 0644); err != nil {
  17. t.Fatal(err)
  18. }
  19. if !readProcBool(procFile) {
  20. t.Fatal("expected proc bool to be true, got false")
  21. }
  22. if err := ioutil.WriteFile(procFile, []byte("0"), 0644); err != nil {
  23. t.Fatal(err)
  24. }
  25. if readProcBool(procFile) {
  26. t.Fatal("expected proc bool to be false, got false")
  27. }
  28. if readProcBool(path.Join(tmpDir, "no-exist")) {
  29. t.Fatal("should be false for non-existent entry")
  30. }
  31. }
  32. func TestCgroupEnabled(t *testing.T) {
  33. cgroupDir, err := ioutil.TempDir("", "cgroup-test")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. defer os.RemoveAll(cgroupDir)
  38. if cgroupEnabled(cgroupDir, "test") {
  39. t.Fatal("cgroupEnabled should be false")
  40. }
  41. if err := ioutil.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0644); err != nil {
  42. t.Fatal(err)
  43. }
  44. if !cgroupEnabled(cgroupDir, "test") {
  45. t.Fatal("cgroupEnabled should be true")
  46. }
  47. }