@@ -24,6 +24,7 @@ import (
2424 "math"
2525 "os"
2626 "path/filepath"
27+ "strconv"
2728 "strings"
2829 "testing"
2930)
@@ -539,3 +540,45 @@ func TestWithRootFromTestdata(t *testing.T) {
539540 })
540541 }
541542}
543+
544+ // TestIntegrationCgroupLimits calls the real CPUQuota() and MemoryLimit()
545+ // functions against the live kernel cgroup interface. It is intended to run
546+ // inside a Docker container started with --cpus and --memory flags.
547+ //
548+ // The test is skipped unless CGROUP_EXPECTED_CPU_QUOTA and
549+ // CGROUP_EXPECTED_MEMORY_LIMIT are set in the environment.
550+ func TestIntegrationCgroupLimits (t * testing.T ) {
551+ cpuStr := os .Getenv ("CGROUP_EXPECTED_CPU_QUOTA" )
552+ memStr := os .Getenv ("CGROUP_EXPECTED_MEMORY_LIMIT" )
553+ if cpuStr == "" && memStr == "" {
554+ t .Skip ("set CGROUP_EXPECTED_CPU_QUOTA and CGROUP_EXPECTED_MEMORY_LIMIT to run" )
555+ }
556+
557+ if cpuStr != "" {
558+ wantCPU , err := strconv .ParseFloat (cpuStr , 64 )
559+ if err != nil {
560+ t .Fatalf ("bad CGROUP_EXPECTED_CPU_QUOTA %q: %v" , cpuStr , err )
561+ }
562+ gotCPU , err := CPUQuota ()
563+ if err != nil {
564+ t .Fatalf ("CPUQuota() error: %v" , err )
565+ }
566+ if math .Abs (gotCPU - wantCPU ) > 0.001 {
567+ t .Errorf ("CPUQuota() = %v, want %v" , gotCPU , wantCPU )
568+ }
569+ }
570+
571+ if memStr != "" {
572+ wantMem , err := strconv .ParseInt (memStr , 10 , 64 )
573+ if err != nil {
574+ t .Fatalf ("bad CGROUP_EXPECTED_MEMORY_LIMIT %q: %v" , memStr , err )
575+ }
576+ gotMem , err := MemoryLimit ()
577+ if err != nil {
578+ t .Fatalf ("MemoryLimit() error: %v" , err )
579+ }
580+ if gotMem != wantMem {
581+ t .Errorf ("MemoryLimit() = %v, want %v" , gotMem , wantMem )
582+ }
583+ }
584+ }
0 commit comments