This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinder_test.go
87 lines (69 loc) · 1.71 KB
/
finder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package devoops_test
import (
"math/rand"
"os/exec"
"github.com/glestaris/devoops"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"github.com/shirou/gopsutil/process"
)
var _ = Describe("ProcessFinder", func() {
var sess *gexec.Session
BeforeEach(func() {
var err error
path, err := exec.LookPath("sleep")
Expect(err).NotTo(HaveOccurred())
sess, err = gexec.Start(
&exec.Cmd{
Path: path,
Args: []string{"sleep", "100"},
Dir: "/tmp",
},
GinkgoWriter, GinkgoWriter,
)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
sess.Kill()
Expect(sess.Wait()).To(gexec.Exit())
})
Describe("FindByPid", func() {
var pid int
BeforeEach(func() {
pid = sess.Command.Process.Pid
})
It("should return a process with the same pid", func() {
proc, err := devoops.FindByPid(pid)
Expect(err).NotTo(HaveOccurred())
Expect(proc.Pid).To(Equal(pid))
})
It("should return a process with the same programm name", func() {
proc, err := devoops.FindByPid(pid)
Expect(err).NotTo(HaveOccurred())
Expect(proc.ProgramName).To(Equal("sleep"))
})
It("should return a process with the same arguments", func() {
proc, err := devoops.FindByPid(pid)
Expect(err).NotTo(HaveOccurred())
Expect(proc.Args).To(Equal([]string{"100"}))
})
Context("when there is no process identified by this pid", func() {
It("should return an error", func() {
pid := findUnusedPid()
_, err := devoops.FindByPid(pid)
Expect(err).To(HaveOccurred())
})
})
})
})
func findUnusedPid() int {
for {
pid := rand.Int31()
ok, err := process.PidExists(pid)
Expect(err).NotTo(HaveOccurred())
if !ok {
return int(pid)
}
}
}