11package systemd
22
33import (
4+ "maps"
5+ "math"
46 "os"
57 "os/exec"
68 "reflect"
@@ -115,6 +117,7 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
115117 res map [string ]string
116118 expError bool
117119 expProps []systemdDbus.Property
120+ resAfter map [string ]string // If nil, res is expected to be unchanged.
118121 }{
119122 {
120123 name : "empty map" ,
@@ -233,13 +236,81 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
233236 },
234237 expError : true ,
235238 },
239+ {
240+ name : "cpu.max" ,
241+ minVer : cpuQuotaPeriodSupportedVersion ,
242+ res : map [string ]string {
243+ "cpu.max" : "500000 1000000" ,
244+ },
245+ expProps : []systemdDbus.Property {
246+ newProp ("CPUQuotaPeriodUSec" , uint64 (1000000 )),
247+ newProp ("CPUQuotaPerSecUSec" , uint64 (500000 )),
248+ },
249+ },
250+ {
251+ name : "cpu.max with round up" ,
252+ minVer : cpuQuotaPeriodSupportedVersion ,
253+ res : map [string ]string {
254+ "cpu.max" : "123456 100000" ,
255+ },
256+ expProps : []systemdDbus.Property {
257+ newProp ("CPUQuotaPeriodUSec" , uint64 (100000 )),
258+ newProp ("CPUQuotaPerSecUSec" , uint64 (1240000 )),
259+ },
260+ resAfter : map [string ]string {
261+ "cpu.max" : "124000 100000" ,
262+ },
263+ },
264+ {
265+ name : "cpu.max with round up, no period" ,
266+ minVer : cpuQuotaPeriodSupportedVersion ,
267+ res : map [string ]string {
268+ "cpu.max" : "123456" ,
269+ },
270+ expProps : []systemdDbus.Property {
271+ newProp ("CPUQuotaPeriodUSec" , uint64 (100000 )),
272+ newProp ("CPUQuotaPerSecUSec" , uint64 (1240000 )),
273+ },
274+ resAfter : map [string ]string {
275+ "cpu.max" : "124000" ,
276+ },
277+ },
278+ {
279+ name : "cpu.max too large" ,
280+ minVer : cpuQuotaPeriodSupportedVersion ,
281+ res : map [string ]string {
282+ "cpu.max" : "9223372036854700 100000" ,
283+ },
284+ expProps : []systemdDbus.Property {
285+ newProp ("CPUQuotaPeriodUSec" , uint64 (100000 )),
286+ newProp ("CPUQuotaPerSecUSec" , uint64 (math .MaxUint64 )),
287+ },
288+ resAfter : map [string ]string {
289+ "cpu.max" : "max 100000" ,
290+ },
291+ },
292+ {
293+ name : "cpu.max=max" ,
294+ minVer : cpuQuotaPeriodSupportedVersion ,
295+ res : map [string ]string {
296+ "cpu.max" : "max 100000" ,
297+ },
298+ expProps : []systemdDbus.Property {
299+ newProp ("CPUQuotaPeriodUSec" , uint64 (100000 )),
300+ newProp ("CPUQuotaPerSecUSec" , uint64 (math .MaxUint64 )),
301+ },
302+ },
236303 }
237304
238305 for _ , tc := range testCases {
239306 t .Run (tc .name , func (t * testing.T ) {
240307 if tc .minVer != 0 && systemdVersion (cm ) < tc .minVer {
241308 t .Skipf ("requires systemd >= %d" , tc .minVer )
242309 }
310+ resAfter := tc .resAfter
311+ if resAfter == nil {
312+ resAfter = maps .Clone (tc .res )
313+ }
243314 props , err := unifiedResToSystemdProps (cm , tc .res )
244315 if err != nil && ! tc .expError {
245316 t .Fatalf ("expected no error, got: %v" , err )
@@ -250,6 +321,9 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
250321 if ! reflect .DeepEqual (tc .expProps , props ) {
251322 t .Errorf ("wrong properties (exp %+v, got %+v)" , tc .expProps , props )
252323 }
324+ if ! maps .Equal (resAfter , tc .res ) {
325+ t .Errorf ("wrong resources (exp %+v, got %+v)" , resAfter , tc .res )
326+ }
253327 })
254328 }
255329}
@@ -288,6 +362,37 @@ func TestAddCPUQuota(t *testing.T) {
288362 expectedCPUQuotaPerSecUSec : 560000 ,
289363 expectedQuota : 504000 ,
290364 },
365+ {
366+ name : "Large quota with round up" ,
367+ quota : 10000000000001 ,
368+ expectedCPUQuotaPerSecUSec : 100000000010000 ,
369+ expectedQuota : 10000000001000 ,
370+ },
371+ {
372+ name : "Max quota" ,
373+ quota : maxCPUQuota ,
374+ period : 1500 ,
375+ expectedCPUQuotaPerSecUSec : 11728124029610000 ,
376+ expectedQuota : maxCPUQuota ,
377+ },
378+ {
379+ name : "Max quota, round up above max" ,
380+ quota : maxCPUQuota ,
381+ expectedCPUQuotaPerSecUSec : math .MaxUint64 ,
382+ expectedQuota : - 1 ,
383+ },
384+ {
385+ name : "Quota too large" ,
386+ quota : maxCPUQuota + 1 ,
387+ expectedCPUQuotaPerSecUSec : math .MaxUint64 ,
388+ expectedQuota : - 1 ,
389+ },
390+ {
391+ name : "Max int64 quota" ,
392+ quota : math .MaxInt64 ,
393+ expectedCPUQuotaPerSecUSec : math .MaxUint64 ,
394+ expectedQuota : - 1 ,
395+ },
291396 }
292397
293398 for _ , tc := range testCases {
0 commit comments