@@ -86,6 +86,11 @@ func TestStatter(t *testing.T) {
86
86
}
87
87
}
88
88
89
+ // Other times we just want things to run fast.
90
+ withNoWait := func (s * Statter ) {
91
+ s .wait = func (time.Duration ) {}
92
+ }
93
+
89
94
// We don't want to use the actual host CPU here.
90
95
withNproc := func (n int ) Option {
91
96
return func (s * Statter ) {
@@ -98,47 +103,25 @@ func TestStatter(t *testing.T) {
98
103
// we control the data.
99
104
t .Run ("CGroupV1" , func (t * testing.T ) {
100
105
t .Parallel ()
101
-
102
- t .Run ("Limit" , func (t * testing.T ) {
106
+ t .Run ("ContainerCPU/Limit" , func (t * testing.T ) {
103
107
t .Parallel ()
104
-
105
- t .Run ("ContainerCPU" , func (t * testing.T ) {
106
- t .Parallel ()
107
- fs := initFS (t , fsContainerCgroupV1 )
108
- fakeWait := func (time.Duration ) {
109
- // Fake 1 second in ns of usage
110
- mungeFS (t , fs , cgroupV1CPUAcctUsage , "1000000000" )
111
- }
112
- s , err := New (WithFS (fs ), withWait (fakeWait ))
113
- require .NoError (t , err )
114
- cpu , err := s .ContainerCPU ()
115
- require .NoError (t , err )
116
- require .NotNil (t , cpu )
117
- assert .Equal (t , 1.0 , cpu .Used )
118
- require .NotNil (t , cpu .Total )
119
- assert .Equal (t , 2.5 , * cpu .Total )
120
- assert .Equal (t , "cores" , cpu .Unit )
121
- })
122
-
123
- t .Run ("ContainerMemory" , func (t * testing.T ) {
124
- t .Parallel ()
125
- fs := initFS (t , fsContainerCgroupV1 )
126
- fakeWait := func (time.Duration ) {
127
- // Fake 1 second in ns of usage
128
- mungeFS (t , fs , cgroupV1CPUAcctUsage , "1000000000" )
129
- }
130
- s , err := New (WithFS (fs ), withWait (fakeWait ))
131
- require .NoError (t , err )
132
- mem , err := s .ContainerMemory ()
133
- require .NoError (t , err )
134
- require .NotNil (t , mem )
135
- assert .Equal (t , 0.25 , mem .Used )
136
- assert .Equal (t , 1.0 , * mem .Total )
137
- assert .Equal (t , "GB" , mem .Unit )
138
- })
108
+ fs := initFS (t , fsContainerCgroupV1 )
109
+ fakeWait := func (time.Duration ) {
110
+ // Fake 1 second in ns of usage
111
+ mungeFS (t , fs , cgroupV1CPUAcctUsage , "1000000000" )
112
+ }
113
+ s , err := New (WithFS (fs ), withWait (fakeWait ))
114
+ require .NoError (t , err )
115
+ cpu , err := s .ContainerCPU ()
116
+ require .NoError (t , err )
117
+ require .NotNil (t , cpu )
118
+ assert .Equal (t , 1.0 , cpu .Used )
119
+ require .NotNil (t , cpu .Total )
120
+ assert .Equal (t , 2.5 , * cpu .Total )
121
+ assert .Equal (t , "cores" , cpu .Unit )
139
122
})
140
123
141
- t .Run ("NoCPULimit " , func (t * testing.T ) {
124
+ t .Run ("ContainerCPU/NoLimit " , func (t * testing.T ) {
142
125
t .Parallel ()
143
126
fs := initFS (t , fsContainerCgroupV1NoLimit )
144
127
fakeWait := func (time.Duration ) {
@@ -155,55 +138,48 @@ func TestStatter(t *testing.T) {
155
138
assert .Equal (t , 2.0 , * cpu .Total )
156
139
assert .Equal (t , "cores" , cpu .Unit )
157
140
})
141
+
142
+ t .Run ("ContainerMemory" , func (t * testing.T ) {
143
+ t .Parallel ()
144
+ fs := initFS (t , fsContainerCgroupV1 )
145
+ s , err := New (WithFS (fs ), withNoWait )
146
+ require .NoError (t , err )
147
+ mem , err := s .ContainerMemory ()
148
+ require .NoError (t , err )
149
+ require .NotNil (t , mem )
150
+ assert .Equal (t , 0.25 , mem .Used )
151
+ assert .Equal (t , 1.0 , * mem .Total )
152
+ assert .Equal (t , "GB" , mem .Unit )
153
+ })
158
154
})
159
155
160
156
t .Run ("CGroupV2" , func (t * testing.T ) {
161
157
t .Parallel ()
162
- t .Run ("Limit" , func (t * testing.T ) {
163
- t .Parallel ()
164
- t .Run ("ContainerCPU" , func (t * testing.T ) {
165
- t .Parallel ()
166
- fs := initFS (t , fsContainerCgroupV2 )
167
- fakeWait := func (time.Duration ) {
168
- // Fake 1 second in ns of usage
169
- mungeFS (t , fs , cgroupV1CPUAcctUsage , "10000000000" )
170
- }
171
- s , err := New (WithFS (fs ), withWait (fakeWait ))
172
- require .NoError (t , err )
173
- cpu , err := s .ContainerCPU ()
174
- require .NoError (t , err )
175
- require .NotNil (t , cpu )
176
- assert .Equal (t , 1.0 , cpu .Used )
177
- require .NotNil (t , cpu .Total )
178
- assert .Equal (t , 2.5 , * cpu .Total )
179
- assert .Equal (t , "cores" , cpu .Unit )
180
- })
181
158
182
- t .Run ("ContainerMemory" , func (t * testing.T ) {
183
- t .Parallel ()
184
- fs := initFS (t , fsContainerCgroupV2 )
185
- fakeWait := func (time.Duration ) {
186
- // Fake 1 second in ns of usage
187
- mungeFS (t , fs , cgroupV1CPUAcctUsage , "10000000000" )
188
- }
189
- s , err := New (WithFS (fs ), withWait (fakeWait ))
190
- require .NoError (t , err )
191
- mem , err := s .ContainerMemory ()
192
- require .NoError (t , err )
193
- require .NotNil (t , mem )
194
- assert .Equal (t , 0.25 , mem .Used )
195
- assert .NotNil (t , mem .Total )
196
- assert .Equal (t , 1.0 , * mem .Total )
197
- assert .Equal (t , "GB" , mem .Unit )
198
- })
159
+ t .Run ("ContainerCPU/Limit" , func (t * testing.T ) {
160
+ t .Parallel ()
161
+ fs := initFS (t , fsContainerCgroupV2 )
162
+ fakeWait := func (time.Duration ) {
163
+ // Fake 1 second in ns of usage
164
+ mungeFS (t , fs , cgroupV2CPUStat , "usage_usec 1000000" )
165
+ }
166
+ s , err := New (WithFS (fs ), withWait (fakeWait ))
167
+ require .NoError (t , err )
168
+ cpu , err := s .ContainerCPU ()
169
+ require .NoError (t , err )
170
+ require .NotNil (t , cpu )
171
+ assert .Equal (t , 1.0 , cpu .Used )
172
+ require .NotNil (t , cpu .Total )
173
+ assert .Equal (t , 2.5 , * cpu .Total )
174
+ assert .Equal (t , "cores" , cpu .Unit )
199
175
})
200
176
201
- t .Run ("NoCPULimit " , func (t * testing.T ) {
177
+ t .Run ("ContainerCPU/NoLimit " , func (t * testing.T ) {
202
178
t .Parallel ()
203
179
fs := initFS (t , fsContainerCgroupV2NoLimit )
204
180
fakeWait := func (time.Duration ) {
205
181
// Fake 1 second in ns of usage
206
- mungeFS (t , fs , cgroupV1CPUAcctUsage , "100000 " )
182
+ mungeFS (t , fs , cgroupV2CPUStat , "usage_usec 1000000 " )
207
183
}
208
184
s , err := New (WithFS (fs ), withNproc (2 ), withWait (fakeWait ))
209
185
require .NoError (t , err )
@@ -215,6 +191,20 @@ func TestStatter(t *testing.T) {
215
191
assert .Equal (t , 2.0 , * cpu .Total )
216
192
assert .Equal (t , "cores" , cpu .Unit )
217
193
})
194
+
195
+ t .Run ("ContainerMemory" , func (t * testing.T ) {
196
+ t .Parallel ()
197
+ fs := initFS (t , fsContainerCgroupV2 )
198
+ s , err := New (WithFS (fs ), withNoWait )
199
+ require .NoError (t , err )
200
+ mem , err := s .ContainerMemory ()
201
+ require .NoError (t , err )
202
+ require .NotNil (t , mem )
203
+ assert .Equal (t , 0.25 , mem .Used )
204
+ assert .NotNil (t , mem .Total )
205
+ assert .Equal (t , 1.0 , * mem .Total )
206
+ assert .Equal (t , "GB" , mem .Unit )
207
+ })
218
208
})
219
209
}
220
210
0 commit comments