| 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 cgroups |
| 25 | |
| 26 | import ( |
| 27 | "testing" |
| 28 | |
| 29 | "github.com/stretchr/testify/assert" |
| 30 | ) |
| 31 | |
| 32 | func TestNewMountPointFromLine(t *testing.T) { |
| 33 | testTable := []struct { |
| 34 | name string |
| 35 | line string |
| 36 | expected *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: &MountPoint{ |
| 42 | MountID: 1, |
| 43 | ParentID: 0, |
| 44 | DeviceID: "252:0", |
| 45 | Root: "/", |
| 46 | MountPoint: "/", |
| 47 | Options: []string{"rw", "noatime"}, |
| 48 | OptionalFields: []string{}, |
| 49 | FSType: "ext4", |
| 50 | MountSource: "/dev/dm-0", |
| 51 | 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: &MountPoint{ |
| 58 | MountID: 31, |
| 59 | ParentID: 23, |
| 60 | DeviceID: "0:24", |
| 61 | Root: "/docker", |
| 62 | MountPoint: "/sys/fs/cgroup/cpu", |
| 63 | Options: []string{"rw", "nosuid", "nodev", "noexec", "relatime"}, |
| 64 | OptionalFields: []string{"shared:1"}, |
| 65 | FSType: "cgroup", |
| 66 | MountSource: "cgroup", |
| 67 | 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: &MountPoint{ |
| 74 | MountID: 560, |
| 75 | ParentID: 77, |
| 76 | DeviceID: "0:138", |
| 77 | Root: "/", |
| 78 | MountPoint: "/Docker/host", |
| 79 | Options: []string{"rw", "noatime"}, |
| 80 | OptionalFields: []string{}, |
| 81 | FSType: "9p", |
| 82 | MountSource: "drvfs", |
| 83 | 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 := NewMountPointFromLine(tt.line) |
| 98 | if assert.NoError(t, err, tt.name) { |
| 99 | assert.Equal(t, tt.expected, mountPoint, tt.name) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestNewMountPointFromLineErr(t *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 := NewMountPointFromLine(line) |
| 113 | assert.Nil(t, mountPoint, "[%d] %q", i, line) |
| 114 | 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 := NewMountPointFromLine(line) |
| 127 | errExpected := mountPointFormatInvalidError{line} |
| 128 | |
| 129 | assert.Nil(t, mountPoint, "[%d] %q", i, line) |
| 130 | assert.Equal(t, err, errExpected, "[%d] %q", i, line) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestMountPointTranslate(t *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 := NewMountPointFromLine(line) |
| 137 | |
| 138 | assert.NotNil(t, cgroupMountPoint) |
| 139 | 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.Translate(tt.pathToTranslate) |
| 165 | assert.Equal(t, tt.pathTranslated, path, tt.name) |
| 166 | assert.NoError(t, err, tt.name) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestMountPointTranslateError(t *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 := NewMountPointFromLine(line) |
| 173 | |
| 174 | assert.NotNil(t, cgroupMountPoint) |
| 175 | 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.Translate(path) |
| 187 | errExpected := pathNotExposedFromMountPointError{ |
| 188 | mountPoint: cgroupMountPoint.MountPoint, |
| 189 | root: cgroupMountPoint.Root, |
| 190 | path: path, |
| 191 | } |
| 192 | |
| 193 | assert.Equal(t, "", translated, "inaccessiblePaths[%d] == %q", i, path) |
| 194 | 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.Translate(path) |
| 205 | |
| 206 | assert.Equal(t, "", translated, "relPaths[%d] == %q", i, path) |
| 207 | assert.Error(t, err, path) |
| 208 | } |
| 209 | } |