diff --git a/internal/process/defunct_processes.go b/internal/process/defunct_processes.go index d108c96d17b..d8873206eb1 100644 --- a/internal/process/defunct_processes.go +++ b/internal/process/defunct_processes.go @@ -25,7 +25,13 @@ type Stat struct { // DefunctProcesses returns the number of zombie processes in the node. func DefunctProcesses() (defunctCount uint, retErr error) { - directories, err := os.Open(ProcessFS) + return DefunctProcessesForPath(ProcessFS) +} + +// DefunctProcessesForPath retrieves the number of zombie processes from +// a specific process filesystem. +func DefunctProcessesForPath(path string) (defunctCount uint, retErr error) { + directories, err := os.Open(path) if err != nil { return 0, err } @@ -43,7 +49,7 @@ func DefunctProcesses() (defunctCount uint, retErr error) { continue } - stat, err := processStats(name) + stat, err := processStats(path, name) if err != nil { logrus.Debugf("Failed to get the status of process with PID %s: %v", name, err) continue @@ -57,8 +63,8 @@ func DefunctProcesses() (defunctCount uint, retErr error) { } // processStats returns status information of a process as defined in /proc/[pid]/stat -func processStats(pid string) (*Stat, error) { - bytes, err := ioutil.ReadFile(filepath.Join(ProcessFS, pid, "stat")) +func processStats(fsPath, pid string) (*Stat, error) { + bytes, err := ioutil.ReadFile(filepath.Join(fsPath, pid, "stat")) if err != nil { return nil, err } diff --git a/internal/process/defunct_processes_suite_test.go b/internal/process/defunct_processes_suite_test.go new file mode 100644 index 00000000000..9ddc76a2908 --- /dev/null +++ b/internal/process/defunct_processes_suite_test.go @@ -0,0 +1,26 @@ +package process_test + +import ( + "testing" + + . "github.com/cri-o/cri-o/test/framework" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +// TestProcess runs the created specs +func TestProcess(t *testing.T) { + RegisterFailHandler(Fail) + RunFrameworkSpecs(t, "Process") +} + +var t *TestFramework + +var _ = BeforeSuite(func() { + t = NewTestFramework(NilFunc, NilFunc) + t.Setup() +}) + +var _ = AfterSuite(func() { + t.Teardown() +}) diff --git a/internal/process/defunct_processes_test.go b/internal/process/defunct_processes_test.go new file mode 100644 index 00000000000..064c55cf0aa --- /dev/null +++ b/internal/process/defunct_processes_test.go @@ -0,0 +1,58 @@ +package process_test + +import ( + "fmt" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/cri-o/cri-o/internal/process" +) + +// The actual test suite +var _ = t.Describe("Process", func() { + t.Describe("DefunctProcessesForPath", func() { + Context("Should succeed", func() { + It("when given a valid path name and there are defunct processes", func() { + defunctCount, err := process.DefunctProcessesForPath("./testing/proc_success_1") + + Expect(err).To(BeNil()) + Expect(defunctCount).To(Equal(uint(7))) + }) + It("when given a valid path name but there are no defunct processes", func() { + defunctCount, err := process.DefunctProcessesForPath("./testing/proc_success_2") + + Expect(err).To(BeNil()) + Expect(defunctCount).To(Equal(uint(0))) + }) + It("when given a valid path name but there are no processes", func() { + defunctCount, err := process.DefunctProcessesForPath("./testing/proc_success_3") + + Expect(err).To(BeNil()) + Expect(defunctCount).To(Equal(uint(0))) + }) + It("when given a valid path name but there are no directories", func() { + defunctCount, err := process.DefunctProcessesForPath("./testing/proc_success_4") + + Expect(err).To(BeNil()) + Expect(defunctCount).To(Equal(uint(0))) + }) + }) + Context("Should fail", func() { + It("when given an invalid path name", func() { + defunctCount, err := process.DefunctProcessesForPath("./test/proc") + formattedErr := fmt.Sprintf("%v", err) + + Expect(formattedErr).To(Equal("open ./test/proc: no such file or directory")) + Expect(defunctCount).To(Equal(uint(0))) + }) + It("when the given path name does not belong to a directory", func() { + defunctCount, err := process.DefunctProcessesForPath("./testing/proc_fail") + formattedErr := fmt.Sprintf("%v", err) + + Expect(formattedErr).To(Equal("readdirent ./testing/proc_fail: not a directory")) + Expect(defunctCount).To(Equal(uint(0))) + }) + }) + }) +}) diff --git a/internal/process/testing/proc_fail b/internal/process/testing/proc_fail new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/1/cgroup b/internal/process/testing/proc_success_1/1/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/1/stat b/internal/process/testing/proc_success_1/1/stat new file mode 100644 index 00000000000..e036f67ee8f --- /dev/null +++ b/internal/process/testing/proc_success_1/1/stat @@ -0,0 +1 @@ +1 (systemd) S 0 1 1 0 -1 4194560 26808 283533 44 586 41 78 514 255 20 0 1 0 36 180686848 4263 18446744073709551615 1 1 0 0 0 0 671173123 4096 1260 0 0 0 17 0 0 0 12 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/1/statm b/internal/process/testing/proc_success_1/1/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/10/cgroup b/internal/process/testing/proc_success_1/10/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/10/stat b/internal/process/testing/proc_success_1/10/stat new file mode 100644 index 00000000000..d2f93cd88ec --- /dev/null +++ b/internal/process/testing/proc_success_1/10/stat @@ -0,0 +1 @@ +10 (hello world) Z 2 0 0 0 -1 2129984 0 0 0 0 0 0 0 0 20 0 1 0 36 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/10/statm b/internal/process/testing/proc_success_1/10/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/102/cgroup b/internal/process/testing/proc_success_1/102/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/102/stat b/internal/process/testing/proc_success_1/102/stat new file mode 100644 index 00000000000..9102e6334a8 --- /dev/null +++ b/internal/process/testing/proc_success_1/102/stat @@ -0,0 +1 @@ +102 (kworker/u16:1-events_unbound) I 2 0 0 0 -1 69238880 0 4350 0 0 0 170 0 1 20 0 1 0 65 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 6 0 0 38 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/102/statm b/internal/process/testing/proc_success_1/102/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/1037/cgroup b/internal/process/testing/proc_success_1/1037/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/1037/stat b/internal/process/testing/proc_success_1/1037/stat new file mode 100644 index 00000000000..5624573708d --- /dev/null +++ b/internal/process/testing/proc_success_1/1037/stat @@ -0,0 +1 @@ +1037 ( 2 gdm 5 ) Z 1 1037 1037 0 -1 4194560 1093 2034 2 2 2 2 1 1 20 0 3 0 1139 480079872 2246 18446744073709551615 1 1 0 0 0 0 512 4096 81923 0 0 0 17 4 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/1037/statm b/internal/process/testing/proc_success_1/1037/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/2096/cgroup b/internal/process/testing/proc_success_1/2096/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/2096/stat b/internal/process/testing/proc_success_1/2096/stat new file mode 100644 index 00000000000..269b013d8c8 --- /dev/null +++ b/internal/process/testing/proc_success_1/2096/stat @@ -0,0 +1 @@ +2096 (gsd-disk-utilit) Z 1725 1725 1725 0 -1 4194304 624 91 0 0 0 0 0 0 20 0 3 0 3902 238149632 1479 18446744073709551615 94315514339328 94315514343973 140734996731264 0 0 0 0 4096 0 0 0 0 17 0 0 0 0 0 0 94315514354600 94315514355716 94315541213184 140734996732406 140734996732443 140734996732443 140734996733907 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/2096/statm b/internal/process/testing/proc_success_1/2096/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/215/cgroup b/internal/process/testing/proc_success_1/215/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/215/stat b/internal/process/testing/proc_success_1/215/stat new file mode 100644 index 00000000000..6d0815d1750 --- /dev/null +++ b/internal/process/testing/proc_success_1/215/stat @@ -0,0 +1 @@ +215 (hello world 2) Z 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 133 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 6 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/215/statm b/internal/process/testing/proc_success_1/215/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/3/cgroup b/internal/process/testing/proc_success_1/3/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/3/stat b/internal/process/testing/proc_success_1/3/stat new file mode 100644 index 00000000000..3edc4680968 --- /dev/null +++ b/internal/process/testing/proc_success_1/3/stat @@ -0,0 +1 @@ +3 (rcu_gp) I 2 0 0 0 -1 69238880 0 0 0 0 0 0 0 0 0 -20 1 0 36 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/3/statm b/internal/process/testing/proc_success_1/3/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/4010/cgroup b/internal/process/testing/proc_success_1/4010/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/4010/stat b/internal/process/testing/proc_success_1/4010/stat new file mode 100644 index 00000000000..b8288eda7ae --- /dev/null +++ b/internal/process/testing/proc_success_1/4010/stat @@ -0,0 +1 @@ +4010 (chrome) Z 2628 1759 1759 0 -1 1077936192 4246 0 0 0 23 7 0 0 20 0 13 0 96262 22110556160 22471 18446744073709551615 93992801384832 93992937871968 140735240563200 0 0 0 0 4098 1098993405 0 0 0 17 5 0 0 0 0 0 93992945617536 93992946127616 93992964468736 140735240566968 140735240567080 140735240567080 140735240568798 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/4010/statm b/internal/process/testing/proc_success_1/4010/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/423/cgroup b/internal/process/testing/proc_success_1/423/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/423/stat b/internal/process/testing/proc_success_1/423/stat new file mode 100644 index 00000000000..bfe13698d2c --- /dev/null +++ b/internal/process/testing/proc_success_1/423/stat @@ -0,0 +1 @@ +423 ((address) (program)) Z 1631 1759 1759 0 -1 4194560 103744 10166 13 3 12356 4231 136 4 20 0 28 0 3796 5462523904 57718 18446744073709551615 1 1 0 0 0 0 0 16781312 83192 0 0 0 17 1 0 0 7 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/423/statm b/internal/process/testing/proc_success_1/423/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/809/cgroup b/internal/process/testing/proc_success_1/809/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/809/stat b/internal/process/testing/proc_success_1/809/stat new file mode 100644 index 00000000000..bb219a31288 --- /dev/null +++ b/internal/process/testing/proc_success_1/809/stat @@ -0,0 +1 @@ +809 ((chronyd)) Z 1 808 808 0 -1 4194624 219 0 2 0 1 4 0 0 20 0 1 0 402 97402880 1023 18446744073709551615 1 1 0 0 0 0 0 4096 16391 0 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_1/809/statm b/internal/process/testing/proc_success_1/809/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/cgroups b/internal/process/testing/proc_success_1/cgroups new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_1/sys/dev b/internal/process/testing/proc_success_1/sys/dev new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_2/1/cgroup b/internal/process/testing/proc_success_2/1/cgroup new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_2/1/stat b/internal/process/testing/proc_success_2/1/stat new file mode 100644 index 00000000000..e036f67ee8f --- /dev/null +++ b/internal/process/testing/proc_success_2/1/stat @@ -0,0 +1 @@ +1 (systemd) S 0 1 1 0 -1 4194560 26808 283533 44 586 41 78 514 255 20 0 1 0 36 180686848 4263 18446744073709551615 1 1 0 0 0 0 671173123 4096 1260 0 0 0 17 0 0 0 12 0 0 0 0 0 0 0 0 0 0 \ No newline at end of file diff --git a/internal/process/testing/proc_success_2/1/statm b/internal/process/testing/proc_success_2/1/statm new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_2/cgroups b/internal/process/testing/proc_success_2/cgroups new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_2/sys/dev b/internal/process/testing/proc_success_2/sys/dev new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_3/cgroups b/internal/process/testing/proc_success_3/cgroups new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_3/sys/dev b/internal/process/testing/proc_success_3/sys/dev new file mode 100644 index 00000000000..e69de29bb2d diff --git a/internal/process/testing/proc_success_4/cgroups b/internal/process/testing/proc_success_4/cgroups new file mode 100644 index 00000000000..e69de29bb2d