...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package chaosdaemon
17
18 import (
19 "context"
20
21 . "github.com/onsi/ginkgo/v2"
22 . "github.com/onsi/gomega"
23 "github.com/pkg/errors"
24
25 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
26 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
27 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
28 "github.com/chaos-mesh/chaos-mesh/pkg/log"
29 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
30 )
31
32 var _ = Describe("container kill", func() {
33 defer mock.With("MockContainerdClient", &test.MockClient{})()
34 logger, err := log.NewDefaultZapLogger()
35 Expect(err).To(BeNil())
36 s, _ := newDaemonServer(&crclients.CrClientConfig{
37 Runtime: crclients.ContainerRuntimeContainerd}, nil, logger)
38
39 Context("ContainerKill", func() {
40 It("should work", func() {
41 _, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
42 Action: &pb.ContainerAction{
43 Action: pb.ContainerAction_KILL,
44 },
45 ContainerId: "containerd://container-id",
46 })
47 Expect(err).To(BeNil())
48 })
49
50 It("should fail on wrong action type", func() {
51 const wrongActionType = 9527
52 _, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
53 Action: &pb.ContainerAction{
54 Action: pb.ContainerAction_Action(wrongActionType),
55 },
56 ContainerId: "containerd://container-id",
57 })
58 Expect(err).ToNot(BeNil())
59 Expect(err.Error()).To(ContainSubstring("not kill"))
60 })
61
62 It("should fail on container kill", func() {
63 const errorStr = "mock error on container kill"
64 defer mock.With("KillError", errors.New(errorStr))()
65 _, err := s.ContainerKill(context.TODO(), &pb.ContainerRequest{
66 Action: &pb.ContainerAction{
67 Action: pb.ContainerAction_KILL,
68 },
69 ContainerId: "containerd://container-id",
70 })
71 Expect(err).ToNot(BeNil())
72 Expect(err.Error()).To(Equal(errorStr))
73 })
74 })
75 })
76