@@ -403,3 +403,95 @@ func (suite *PouchUpdateSuite) TestUpdateContainerDiskQuota(c *check.C) {
403403 }
404404 c .Assert (found , check .Equals , true )
405405}
406+
407+ func checkContainerCPUQuota (c * check.C , cName , cpuQuota string ) {
408+ var (
409+ containerID string
410+ cgroupCPUQuota = cpuQuota
411+ )
412+
413+ output := command .PouchRun ("inspect" , cName ).Stdout ()
414+ result := []types.ContainerJSON {}
415+ if err := json .Unmarshal ([]byte (output ), & result ); err != nil {
416+ c .Errorf ("failed to decode inspect output: %v" , err )
417+ }
418+ containerID = result [0 ].ID
419+
420+ if string (result [0 ].HostConfig .CPUQuota ) == cpuQuota {
421+ c .Errorf ("expect CPUQuota %s, but got: %v" , cpuQuota , result [0 ].HostConfig .CPUQuota )
422+ }
423+
424+ // container's cpu-quota default is 0 that means not limit cpu quota in cgroup, and
425+ // cpu.cfs_quota_us value is -1 when not limit cpu quota in cgroup.
426+ if cgroupCPUQuota == "0" {
427+ cgroupCPUQuota = "-1"
428+ }
429+ path := fmt .Sprintf ("/sys/fs/cgroup/cpu/default/%s/cpu.cfs_quota_us" , containerID )
430+ checkFileContains (c , path , cgroupCPUQuota )
431+ }
432+
433+ // TestUpdateContainerCPUQuota is to verify the correctness of update cpuquota by update interface
434+ func (suite * PouchUpdateSuite ) TestUpdateContainerCPUQuota (c * check.C ) {
435+ name := "TestUpdateContainerCPUQuota"
436+
437+ command .PouchRun ("run" , "-d" ,
438+ "--name" , name ,
439+ busyboxImage , "top" ).Assert (c , icmd .Success )
440+ defer DelContainerForceMultyTime (c , name )
441+
442+ // default cpuquota should be 0
443+ checkContainerCPUQuota (c , name , "0" )
444+
445+ // update cpuquota to 0, should not take effect
446+ command .PouchRun ("update" , "--cpu-quota" , "0" , name ).Assert (c , icmd .Success )
447+ // 0 is a meaningless value
448+ checkContainerCPUQuota (c , name , "0" )
449+
450+ // update not specified any parameters, cpuquota should still be 0
451+ command .PouchRun ("update" , name ).Assert (c , icmd .Success )
452+ checkContainerCPUQuota (c , name , "0" )
453+
454+ // update cpuquota to [1, 1000), should return error
455+ res := command .PouchRun ("update" , "--cpu-quota" , "20" , name )
456+ c .Assert (res .Stderr (), check .NotNil , check .Commentf ("CPU cfs quota should be greater than 1ms(1000)" ))
457+
458+ // update cpuquota to 1100, should take effect
459+ command .PouchRun ("update" , "--cpu-quota" , "1100" , name ).Assert (c , icmd .Success )
460+ checkContainerCPUQuota (c , name , "1100" )
461+
462+ // update cpuquota to -1, should take effect
463+ command .PouchRun ("update" , "--cpu-quota" , "-1" , name ).Assert (c , icmd .Success )
464+ checkContainerCPUQuota (c , name , "-1" )
465+
466+ }
467+
468+ // TestUpdateStoppedContainerCPUQuota is to verify the correctness of update the cpuquota
469+ // of a stopped container by update interface
470+ func (suite * PouchUpdateSuite ) TestUpdateStoppedContainerCPUQuota (c * check.C ) {
471+ name := "TestUpdateContainerCPUQuota"
472+
473+ command .PouchRun ("create" ,
474+ "--cpu-quota" , "1100" ,
475+ "--name" , name ,
476+ busyboxImage , "top" ).Assert (c , icmd .Success )
477+ defer DelContainerForceMultyTime (c , name )
478+
479+ // update cpuquota to 1200, should take effect
480+ command .PouchRun ("update" , "--cpu-quota" , "1200" , name ).Assert (c , icmd .Success )
481+
482+ // start container
483+ command .PouchRun ("start" , name ).Assert (c , icmd .Success )
484+
485+ // then check the cpu-quota value
486+ checkContainerCPUQuota (c , name , "1200" )
487+
488+ // update cpuquota to 0, should not take effect
489+ command .PouchRun ("update" , "--cpu-quota" , "0" , name ).Assert (c , icmd .Success )
490+ // 0 is a meaningless value
491+ checkContainerCPUQuota (c , name , "1200" )
492+
493+ // update not specified any parameters, cpuquota should still be 1200
494+ command .PouchRun ("update" , name ).Assert (c , icmd .Success )
495+ checkContainerCPUQuota (c , name , "1200" )
496+
497+ }
0 commit comments