github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/cgroups/fs/pids_test.go (about) 1 package fs 2 3 import ( 4 "strconv" 5 "testing" 6 7 "github.com/opencontainers/runc/libcontainer/cgroups" 8 "github.com/opencontainers/runc/libcontainer/cgroups/fscommon" 9 "github.com/opencontainers/runc/libcontainer/configs" 10 ) 11 12 const ( 13 maxUnlimited = -1 14 maxLimited = 1024 15 ) 16 17 func TestPidsSetMax(t *testing.T) { 18 path := tempDir(t, "pids") 19 20 writeFileContents(t, path, map[string]string{ 21 "pids.max": "max", 22 }) 23 24 r := &configs.Resources{ 25 PidsLimit: maxLimited, 26 } 27 pids := &PidsGroup{} 28 if err := pids.Set(path, r); err != nil { 29 t.Fatal(err) 30 } 31 32 value, err := fscommon.GetCgroupParamUint(path, "pids.max") 33 if err != nil { 34 t.Fatal(err) 35 } 36 if value != maxLimited { 37 t.Fatalf("Expected %d, got %d for setting pids.max - limited", maxLimited, value) 38 } 39 } 40 41 func TestPidsSetUnlimited(t *testing.T) { 42 path := tempDir(t, "pids") 43 44 writeFileContents(t, path, map[string]string{ 45 "pids.max": strconv.Itoa(maxLimited), 46 }) 47 48 r := &configs.Resources{ 49 PidsLimit: maxUnlimited, 50 } 51 pids := &PidsGroup{} 52 if err := pids.Set(path, r); err != nil { 53 t.Fatal(err) 54 } 55 56 value, err := fscommon.GetCgroupParamString(path, "pids.max") 57 if err != nil { 58 t.Fatal(err) 59 } 60 if value != "max" { 61 t.Fatalf("Expected %s, got %s for setting pids.max - unlimited", "max", value) 62 } 63 } 64 65 func TestPidsStats(t *testing.T) { 66 path := tempDir(t, "pids") 67 68 writeFileContents(t, path, map[string]string{ 69 "pids.current": strconv.Itoa(1337), 70 "pids.max": strconv.Itoa(maxLimited), 71 }) 72 73 pids := &PidsGroup{} 74 stats := *cgroups.NewStats() 75 if err := pids.GetStats(path, &stats); err != nil { 76 t.Fatal(err) 77 } 78 79 if stats.PidsStats.Current != 1337 { 80 t.Fatalf("Expected %d, got %d for pids.current", 1337, stats.PidsStats.Current) 81 } 82 83 if stats.PidsStats.Limit != maxLimited { 84 t.Fatalf("Expected %d, got %d for pids.max", maxLimited, stats.PidsStats.Limit) 85 } 86 } 87 88 func TestPidsStatsUnlimited(t *testing.T) { 89 path := tempDir(t, "pids") 90 91 writeFileContents(t, path, map[string]string{ 92 "pids.current": strconv.Itoa(4096), 93 "pids.max": "max", 94 }) 95 96 pids := &PidsGroup{} 97 stats := *cgroups.NewStats() 98 if err := pids.GetStats(path, &stats); err != nil { 99 t.Fatal(err) 100 } 101 102 if stats.PidsStats.Current != 4096 { 103 t.Fatalf("Expected %d, got %d for pids.current", 4096, stats.PidsStats.Current) 104 } 105 106 if stats.PidsStats.Limit != 0 { 107 t.Fatalf("Expected %d, got %d for pids.max", 0, stats.PidsStats.Limit) 108 } 109 }