Debian Codegraph @

internal/memorylimiter/cgroups/mountpoint_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 "testing"
32
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/require"
35 )
36
37 func TestNewMountPointFromLine(t *testing.T) {
38 testTable := []struct {
39 name string
40 line string
41 expected *MountPoint
42 }{
43 {
44 name: "root",
45 line: "1 0 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
46 expected: &MountPoint{
47 MountID: 1,
48 ParentID: 0,
49 DeviceID: "252:0",
50 Root: "/",
51 MountPoint: "/",
52 Options: []string{"rw", "noatime"},
53 OptionalFields: []string{},
54 FSType: "ext4",
55 MountSource: "/dev/dm-0",
56 SuperOptions: []string{"rw", "errors=remount-ro", "data=ordered"},
57 },
58 },
59 {
60 name: "cgroup",
61 line: "31 23 0:24 /docker /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu",
62 expected: &MountPoint{
63 MountID: 31,
64 ParentID: 23,
65 DeviceID: "0:24",
66 Root: "/docker",
67 MountPoint: "/sys/fs/cgroup/cpu",
68 Options: []string{"rw", "nosuid", "nodev", "noexec", "relatime"},
69 OptionalFields: []string{"shared:1"},
70 FSType: "cgroup",
71 MountSource: "cgroup",
72 SuperOptions: []string{"rw", "cpu"},
73 },
74 },
75 }
76
77 for _, tt := range testTable {
78 mountPoint, err := NewMountPointFromLine(tt.line)
79 assert.Equal(t, tt.expected, mountPoint, tt.name)
80 assert.NoError(t, err, tt.name)
81 }
82 }
83
84 func TestNewMountPointFromLineErr(t *testing.T) {
85 linesWithInvalidIDs := []string{
86 "invalidMountID 0 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
87 "1 invalidParentID 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
88 "invalidMountID invalidParentID 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
89 }
90
91 for i, line := range linesWithInvalidIDs {
92 mountPoint, err := NewMountPointFromLine(line)
93 assert.Nil(t, mountPoint, "[%d] %q", i, line)
94 require.Error(t, err, line)
95 }
96
97 linesWithInvalidFields := []string{
98 "1 0 252:0 / / rw,noatime ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
99 "1 0 252:0 / / rw,noatime shared:1 - ext4 /dev/dm-0",
100 "1 0 252:0 / / rw,noatime shared:1 ext4 - /dev/dm-0 rw,errors=remount-ro,data=ordered",
101 "1 0 252:0 / / rw,noatime shared:1 ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
102 "random line",
103 }
104
105 for i, line := range linesWithInvalidFields {
106 mountPoint, err := NewMountPointFromLine(line)
107 errExpected := mountPointFormatInvalidError{line}
108
109 assert.Nil(t, mountPoint, "[%d] %q", i, line)
110 assert.Equal(t, errExpected, err, "[%d] %q", i, line)
111 }
112 }
113
114 func TestMountPointTranslate(t *testing.T) {
115 line := "31 23 0:24 /docker/0123456789abcdef /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu"
116 cgroupMountPoint, err := NewMountPointFromLine(line)
117
118 assert.NotNil(t, cgroupMountPoint)
119 require.NoError(t, err)
120
121 testTable := []struct {
122 name string
123 pathToTranslate string
124 pathTranslated string
125 }{
126 {
127 name: "root",
128 pathToTranslate: "/docker/0123456789abcdef",
129 pathTranslated: "/sys/fs/cgroup/cpu",
130 },
131 {
132 name: "root-with-extra-slash",
133 pathToTranslate: "/docker/0123456789abcdef/",
134 pathTranslated: "/sys/fs/cgroup/cpu",
135 },
136 {
137 name: "descendant-from-root",
138 pathToTranslate: "/docker/0123456789abcdef/large/cpu.cfs_quota_us",
139 pathTranslated: "/sys/fs/cgroup/cpu/large/cpu.cfs_quota_us",
140 },
141 }
142
143 for _, tt := range testTable {
144 path, err := cgroupMountPoint.Translate(tt.pathToTranslate)
145 assert.Equal(t, tt.pathTranslated, path, tt.name)
146 assert.NoError(t, err, tt.name)
147 }
148 }
149
150 func TestMountPointTranslateError(t *testing.T) {
151 line := "31 23 0:24 /docker/0123456789abcdef /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu"
152 cgroupMountPoint, err := NewMountPointFromLine(line)
153
154 assert.NotNil(t, cgroupMountPoint)
155 require.NoError(t, err)
156
157 inaccessiblePaths := []string{
158 "/",
159 "/docker",
160 "/docker/0123456789abcdef-let-me-hack-this-path",
161 "/docker/0123456789abcde/abc/../../def",
162 "/system.slice/docker.service",
163 }
164
165 for i, path := range inaccessiblePaths {
166 translated, err := cgroupMountPoint.Translate(path)
167 errExpected := pathNotExposedFromMountPointError{
168 mountPoint: cgroupMountPoint.MountPoint,
169 root: cgroupMountPoint.Root,
170 path: path,
171 }
172
173 assert.Empty(t, translated, "inaccessiblePaths[%d] == %q", i, path)
174 assert.Equal(t, errExpected, err, "inaccessiblePaths[%d] == %q", i, path)
175 }
176
177 relPaths := []string{
178 "docker",
179 "docker/0123456789abcde/large",
180 "system.slice/docker.service",
181 }
182
183 for i, path := range relPaths {
184 translated, err := cgroupMountPoint.Translate(path)
185
186 assert.Empty(t, translated, "relPaths[%d] == %q", i, path)
187 assert.Error(t, err, path)
188 }
189 }