1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package bpm
17
18 import (
19 "context"
20 "fmt"
21 "math/rand"
22 "strings"
23 "time"
24
25 . "github.com/onsi/ginkgo/v2"
26 . "github.com/onsi/gomega"
27
28 "github.com/chaos-mesh/chaos-mesh/pkg/log"
29 )
30
31 func RandomeIdentifier() string {
32 var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
33
34 s := make([]rune, 10)
35 for i := range s {
36 s[i] = letters[rand.Intn(len(letters))]
37 }
38 return string(s)
39 }
40
41 func WaitProcess(m *BackgroundProcessManager, proc *Process, exceedTime time.Duration) {
42 timeExceed := false
43 select {
44 case <-proc.Stopped():
45 case <-time.Tick(exceedTime):
46 timeExceed = true
47 }
48 Expect(timeExceed).To(BeFalse())
49 }
50
51 var _ = Describe("background process manager", func() {
52 log, err := log.NewDefaultZapLogger()
53 Expect(err).To(BeNil())
54 m := StartBackgroundProcessManager(nil, log)
55
56 Context("normally exited process", func() {
57 It("should work", func() {
58 cmd := DefaultProcessBuilder("sleep", "2").Build(context.Background())
59 p, err := m.StartProcess(context.Background(), cmd)
60 Expect(err).To(BeNil())
61
62 WaitProcess(m, p, time.Second*3)
63 })
64
65 It("processes with the same identifier", func() {
66 identifier := RandomeIdentifier()
67
68 cmd := DefaultProcessBuilder("sleep", "2").
69 SetIdentifier(identifier).
70 Build(context.Background())
71 p1, err := m.StartProcess(context.Background(), cmd)
72 Expect(err).To(BeNil())
73
74
75 cmd2 := DefaultProcessBuilder("sleep", "2").
76 SetIdentifier(identifier).
77 Build(context.Background())
78 _, err = m.StartProcess(context.Background(), cmd2)
79 Expect(err).NotTo(BeNil())
80 Expect(strings.Contains(err.Error(), fmt.Sprintf("process with identifier %s is running", identifier))).To(BeTrue())
81
82 WaitProcess(m, p1, time.Second*3)
83 cmd3 := DefaultProcessBuilder("sleep", "2").
84 SetIdentifier(identifier).
85 Build(context.TODO())
86 p3, err := m.StartProcess(context.Background(), cmd3)
87 Expect(err).To(BeNil())
88
89 WaitProcess(m, p3, time.Second*3)
90 })
91 })
92
93 Context("kill process", func() {
94 It("should work", func() {
95 cmd := DefaultProcessBuilder("sleep", "2").Build(context.Background())
96 p, err := m.StartProcess(context.Background(), cmd)
97 Expect(err).To(BeNil())
98
99 err = m.KillBackgroundProcess(context.Background(), p.Uid)
100 Expect(err).To(BeNil())
101
102 WaitProcess(m, p, time.Second*0)
103 })
104
105 It("process with the same identifier", func() {
106 identifier := RandomeIdentifier()
107
108 cmd := DefaultProcessBuilder("sleep", "2").
109 SetIdentifier(identifier).
110 Build(context.Background())
111 p1, err := m.StartProcess(context.Background(), cmd)
112 Expect(err).To(BeNil())
113
114
115 cmd2 := DefaultProcessBuilder("sleep", "2").
116 SetIdentifier(identifier).
117 Build(context.Background())
118 _, err = m.StartProcess(context.Background(), cmd2)
119 Expect(err).NotTo(BeNil())
120 Expect(strings.Contains(err.Error(), fmt.Sprintf("process with identifier %s is running", identifier))).To(BeTrue())
121 WaitProcess(m, p1, time.Second*3)
122
123 cmd3 := DefaultProcessBuilder("sleep", "2").
124 SetIdentifier(identifier).
125 Build(context.Background())
126 p3, err := m.StartProcess(context.Background(), cmd3)
127 Expect(err).To(BeNil())
128
129 err = m.KillBackgroundProcess(context.Background(), p3.Uid)
130 Expect(err).To(BeNil())
131
132 cmd4 := DefaultProcessBuilder("sleep", "2").
133 SetIdentifier(identifier).
134 Build(context.Background())
135 p4, err := m.StartProcess(context.Background(), cmd4)
136 Expect(err).To(BeNil())
137 WaitProcess(m, p4, time.Second*3)
138 })
139 })
140
141 Context("get identifiers", func() {
142 It("should work", func() {
143 identifier := RandomeIdentifier()
144 cmd := DefaultProcessBuilder("sleep", "2").
145 SetIdentifier(identifier).
146 Build(context.Background())
147
148 p, err := m.StartProcess(context.Background(), cmd)
149 Expect(err).To(BeNil())
150
151 ids := m.GetIdentifiers()
152 Expect(ids).To(Equal([]string{identifier}))
153
154 WaitProcess(m, p, time.Second*3)
155
156
157 time.Sleep(time.Second * 2)
158 ids = m.GetIdentifiers()
159 Expect(len(ids)).To(Equal(0))
160 })
161
162 It("should work with nil identifier", func() {
163 cmd := DefaultProcessBuilder("sleep", "2").Build(context.Background())
164
165 p, err := m.StartProcess(context.Background(), cmd)
166 Expect(err).To(BeNil())
167
168 ids := m.GetIdentifiers()
169 Expect(len(ids)).To(Equal(0))
170
171 WaitProcess(m, p, time.Second*5)
172 })
173 })
174
175 Context("get uid", func() {
176 It("kill process", func() {
177 cmd := DefaultProcessBuilder("sleep", "2").Build(context.Background())
178 p, err := m.StartProcess(context.Background(), cmd)
179 Expect(err).To(BeNil())
180
181 uid, loaded := m.GetUID(p.Pair)
182 Expect(loaded).To(BeTrue())
183
184 err = m.KillBackgroundProcess(context.Background(), uid)
185 Expect(err).To(BeNil())
186
187 WaitProcess(m, p, time.Second*0)
188 })
189 })
190 })
191