@@ -75,6 +75,38 @@ full avg10=1.00 avg60=1.01 avg300=1.00 total=157622356`
7575 assert .Equal (t , & st .Full , & expected .Full )
7676}
7777
78+ // TestConvertCPUSharesToCgroupV2Value tests the ConvertCPUSharesToCgroupV2Value function.
79+ // Taken from https://github.com/opencontainers/cgroups/blob/v0.0.5/utils_test.go#L537-L564
80+ // (Apache License 2.0)
81+ func TestConvertCPUSharesToCgroupV2Value (t * testing.T ) {
82+ const (
83+ sharesMin = 2
84+ sharesMax = 262144
85+ sharesDef = 1024
86+ weightMin = 1
87+ weightMax = 10000
88+ weightDef = 100
89+ unset = 0
90+ )
91+ cases := map [uint64 ]uint64 {
92+ unset : unset ,
93+
94+ sharesMin - 1 : weightMin , // Below the minimum (out of range).
95+ sharesMin : weightMin , // Minimum.
96+ sharesMin + 1 : weightMin + 1 , // Just above the minimum.
97+ sharesDef : weightDef , // Default.
98+ sharesMax - 1 : weightMax , // Just below the maximum.
99+ sharesMax : weightMax , // Maximum.
100+ sharesMax + 1 : weightMax , // Above the maximum (out of range).
101+ }
102+ for shares , want := range cases {
103+ got := ConvertCPUSharesToCgroupV2Value (shares )
104+ if got != want {
105+ t .Errorf ("ConvertCPUSharesToCgroupV2Value(%d): got %d, want %d" , shares , got , want )
106+ }
107+ }
108+ }
109+
78110func TestToResources (t * testing.T ) {
79111 var (
80112 quota int64 = 8000
@@ -84,7 +116,7 @@ func TestToResources(t *testing.T) {
84116 mem int64 = 300
85117 swap int64 = 500
86118 )
87- weight := 1 + (( shares - 2 ) * 9999 ) / 262142
119+ weight := ConvertCPUSharesToCgroupV2Value ( shares )
88120 res := specs.LinuxResources {
89121 CPU : & specs.LinuxCPU {Quota : & quota , Period : & period , Shares : & shares },
90122 Memory : & specs.LinuxMemory {Limit : & mem , Swap : & swap },
0 commit comments