sysinfo_linux_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/v3/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()
  46. assert.Assert(t, sysInfo != nil)
  47. checkSysInfo(t, sysInfo)
  48. }
  49. func checkSysInfo(t *testing.T, sysInfo *SysInfo) {
  50. // Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE
  51. if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL {
  52. // Make sure the kernel has CONFIG_SECCOMP_FILTER.
  53. if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL {
  54. assert.Assert(t, sysInfo.Seccomp)
  55. }
  56. } else {
  57. assert.Assert(t, !sysInfo.Seccomp)
  58. }
  59. }
  60. func TestNewAppArmorEnabled(t *testing.T) {
  61. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  62. if _, err := os.Stat("/sys/kernel/security/apparmor"); err != nil {
  63. t.Skip("AppArmor Must be Enabled")
  64. }
  65. sysInfo := New()
  66. assert.Assert(t, sysInfo.AppArmor)
  67. }
  68. func TestNewAppArmorDisabled(t *testing.T) {
  69. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  70. if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
  71. t.Skip("AppArmor Must be Disabled")
  72. }
  73. sysInfo := New()
  74. assert.Assert(t, !sysInfo.AppArmor)
  75. }
  76. func TestNewCgroupNamespacesEnabled(t *testing.T) {
  77. // If cgroup namespaces are supported in the kernel, then sysInfo.CgroupNamespaces should be TRUE
  78. if _, err := os.Stat("/proc/self/ns/cgroup"); err != nil {
  79. t.Skip("cgroup namespaces must be enabled")
  80. }
  81. sysInfo := New()
  82. assert.Assert(t, sysInfo.CgroupNamespaces)
  83. }
  84. func TestNewCgroupNamespacesDisabled(t *testing.T) {
  85. // If cgroup namespaces are *not* supported in the kernel, then sysInfo.CgroupNamespaces should be FALSE
  86. if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
  87. t.Skip("cgroup namespaces must be disabled")
  88. }
  89. sysInfo := New()
  90. assert.Assert(t, !sysInfo.CgroupNamespaces)
  91. }
  92. func TestNumCPU(t *testing.T) {
  93. cpuNumbers := NumCPU()
  94. if cpuNumbers <= 0 {
  95. t.Fatal("CPU returned must be greater than zero")
  96. }
  97. }