Signed-off-by: Naveed Jamil <naveed.jamil@tenpearls.com>
| ... | ... |
@@ -5,20 +5,20 @@ import ( |
| 5 | 5 |
"os" |
| 6 | 6 |
"path" |
| 7 | 7 |
"path/filepath" |
| 8 |
+ "syscall" |
|
| 8 | 9 |
"testing" |
| 10 |
+ |
|
| 11 |
+ "github.com/stretchr/testify/require" |
|
| 9 | 12 |
) |
| 10 | 13 |
|
| 11 | 14 |
func TestReadProcBool(t *testing.T) {
|
| 12 | 15 |
tmpDir, err := ioutil.TempDir("", "test-sysinfo-proc")
|
| 13 |
- if err != nil {
|
|
| 14 |
- t.Fatal(err) |
|
| 15 |
- } |
|
| 16 |
+ require.NoError(t, err) |
|
| 16 | 17 |
defer os.RemoveAll(tmpDir) |
| 17 | 18 |
|
| 18 | 19 |
procFile := filepath.Join(tmpDir, "read-proc-bool") |
| 19 |
- if err := ioutil.WriteFile(procFile, []byte("1"), 0644); err != nil {
|
|
| 20 |
- t.Fatal(err) |
|
| 21 |
- } |
|
| 20 |
+ err = ioutil.WriteFile(procFile, []byte("1"), 0644)
|
|
| 21 |
+ require.NoError(t, err) |
|
| 22 | 22 |
|
| 23 | 23 |
if !readProcBool(procFile) {
|
| 24 | 24 |
t.Fatal("expected proc bool to be true, got false")
|
| ... | ... |
@@ -39,20 +39,66 @@ func TestReadProcBool(t *testing.T) {
|
| 39 | 39 |
|
| 40 | 40 |
func TestCgroupEnabled(t *testing.T) {
|
| 41 | 41 |
cgroupDir, err := ioutil.TempDir("", "cgroup-test")
|
| 42 |
- if err != nil {
|
|
| 43 |
- t.Fatal(err) |
|
| 44 |
- } |
|
| 42 |
+ require.NoError(t, err) |
|
| 45 | 43 |
defer os.RemoveAll(cgroupDir) |
| 46 | 44 |
|
| 47 | 45 |
if cgroupEnabled(cgroupDir, "test") {
|
| 48 | 46 |
t.Fatal("cgroupEnabled should be false")
|
| 49 | 47 |
} |
| 50 | 48 |
|
| 51 |
- if err := ioutil.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0644); err != nil {
|
|
| 52 |
- t.Fatal(err) |
|
| 53 |
- } |
|
| 49 |
+ err = ioutil.WriteFile(path.Join(cgroupDir, "test"), []byte{}, 0644)
|
|
| 50 |
+ require.NoError(t, err) |
|
| 54 | 51 |
|
| 55 | 52 |
if !cgroupEnabled(cgroupDir, "test") {
|
| 56 | 53 |
t.Fatal("cgroupEnabled should be true")
|
| 57 | 54 |
} |
| 58 | 55 |
} |
| 56 |
+ |
|
| 57 |
+func TestNew(t *testing.T) {
|
|
| 58 |
+ sysInfo := New(false) |
|
| 59 |
+ require.NotNil(t, sysInfo) |
|
| 60 |
+ checkSysInfo(t, sysInfo) |
|
| 61 |
+ |
|
| 62 |
+ sysInfo = New(true) |
|
| 63 |
+ require.NotNil(t, sysInfo) |
|
| 64 |
+ checkSysInfo(t, sysInfo) |
|
| 65 |
+} |
|
| 66 |
+ |
|
| 67 |
+func checkSysInfo(t *testing.T, sysInfo *SysInfo) {
|
|
| 68 |
+ // Check if Seccomp is supported, via CONFIG_SECCOMP.then sysInfo.Seccomp must be TRUE , else FALSE |
|
| 69 |
+ if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_GET_SECCOMP, 0, 0); err != syscall.EINVAL {
|
|
| 70 |
+ // Make sure the kernel has CONFIG_SECCOMP_FILTER. |
|
| 71 |
+ if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_SECCOMP, SeccompModeFilter, 0); err != syscall.EINVAL {
|
|
| 72 |
+ require.True(t, sysInfo.Seccomp) |
|
| 73 |
+ } |
|
| 74 |
+ } else {
|
|
| 75 |
+ require.False(t, sysInfo.Seccomp) |
|
| 76 |
+ } |
|
| 77 |
+} |
|
| 78 |
+ |
|
| 79 |
+func TestNewAppArmorEnabled(t *testing.T) {
|
|
| 80 |
+ // Check if AppArmor is supported. then it must be TRUE , else FALSE |
|
| 81 |
+ if _, err := os.Stat("/sys/kernel/security/apparmor"); err != nil {
|
|
| 82 |
+ t.Skip("App Armor Must be Enabled")
|
|
| 83 |
+ } |
|
| 84 |
+ |
|
| 85 |
+ sysInfo := New(true) |
|
| 86 |
+ require.True(t, sysInfo.AppArmor) |
|
| 87 |
+} |
|
| 88 |
+ |
|
| 89 |
+func TestNewAppArmorDisabled(t *testing.T) {
|
|
| 90 |
+ // Check if AppArmor is supported. then it must be TRUE , else FALSE |
|
| 91 |
+ if _, err := os.Stat("/sys/kernel/security/apparmor"); !os.IsNotExist(err) {
|
|
| 92 |
+ t.Skip("App Armor Must be Disabled")
|
|
| 93 |
+ } |
|
| 94 |
+ |
|
| 95 |
+ sysInfo := New(true) |
|
| 96 |
+ require.False(t, sysInfo.AppArmor) |
|
| 97 |
+} |
|
| 98 |
+ |
|
| 99 |
+func TestNumCPU(t *testing.T) {
|
|
| 100 |
+ cpuNumbers := NumCPU() |
|
| 101 |
+ if cpuNumbers <= 0 {
|
|
| 102 |
+ t.Fatal("CPU returned must be greater than zero")
|
|
| 103 |
+ } |
|
| 104 |
+} |