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