1package main
2
3import (
4	"context"
5	"time"
6
7	"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
8	"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
9	"github.com/Microsoft/hcsshim/internal/shimdiag"
10	v1 "github.com/containerd/cgroups/stats/v1"
11	"github.com/containerd/containerd/errdefs"
12	"github.com/containerd/containerd/runtime/v2/task"
13	specs "github.com/opencontainers/runtime-spec/specs-go"
14	"github.com/pkg/errors"
15)
16
17var _ = (shimTask)(&testShimTask{})
18
19type testShimTask struct {
20	id string
21
22	isWCOW bool
23	exec   *testShimExec
24	execs  map[string]*testShimExec
25}
26
27func (tst *testShimTask) ID() string {
28	return tst.id
29}
30
31func (tst *testShimTask) CreateExec(ctx context.Context, req *task.ExecProcessRequest, s *specs.Process) error {
32	return errdefs.ErrNotImplemented
33}
34
35func (tst *testShimTask) GetExec(eid string) (shimExec, error) {
36	if eid == "" {
37		return tst.exec, nil
38	}
39	e, ok := tst.execs[eid]
40	if ok {
41		return e, nil
42	}
43	return nil, errdefs.ErrNotFound
44}
45
46func (tst *testShimTask) KillExec(ctx context.Context, eid string, signal uint32, all bool) error {
47	e, err := tst.GetExec(eid)
48	if err != nil {
49		return err
50	}
51	return e.Kill(ctx, signal)
52}
53
54func (tst *testShimTask) DeleteExec(ctx context.Context, eid string) (int, uint32, time.Time, error) {
55	e, err := tst.GetExec(eid)
56	if err != nil {
57		return 0, 0, time.Time{}, err
58	}
59	status := e.Status()
60	if eid != "" {
61		delete(tst.execs, eid)
62	}
63	return int(status.Pid), status.ExitStatus, status.ExitedAt, nil
64}
65
66func (tst *testShimTask) Pids(ctx context.Context) ([]options.ProcessDetails, error) {
67	pairs := []options.ProcessDetails{
68		{
69			ProcessID: uint32(tst.exec.Pid()),
70			ExecID:    tst.exec.ID(),
71		},
72	}
73	for _, p := range tst.execs {
74		pairs = append(pairs, options.ProcessDetails{
75			ProcessID: uint32(p.pid),
76			ExecID:    p.id,
77		})
78	}
79	return pairs, nil
80}
81
82func (tst *testShimTask) Wait() *task.StateResponse {
83	return tst.exec.Wait()
84}
85
86func (tst *testShimTask) ExecInHost(ctx context.Context, req *shimdiag.ExecProcessRequest) (int, error) {
87	return 0, errors.New("not implemented")
88}
89
90func (tst *testShimTask) DumpGuestStacks(ctx context.Context) string {
91	return ""
92}
93
94func (tst *testShimTask) Stats(ctx context.Context) (*stats.Statistics, error) {
95	if tst.isWCOW {
96		return getWCOWTestStats(), nil
97	}
98	return getLCOWTestStats(), nil
99}
100
101func getWCOWTestStats() *stats.Statistics {
102	return &stats.Statistics{
103		Container: &stats.Statistics_Windows{
104			Windows: &stats.WindowsContainerStatistics{
105				UptimeNS: 100,
106				Processor: &stats.WindowsContainerProcessorStatistics{
107					TotalRuntimeNS:  100,
108					RuntimeUserNS:   100,
109					RuntimeKernelNS: 100,
110				},
111				Memory: &stats.WindowsContainerMemoryStatistics{
112					MemoryUsageCommitBytes:            100,
113					MemoryUsageCommitPeakBytes:        100,
114					MemoryUsagePrivateWorkingSetBytes: 100,
115				},
116				Storage: &stats.WindowsContainerStorageStatistics{
117					ReadCountNormalized:  100,
118					ReadSizeBytes:        100,
119					WriteCountNormalized: 100,
120					WriteSizeBytes:       100,
121				},
122			},
123		},
124		VM: &stats.VirtualMachineStatistics{
125			Processor: &stats.VirtualMachineProcessorStatistics{
126				TotalRuntimeNS: 100,
127			},
128			Memory: &stats.VirtualMachineMemoryStatistics{
129				WorkingSetBytes: 100,
130			},
131		},
132	}
133}
134
135func getLCOWTestStats() *stats.Statistics {
136	return &stats.Statistics{
137		Container: &stats.Statistics_Linux{
138			Linux: &v1.Metrics{
139				CPU: &v1.CPUStat{
140					Usage: &v1.CPUUsage{
141						Total: 100,
142					},
143				},
144				Memory: &v1.MemoryStat{
145					TotalInactiveFile: 100,
146					Usage: &v1.MemoryEntry{
147						Usage: 200,
148					},
149				},
150			},
151		},
152		VM: &stats.VirtualMachineStatistics{
153			Processor: &stats.VirtualMachineProcessorStatistics{
154				TotalRuntimeNS: 100,
155			},
156			Memory: &stats.VirtualMachineMemoryStatistics{
157				WorkingSetBytes: 100,
158			},
159		},
160	}
161}
162