github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/intelrdt/util_test.go (about) 1 /* 2 * Utility for testing Intel RDT operations. 3 * Creates a mock of the Intel RDT "resource control" filesystem for the duration of the test. 4 */ 5 package intelrdt 6 7 import ( 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/opencontainers/runc/libcontainer/configs" 13 ) 14 15 type intelRdtTestUtil struct { 16 config *configs.Config 17 18 // Path to the mock Intel RDT "resource control" filesystem directory 19 IntelRdtPath string 20 21 t *testing.T 22 } 23 24 // Creates a new test util 25 func NewIntelRdtTestUtil(t *testing.T) *intelRdtTestUtil { 26 config := &configs.Config{ 27 IntelRdt: &configs.IntelRdt{}, 28 } 29 30 // Assign fake intelRtdRoot value, returned by Root(). 31 intelRdtRoot = t.TempDir() 32 // Make sure Root() won't even try to parse mountinfo. 33 rootOnce.Do(func() {}) 34 35 testIntelRdtPath := filepath.Join(intelRdtRoot, "resctrl") 36 37 // Ensure the full mock Intel RDT "resource control" filesystem path exists 38 if err := os.MkdirAll(testIntelRdtPath, 0o755); err != nil { 39 t.Fatal(err) 40 } 41 return &intelRdtTestUtil{config: config, IntelRdtPath: testIntelRdtPath, t: t} 42 } 43 44 // Write the specified contents on the mock of the specified Intel RDT "resource control" files 45 func (c *intelRdtTestUtil) writeFileContents(fileContents map[string]string) { 46 for file, contents := range fileContents { 47 err := writeFile(c.IntelRdtPath, file, contents) 48 if err != nil { 49 c.t.Fatal(err) 50 } 51 } 52 }