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