github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/intelrdt/monitoring_test.go (about) 1 package intelrdt 2 3 import ( 4 "os" 5 "path/filepath" 6 "strconv" 7 "strings" 8 "testing" 9 ) 10 11 func TestParseMonFeatures(t *testing.T) { 12 t.Run("All features available", func(t *testing.T) { 13 parsedMonFeatures, err := parseMonFeatures( 14 strings.NewReader("mbm_total_bytes\nmbm_local_bytes\nllc_occupancy")) 15 if err != nil { 16 t.Errorf("Error while parsing mon features err = %v", err) 17 } 18 19 expectedMonFeatures := monFeatures{true, true, true} 20 21 if parsedMonFeatures != expectedMonFeatures { 22 t.Error("Cannot gather all features!") 23 } 24 }) 25 26 t.Run("No features available", func(t *testing.T) { 27 parsedMonFeatures, err := parseMonFeatures(strings.NewReader("")) 28 if err != nil { 29 t.Errorf("Error while parsing mon features err = %v", err) 30 } 31 32 expectedMonFeatures := monFeatures{false, false, false} 33 34 if parsedMonFeatures != expectedMonFeatures { 35 t.Error("Expected no features available but there is any!") 36 } 37 }) 38 } 39 40 func mockResctrlL3_MON(t *testing.T, NUMANodes []string, mocks map[string]uint64) string { 41 t.Helper() 42 testDir := t.TempDir() 43 monDataPath := filepath.Join(testDir, "mon_data") 44 45 for _, numa := range NUMANodes { 46 numaPath := filepath.Join(monDataPath, numa) 47 err := os.MkdirAll(numaPath, 0o700) 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 for fileName, value := range mocks { 53 err := os.WriteFile(filepath.Join(numaPath, fileName), []byte(strconv.FormatUint(value, 10)), 0o644) 54 if err != nil { 55 t.Fatal(err) 56 } 57 } 58 59 } 60 61 return testDir 62 } 63 64 func TestGetMonitoringStats(t *testing.T) { 65 enabledMonFeatures.mbmTotalBytes = true 66 enabledMonFeatures.mbmLocalBytes = true 67 enabledMonFeatures.llcOccupancy = true 68 mbmEnabled = true 69 cmtEnabled = true 70 71 mocksNUMANodesToCreate := []string{"mon_l3_00", "mon_l3_01"} 72 73 mocksFilesToCreate := map[string]uint64{ 74 "mbm_total_bytes": 9123911, 75 "mbm_local_bytes": 2361361, 76 "llc_occupancy": 123331, 77 } 78 79 mockedL3_MON := mockResctrlL3_MON(t, mocksNUMANodesToCreate, mocksFilesToCreate) 80 81 t.Run("Gather monitoring stats", func(t *testing.T) { 82 var stats Stats 83 err := getMonitoringStats(mockedL3_MON, &stats) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 expectedMBMStats := MBMNumaNodeStats{ 89 MBMTotalBytes: mocksFilesToCreate["mbm_total_bytes"], 90 MBMLocalBytes: mocksFilesToCreate["mbm_local_bytes"], 91 } 92 93 expectedCMTStats := CMTNumaNodeStats{LLCOccupancy: mocksFilesToCreate["llc_occupancy"]} 94 95 for _, gotMBMStat := range *stats.MBMStats { 96 checkMBMStatCorrection(gotMBMStat, expectedMBMStats, t) 97 } 98 99 for _, gotCMTStat := range *stats.CMTStats { 100 checkCMTStatCorrection(gotCMTStat, expectedCMTStats, t) 101 } 102 }) 103 }