github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/cgroups/fs2/psi_test.go (about)

     1  package fs2
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/opencontainers/runc/libcontainer/cgroups"
    10  )
    11  
    12  func TestStatCPUPSI(t *testing.T) {
    13  	const examplePSIData = `some avg10=1.71 avg60=2.36 avg300=2.57 total=230548833
    14  full avg10=1.00 avg60=1.01 avg300=1.00 total=157622356`
    15  
    16  	// We're using a fake cgroupfs.
    17  	cgroups.TestMode = true
    18  
    19  	fakeCgroupDir := t.TempDir()
    20  	statPath := filepath.Join(fakeCgroupDir, "cpu.pressure")
    21  
    22  	if err := os.WriteFile(statPath, []byte(examplePSIData), 0o644); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  
    26  	st, err := statPSI(fakeCgroupDir, "cpu.pressure")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	if !reflect.DeepEqual(*st, cgroups.PSIStats{
    32  		Some: cgroups.PSIData{
    33  			Avg10:  1.71,
    34  			Avg60:  2.36,
    35  			Avg300: 2.57,
    36  			Total:  230548833,
    37  		},
    38  		Full: cgroups.PSIData{
    39  			Avg10:  1.00,
    40  			Avg60:  1.01,
    41  			Avg300: 1.00,
    42  			Total:  157622356,
    43  		},
    44  	}) {
    45  		t.Errorf("unexpected PSI result: %+v", st)
    46  	}
    47  }