1package main
2
3import (
4	"reflect"
5	"testing"
6
7	"github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats"
8	v1 "github.com/containerd/cgroups/stats/v1"
9	"github.com/pkg/errors"
10)
11
12func verifyExpectedError(t *testing.T, resp interface{}, actual, expected error) {
13	if actual == nil || errors.Cause(actual) != expected {
14		t.Fatalf("expected error: %v, got: %v", expected, actual)
15	}
16
17	isnil := false
18	ty := reflect.TypeOf(resp)
19	if ty == nil {
20		isnil = true
21	} else {
22		isnil = reflect.ValueOf(resp).IsNil()
23	}
24	if !isnil {
25		t.Fatalf("expect nil response for error return, got: %v", resp)
26	}
27}
28
29func verifyExpectedStats(t *testing.T, isWCOW, ownsHost bool, s *stats.Statistics) {
30	if isWCOW {
31		verifyExpectedWindowsContainerStatistics(t, s.GetWindows())
32	} else {
33		verifyExpectedCgroupMetrics(t, s.GetLinux())
34	}
35	if ownsHost {
36		verifyExpectedVirtualMachineStatistics(t, s.VM)
37	}
38}
39
40func verifyExpectedWindowsContainerStatistics(t *testing.T, w *stats.WindowsContainerStatistics) {
41	if w == nil {
42		t.Fatal("expected non-nil WindowsContainerStatistics")
43	}
44	if w.UptimeNS != 100 {
45		t.Fatalf("expected WindowsContainerStatistics.UptimeNS == 100, got: %d", w.UptimeNS)
46	}
47	if w.Processor == nil {
48		t.Fatal("expected non-nil WindowsContainerStatistics.Processor")
49	}
50	if w.Processor.TotalRuntimeNS != 100 {
51		t.Fatalf("expected WindowsContainerStatistics.Processor.TotalRuntimeNS == 100, got: %d", w.Processor.TotalRuntimeNS)
52	}
53	if w.Processor.RuntimeUserNS != 100 {
54		t.Fatalf("expected WindowsContainerStatistics.Processor.RuntimeUserNS == 100, got: %d", w.Processor.RuntimeUserNS)
55	}
56	if w.Processor.RuntimeKernelNS != 100 {
57		t.Fatalf("expected WindowsContainerStatistics.Processor.RuntimeKernelNS == 100, got: %d", w.Processor.RuntimeKernelNS)
58	}
59	if w.Memory == nil {
60		t.Fatal("expected non-nil WindowsContainerStatistics.Memory")
61	}
62	if w.Memory.MemoryUsageCommitBytes != 100 {
63		t.Fatalf("expected WindowsContainerStatistics.Memory.MemoryUsageCommitBytes == 100, got: %d", w.Memory.MemoryUsageCommitBytes)
64	}
65	if w.Memory.MemoryUsageCommitPeakBytes != 100 {
66		t.Fatalf("expected WindowsContainerStatistics.Memory.MemoryUsageCommitPeakBytes == 100, got: %d", w.Memory.MemoryUsageCommitPeakBytes)
67	}
68	if w.Memory.MemoryUsagePrivateWorkingSetBytes != 100 {
69		t.Fatalf("expected WindowsContainerStatistics.Memory.MemoryUsagePrivateWorkingSetBytes == 100, got: %d", w.Memory.MemoryUsagePrivateWorkingSetBytes)
70	}
71	if w.Storage == nil {
72		t.Fatal("expected non-nil WindowsContainerStatistics.Memory")
73	}
74	if w.Storage.ReadCountNormalized != 100 {
75		t.Fatalf("expected WindowsContainerStatistics.Storage.ReadCountNormalized == 100, got: %d", w.Storage.ReadCountNormalized)
76	}
77	if w.Storage.ReadSizeBytes != 100 {
78		t.Fatalf("expected WindowsContainerStatistics.Storage.ReadSizeBytes == 100, got: %d", w.Storage.ReadSizeBytes)
79	}
80	if w.Storage.WriteCountNormalized != 100 {
81		t.Fatalf("expected WindowsContainerStatistics.Storage.WriteCountNormalized == 100, got: %d", w.Storage.WriteCountNormalized)
82	}
83	if w.Storage.WriteSizeBytes != 100 {
84		t.Fatalf("expected WindowsContainerStatistics.Storage.WriteSizeBytes == 100, got: %d", w.Storage.WriteSizeBytes)
85	}
86}
87
88func verifyExpectedCgroupMetrics(t *testing.T, v *v1.Metrics) {
89	if v == nil {
90		t.Fatal("expected non-nil cgroups Metrics")
91	}
92	if v.CPU == nil {
93		t.Fatal("expected non-nil Metrics.CPU")
94	}
95	if v.CPU.Usage.Total != 100 {
96		t.Fatalf("Expected Metrics.CPU.Usage == 100, got: %d", v.CPU.Usage)
97	}
98	if v.Memory == nil {
99		t.Fatal("expected non-nil Metrics.Memory")
100	}
101	if v.Memory.TotalInactiveFile != 100 {
102		t.Fatalf("Expected Metrics.Memory.TotalInactiveFile == 100, got: %d", v.Memory.TotalInactiveFile)
103	}
104	if v.Memory.Usage == nil {
105		t.Fatal("expected non-nil Metrics.Memory.Usage")
106	}
107	if v.Memory.Usage.Usage != 200 {
108		t.Fatalf("Expected Metrics.Memory.Usage.Usage == 200, got: %d", v.Memory.Usage.Usage)
109	}
110}
111
112func verifyExpectedVirtualMachineStatistics(t *testing.T, v *stats.VirtualMachineStatistics) {
113	if v == nil {
114		t.Fatal("expected non-nil VirtualMachineStatistics")
115	}
116	if v.Processor == nil {
117		t.Fatal("expected non-nil VirtualMachineStatistics.Processor")
118	}
119	if v.Processor.TotalRuntimeNS != 100 {
120		t.Fatalf("expected VirtualMachineStatistics.Processor.TotalRuntimeNS == 100, got: %d", v.Processor.TotalRuntimeNS)
121	}
122	if v.Memory == nil {
123		t.Fatal("expected non-nil VirtualMachineStatistics.Memory")
124	}
125	if v.Memory.WorkingSetBytes != 100 {
126		t.Fatalf("expected VirtualMachineStatistics.Memory.WorkingSetBytes == 100, got: %d", v.Memory.WorkingSetBytes)
127	}
128}
129