1
2
3 package main
4
5 import (
6 "context"
7 "time"
8
9 "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
10 "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
11 "github.com/Microsoft/hcsshim/internal/shimdiag"
12 "github.com/Microsoft/hcsshim/pkg/ctrdtaskapi"
13 v1 "github.com/containerd/cgroups/stats/v1"
14 "github.com/containerd/containerd/errdefs"
15 "github.com/containerd/containerd/runtime/v2/task"
16 "github.com/containerd/typeurl"
17 specs "github.com/opencontainers/runtime-spec/specs-go"
18 "github.com/pkg/errors"
19 )
20
21 var _ = (shimTask)(&testShimTask{})
22
23 type testShimTask struct {
24 id string
25
26 isWCOW bool
27 exec *testShimExec
28 execs map[string]*testShimExec
29 }
30
31 func (tst *testShimTask) ID() string {
32 return tst.id
33 }
34
35 func (tst *testShimTask) CreateExec(ctx context.Context, req *task.ExecProcessRequest, s *specs.Process) error {
36 return errdefs.ErrNotImplemented
37 }
38
39 func (tst *testShimTask) GetExec(eid string) (shimExec, error) {
40 if eid == "" {
41 return tst.exec, nil
42 }
43 e, ok := tst.execs[eid]
44 if ok {
45 return e, nil
46 }
47 return nil, errdefs.ErrNotFound
48 }
49
50 func (tst *testShimTask) ListExecs() ([]shimExec, error) {
51 var execs []shimExec
52 for _, v := range tst.execs {
53 execs = append(execs, v)
54 }
55 return execs, nil
56 }
57
58 func (tst *testShimTask) KillExec(ctx context.Context, eid string, signal uint32, all bool) error {
59 e, err := tst.GetExec(eid)
60 if err != nil {
61 return err
62 }
63 return e.Kill(ctx, signal)
64 }
65
66 func (tst *testShimTask) DeleteExec(ctx context.Context, eid string) (int, uint32, time.Time, error) {
67 e, err := tst.GetExec(eid)
68 if err != nil {
69 return 0, 0, time.Time{}, err
70 }
71 status := e.Status()
72 if eid != "" {
73 delete(tst.execs, eid)
74 }
75 return int(status.Pid), status.ExitStatus, status.ExitedAt, nil
76 }
77
78 func (tst *testShimTask) Pids(ctx context.Context) ([]options.ProcessDetails, error) {
79 pairs := []options.ProcessDetails{
80 {
81 ProcessID: uint32(tst.exec.Pid()),
82 ExecID: tst.exec.ID(),
83 },
84 }
85 for _, p := range tst.execs {
86 pairs = append(pairs, options.ProcessDetails{
87 ProcessID: uint32(p.pid),
88 ExecID: p.id,
89 })
90 }
91 return pairs, nil
92 }
93
94 func (tst *testShimTask) Wait() *task.StateResponse {
95 return tst.exec.Wait()
96 }
97
98 func (tst *testShimTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequest) (int, error) {
99 return 0, errors.New("not implemented")
100 }
101
102 func (tst *testShimTask) DumpGuestStacks(ctx context.Context) string {
103 return ""
104 }
105
106 func (tst *testShimTask) Update(ctx context.Context, req *task.UpdateTaskRequest) error {
107 data, err := typeurl.UnmarshalAny(req.Resources)
108 if err != nil {
109 return errors.Wrapf(err, "failed to unmarshal resources for container %s update request", req.ID)
110 }
111 if err := verifyTaskUpdateResourcesType(data); err != nil {
112 return err
113 }
114
115 if tst.isWCOW {
116 switch request := data.(type) {
117 case *ctrdtaskapi.ContainerMount:
118
119 if isMountTypeSupported(request.HostPath, request.Type) {
120 return nil
121 } else {
122 return errNotSupportedResourcesRequest
123 }
124 default:
125 return nil
126 }
127 }
128 return nil
129 }
130
131 func (tst *testShimTask) Share(ctx context.Context, req *shimdiag.ShareRequest) error {
132 return errors.New("not implemented")
133 }
134
135 func (tst *testShimTask) ProcessorInfo(ctx context.Context) (*processorInfo, error) {
136 return nil, errors.New("not implemented")
137 }
138
139 func (tst *testShimTask) Stats(ctx context.Context) (*stats.Statistics, error) {
140 if tst.isWCOW {
141 return getWCOWTestStats(), nil
142 }
143 return getLCOWTestStats(), nil
144 }
145
146 func getWCOWTestStats() *stats.Statistics {
147 return &stats.Statistics{
148 Container: &stats.Statistics_Windows{
149 Windows: &stats.WindowsContainerStatistics{
150 UptimeNS: 100,
151 Processor: &stats.WindowsContainerProcessorStatistics{
152 TotalRuntimeNS: 100,
153 RuntimeUserNS: 100,
154 RuntimeKernelNS: 100,
155 },
156 Memory: &stats.WindowsContainerMemoryStatistics{
157 MemoryUsageCommitBytes: 100,
158 MemoryUsageCommitPeakBytes: 100,
159 MemoryUsagePrivateWorkingSetBytes: 100,
160 },
161 Storage: &stats.WindowsContainerStorageStatistics{
162 ReadCountNormalized: 100,
163 ReadSizeBytes: 100,
164 WriteCountNormalized: 100,
165 WriteSizeBytes: 100,
166 },
167 },
168 },
169 VM: &stats.VirtualMachineStatistics{
170 Processor: &stats.VirtualMachineProcessorStatistics{
171 TotalRuntimeNS: 100,
172 },
173 Memory: &stats.VirtualMachineMemoryStatistics{
174 WorkingSetBytes: 100,
175 },
176 },
177 }
178 }
179
180 func getLCOWTestStats() *stats.Statistics {
181 return &stats.Statistics{
182 Container: &stats.Statistics_Linux{
183 Linux: &v1.Metrics{
184 CPU: &v1.CPUStat{
185 Usage: &v1.CPUUsage{
186 Total: 100,
187 },
188 },
189 Memory: &v1.MemoryStat{
190 TotalInactiveFile: 100,
191 Usage: &v1.MemoryEntry{
192 Usage: 200,
193 },
194 },
195 },
196 },
197 VM: &stats.VirtualMachineStatistics{
198 Processor: &stats.VirtualMachineProcessorStatistics{
199 TotalRuntimeNS: 100,
200 },
201 Memory: &stats.VirtualMachineMemoryStatistics{
202 WorkingSetBytes: 100,
203 },
204 },
205 }
206 }
207
View as plain text