sysinfo_linux_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path"
  6. "path/filepath"
  7. "testing"
  8. "golang.org/x/sys/unix"
  9. "gotest.tools/assert"
  10. )
  11. func TestReadProcBool(t *testing.T) {
  12. tmpDir, err := ioutil.TempDir("", "test-sysinfo-proc")
  13. assert.NilError(t, err)
  14. defer os.RemoveAll(tmpDir)
  15. procFile := filepath.Join(tmpDir, "read-proc-bool")
  16. err = ioutil.WriteFile(procFile, []byte("1"), 0644)
  17. assert.NilError(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. assert.NilError(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. assert.NilError(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. assert.Assert(t, sysInfo != nil)
  47. checkSysInfo(t, sysInfo)
  48. sysInfo = New(true)
  49. assert.Assert(t, sysInfo != nil)
  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 := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL {
  55. // Make sure the kernel has CONFIG_SECCOMP_FILTER.
  56. if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL {
  57. assert.Assert(t, sysInfo.Seccomp)
  58. }
  59. } else {
  60. assert.Assert(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. assert.Assert(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. assert.Assert(t, !sysInfo.AppArmor)
  78. }
  79. func TestNewCgroupNamespacesEnabled(t *testing.T) {
  80. // If cgroup namespaces are supported in the kernel, then sysInfo.CgroupNamespaces should be TRUE
  81. if _, err := os.Stat("/proc/self/ns/cgroup"); err != nil {
  82. t.Skip("cgroup namespaces must be enabled")
  83. }
  84. sysInfo := New(true)
  85. assert.Assert(t, sysInfo.CgroupNamespaces)
  86. }
  87. func TestNewCgroupNamespacesDisabled(t *testing.T) {
  88. // If cgroup namespaces are *not* supported in the kernel, then sysInfo.CgroupNamespaces should be FALSE
  89. if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
  90. t.Skip("cgroup namespaces must be disabled")
  91. }
  92. sysInfo := New(true)
  93. assert.Assert(t, !sysInfo.CgroupNamespaces)
  94. }
  95. func TestNumCPU(t *testing.T) {
  96. cpuNumbers := NumCPU()
  97. if cpuNumbers <= 0 {
  98. t.Fatal("CPU returned must be greater than zero")
  99. }
  100. }