@@ -83,22 +83,72 @@ func TestSystemdCgroupCpuController_NilWeight(t *testing.T) {
83
83
}
84
84
85
85
func TestExtractQuotaAndPeriod (t * testing.T ) {
86
- var (
87
- period uint64
88
- quota int64
86
+ const (
87
+ defaultQuota int64 = math . MaxInt64
88
+ defaultPeriod uint64 = 100000
89
89
)
90
- quota = 10000
91
- period = 8000
92
- cpuMax := NewCPUMax (& quota , & period )
93
- tquota , tPeriod := cpuMax .extractQuotaAndPeriod ()
94
-
95
- assert .Equal (t , quota , tquota )
96
- assert .Equal (t , period , tPeriod )
97
-
98
- // case with nil quota which makes it "max" - max int val
99
- cpuMax2 := NewCPUMax (nil , & period )
100
- tquota2 , tPeriod2 := cpuMax2 .extractQuotaAndPeriod ()
101
90
102
- assert .Equal (t , int64 (math .MaxInt64 ), tquota2 )
103
- assert .Equal (t , period , tPeriod2 )
91
+ require .Equal (t , defaultCPUMaxPeriodStr , strconv .Itoa (defaultCPUMaxPeriod ), "Constant for default period does not match its string type constant." )
92
+
93
+ // Default "max 100000"
94
+ cpuMax := NewCPUMax (nil , nil )
95
+ assert .Equal (t , CPUMax ("max 100000" ), cpuMax )
96
+ quota , period , err := cpuMax .extractQuotaAndPeriod ()
97
+ assert .NoError (t , err )
98
+ assert .Equal (t , defaultQuota , quota )
99
+ assert .Equal (t , defaultPeriod , period )
100
+
101
+ // Only specifing limit is valid.
102
+ cpuMax = CPUMax ("max" )
103
+ quota , period , err = cpuMax .extractQuotaAndPeriod ()
104
+ assert .NoError (t , err )
105
+ assert .Equal (t , defaultQuota , quota )
106
+ assert .Equal (t , defaultPeriod , period )
107
+
108
+ tests := []struct {
109
+ cpuMax string
110
+ quota int64
111
+ period uint64
112
+ }{
113
+ {
114
+ cpuMax : "0 0" ,
115
+ quota : 0 ,
116
+ period : 0 ,
117
+ },
118
+ {
119
+ cpuMax : "10000 8000" ,
120
+ quota : 10000 ,
121
+ period : 8000 ,
122
+ },
123
+ {
124
+ cpuMax : "42000 4200" ,
125
+ quota : 42000 ,
126
+ period : 4200 ,
127
+ },
128
+ {
129
+ cpuMax : "9223372036854775807 18446744073709551615" ,
130
+ quota : 9223372036854775807 ,
131
+ period : 18446744073709551615 ,
132
+ },
133
+ }
134
+
135
+ for _ , test := range tests {
136
+ t .Run (test .cpuMax , func (t * testing.T ) {
137
+ cpuMax := NewCPUMax (& test .quota , & test .period )
138
+ assert .Equal (t , CPUMax (test .cpuMax ), cpuMax )
139
+
140
+ tquota , tPeriod , err := cpuMax .extractQuotaAndPeriod ()
141
+ assert .NoError (t , err )
142
+ assert .Equal (t , test .quota , tquota )
143
+ assert .Equal (t , test .period , tPeriod )
144
+ })
145
+ }
146
+
147
+ // Negative test cases result in errors.
148
+ for i , cpuMax := range []string {"" , " " , "max 100000 100000" } {
149
+ t .Run (fmt .Sprintf ("negative-test-%d" , i + 1 ), func (t * testing.T ) {
150
+ _ , _ , err = CPUMax (cpuMax ).extractQuotaAndPeriod ()
151
+ assert .Error (t , err )
152
+ })
153
+ }
104
154
}
0 commit comments