1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package docker
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 TestDockerClient(t *testing.T) {
32 RegisterFailHandler(Fail)
33
34 RunSpecs(t, "Docker Container Client Test Suit")
35 }
36
37 var _ = Describe("docker client", func() {
38 Context("DockerClient 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 := DockerClient{client: m}
44 pid, err := c.GetPidFromContainerID(context.TODO(), "docker://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 := DockerClient{client: m}
52 _, err := c.GetPidFromContainerID(context.TODO(), "containerd://this-is-a-wrong-protocol")
53 Expect(err).NotTo(BeNil())
54 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", dockerProtocolPrefix)))
55 })
56
57 It("should error on ContainerInspectError", func() {
58 errorStr := "this is a mocked error"
59 defer mock.With("ContainerInspectError", errors.New(errorStr))()
60 m := &test.MockClient{}
61 c := DockerClient{client: m}
62 _, err := c.GetPidFromContainerID(context.TODO(), "docker://valid-container-id")
63 Expect(err).NotTo(BeNil())
64 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
65 })
66 })
67
68 Context("DockerClient ContainerKillByContainerID", func() {
69 It("should work", func() {
70 m := &test.MockClient{}
71 c := DockerClient{client: m}
72 err := c.ContainerKillByContainerID(context.TODO(), "docker://valid-container-id")
73 Expect(err).To(BeNil())
74 })
75
76 It("should error on ContainerKill", func() {
77 errorStr := "this is a mocked error on ContainerKill"
78 m := &test.MockClient{}
79 c := DockerClient{client: m}
80 defer mock.With("ContainerKillError", errors.New(errorStr))()
81 err := c.ContainerKillByContainerID(context.TODO(), "docker://valid-container-id")
82 Expect(err).ToNot(BeNil())
83 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
84 })
85
86 It("should error on wrong protocol", func() {
87 m := &test.MockClient{}
88 c := DockerClient{client: m}
89 err := c.ContainerKillByContainerID(context.TODO(), "containerd://this-is-a-wrong-protocol")
90 Expect(err).ToNot(BeNil())
91 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", dockerProtocolPrefix)))
92 })
93
94 It("should error on short protocol", func() {
95 m := &test.MockClient{}
96 c := DockerClient{client: m}
97 err := c.ContainerKillByContainerID(context.TODO(), "dock:")
98 Expect(err).ToNot(BeNil())
99 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a docker container id"))
100 })
101 })
102
103 Context("DockerClient ListContainerIDs", func() {
104 It("should work", func() {
105 containerID := "valid-container-id"
106 containerIDWithPrefix := fmt.Sprintf("%s%s", dockerProtocolPrefix, containerID)
107 defer mock.With("containerID", containerID)()
108
109 m := &test.MockClient{}
110 c := DockerClient{client: m}
111 containerIDs, err := c.ListContainerIDs(context.Background())
112
113 Expect(err).To(BeNil())
114 Expect(containerIDs).To(Equal([]string{containerIDWithPrefix}))
115 })
116 })
117
118 Context("DockerClient GetLabelsFromContainerID", func() {
119 It("should work", func() {
120 sampleLabels := map[string]string{
121 "io.kubernetes.pod.namespace": "default",
122 "io.kubernetes.pod.name": "busybox-5f8dd756dd-6rjzw",
123 "io.kubernetes.container.name": "busybox",
124 }
125 defer mock.With("labels", sampleLabels)()
126
127 m := &test.MockClient{}
128 c := DockerClient{client: m}
129 labels, err := c.GetLabelsFromContainerID(context.Background(), "docker://valid-container-id")
130
131 Expect(err).To(BeNil())
132 Expect(labels).To(Equal(sampleLabels))
133 })
134
135 It("should error on wrong protocol", func() {
136 sampleLabels := map[string]string{
137 "io.kubernetes.pod.namespace": "default",
138 "io.kubernetes.pod.name": "busybox-5f8dd756dd-6rjzw",
139 "io.kubernetes.container.name": "busybox",
140 }
141 defer mock.With("labels", sampleLabels)()
142
143 m := &test.MockClient{}
144 c := DockerClient{client: m}
145 _, err := c.GetLabelsFromContainerID(context.Background(), "containerd://this-is-a-wrong-protocol")
146
147 Expect(err).ToNot(BeNil())
148 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", dockerProtocolPrefix)))
149 })
150
151 It("should error on short protocol", func() {
152 m := &test.MockClient{}
153 c := DockerClient{client: m}
154 _, err := c.GetLabelsFromContainerID(context.TODO(), "dock:")
155
156 Expect(err).ToNot(BeNil())
157 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a docker container id"))
158 })
159 })
160 })
161