File: util_test.go

package info (click to toggle)
golang-github-opencontainers-cgroups 0.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 844 kB
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Utility for testing cgroup operations.

Creates a mock of the cgroup filesystem for the duration of the test.
*/
package fs

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/opencontainers/cgroups"
)

func init() {
	cgroups.TestMode = true
}

// tempDir creates a new test directory for the specified subsystem.
func tempDir(t testing.TB, subsystem string) string {
	path := filepath.Join(t.TempDir(), subsystem)
	// Ensure the full mock cgroup path exists.
	if err := os.Mkdir(path, 0o755); err != nil {
		t.Fatal(err)
	}
	return path
}

// writeFileContents writes the specified contents on the mock of the specified
// cgroup files.
func writeFileContents(t testing.TB, path string, fileContents map[string]string) {
	for file, contents := range fileContents {
		err := cgroups.WriteFile(path, file, contents)
		if err != nil {
			t.Fatal(err)
		}
	}
}