Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions internal/process/defunct_processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

G304: Potential file inclusion via variable
(at-me in a reply with help or ignore)

if err != nil {
return 0, err
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions internal/process/defunct_processes_suite_test.go
Original file line number Diff line number Diff line change
@@ -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()
})
58 changes: 58 additions & 0 deletions internal/process/defunct_processes_test.go
Original file line number Diff line number Diff line change
@@ -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)))
})
})
})
})
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/1/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/10/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/102/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/1037/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/2096/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/215/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/3/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/4010/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/423/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_1/809/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions internal/process/testing/proc_success_2/1/stat
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.