1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package chaosdaemon
17
18 import (
19 "context"
20 "errors"
21 "io/ioutil"
22 "os"
23 "os/exec"
24
25 . "github.com/onsi/ginkgo"
26 . "github.com/onsi/gomega"
27
28 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
29 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
30 pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
31 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
32 )
33
34 var _ = Describe("ipset server", func() {
35 defer mock.With("MockContainerdClient", &test.MockClient{})()
36 s, _ := newDaemonServer(crclients.ContainerRuntimeContainerd)
37
38 Context("createIPSet", func() {
39 It("should work", func() {
40 defer mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
41 Expect(cmd).To(Equal("/usr/local/bin/nsexec"))
42 Expect(args[0]).To(Equal("-n"))
43 Expect(args[1]).To(Equal("/proc/1/ns/net"))
44 Expect(args[2]).To(Equal("--"))
45 Expect(args[3]).To(Equal("ipset"))
46 Expect(args[4]).To(Equal("create"))
47 Expect(args[5]).To(Equal("name"))
48 Expect(args[6]).To(Equal("hash:net"))
49 return exec.Command("echo", "mock command")
50 })()
51 err := createIPSet(context.TODO(), true, 1, "name")
52 Expect(err).To(BeNil())
53 })
54
55 It("should work since ipset exist", func() {
56
57 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
58 echo $1
59 cat > /tmp/mockfail.sh << EOF
60 #! /bin/sh
61 exit 0
62 EOF
63 exit 1
64 `), 0755)
65 Expect(err).To(BeNil())
66 defer os.Remove("/tmp/mockfail.sh")
67 defer mock.With("MockProcessBuild", func(ctx context.Context, cmd string, args ...string) *exec.Cmd {
68 return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
69 })()
70 err = createIPSet(context.TODO(), true, 1, "name")
71 Expect(err).To(BeNil())
72 })
73
74 It("shoud fail on the first command", func() {
75
76 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
77 echo $1
78 exit 1
79 `), 0755)
80 Expect(err).To(BeNil())
81 defer os.Remove("/tmp/mockfail.sh")
82 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
83 return exec.Command("/tmp/mockfail.sh", "fail msg")
84 })()
85 err = createIPSet(context.TODO(), true, 1, "name")
86 Expect(err).ToNot(BeNil())
87 })
88
89 It("shoud fail on the second command", func() {
90
91 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
92 echo $1
93 exit 1
94 `), 0755)
95 Expect(err).To(BeNil())
96 defer os.Remove("/tmp/mockfail.sh")
97 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
98 return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
99 })()
100 err = createIPSet(context.TODO(), true, 1, "name")
101 Expect(err).ToNot(BeNil())
102 })
103 })
104
105 Context("addIpsToIPSet", func() {
106 It("should work", func() {
107 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
108 return exec.Command("echo", "mock command")
109 })()
110 err := addCIDRsToIPSet(context.TODO(), true, 1, "name", []string{"1.1.1.1"})
111 Expect(err).To(BeNil())
112 })
113
114 It("should work since ip exist", func() {
115
116 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
117 echo $1
118 exit 1
119 `), 0755)
120 Expect(err).To(BeNil())
121 defer os.Remove("/tmp/mockfail.sh")
122 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
123 return exec.Command("/tmp/mockfail.sh", ipExistErr)
124 })()
125 err = addCIDRsToIPSet(context.TODO(), true, 1, "name", []string{"1.1.1.1"})
126 Expect(err).To(BeNil())
127 })
128
129 It("should fail", func() {
130
131 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
132 echo $1
133 exit 1
134 `), 0755)
135 Expect(err).To(BeNil())
136 defer os.Remove("/tmp/mockfail.sh")
137 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
138 return exec.Command("/tmp/mockfail.sh", "fail msg")
139 })()
140 err = addCIDRsToIPSet(context.TODO(), true, 1, "name", []string{"1.1.1.1"})
141 Expect(err).ToNot(BeNil())
142 })
143 })
144
145 Context("renameIPSet", func() {
146 It("should work", func() {
147 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
148 return exec.Command("echo", "mock command")
149 })()
150 err := renameIPSet(context.TODO(), true, 1, "name", "newname")
151 Expect(err).To(BeNil())
152 })
153
154 It("should work since ipset exist", func() {
155
156 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
157 echo $1
158 cat > /tmp/mockfail.sh << EOF
159 #! /bin/sh
160 exit 0
161 EOF
162 exit 1
163 `), 0755)
164 Expect(err).To(BeNil())
165 defer os.Remove("/tmp/mockfail.sh")
166 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
167 return exec.Command("/tmp/mockfail.sh", ipsetNewNameExistErr)
168 })()
169 err = renameIPSet(context.TODO(), true, 1, "name", "newname")
170 Expect(err).To(BeNil())
171 })
172
173 It("shoud fail on the first command", func() {
174
175 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
176 echo $1
177 exit 1
178 `), 0755)
179 Expect(err).To(BeNil())
180 defer os.Remove("/tmp/mockfail.sh")
181 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
182 return exec.Command("/tmp/mockfail.sh", "fail msg")
183 })()
184 err = renameIPSet(context.TODO(), true, 1, "name", "newname")
185 Expect(err).ToNot(BeNil())
186 })
187
188 It("shoud fail on the second command", func() {
189
190 err := ioutil.WriteFile("/tmp/mockfail.sh", []byte(`#! /bin/sh
191 echo $1
192 exit 1
193 `), 0755)
194 Expect(err).To(BeNil())
195 defer os.Remove("/tmp/mockfail.sh")
196 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
197 return exec.Command("/tmp/mockfail.sh", ipsetExistErr)
198 })()
199 err = renameIPSet(context.TODO(), true, 1, "name", "newname")
200 Expect(err).ToNot(BeNil())
201 })
202 })
203
204 Context("FlushIPSets", func() {
205 It("should work", func() {
206 defer mock.With("MockProcessBuild", func(context.Context, string, ...string) *exec.Cmd {
207 return exec.Command("echo", "mock command")
208 })()
209 _, err := s.FlushIPSets(context.TODO(), &pb.IPSetsRequest{
210 Ipsets: []*pb.IPSet{{
211 Name: "ipset-name",
212 Cidrs: []string{"1.1.1.1/32"},
213 }},
214 ContainerId: "containerd://container-id",
215 EnterNS: true,
216 })
217 Expect(err).To(BeNil())
218 })
219
220 It("should fail on get pid", func() {
221 const errorStr = "mock get pid error"
222 defer mock.With("TaskError", errors.New(errorStr))()
223 _, err := s.FlushIPSets(context.TODO(), &pb.IPSetsRequest{
224 Ipsets: []*pb.IPSet{{
225 Name: "ipset-name",
226 Cidrs: []string{"1.1.1.1/32"},
227 }},
228 ContainerId: "containerd://container-id",
229 EnterNS: true,
230 })
231 Expect(err).ToNot(BeNil())
232 Expect(err.Error()).To(Equal(errorStr))
233 })
234 })
235 })
236