1
2
3
4
5
6
7
8
9
10
11
12
13
14 package inject
15
16 import (
17 "testing"
18
19 . "github.com/onsi/gomega"
20 )
21
22 func TestIsCommonScripts(t *testing.T) {
23 g := NewGomegaWithT(t)
24
25 type TestCase struct {
26 name string
27 cmd string
28 expectedValue bool
29 }
30
31 tcs := []TestCase{
32 {
33 name: "command scripts: bash",
34 cmd: "bash",
35 expectedValue: true,
36 },
37 {
38 name: "command scripts: bash -c echo 1",
39 cmd: "bash -c echo 1",
40 expectedValue: true,
41 },
42 {
43 name: "command scripts: sh",
44 cmd: "sh",
45 expectedValue: true,
46 },
47 {
48 name: "command scripts: /bin/sh",
49 cmd: "/bin/sh",
50 expectedValue: true,
51 },
52 {
53 name: "command scripts: /bin/bash",
54 cmd: "/bin/bash",
55 expectedValue: true,
56 },
57 {
58 name: "command scripts: /usr/bin/bash",
59 cmd: "/usr/bin/bash",
60 expectedValue: true,
61 },
62 {
63 name: "not command scripts: /usr/bin/echo",
64 cmd: "/usr/bin/echo",
65 expectedValue: false,
66 },
67 {
68 name: "not command scripts: /chaos-mesh",
69 cmd: "/chaos-mesh",
70 expectedValue: false,
71 },
72 }
73
74 for _, tc := range tcs {
75 g.Expect(isCommonScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
76 }
77 }
78
79 func TestIsShellScripts(t *testing.T) {
80 g := NewGomegaWithT(t)
81
82 type TestCase struct {
83 name string
84 cmd string
85 expectedValue bool
86 }
87
88 tcs := []TestCase{
89 {
90 name: "bash",
91 cmd: "bash",
92 expectedValue: true,
93 },
94 {
95 name: "bash -c echo 1",
96 cmd: "bash -c echo 1",
97 expectedValue: true,
98 },
99 {
100 name: "/usr/bin/bash",
101 cmd: "/usr/bin/bash",
102 expectedValue: true,
103 },
104 {
105 name: "/usr/bin/echo",
106 cmd: "/usr/bin/echo",
107 expectedValue: false,
108 },
109 {
110 name: "/chaos-mesh",
111 cmd: "/chaos-mesh",
112 expectedValue: false,
113 },
114 }
115
116 for _, tc := range tcs {
117 g.Expect(isShellScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
118 }
119 }
120
121 func TestIsPythonScripts(t *testing.T) {
122 g := NewGomegaWithT(t)
123
124 type TestCase struct {
125 name string
126 cmd string
127 expectedValue bool
128 }
129
130 tcs := []TestCase{
131 {
132 name: "python",
133 cmd: "python",
134 expectedValue: true,
135 },
136 {
137 name: "bash -c echo 1",
138 cmd: "bash -c echo 1",
139 expectedValue: false,
140 },
141 {
142 name: "/bin/python",
143 cmd: "/bin/python",
144 expectedValue: true,
145 },
146 {
147 name: "/usr/bin/bash",
148 cmd: "/usr/bin/bash",
149 expectedValue: false,
150 },
151 {
152 name: "/usr/bin/echo",
153 cmd: "/usr/bin/echo",
154 expectedValue: false,
155 },
156 {
157 name: "/chaos-mesh",
158 cmd: "/chaos-mesh",
159 expectedValue: false,
160 },
161 }
162
163 for _, tc := range tcs {
164 g.Expect(isPythonScripts(tc.cmd)).To(Equal(tc.expectedValue), tc.name)
165 }
166 }
167
168 func TestMergeCommandsAction(t *testing.T) {
169 g := NewGomegaWithT(t)
170
171 type TestCase struct {
172 name string
173 commands []string
174 expectedValue string
175 }
176
177 tcs := []TestCase{
178 {
179 name: "scripts files",
180 commands: []string{
181 "/bin/sh",
182 "start_chaos_mesh.sh",
183 },
184 expectedValue: "/bin/sh start_chaos_mesh.sh\n",
185 },
186 {
187 name: "common scripts",
188 commands: []string{
189 "bash",
190 "-ec",
191 "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
192 },
193 expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
194 },
195 {
196 name: "common start scripts",
197 commands: []string{
198 "/chaos-mesh",
199 "--c t",
200 "--v 2",
201 "--log stdout",
202 },
203 expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
204 },
205 {
206 name: "one line",
207 commands: []string{
208 "/chaos-mesh --c t --v 2 --log stdout",
209 },
210 expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
211 },
212 }
213
214 for _, tc := range tcs {
215 g.Expect(mergeCommandsAction(tc.commands)).To(Equal(tc.expectedValue), tc.name)
216 }
217 }
218
219 func TestMergeOriginCommandsAndArgs(t *testing.T) {
220 g := NewGomegaWithT(t)
221
222 type TestCase struct {
223 name string
224 commands []string
225 args []string
226 expectedValue string
227 }
228
229 tcs := []TestCase{
230 {
231 name: "only commands",
232 commands: []string{
233 "bash",
234 "-ec",
235 "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
236 },
237 expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
238 },
239 {
240 name: "only args",
241 args: []string{
242 "bash",
243 "-ec",
244 "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2",
245 },
246 expectedValue: "echo $HOSENAME\n /start_chaos_mesh.sh -s t1 \n -v t2\n",
247 },
248 {
249 name: "commands and args",
250 commands: []string{
251 "/chaos-mesh",
252 },
253 args: []string{
254 "--c t",
255 "--v 2",
256 "--log stdout",
257 },
258 expectedValue: "/chaos-mesh --c t --v 2 --log stdout\n",
259 },
260 }
261
262 for _, tc := range tcs {
263 g.Expect(mergeOriginCommandsAndArgs(tc.commands, tc.args)).To(Equal(tc.expectedValue), tc.name)
264 }
265 }
266
267 func TestMergeCommands(t *testing.T) {
268 g := NewGomegaWithT(t)
269
270 type TestCase struct {
271 name string
272 inject []string
273 origin []string
274 args []string
275 expectedValue []string
276 }
277
278 tcs := []TestCase{
279 {
280 name: "scripts file",
281 inject: []string{
282 "/check.sh -v 1",
283 },
284 origin: []string{
285 "/bin/sh",
286 "/start.sh",
287 },
288 expectedValue: []string{
289 "/bin/sh",
290 "-ec",
291 "/check.sh -v 1\n/bin/sh /start.sh\n",
292 },
293 },
294 {
295 name: "common scripts",
296 inject: []string{
297 "/check.sh -v 1",
298 },
299 origin: []string{
300 "bash",
301 "-c",
302 "set -ex\n[[ `hostname` =~ -([0-9]+)$ ]] || exit 1\n/tiflash server --config-file /data/config.toml",
303 },
304 expectedValue: []string{
305 "/bin/sh",
306 "-ec",
307 "/check.sh -v 1\nset -ex\n[[ `hostname` =~ -([0-9]+)$ ]] || exit 1\n/tiflash server --config-file /data/config.toml\n",
308 },
309 },
310 }
311
312 for _, tc := range tcs {
313 g.Expect(MergeCommands(tc.inject, tc.origin, tc.args)).To(Equal(tc.expectedValue), tc.name)
314 }
315 }
316