github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/cgroups/fs/pids_test.go (about)

     1  // +build linux
     2  
     3  package fs
     4  
     5  import (
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/opencontainers/runc/libcontainer/cgroups"
    10  )
    11  
    12  const (
    13  	maxUnlimited = -1
    14  	maxLimited   = 1024
    15  )
    16  
    17  func TestPidsSetMax(t *testing.T) {
    18  	helper := NewCgroupTestUtil("pids", t)
    19  	defer helper.cleanup()
    20  
    21  	helper.writeFileContents(map[string]string{
    22  		"pids.max": "max",
    23  	})
    24  
    25  	helper.CgroupData.config.Resources.PidsLimit = maxLimited
    26  	pids := &PidsGroup{}
    27  	if err := pids.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	value, err := getCgroupParamUint(helper.CgroupPath, "pids.max")
    32  	if err != nil {
    33  		t.Fatalf("Failed to parse pids.max - %s", err)
    34  	}
    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  	helper := NewCgroupTestUtil("pids", t)
    43  	defer helper.cleanup()
    44  
    45  	helper.writeFileContents(map[string]string{
    46  		"pids.max": strconv.Itoa(maxLimited),
    47  	})
    48  
    49  	helper.CgroupData.config.Resources.PidsLimit = maxUnlimited
    50  	pids := &PidsGroup{}
    51  	if err := pids.Set(helper.CgroupPath, helper.CgroupData.config); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	value, err := getCgroupParamString(helper.CgroupPath, "pids.max")
    56  	if err != nil {
    57  		t.Fatalf("Failed to parse pids.max - %s", err)
    58  	}
    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  	helper := NewCgroupTestUtil("pids", t)
    67  	defer helper.cleanup()
    68  
    69  	helper.writeFileContents(map[string]string{
    70  		"pids.current": strconv.Itoa(1337),
    71  		"pids.max":     strconv.Itoa(maxLimited),
    72  	})
    73  
    74  	pids := &PidsGroup{}
    75  	stats := *cgroups.NewStats()
    76  	if err := pids.GetStats(helper.CgroupPath, &stats); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if stats.PidsStats.Current != 1337 {
    81  		t.Fatalf("Expected %d, got %d for pids.current", 1337, stats.PidsStats.Current)
    82  	}
    83  
    84  	if stats.PidsStats.Limit != maxLimited {
    85  		t.Fatalf("Expected %d, got %d for pids.max", maxLimited, stats.PidsStats.Limit)
    86  	}
    87  }
    88  
    89  func TestPidsStatsUnlimited(t *testing.T) {
    90  	helper := NewCgroupTestUtil("pids", t)
    91  	defer helper.cleanup()
    92  
    93  	helper.writeFileContents(map[string]string{
    94  		"pids.current": strconv.Itoa(4096),
    95  		"pids.max":     "max",
    96  	})
    97  
    98  	pids := &PidsGroup{}
    99  	stats := *cgroups.NewStats()
   100  	if err := pids.GetStats(helper.CgroupPath, &stats); err != nil {
   101  		t.Fatal(err)
   102  	}
   103  
   104  	if stats.PidsStats.Current != 4096 {
   105  		t.Fatalf("Expected %d, got %d for pids.current", 4096, stats.PidsStats.Current)
   106  	}
   107  
   108  	if stats.PidsStats.Limit != 0 {
   109  		t.Fatalf("Expected %d, got %d for pids.max", 0, stats.PidsStats.Limit)
   110  	}
   111  }