1
2
3
4
19
20 package winstats
21
22 import (
23 "os"
24 "strconv"
25 "testing"
26 "time"
27
28 cadvisorapi "github.com/google/cadvisor/info/v1"
29 "github.com/google/uuid"
30 "github.com/stretchr/testify/assert"
31 "k8s.io/apimachinery/pkg/util/wait"
32 )
33
34 func TestMonitoring(t *testing.T) {
35 counterClient, err := NewPerfCounterClient()
36 assert.NoError(t, err)
37
38
39 assert.NotNil(t, counterClient.(*StatsClient).client.getNodeInfo())
40
41
42 if pollErr := wait.Poll(100*time.Millisecond, 5*perfCounterUpdatePeriod, func() (bool, error) {
43 metrics, _ := counterClient.(*StatsClient).client.getNodeMetrics()
44 if metrics.memoryPrivWorkingSetBytes != 0 {
45 return true, nil
46 }
47
48 return false, nil
49 }); pollErr != nil {
50 t.Fatalf("Encountered error: `%v'", pollErr)
51 }
52 }
53
54 func TestGetMachineInfo(t *testing.T) {
55 p := perfCounterNodeStatsClient{
56 nodeInfo: nodeInfo{
57 memoryPhysicalCapacityBytes: 100,
58 },
59 }
60
61 machineInfo, err := p.getMachineInfo()
62 assert.NoError(t, err)
63 assert.Equal(t, uint64(100), machineInfo.MemoryCapacity)
64 hostname, _ := os.Hostname()
65 assert.Equal(t, hostname, machineInfo.MachineID)
66
67
68 _, err = uuid.Parse(machineInfo.SystemUUID)
69 assert.NoError(t, err)
70
71 id, err := strconv.Atoi(machineInfo.BootID)
72 assert.NoError(t, err)
73 assert.NotZero(t, id)
74 }
75
76 func TestGetVersionInfo(t *testing.T) {
77 client := perfCounterNodeStatsClient{
78 nodeInfo: nodeInfo{
79 kernelVersion: "foo",
80 osImageVersion: "lish",
81 },
82 }
83
84 info, _ := client.getVersionInfo()
85 expected := &cadvisorapi.VersionInfo{
86 KernelVersion: "foo",
87 ContainerOsVersion: "lish",
88 }
89 assert.Equal(t, expected, info)
90 }
91
92 func TestCollectMetricsData(t *testing.T) {
93 p := perfCounterNodeStatsClient{}
94
95 cpuCounter := &fakePerfCounterImpl{
96 value: 1,
97 raiseError: true,
98 }
99 memWorkingSetCounter := &fakePerfCounterImpl{
100 value: 2,
101 raiseError: true,
102 }
103 memCommittedBytesCounter := &fakePerfCounterImpl{
104 value: 3,
105 raiseError: true,
106 }
107 networkAdapterCounter := newFakedNetworkCounters(true)
108
109
110 p.collectMetricsData(cpuCounter, memWorkingSetCounter, memCommittedBytesCounter, networkAdapterCounter)
111 metrics, _ := p.getNodeMetrics()
112 expectedMetrics := nodeMetrics{}
113 assert.Equal(t, expectedMetrics, metrics)
114
115 cpuCounter.raiseError = false
116 p.collectMetricsData(cpuCounter, memWorkingSetCounter, memCommittedBytesCounter, networkAdapterCounter)
117 metrics, _ = p.getNodeMetrics()
118 assert.Equal(t, expectedMetrics, metrics)
119
120 memWorkingSetCounter.raiseError = false
121 p.collectMetricsData(cpuCounter, memWorkingSetCounter, memCommittedBytesCounter, networkAdapterCounter)
122 metrics, _ = p.getNodeMetrics()
123 assert.Equal(t, expectedMetrics, metrics)
124
125 memCommittedBytesCounter.raiseError = false
126 p.collectMetricsData(cpuCounter, memWorkingSetCounter, memCommittedBytesCounter, networkAdapterCounter)
127 metrics, _ = p.getNodeMetrics()
128 assert.Equal(t, expectedMetrics, metrics)
129
130 networkAdapterCounter = newFakedNetworkCounters(false)
131 p.collectMetricsData(cpuCounter, memWorkingSetCounter, memCommittedBytesCounter, networkAdapterCounter)
132 metrics, _ = p.getNodeMetrics()
133 expectedMetrics = nodeMetrics{
134 cpuUsageCoreNanoSeconds: uint64(ProcessorCount()) * 1e7,
135 cpuUsageNanoCores: 0,
136 memoryPrivWorkingSetBytes: 2,
137 memoryCommittedBytes: 3,
138 interfaceStats: networkAdapterCounter.listInterfaceStats(),
139 timeStamp: time.Now(),
140 }
141 assert.Equal(t, expectedMetrics, metrics)
142 }
143
144 func TestConvertCPUValue(t *testing.T) {
145 testCases := []struct {
146 cpuValue uint64
147 expected uint64
148 }{
149 {cpuValue: uint64(50), expected: uint64(2000000000)},
150 {cpuValue: uint64(0), expected: uint64(0)},
151 {cpuValue: uint64(100), expected: uint64(4000000000)},
152 }
153 var cpuCores = 4
154
155 for _, tc := range testCases {
156 p := perfCounterNodeStatsClient{}
157 newValue := p.convertCPUValue(cpuCores, tc.cpuValue)
158 assert.Equal(t, tc.expected, newValue)
159 }
160 }
161
162 func TestGetCPUUsageNanoCores(t *testing.T) {
163
164 perfCounterUpdatePeriodSeconds := uint64(perfCounterUpdatePeriod / time.Second)
165 testCases := []struct {
166 latestValue uint64
167 previousValue uint64
168 expected uint64
169 }{
170 {latestValue: uint64(0), previousValue: uint64(0), expected: uint64(0)},
171 {latestValue: uint64(2000000000), previousValue: uint64(0), expected: uint64(200000000 * perfCounterUpdatePeriodSeconds)},
172 {latestValue: uint64(5000000000), previousValue: uint64(2000000000), expected: uint64(300000000 * perfCounterUpdatePeriodSeconds)},
173 }
174
175 for _, tc := range testCases {
176 p := perfCounterNodeStatsClient{}
177 p.cpuUsageCoreNanoSecondsCache = cpuUsageCoreNanoSecondsCache{
178 latestValue: tc.latestValue,
179 previousValue: tc.previousValue,
180 }
181 cpuUsageNanoCores := p.getCPUUsageNanoCores()
182 assert.Equal(t, tc.expected, cpuUsageNanoCores)
183 }
184 }
185
186 func testGetPhysicallyInstalledSystemMemoryBytes(t *testing.T) {
187 totalMemory, err := getPhysicallyInstalledSystemMemoryBytes()
188 assert.NoError(t, err)
189 assert.NotZero(t, totalMemory)
190 }
191
View as plain text