...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package containerd
15
16 import (
17 "context"
18 "errors"
19 "fmt"
20
21 . "github.com/onsi/ginkgo"
22 . "github.com/onsi/gomega"
23
24 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
25 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
26 )
27
28 var _ = Describe("containerd client", func() {
29 Context("ContainerdClient GetPidFromContainerID", func() {
30 It("should return the magic number 9527", func() {
31 defer func() {
32 err := mock.With("pid", int(9527))()
33 Expect(err).To(BeNil())
34 }()
35
36 m := &test.MockClient{}
37 c := ContainerdClient{client: m}
38 pid, err := c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
39 Expect(err).To(BeNil())
40 Expect(pid).To(Equal(uint32(9527)))
41 })
42
43 It("should error with wrong protocol", func() {
44 m := &test.MockClient{}
45 c := ContainerdClient{client: m}
46 _, err := c.GetPidFromContainerID(context.TODO(), "docker://this-is-a-wrong-protocol")
47 Expect(err).NotTo(BeNil())
48 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", containerdProtocolPrefix)))
49 })
50
51 It("should error with specified string", func() {
52 errorStr := "this is a mocked error"
53 mock.With("LoadContainerError", errors.New(errorStr))
54 m := &test.MockClient{}
55 c := ContainerdClient{client: m}
56 _, err := c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
57 Expect(err).NotTo(BeNil())
58 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
59 err = mock.Reset("LoadContainerError")
60 Expect(err).NotTo(BeNil())
61
62 mock.With("TaskError", errors.New(errorStr))
63 m = &test.MockClient{}
64 c = ContainerdClient{client: m}
65 _, err = c.GetPidFromContainerID(context.TODO(), "containerd://valid-container-id")
66 Expect(err).NotTo(BeNil())
67 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
68 err = mock.Reset("TaskError")
69 Expect(err).NotTo(BeNil())
70 })
71 })
72
73 Context("ContainerdClient ContainerKillByContainerID", func() {
74 It("should work", func() {
75 m := &test.MockClient{}
76 c := ContainerdClient{client: m}
77 err := c.ContainerKillByContainerID(context.TODO(), "containerd://valid-container-id")
78 Expect(err).To(BeNil())
79 })
80
81 errorPoints := []string{"LoadContainer", "Task", "Kill"}
82 for _, e := range errorPoints {
83 It(fmt.Sprintf("should error on %s", e), func() {
84 errorStr := fmt.Sprintf("this is a mocked error on %s", e)
85 m := &test.MockClient{}
86 c := ContainerdClient{client: m}
87 defer mock.With(e+"Error", errors.New(errorStr))()
88 err := c.ContainerKillByContainerID(context.TODO(), "containerd://valid-container-id")
89 Expect(err).ToNot(BeNil())
90 Expect(fmt.Sprintf("%s", err)).To(Equal(errorStr))
91 })
92 }
93
94 It("should error on wrong protocol", func() {
95 m := &test.MockClient{}
96 c := ContainerdClient{client: m}
97 err := c.ContainerKillByContainerID(context.TODO(), "docker://this-is-a-wrong-protocol")
98 Expect(err).ToNot(BeNil())
99 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring(fmt.Sprintf("expected %s but got", containerdProtocolPrefix)))
100 })
101
102 It("should error on short protocol", func() {
103 m := &test.MockClient{}
104 c := ContainerdClient{client: m}
105 err := c.ContainerKillByContainerID(context.TODO(), "dock:")
106 Expect(err).ToNot(BeNil())
107 Expect(fmt.Sprintf("%s", err)).To(ContainSubstring("is not a containerd container id"))
108 })
109 })
110 })
111