File: pids_test.go

package info (click to toggle)
golang-github-opencontainers-cgroups 0.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 876 kB
  • sloc: makefile: 2
file content (159 lines) | stat: -rw-r--r-- 3,360 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package fs

import (
	"strconv"
	"testing"

	"github.com/opencontainers/cgroups"
	"github.com/opencontainers/cgroups/fscommon"
)

const (
	maxUnlimited = -1
	maxLimited   = 1024
)

func toPtr[T any](v T) *T { return &v }

func TestPidsSetMax(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.max": "max",
	})

	r := &cgroups.Resources{
		PidsLimit: toPtr[int64](maxLimited),
	}
	pids := &PidsGroup{}
	if err := pids.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamUint(path, "pids.max")
	if err != nil {
		t.Fatal(err)
	}
	if value != maxLimited {
		t.Fatalf("Expected %d, got %d for setting pids.max - limited", maxLimited, value)
	}
}

func TestPidsSetZero(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.max": "max",
	})

	r := &cgroups.Resources{
		PidsLimit: toPtr[int64](0),
	}
	pids := &PidsGroup{}
	if err := pids.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamUint(path, "pids.max")
	if err != nil {
		t.Fatal(err)
	}
	// See comment in (*PidsGroup).Set for why we set to 1 here.
	if value != 1 {
		t.Fatalf("Expected 1, got %d for setting pids.max = 0", value)
	}
}

func TestPidsUnset(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.max": "12345",
	})

	r := &cgroups.Resources{
		PidsLimit: nil,
	}
	pids := &PidsGroup{}
	if err := pids.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamUint(path, "pids.max")
	if err != nil {
		t.Fatal(err)
	}
	if value != 12345 {
		t.Fatalf("Expected 12345, got %d for not setting pids.max", value)
	}
}

func TestPidsSetUnlimited(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.max": strconv.Itoa(maxLimited),
	})

	r := &cgroups.Resources{
		PidsLimit: toPtr[int64](maxUnlimited),
	}
	pids := &PidsGroup{}
	if err := pids.Set(path, r); err != nil {
		t.Fatal(err)
	}

	value, err := fscommon.GetCgroupParamString(path, "pids.max")
	if err != nil {
		t.Fatal(err)
	}
	if value != "max" {
		t.Fatalf("Expected %s, got %s for setting pids.max - unlimited", "max", value)
	}
}

func TestPidsStats(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.current": strconv.Itoa(1337),
		"pids.max":     strconv.Itoa(maxLimited),
	})

	pids := &PidsGroup{}
	stats := *cgroups.NewStats()
	if err := pids.GetStats(path, &stats); err != nil {
		t.Fatal(err)
	}

	if stats.PidsStats.Current != 1337 {
		t.Fatalf("Expected %d, got %d for pids.current", 1337, stats.PidsStats.Current)
	}

	if stats.PidsStats.Limit != maxLimited {
		t.Fatalf("Expected %d, got %d for pids.max", maxLimited, stats.PidsStats.Limit)
	}
}

func TestPidsStatsUnlimited(t *testing.T) {
	path := tempDir(t, "pids")

	writeFileContents(t, path, map[string]string{
		"pids.current": strconv.Itoa(4096),
		"pids.max":     "max",
	})

	pids := &PidsGroup{}
	stats := *cgroups.NewStats()
	if err := pids.GetStats(path, &stats); err != nil {
		t.Fatal(err)
	}

	if stats.PidsStats.Current != 4096 {
		t.Fatalf("Expected %d, got %d for pids.current", 4096, stats.PidsStats.Current)
	}

	if stats.PidsStats.Limit != 0 {
		t.Fatalf("Expected %d, got %d for pids.max", 0, stats.PidsStats.Limit)
	}
}