sysinfo_linux_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package sysinfo
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "syscall"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestReadProcBool(t *testing.T) {
  12. tmpDir, err := ioutil.TempDir("", "test-sysinfo-proc")
  13. require.NoError(t, err)
  14. defer os.RemoveAll(tmpDir)
  15. procFile := filepath.Join(tmpDir, "read-proc-bool")
  16. err = ioutil.WriteFile(procFile, []byte("1"), 0644)
  17. require.NoError(t, err)
  18. if !readProcBool(procFile) {
  19. t.Fatal("expected proc bool to be true, got false")
  20. }
  21. if err := ioutil.WriteFile(procFile, []byte("0"), 0644); err != nil {
  22. t.Fatal(err)
  23. }
  24. if readProcBool(procFile) {
  25. t.Fatal("expected proc bool to be false, got true")
  26. }
  27. if readProcBool(path.Join(tmpDir, "no-exist")) {
  28. t.Fatal("should be false for non-existent entry")
  29. }
  30. }
  31. func TestCgroupEnabled(t *testing.T) {
  32. cgroupDir, err := ioutil.TempDir("", "cgroup-test")
  33. require.NoError(t, err)
  34. defer os.RemoveAll(cgroupDir)
  35. if cgroupEnabled(cgroupDir, "test") {
  36. t.Fatal("cgroupEnabled should be false")
  37. }
  38. err = ioutil.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0644)
  39. require.NoError(t, err)
  40. if !cgroupEnabled(cgroupDir, "test") {
  41. t.Fatal("cgroupEnabled should be true")
  42. }
  43. }
  44. func TestNew(t *testing.T) {
  45. sysInfo := New(false)
  46. require.NotNil(t, sysInfo)
  47. checkSysInfo(t, sysInfo)
  48. sysInfo = New(true)
  49. require.NotNil(t, sysInfo)
  50. checkSysInfo(t, sysInfo)
  51. }
  52. func checkSysInfo(t *testing.T, sysInfo *SysInfo) {
  53. // Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE
  54. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_GET_SECCOMP, 0, 0); err != syscall.EINVAL {
  55. // Make sure the kernel has CONFIG_SECCOMP_FILTER.
  56. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_SECCOMP, SeccompModeFilter, 0); err != syscall.EINVAL {
  57. require.True(t, sysInfo.Seccomp)
  58. }
  59. } else {
  60. require.False(t, sysInfo.Seccomp)
  61. }
  62. }
  63. func TestNewAppArmorEnabled(t *testing.T) {
  64. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  65. if _, err := os.Stat("/sys/kernel/security/apparmor"); err != nil {
  66. t.Skip("App Armor Must be Enabled")
  67. }
  68. sysInfo := New(true)
  69. require.True(t, sysInfo.AppArmor)
  70. }
  71. func TestNewAppArmorDisabled(t *testing.T) {
  72. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  73. if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
  74. t.Skip("App Armor Must be Disabled")
  75. }
  76. sysInfo := New(true)
  77. require.False(t, sysInfo.AppArmor)
  78. }
  79. func TestNumCPU(t *testing.T) {
  80. cpuNumbers := NumCPU()
  81. if cpuNumbers <= 0 {
  82. t.Fatal("CPU returned must be greater than zero")
  83. }
  84. }