Debian Codegraph @

internal/cgroups/mountpoint_test.go

raw symbols SCIP
1 // Copyright (c) 2017 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 //go:build linux
22 // +build linux
23
24 package cgroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/
25
26 import (
27 "testingscip-go gomod github.com/golang/go/src go1.18 testing/"
28
29 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/"
30 )
31
32 func TestNewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestNewMountPointFromLine().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
33 testTable := []struct {
34 name string
35 line string
36 expected *MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#
37 }{
38 {
39 name: "root",
40 line: "1 0 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
41 expected: &MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#{
42 MountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountID.: 1,
43 ParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#ParentID.: 0,
44 DeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#DeviceID.: "252:0",
45 Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root.: "/",
46 MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint.: "/",
47 Optionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Options.: []string{"rw", "noatime"},
48 OptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#OptionalFields.: []string{},
49 FSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#FSType.: "ext4",
50 MountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountSource.: "/dev/dm-0",
51 SuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#SuperOptions.: []string{"rw", "errors=remount-ro", "data=ordered"},
52 },
53 },
54 {
55 name: "cgroup",
56 line: "31 23 0:24 /docker /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu",
57 expected: &MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#{
58 MountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountID.: 31,
59 ParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#ParentID.: 23,
60 DeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#DeviceID.: "0:24",
61 Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root.: "/docker",
62 MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint.: "/sys/fs/cgroup/cpu",
63 Optionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Options.: []string{"rw", "nosuid", "nodev", "noexec", "relatime"},
64 OptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#OptionalFields.: []string{"shared:1"},
65 FSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#FSType.: "cgroup",
66 MountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountSource.: "cgroup",
67 SuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#SuperOptions.: []string{"rw", "cpu"},
68 },
69 },
70 {
71 name: "wsl",
72 line: `560 77 0:138 / /Docker/host rw,noatime - 9p drvfs rw,dirsync,aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/,mmap,access=client,msize=262144,trans=virtio`,
73 expected: &MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#{
74 MountIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountID.: 560,
75 ParentIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#ParentID.: 77,
76 DeviceIDscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#DeviceID.: "0:138",
77 Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root.: "/",
78 MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint.: "/Docker/host",
79 Optionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Options.: []string{"rw", "noatime"},
80 OptionalFieldsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#OptionalFields.: []string{},
81 FSTypescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#FSType.: "9p",
82 MountSourcescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountSource.: "drvfs",
83 SuperOptionsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#SuperOptions.: []string{
84 "rw",
85 "dirsync",
86 `aname=drvfs;path=C:\Program Files\Docker\Docker\resources;symlinkroot=/mnt/`,
87 "mmap",
88 "access=client",
89 "msize=262144",
90 "trans=virtio",
91 },
92 },
93 },
94 }
95
96 for _, tt := range testTable {
97 mountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(tt.line)
98 if assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err, tt.name) {
99 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.expected, mountPoint, tt.name)
100 }
101 }
102 }
103
104 func TestNewMountPointFromLineErrscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestNewMountPointFromLineErr().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
105 linesWithInvalidIDs := []string{
106 "invalidMountID 0 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
107 "1 invalidParentID 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
108 "invalidMountID invalidParentID 252:0 / / rw,noatime - ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
109 }
110
111 for i, line := range linesWithInvalidIDs {
112 mountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(line)
113 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Nil().(t, mountPoint, "[%d] %q", i, line)
114 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Error().(t, err, line)
115 }
116
117 linesWithInvalidFields := []string{
118 "1 0 252:0 / / rw,noatime ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
119 "1 0 252:0 / / rw,noatime shared:1 - ext4 /dev/dm-0",
120 "1 0 252:0 / / rw,noatime shared:1 ext4 - /dev/dm-0 rw,errors=remount-ro,data=ordered",
121 "1 0 252:0 / / rw,noatime shared:1 ext4 /dev/dm-0 rw,errors=remount-ro,data=ordered",
122 "random line",
123 }
124
125 for i, line := range linesWithInvalidFields {
126 mountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(line)
127 errExpected := mountPointFormatInvalidErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/mountPointFormatInvalidError#{line}
128
129 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Nilscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Nil().(t, mountPoint, "[%d] %q", i, line)
130 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, err, errExpected, "[%d] %q", i, line)
131 }
132 }
133
134 func TestMountPointTranslatescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestMountPointTranslate().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
135 line := "31 23 0:24 /docker/0123456789abcdef /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu"
136 cgroupMountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(line)
137
138 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NotNilscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NotNil().(t, cgroupMountPoint)
139 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err)
140
141 testTable := []struct {
142 name string
143 pathToTranslate string
144 pathTranslated string
145 }{
146 {
147 name: "root",
148 pathToTranslate: "/docker/0123456789abcdef",
149 pathTranslated: "/sys/fs/cgroup/cpu",
150 },
151 {
152 name: "root-with-extra-slash",
153 pathToTranslate: "/docker/0123456789abcdef/",
154 pathTranslated: "/sys/fs/cgroup/cpu",
155 },
156 {
157 name: "descendant-from-root",
158 pathToTranslate: "/docker/0123456789abcdef/large/cpu.cfs_quota_us",
159 pathTranslated: "/sys/fs/cgroup/cpu/large/cpu.cfs_quota_us",
160 },
161 }
162
163 for _, tt := range testTable {
164 path, err := cgroupMountPoint.Translatescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Translate().(tt.pathToTranslate)
165 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.pathTranslated, path, tt.name)
166 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err, tt.name)
167 }
168 }
169
170 func TestMountPointTranslateErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestMountPointTranslateError().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
171 line := "31 23 0:24 /docker/0123456789abcdef /sys/fs/cgroup/cpu rw,nosuid,nodev,noexec,relatime shared:1 - cgroup cgroup rw,cpu"
172 cgroupMountPoint, err := NewMountPointFromLinescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/NewMountPointFromLine().(line)
173
174 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NotNilscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NotNil().(t, cgroupMountPoint)
175 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err)
176
177 inaccessiblePaths := []string{
178 "/",
179 "/docker",
180 "/docker/0123456789abcdef-let-me-hack-this-path",
181 "/docker/0123456789abcde/abc/../../def",
182 "/system.slice/docker.service",
183 }
184
185 for i, path := range inaccessiblePaths {
186 translated, err := cgroupMountPoint.Translatescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Translate().(path)
187 errExpected := pathNotExposedFromMountPointErrorscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#{
188 mountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#mountPoint.: cgroupMountPoint.MountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#MountPoint.,
189 rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#root.: cgroupMountPoint.Rootscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Root.,
190 pathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/pathNotExposedFromMountPointError#path.: path,
191 }
192
193 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, "", translated, "inaccessiblePaths[%d] == %q", i, path)
194 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, errExpected, err, "inaccessiblePaths[%d] == %q", i, path)
195 }
196
197 relPaths := []string{
198 "docker",
199 "docker/0123456789abcde/large",
200 "system.slice/docker.service",
201 }
202
203 for i, path := range relPaths {
204 translated, err := cgroupMountPoint.Translatescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/MountPoint#Translate().(path)
205
206 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, "", translated, "relPaths[%d] == %q", i, path)
207 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Error().(t, err, path)
208 }
209 }