2018-02-05 21:05:59 +00:00
|
|
|
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
|
2015-06-17 02:36:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-06-05 08:50:15 +00:00
|
|
|
|
2017-07-17 08:36:46 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2015-06-17 02:36:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadProcBool(t *testing.T) {
|
2021-08-24 10:10:50 +00:00
|
|
|
tmpDir, err := os.MkdirTemp("", "test-sysinfo-proc")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2015-06-17 02:36:20 +00:00
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
procFile := filepath.Join(tmpDir, "read-proc-bool")
|
2023-08-24 15:58:12 +00:00
|
|
|
err = os.WriteFile(procFile, []byte("1"), 0o644)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2015-06-17 02:36:20 +00:00
|
|
|
|
|
|
|
if !readProcBool(procFile) {
|
|
|
|
t.Fatal("expected proc bool to be true, got false")
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:58:12 +00:00
|
|
|
if err := os.WriteFile(procFile, []byte("0"), 0o644); err != nil {
|
2015-06-17 02:36:20 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if readProcBool(procFile) {
|
2017-06-02 00:13:06 +00:00
|
|
|
t.Fatal("expected proc bool to be false, got true")
|
2015-06-17 02:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if readProcBool(path.Join(tmpDir, "no-exist")) {
|
|
|
|
t.Fatal("should be false for non-existent entry")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCgroupEnabled(t *testing.T) {
|
2021-08-24 10:10:50 +00:00
|
|
|
cgroupDir, err := os.MkdirTemp("", "cgroup-test")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2015-06-17 02:36:20 +00:00
|
|
|
defer os.RemoveAll(cgroupDir)
|
|
|
|
|
|
|
|
if cgroupEnabled(cgroupDir, "test") {
|
|
|
|
t.Fatal("cgroupEnabled should be false")
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:58:12 +00:00
|
|
|
err = os.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0o644)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2015-06-17 02:36:20 +00:00
|
|
|
|
|
|
|
if !cgroupEnabled(cgroupDir, "test") {
|
|
|
|
t.Fatal("cgroupEnabled should be true")
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 08:50:15 +00:00
|
|
|
|
|
|
|
func TestNew(t *testing.T) {
|
2021-07-14 14:45:02 +00:00
|
|
|
sysInfo := New()
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, sysInfo != nil)
|
2017-06-05 08:50:15 +00:00
|
|
|
checkSysInfo(t, sysInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSysInfo(t *testing.T, sysInfo *SysInfo) {
|
|
|
|
// Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE
|
2017-07-17 08:36:46 +00:00
|
|
|
if err := unix.Prctl(unix.PR_GET_SECCOMP, 0, 0, 0, 0); err != unix.EINVAL {
|
2017-06-05 08:50:15 +00:00
|
|
|
// Make sure the kernel has CONFIG_SECCOMP_FILTER.
|
2017-07-17 08:36:46 +00:00
|
|
|
if err := unix.Prctl(unix.PR_SET_SECCOMP, unix.SECCOMP_MODE_FILTER, 0, 0, 0); err != unix.EINVAL {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, sysInfo.Seccomp)
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, !sysInfo.Seccomp)
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAppArmorEnabled(t *testing.T) {
|
|
|
|
// Check if AppArmor is supported. then it must be TRUE , else FALSE
|
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); err != nil {
|
2021-07-14 15:04:12 +00:00
|
|
|
t.Skip("AppArmor Must be Enabled")
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:45:02 +00:00
|
|
|
sysInfo := New()
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, sysInfo.AppArmor)
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAppArmorDisabled(t *testing.T) {
|
|
|
|
// Check if AppArmor is supported. then it must be TRUE , else FALSE
|
|
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
2021-07-14 15:04:12 +00:00
|
|
|
t.Skip("AppArmor Must be Disabled")
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:45:02 +00:00
|
|
|
sysInfo := New()
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Assert(t, !sysInfo.AppArmor)
|
2017-06-05 08:50:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 23:07:19 +00:00
|
|
|
func TestNewCgroupNamespacesEnabled(t *testing.T) {
|
|
|
|
// If cgroup namespaces are supported in the kernel, then sysInfo.CgroupNamespaces should be TRUE
|
|
|
|
if _, err := os.Stat("/proc/self/ns/cgroup"); err != nil {
|
|
|
|
t.Skip("cgroup namespaces must be enabled")
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:45:02 +00:00
|
|
|
sysInfo := New()
|
2018-12-14 23:07:19 +00:00
|
|
|
assert.Assert(t, sysInfo.CgroupNamespaces)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewCgroupNamespacesDisabled(t *testing.T) {
|
|
|
|
// If cgroup namespaces are *not* supported in the kernel, then sysInfo.CgroupNamespaces should be FALSE
|
|
|
|
if _, err := os.Stat("/proc/self/ns/cgroup"); !os.IsNotExist(err) {
|
|
|
|
t.Skip("cgroup namespaces must be disabled")
|
|
|
|
}
|
|
|
|
|
2021-07-14 14:45:02 +00:00
|
|
|
sysInfo := New()
|
2018-12-14 23:07:19 +00:00
|
|
|
assert.Assert(t, !sysInfo.CgroupNamespaces)
|
|
|
|
}
|
|
|
|
|
2017-06-05 08:50:15 +00:00
|
|
|
func TestNumCPU(t *testing.T) {
|
|
|
|
cpuNumbers := NumCPU()
|
|
|
|
if cpuNumbers <= 0 {
|
|
|
|
t.Fatal("CPU returned must be greater than zero")
|
|
|
|
}
|
|
|
|
}
|