Debian Codegraph @

internal/memorylimiter/cgroups/cgroups_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Keep the original Uber license.
5
6 // Copyright (c) 2017 Uber Technologies, Inc.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 // THE SOFTWARE.
25
26 //go:build linux
27
28 package cgroups
29
30 import (
31 "path/filepath"
32 "testing"
33
34 "github.com/stretchr/testify/assert"
35 "github.com/stretchr/testify/require"
36 )
37
38 func TestNewCGroups(t *testing.T) {
39 cgroupsProcCGroupPath := filepath.Join(testDataProcPath, "cgroups", "cgroup")
40 cgroupsProcMountInfoPath := filepath.Join(testDataProcPath, "cgroups", "mountinfo")
41
42 testTable := []struct {
43 subsys string
44 path string
45 }{
46 {_cgroupSubsysCPU, "/sys/fs/cgroup/cpu,cpuacct"},
47 {_cgroupSubsysCPUAcct, "/sys/fs/cgroup/cpu,cpuacct"},
48 {_cgroupSubsysCPUSet, "/sys/fs/cgroup/cpuset"},
49 {_cgroupSubsysMemory, "/sys/fs/cgroup/memory/large"},
50 }
51
52 cgroups, err := NewCGroups(cgroupsProcMountInfoPath, cgroupsProcCGroupPath)
53 assert.Len(t, cgroups, len(testTable))
54 require.NoError(t, err)
55
56 for _, tt := range testTable {
57 cgroup, exists := cgroups[tt.subsys]
58 assert.True(t, exists, "%q expected to present in `cgroups`", tt.subsys)
59 assert.Equal(t, tt.path, cgroup.path, "%q expected for `cgroups[%q].path`, got %q", tt.path, tt.subsys, cgroup.path)
60 }
61 }
62
63 func TestNewCGroupsWithErrors(t *testing.T) {
64 testTable := []struct {
65 mountInfoPath string
66 cgroupPath string
67 }{
68 {"non-existing-file", "/dev/null"},
69 {"/dev/null", "non-existing-file"},
70 {
71 "/dev/null",
72 filepath.Join(testDataProcPath, "invalid-cgroup", "cgroup"),
73 },
74 {
75 filepath.Join(testDataProcPath, "invalid-mountinfo", "mountinfo"),
76 "/dev/null",
77 },
78 {
79 filepath.Join(testDataProcPath, "untranslatable", "mountinfo"),
80 filepath.Join(testDataProcPath, "untranslatable", "cgroup"),
81 },
82 }
83
84 for _, tt := range testTable {
85 cgroups, err := NewCGroups(tt.mountInfoPath, tt.cgroupPath)
86 assert.Nil(t, cgroups)
87 assert.Error(t, err)
88 }
89 }
90
91 func TestCGroupsMemoryQuota(t *testing.T) {
92 testTable := []struct {
93 name string
94 expectedQuota int64
95 expectedDefined bool
96 shouldHaveError bool
97 }{
98 {
99 name: "undefined",
100 expectedQuota: int64(-1.0),
101 expectedDefined: false,
102 shouldHaveError: true,
103 },
104 {
105 name: "memory",
106 expectedQuota: int64(8796093018112),
107 expectedDefined: true,
108 shouldHaveError: false,
109 },
110 }
111
112 cgroups := make(CGroups)
113
114 quota, defined, err := cgroups.MemoryQuota()
115 assert.Equal(t, int64(-1), quota, "nonexistent")
116 assert.False(t, defined, "nonexistent")
117 require.NoError(t, err, "nonexistent")
118
119 for _, tt := range testTable {
120 cgroupPath := filepath.Join(testDataCGroupsPath, tt.name)
121 cgroups[_cgroupSubsysMemory] = NewCGroup(cgroupPath)
122
123 quota, defined, err := cgroups.MemoryQuota()
124 assert.Equal(t, tt.expectedQuota, quota, tt.name)
125 assert.Equal(t, tt.expectedDefined, defined, tt.name)
126
127 if tt.shouldHaveError {
128 assert.Error(t, err, tt.name)
129 } else {
130 assert.NoError(t, err, tt.name)
131 }
132 }
133 }
134
135 func TestCGroupsIsCGroupV2(t *testing.T) {
136 testTable := []struct {
137 name string
138 expectedIsV2 bool
139 shouldHaveError bool
140 }{
141 {
142 name: "cgroupv1",
143 expectedIsV2: false,
144 shouldHaveError: false,
145 },
146 {
147 name: "cgroupv1v2",
148 expectedIsV2: false,
149 shouldHaveError: false,
150 },
151 {
152 name: "cgroupv2",
153 expectedIsV2: true,
154 shouldHaveError: false,
155 },
156 {
157 name: "nonexistent",
158 expectedIsV2: false,
159 shouldHaveError: true,
160 },
161 }
162
163 for _, tt := range testTable {
164 mountInfoPath := filepath.Join(testDataProcPath, "v2", tt.name, "mountinfo")
165 isV2, err := isCGroupV2(mountInfoPath)
166
167 assert.Equal(t, tt.expectedIsV2, isV2, tt.name)
168
169 if tt.shouldHaveError {
170 assert.Error(t, err, tt.name)
171 } else {
172 assert.NoError(t, err, tt.name)
173 }
174 }
175 }
176
177 func TestCGroupsMemoryQuotaV2(t *testing.T) {
178 testTable := []struct {
179 name string
180 expectedQuota int64
181 expectedDefined bool
182 shouldHaveError bool
183 }{
184 {
185 name: "memory",
186 expectedQuota: int64(250000000),
187 expectedDefined: true,
188 shouldHaveError: false,
189 },
190 {
191 name: "undefined",
192 expectedQuota: int64(-1),
193 expectedDefined: false,
194 shouldHaveError: false,
195 },
196 {
197 name: "invalid",
198 expectedQuota: int64(-1),
199 expectedDefined: false,
200 shouldHaveError: true,
201 },
202 {
203 name: "empty",
204 expectedQuota: int64(-1),
205 expectedDefined: false,
206 shouldHaveError: true,
207 },
208 }
209
210 quota, defined, err := memoryQuotaV2("nonexistent", "nonexistent")
211 assert.Equal(t, int64(-1), quota, "nonexistent")
212 assert.False(t, defined, "nonexistent")
213 require.NoError(t, err, "nonexistent")
214
215 cgroupBasePath := filepath.Join(testDataCGroupsPath, "v2")
216 for _, tt := range testTable {
217 cgroupPath := filepath.Join(cgroupBasePath, tt.name)
218 quota, defined, err := memoryQuotaV2(cgroupPath, "memory.max")
219 assert.Equal(t, tt.expectedQuota, quota, tt.name)
220 assert.Equal(t, tt.expectedDefined, defined, tt.name)
221
222 if tt.shouldHaveError {
223 assert.Error(t, err, tt.name)
224 } else {
225 assert.NoError(t, err, tt.name)
226 }
227 }
228 }