sysinfo_linux_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
  2. import (
  3. "os"
  4. "path"
  5. "path/filepath"
  6. "testing"
  7. "golang.org/x/sys/unix"
  8. "gotest.tools/v3/assert"
  9. )
  10. func TestReadProcBool(t *testing.T) {
  11. tmpDir, err := os.MkdirTemp("", "test-sysinfo-proc")
  12. assert.NilError(t, err)
  13. defer os.RemoveAll(tmpDir)
  14. procFile := filepath.Join(tmpDir, "read-proc-bool")
  15. err = os.WriteFile(procFile, []byte("1"), 0o644)
  16. assert.NilError(t, err)
  17. if !readProcBool(procFile) {
  18. t.Fatal("expected proc bool to be true, got false")
  19. }
  20. if err := os.WriteFile(procFile, []byte("0"), 0o644); err != nil {
  21. t.Fatal(err)
  22. }
  23. if readProcBool(procFile) {
  24. t.Fatal("expected proc bool to be false, got true")
  25. }
  26. if readProcBool(path.Join(tmpDir, "no-exist")) {
  27. t.Fatal("should be false for non-existent entry")
  28. }
  29. }
  30. func TestCgroupEnabled(t *testing.T) {
  31. cgroupDir, err := os.MkdirTemp("", "cgroup-test")
  32. assert.NilError(t, err)
  33. defer os.RemoveAll(cgroupDir)
  34. if cgroupEnabled(cgroupDir, "test") {
  35. t.Fatal("cgroupEnabled should be false")
  36. }
  37. err = os.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0o644)
  38. assert.NilError(t, err)
  39. if !cgroupEnabled(cgroupDir, "test") {
  40. t.Fatal("cgroupEnabled should be true")
  41. }
  42. }
  43. func TestNew(t *testing.T) {
  44. sysInfo := New()
  45. assert.Assert(t, sysInfo != nil)
  46. checkSysInfo(t, sysInfo)
  47. }
  48. func checkSysInfo(t *testing.T, sysInfo *SysInfo) {
  49. // Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE
  50. if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL {
  51. // Make sure the kernel has CONFIG_SECCOMP_FILTER.
  52. if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL {
  53. assert.Assert(t, sysInfo.Seccomp)
  54. }
  55. } else {
  56. assert.Assert(t, !sysInfo.Seccomp)
  57. }
  58. }
  59. func TestNewAppArmorEnabled(t *testing.T) {
  60. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  61. if _, err := os.Stat("/sys/kernel/security/apparmor"); err != nil {
  62. t.Skip("AppArmor Must be Enabled")
  63. }
  64. sysInfo := New()
  65. assert.Assert(t, sysInfo.AppArmor)
  66. }
  67. func TestNewAppArmorDisabled(t *testing.T) {
  68. // Check if AppArmor is supported. then it must be TRUE , else FALSE
  69. if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
  70. t.Skip("AppArmor Must be Disabled")
  71. }
  72. sysInfo := New()
  73. assert.Assert(t, !sysInfo.AppArmor)
  74. }
  75. func TestNewCgroupNamespacesEnabled(t *testing.T) {
  76. // If cgroup namespaces are supported in the kernel, then sysInfo.CgroupNamespaces should be TRUE
  77. if _, err := os.Stat("/proc/self/ns/cgroup"); err != nil {
  78. t.Skip("cgroup namespaces must be enabled")
  79. }
  80. sysInfo := New()
  81. assert.Assert(t, sysInfo.CgroupNamespaces)
  82. }
  83. func TestNewCgroupNamespacesDisabled(t *testing.T) {
  84. // If cgroup namespaces are *not* supported in the kernel, then sysInfo.CgroupNamespaces should be FALSE
  85. if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
  86. t.Skip("cgroup namespaces must be disabled")
  87. }
  88. sysInfo := New()
  89. assert.Assert(t, !sysInfo.CgroupNamespaces)
  90. }
  91. func TestNumCPU(t *testing.T) {
  92. cpuNumbers := NumCPU()
  93. if cpuNumbers <= 0 {
  94. t.Fatal("CPU returned must be greater than zero")
  95. }
  96. }