...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package test
17
18 import (
19 "context"
20 "syscall"
21
22 "github.com/containerd/containerd"
23 "github.com/containerd/containerd/cio"
24 "github.com/docker/docker/api/types"
25
26 "github.com/chaos-mesh/chaos-mesh/pkg/mock"
27 )
28
29 type MockClient struct{}
30
31 func (m *MockClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
32 if err := mock.On("ContainerInspectError"); err != nil {
33 return types.ContainerJSON{}, err.(error)
34 }
35
36 var pid int
37 if p := mock.On("pid"); p != nil {
38 pid = p.(int)
39 }
40 return types.ContainerJSON{
41 ContainerJSONBase: &types.ContainerJSONBase{
42 State: &types.ContainerState{
43 Pid: pid,
44 },
45 },
46 }, nil
47 }
48
49 func (m *MockClient) ContainerKill(ctx context.Context, containerID, signal string) error {
50 if err := mock.On("ContainerKillError"); err != nil {
51 return err.(error)
52 }
53 return nil
54 }
55
56 func (m *MockClient) LoadContainer(ctx context.Context, id string) (containerd.Container, error) {
57 if err := mock.On("LoadContainerError"); err != nil {
58 return nil, err.(error)
59 }
60
61 return &MockContainer{}, nil
62 }
63
64 type MockContainer struct {
65 containerd.Container
66 }
67
68 func (m *MockContainer) Task(context.Context, cio.Attach) (containerd.Task, error) {
69 if err := mock.On("TaskError"); err != nil {
70 return nil, err.(error)
71 }
72
73 return &MockTask{}, nil
74 }
75
76 type MockTask struct {
77 containerd.Task
78 }
79
80 func (m *MockTask) Pid() uint32 {
81 var pid int
82 if p := mock.On("pid"); p != nil {
83 pid = p.(int)
84 }
85 return uint32(pid)
86 }
87
88 func (m *MockTask) Kill(context.Context, syscall.Signal, ...containerd.KillOpts) error {
89 if err := mock.On("KillError"); err != nil {
90 return err.(error)
91 }
92 return nil
93 }
94