1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package containerd
17
18 import (
19 "context"
20 "fmt"
21 "testing"
22
23 . "github.com/onsi/ginkgo/v2"
24 . "github.com/onsi/gomega"
25 "github.com/pkg/errors"
26
27 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
28 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
29 )
30
31 func TestContainerdClient(t *testing.T) {
32 RegisterFailHandler(Fail)
33
34 RunSpecs(t, "Containerd Container Client Test Suit")
35 }
36
37 var _ = Describe("containerd client", func() {
38 Context("ContainerdClient GetPidFromContainerID", func() {
39 It("should return the magic number 9527", func() {
40 defer mock.With("pid", int(9527))()
41
42 m := &test.MockClient{}
43 c := ContainerdClient{client: m}
44 pid, err := c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
45 Expect(err).To(BeNil())
46 Expect(pid).To(Equal(uint32(9527)))
47 })
48
49 It("should error with wrong protocol", func() {
50 m := &test.MockClient{}
51 c := ContainerdClient{client: m}
52 _, err := c.GetPidFromContainerID(context.TODO(), "docker://this-is-a-wrong-protocol")
53 Expect(err).NotTo(BeNil())
54 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", containerdProtocolPrefix)))
55 })
56
57 It("should error with specified string", func() {
58 errorStr := "this is a mocked error"
59 mock.With("LoadContainerError", errors.New(errorStr))
60 m := &test.MockClient{}
61 c := ContainerdClient{client: m}
62 _, err := c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
63 Expect(err).NotTo(BeNil())
64 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
65 mock.Reset("LoadContainerError")
66
67 mock.With("TaskError", errors.New(errorStr))
68 m = &test.MockClient{}
69 c = ContainerdClient{client: m}
70 _, err = c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
71 Expect(err).NotTo(BeNil())
72 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
73 mock.Reset("TaskError")
74 })
75 })
76
77 Context("ContainerdClient ContainerKillByContainerID", func() {
78 It("should work", func() {
79 m := &test.MockClient{}
80 c := ContainerdClient{client: m}
81 err := c.ContainerKillByContainerID(context.TODO(), "containerd://valid-container-id")
82 Expect(err).To(BeNil())
83 })
84
85 errorPoints := []string{"LoadContainer", "Task", "Kill"}
86 for _, e := range errorPoints {
87 It(fmt.Sprintf("should error on %s", e), func() {
88 errorStr := fmt.Sprintf("this is a mocked error on %s", e)
89 m := &test.MockClient{}
90 c := ContainerdClient{client: m}
91 defer mock.With(e+"Error", errors.New(errorStr))()
92 err := c.ContainerKillByContainerID(context.TODO(), "containerd://valid-container-id")
93 Expect(err).ToNot(BeNil())
94 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
95 })
96 }
97
98 It("should error on wrong protocol", func() {
99 m := &test.MockClient{}
100 c := ContainerdClient{client: m}
101 err := c.ContainerKillByContainerID(context.TODO(), "docker://this-is-a-wrong-protocol")
102 Expect(err).ToNot(BeNil())
103 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", containerdProtocolPrefix)))
104 })
105
106 It("should error on short protocol", func() {
107 m := &test.MockClient{}
108 c := ContainerdClient{client: m}
109 err := c.ContainerKillByContainerID(context.TODO(), "dock:")
110 Expect(err).ToNot(BeNil())
111 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a containerd container id"))
112 })
113 })
114
115 Context("ContainerdClient ListContainerIDs", func() {
116 It("should work", func() {
117 containerID := "valid-container-id"
118 containerIDWithPrefix := fmt.Sprintf("%s%s", containerdProtocolPrefix, containerID)
119 defer mock.With("containerID", containerID)()
120
121 m := &test.MockClient{}
122 c := ContainerdClient{client: m}
123 containerIDs, err := c.ListContainerIDs(context.Background())
124
125 Expect(err).To(BeNil())
126 Expect(containerIDs).To(Equal([]string{containerIDWithPrefix}))
127 })
128 })
129
130 Context("ContainerdClient GetLabelsFromContainerID", func() {
131 It("should work", func() {
132 sampleLabels := map[string]string{
133 "io.kubernetes.pod.namespace": "default",
134 "io.kubernetes.pod.name": "busybox-5f8dd756dd-6rjzw",
135 "io.kubernetes.container.name": "busybox",
136 }
137 defer mock.With("labels", sampleLabels)()
138
139 m := &test.MockClient{}
140 c := ContainerdClient{client: m}
141 labels, err := c.GetLabelsFromContainerID(context.Background(), "containerd://valid-container-id")
142
143 Expect(err).To(BeNil())
144 Expect(labels).To(Equal(sampleLabels))
145 })
146
147 It("should error on wrong protocol", func() {
148 m := &test.MockClient{}
149 c := ContainerdClient{client: m}
150 _, err := c.GetLabelsFromContainerID(context.Background(), "docker://this-is-a-wrong-protocol")
151
152 Expect(err).ToNot(BeNil())
153 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", containerdProtocolPrefix)))
154 })
155
156 It("should error on short protocol", func() {
157 m := &test.MockClient{}
158 c := ContainerdClient{client: m}
159 _, err := c.GetLabelsFromContainerID(context.TODO(), "dock:")
160
161 Expect(err).ToNot(BeNil())
162 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a containerd container id"))
163 })
164 })
165 })
166