@@ -17,6 +17,7 @@ limitations under the License.
1717package cpuburst
1818
1919import (
20+ "fmt"
2021 "testing"
2122
2223 "github.com/bytedance/mockey"
@@ -242,12 +243,128 @@ func TestManagerImpl_UpdateCPUBurst(t *testing.T) {
242243 "/sys/fs/cgroup/cpu/test-pod-2/test-container-2-id" : 50 ,
243244 },
244245 },
246+ {
247+ name : "get container ID fails returns error" ,
248+ pods : []* v1.Pod {
249+ {
250+ ObjectMeta : metav1.ObjectMeta {
251+ UID : "test-pod" ,
252+ Annotations : map [string ]string {
253+ consts .PodAnnotationQoSLevelKey : consts .PodAnnotationQoSLevelDedicatedCores ,
254+ consts .PodAnnotationCPUEnhancementKey : `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}` ,
255+ },
256+ },
257+ Spec : v1.PodSpec {
258+ Containers : []v1.Container {
259+ {
260+ Name : "test-container-1" ,
261+ },
262+ {
263+ Name : "test-container-2" ,
264+ },
265+ },
266+ },
267+ },
268+ },
269+ wantErr : true ,
270+ },
271+ {
272+ name : "Checking container cgroup exists fails, returns error" ,
273+ pods : []* v1.Pod {
274+ {
275+ ObjectMeta : metav1.ObjectMeta {
276+ UID : "test-pod-1" ,
277+ Annotations : map [string ]string {
278+ consts .PodAnnotationQoSLevelKey : consts .PodAnnotationQoSLevelDedicatedCores ,
279+ consts .PodAnnotationCPUEnhancementKey : `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}` ,
280+ },
281+ },
282+ Spec : v1.PodSpec {
283+ Containers : []v1.Container {
284+ {
285+ Name : "test-container-1" ,
286+ },
287+ {
288+ Name : "test-container-2" ,
289+ },
290+ },
291+ },
292+ Status : v1.PodStatus {
293+ ContainerStatuses : []v1.ContainerStatus {
294+ {
295+ Name : "test-container-1" ,
296+ ContainerID : "test-container-1-id" ,
297+ },
298+ {
299+ Name : "test-container-2" ,
300+ ContainerID : "test-container-2-id" ,
301+ },
302+ },
303+ },
304+ },
305+ },
306+ mocks : func (s resultState ) {
307+ mockey .Mock (common .IsContainerCgroupExist ).Return (false , fmt .Errorf ("test error" )).Build ()
308+ },
309+ wantErr : true ,
310+ },
311+ {
312+ name : "Container cgroup path does not exist, just skip" ,
313+ pods : []* v1.Pod {
314+ {
315+ ObjectMeta : metav1.ObjectMeta {
316+ UID : "test-pod-1" ,
317+ Annotations : map [string ]string {
318+ consts .PodAnnotationQoSLevelKey : consts .PodAnnotationQoSLevelDedicatedCores ,
319+ consts .PodAnnotationCPUEnhancementKey : `{"cpu_burst_policy":"static", "cpu_burst_percent":"50"}` ,
320+ },
321+ },
322+ Spec : v1.PodSpec {
323+ Containers : []v1.Container {
324+ {
325+ Name : "test-container-1" ,
326+ },
327+ {
328+ Name : "test-container-2" ,
329+ },
330+ },
331+ },
332+ Status : v1.PodStatus {
333+ ContainerStatuses : []v1.ContainerStatus {
334+ {
335+ Name : "test-container-1" ,
336+ ContainerID : "test-container-1-id" ,
337+ },
338+ {
339+ Name : "test-container-2" ,
340+ ContainerID : "test-container-2-id" ,
341+ },
342+ },
343+ },
344+ },
345+ },
346+ mocks : func (s resultState ) {
347+ mockey .Mock (common .IsContainerCgroupExist ).Return (false , nil ).Build ()
348+ mockey .Mock (common .GetContainerAbsCgroupPath ).To (func (_ , podUID , containerID string ) (string , error ) {
349+ return "/sys/fs/cgroup/cpu/" + podUID + "/" + containerID , nil
350+ }).Build ()
351+ mockey .Mock (manager .GetCPUWithAbsolutePath ).Return (& common.CPUStats {CpuQuota : 200 }, nil ).Build ()
352+ mockey .Mock (manager .ApplyCPUWithAbsolutePath ).
353+ To (func (absPath string , cpuData * common.CPUData ) error {
354+ s [absPath ] = cpuData .CpuBurst
355+ return nil
356+ }).Build ()
357+ },
358+ wantResults : make (resultState ),
359+ },
245360 }
246361
247362 for _ , tt := range tests {
248363 mockey .PatchConvey (tt .name , t , func () {
249364 results := make (resultState )
250- tt .mocks (results )
365+ if tt .mocks != nil {
366+ tt .mocks (results )
367+ }
251368
252369 cpuBurstManager := NewManager (generateTestMetaServer (tt .pods ))
253370
@@ -257,7 +374,9 @@ func TestManagerImpl_UpdateCPUBurst(t *testing.T) {
257374 return
258375 }
259376
260- assert .Equal (t , tt .wantResults , results )
377+ if ! tt .wantErr {
378+ assert .Equal (t , tt .wantResults , results )
379+ }
261380 })
262381 }
263382}
0 commit comments