github.com/opencontainers/runc@v1.2.0-rc.1.0.20240520010911-492dc558cdd6/libcontainer/cgroups/fs/util_test.go (about) 1 /* 2 Utility for testing cgroup operations. 3 4 Creates a mock of the cgroup filesystem for the duration of the test. 5 */ 6 package fs 7 8 import ( 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/opencontainers/runc/libcontainer/cgroups" 14 ) 15 16 func init() { 17 cgroups.TestMode = true 18 } 19 20 // tempDir creates a new test directory for the specified subsystem. 21 func tempDir(t *testing.T, subsystem string) string { 22 path := filepath.Join(t.TempDir(), subsystem) 23 // Ensure the full mock cgroup path exists. 24 if err := os.Mkdir(path, 0o755); err != nil { 25 t.Fatal(err) 26 } 27 return path 28 } 29 30 // writeFileContents writes the specified contents on the mock of the specified 31 // cgroup files. 32 func writeFileContents(t *testing.T, path string, fileContents map[string]string) { 33 for file, contents := range fileContents { 34 err := cgroups.WriteFile(path, file, contents) 35 if err != nil { 36 t.Fatal(err) 37 } 38 } 39 }