...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package chaosdaemon
15
16 import (
17 "context"
18 "errors"
19
20 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
21 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients/test"
22
23 . "github.com/onsi/ginkgo"
24 . "github.com/onsi/gomega"
25
26 pb "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
27 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
28 )
29
30 var _ = Describe("time server", func() {
31 defer mock.With("MockContainerdClient", &test.MockClient{})()
32 s, _ := newDaemonServer(crclients.ContainerRuntimeContainerd)
33
34 Context("SetTimeOffset", func() {
35 It("should work", func() {
36
37 const ignore = true
38 defer mock.With("ModifyTimeError", ignore)()
39
40 _, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
41 ContainerId: "containerd://container-id",
42 })
43 Expect(err).To(BeNil())
44 })
45
46 It("should fail on get pid", func() {
47 const errorStr = "mock error on load container"
48 defer mock.With("LoadContainerError", errors.New(errorStr))()
49
50 _, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
51 ContainerId: "containerd://container-id",
52 })
53 Expect(err).ToNot(BeNil())
54 Expect(err.Error()).To(Equal(errorStr))
55 })
56
57 It("should fail on modify time", func() {
58 const errorStr = "mock error on modify time"
59 defer mock.With("ModifyTimeError", errors.New(errorStr))()
60
61 _, err := s.SetTimeOffset(context.TODO(), &pb.TimeRequest{
62 ContainerId: "containerd://container-id",
63 })
64 Expect(err).ToNot(BeNil())
65 Expect(err.Error()).To(Equal(errorStr))
66 })
67 })
68
69 Context("RecoverTimeOffset", func() {
70 It("should work", func() {
71
72 const ignore = true
73 defer mock.With("ModifyTimeError", ignore)()
74
75 _, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
76 ContainerId: "containerd://container-id",
77 })
78 Expect(err).To(BeNil())
79 })
80
81 It("should fail on get pid", func() {
82 const errorStr = "mock error on load container"
83 defer mock.With("LoadContainerError", errors.New(errorStr))()
84
85 _, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
86 ContainerId: "containerd://container-id",
87 })
88 Expect(err).ToNot(BeNil())
89 Expect(err.Error()).To(Equal(errorStr))
90 })
91
92 It("should fail on modify time", func() {
93 const errorStr = "mock error on modify time"
94 defer mock.With("ModifyTimeError", errors.New(errorStr))()
95
96 _, err := s.RecoverTimeOffset(context.TODO(), &pb.TimeRequest{
97 ContainerId: "containerd://container-id",
98 })
99 Expect(err).ToNot(BeNil())
100 Expect(err.Error()).To(Equal(errorStr))
101 })
102 })
103 })
104