| 1 |
//go:build linux |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright 2014 The Kubernetes Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package mount |
| 20 |
|
| 21 |
import ( |
| 22 |
"errors" |
| 23 |
"fmt" |
| 24 |
"os" |
| 25 |
"os/exec" |
| 26 |
"path/filepath" |
| 27 |
"reflect" |
| 28 |
"sort" |
| 29 |
"strings" |
| 30 |
"sync" |
| 31 |
"testing" |
| 32 |
"time" |
| 33 |
|
| 34 |
"github.com/stretchr/testify/assert" |
| 35 |
"golang.org/x/sys/unix" |
| 36 |
utilexec "k8s.io/utils/exec" |
| 37 |
testexec "k8s.io/utils/exec/testing" |
| 38 |
"k8s.io/utils/ptr" |
| 39 |
) |
| 40 |
|
| 41 |
func TestReadProcMountsFrom(t *testing.T) { |
| 42 |
successCase := `/dev/0 /path/to/0 type0 flags 0 0 |
| 43 |
/dev/1 /path/to/1 type1 flags 1 1 |
| 44 |
/dev/2 /path/to/2 type2 flags,1,2=3 2 2 |
| 45 |
` |
| 46 |
// NOTE: readProcMountsFrom has been updated to using fnv.New32a() |
| 47 |
mounts, err := parseProcMounts([]byte(successCase)) |
| 48 |
if err != nil { |
| 49 |
t.Errorf("expected success, got %v", err) |
| 50 |
} |
| 51 |
if len(mounts) != 3 { |
| 52 |
t.Fatalf("expected 3 mounts, got %d", len(mounts)) |
| 53 |
} |
| 54 |
mp := MountPoint{"/dev/0", "/path/to/0", "type0", []string{"flags"}, 0, 0} |
| 55 |
if !mountPointsEqual(&mounts[0], &mp) { |
| 56 |
t.Errorf("got unexpected MountPoint[0]: %#v", mounts[0]) |
| 57 |
} |
| 58 |
mp = MountPoint{"/dev/1", "/path/to/1", "type1", []string{"flags"}, 1, 1} |
| 59 |
if !mountPointsEqual(&mounts[1], &mp) { |
| 60 |
t.Errorf("got unexpected MountPoint[1]: %#v", mounts[1]) |
| 61 |
} |
| 62 |
mp = MountPoint{"/dev/2", "/path/to/2", "type2", []string{"flags", "1", "2=3"}, 2, 2} |
| 63 |
if !mountPointsEqual(&mounts[2], &mp) { |
| 64 |
t.Errorf("got unexpected MountPoint[2]: %#v", mounts[2]) |
| 65 |
} |
| 66 |
|
| 67 |
errorCases := []string{ |
| 68 |
"/dev/0 /path/to/mount\n", |
| 69 |
"/dev/1 /path/to/mount type flags a 0\n", |
| 70 |
"/dev/2 /path/to/mount type flags 0 b\n", |
| 71 |
} |
| 72 |
for _, ec := range errorCases { |
| 73 |
_, err := parseProcMounts([]byte(ec)) |
| 74 |
if err == nil { |
| 75 |
t.Errorf("expected error") |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func mountPointsEqual(a, b *MountPoint) bool { |
| 81 |
if a.Device != b.Device || a.Path != b.Path || a.Type != b.Type || !reflect.DeepEqual(a.Opts, b.Opts) || a.Pass != b.Pass || a.Freq != b.Freq { |
| 82 |
return false |
| 83 |
} |
| 84 |
return true |
| 85 |
} |
| 86 |
|
| 87 |
func TestGetMountRefs(t *testing.T) { |
| 88 |
fm := NewFakeMounter( |
| 89 |
[]MountPoint{ |
| 90 |
{Device: "/dev/sdb", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd"}, |
| 91 |
{Device: "/dev/sdb", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod"}, |
| 92 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2"}, |
| 93 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod1"}, |
| 94 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2"}, |
| 95 |
}) |
| 96 |
|
| 97 |
tests := []struct { |
| 98 |
mountPath string |
| 99 |
expectedRefs []string |
| 100 |
}{ |
| 101 |
{ |
| 102 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod", |
| 103 |
[]string{ |
| 104 |
"/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd", |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod1", |
| 109 |
[]string{ |
| 110 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2", |
| 111 |
"/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2", |
| 112 |
}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
"/var/fake/directory/that/doesnt/exist", |
| 116 |
[]string{}, |
| 117 |
}, |
| 118 |
} |
| 119 |
|
| 120 |
for i, test := range tests { |
| 121 |
if refs, err := fm.GetMountRefs(test.mountPath); err != nil || !setEquivalent(test.expectedRefs, refs) { |
| 122 |
t.Errorf("%d. getMountRefs(%q) = %v, %v; expected %v, nil", i, test.mountPath, refs, err, test.expectedRefs) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func setEquivalent(set1, set2 []string) bool { |
| 128 |
map1 := make(map[string]bool) |
| 129 |
map2 := make(map[string]bool) |
| 130 |
for _, s := range set1 { |
| 131 |
map1[s] = true |
| 132 |
} |
| 133 |
for _, s := range set2 { |
| 134 |
map2[s] = true |
| 135 |
} |
| 136 |
|
| 137 |
for s := range map1 { |
| 138 |
if !map2[s] { |
| 139 |
return false |
| 140 |
} |
| 141 |
} |
| 142 |
for s := range map2 { |
| 143 |
if !map1[s] { |
| 144 |
return false |
| 145 |
} |
| 146 |
} |
| 147 |
return true |
| 148 |
} |
| 149 |
|
| 150 |
func TestGetDeviceNameFromMount(t *testing.T) { |
| 151 |
fm := NewFakeMounter( |
| 152 |
[]MountPoint{ |
| 153 |
{ |
| 154 |
Device: "/dev/disk/by-path/prefix-lun-1", |
| 155 |
Path: "/mnt/111", |
| 156 |
}, |
| 157 |
{ |
| 158 |
Device: "/dev/disk/by-path/prefix-lun-1", |
| 159 |
Path: "/mnt/222", |
| 160 |
}, |
| 161 |
}) |
| 162 |
|
| 163 |
tests := []struct { |
| 164 |
mountPath string |
| 165 |
expectedDevice string |
| 166 |
expectedRefs int |
| 167 |
}{ |
| 168 |
{ |
| 169 |
"/mnt/222", |
| 170 |
"/dev/disk/by-path/prefix-lun-1", |
| 171 |
2, |
| 172 |
}, |
| 173 |
} |
| 174 |
|
| 175 |
for i, test := range tests { |
| 176 |
if device, refs, err := GetDeviceNameFromMount(fm, test.mountPath); err != nil || test.expectedRefs != refs || test.expectedDevice != device { |
| 177 |
t.Errorf("%d. GetDeviceNameFromMount(%s) = (%s, %d), %v; expected (%s,%d), nil", i, test.mountPath, device, refs, err, test.expectedDevice, test.expectedRefs) |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestGetMountRefsByDev(t *testing.T) { |
| 183 |
fm := NewFakeMounter( |
| 184 |
[]MountPoint{ |
| 185 |
{Device: "/dev/sdb", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd"}, |
| 186 |
{Device: "/dev/sdb", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod"}, |
| 187 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2"}, |
| 188 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod1"}, |
| 189 |
{Device: "/dev/sdc", Path: "/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2"}, |
| 190 |
}) |
| 191 |
|
| 192 |
tests := []struct { |
| 193 |
mountPath string |
| 194 |
expectedRefs []string |
| 195 |
}{ |
| 196 |
{ |
| 197 |
"/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd", |
| 198 |
[]string{ |
| 199 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd-in-pod", |
| 200 |
}, |
| 201 |
}, |
| 202 |
{ |
| 203 |
"/var/lib/kubelet/plugins/kubernetes.io/gce-pd/mounts/gce-pd2", |
| 204 |
[]string{ |
| 205 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod1", |
| 206 |
"/var/lib/kubelet/pods/some-pod/volumes/kubernetes.io~gce-pd/gce-pd2-in-pod2", |
| 207 |
}, |
| 208 |
}, |
| 209 |
} |
| 210 |
|
| 211 |
for i, test := range tests { |
| 212 |
if refs, err := getMountRefsByDev(fm, test.mountPath); err != nil || !setEquivalent(test.expectedRefs, refs) { |
| 213 |
t.Errorf("%d. getMountRefsByDev(%q) = %v, %v; expected %v, nil", i, test.mountPath, refs, err, test.expectedRefs) |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
func TestPathWithinBase(t *testing.T) { |
| 219 |
tests := []struct { |
| 220 |
name string |
| 221 |
fullPath string |
| 222 |
basePath string |
| 223 |
expected bool |
| 224 |
}{ |
| 225 |
{ |
| 226 |
name: "good subpath", |
| 227 |
fullPath: "/a/b/c", |
| 228 |
basePath: "/a", |
| 229 |
expected: true, |
| 230 |
}, |
| 231 |
{ |
| 232 |
name: "good subpath 2", |
| 233 |
fullPath: "/a/b/c", |
| 234 |
basePath: "/a/b", |
| 235 |
expected: true, |
| 236 |
}, |
| 237 |
{ |
| 238 |
name: "good subpath end slash", |
| 239 |
fullPath: "/a/b/c/", |
| 240 |
basePath: "/a/b", |
| 241 |
expected: true, |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "good subpath backticks", |
| 245 |
fullPath: "/a/b/../c", |
| 246 |
basePath: "/a", |
| 247 |
expected: true, |
| 248 |
}, |
| 249 |
{ |
| 250 |
name: "good subpath equal", |
| 251 |
fullPath: "/a/b/c", |
| 252 |
basePath: "/a/b/c", |
| 253 |
expected: true, |
| 254 |
}, |
| 255 |
{ |
| 256 |
name: "good subpath equal 2", |
| 257 |
fullPath: "/a/b/c/", |
| 258 |
basePath: "/a/b/c", |
| 259 |
expected: true, |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "good subpath root", |
| 263 |
fullPath: "/a", |
| 264 |
basePath: "/", |
| 265 |
expected: true, |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "bad subpath parent", |
| 269 |
fullPath: "/a/b/c", |
| 270 |
basePath: "/a/b/c/d", |
| 271 |
expected: false, |
| 272 |
}, |
| 273 |
{ |
| 274 |
name: "bad subpath outside", |
| 275 |
fullPath: "/b/c", |
| 276 |
basePath: "/a/b/c", |
| 277 |
expected: false, |
| 278 |
}, |
| 279 |
{ |
| 280 |
name: "bad subpath prefix", |
| 281 |
fullPath: "/a/b/cd", |
| 282 |
basePath: "/a/b/c", |
| 283 |
expected: false, |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "bad subpath backticks", |
| 287 |
fullPath: "/a/../b", |
| 288 |
basePath: "/a", |
| 289 |
expected: false, |
| 290 |
}, |
| 291 |
{ |
| 292 |
name: "configmap subpath", |
| 293 |
fullPath: "/var/lib/kubelet/pods/uuid/volumes/kubernetes.io~configmap/config/..timestamp/file.txt", |
| 294 |
basePath: "/var/lib/kubelet/pods/uuid/volumes/kubernetes.io~configmap/config", |
| 295 |
expected: true, |
| 296 |
}, |
| 297 |
} |
| 298 |
for _, test := range tests { |
| 299 |
if PathWithinBase(test.fullPath, test.basePath) != test.expected { |
| 300 |
t.Errorf("test %q failed: expected %v", test.name, test.expected) |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
func TestSearchMountPoints(t *testing.T) { |
| 306 |
base := ` |
| 307 |
19 25 0:18 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw |
| 308 |
20 25 0:4 / /proc rw,nosuid,nodev,noexec,relatime shared:12 - proc proc rw |
| 309 |
21 25 0:6 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=4058156k,nr_inodes=1014539,mode=755 |
| 310 |
22 21 0:14 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000 |
| 311 |
23 25 0:19 / /run rw,nosuid,noexec,relatime shared:5 - tmpfs tmpfs rw,size=815692k,mode=755 |
| 312 |
25 0 252:0 / / rw,relatime shared:1 - ext4 /dev/mapper/ubuntu--vg-root rw,errors=remount-ro,data=ordered |
| 313 |
26 19 0:12 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw |
| 314 |
27 21 0:21 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw |
| 315 |
28 23 0:22 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k |
| 316 |
29 19 0:23 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755 |
| 317 |
30 29 0:24 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd |
| 318 |
31 19 0:25 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:11 - pstore pstore rw |
| 319 |
32 29 0:26 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:13 - cgroup cgroup rw,devices |
| 320 |
33 29 0:27 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:14 - cgroup cgroup rw,freezer |
| 321 |
34 29 0:28 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,pids |
| 322 |
35 29 0:29 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,blkio |
| 323 |
36 29 0:30 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,memory |
| 324 |
37 29 0:31 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,perf_event |
| 325 |
38 29 0:32 / /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,hugetlb |
| 326 |
39 29 0:33 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,cpu,cpuacct |
| 327 |
40 29 0:34 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:21 - cgroup cgroup rw,cpuset |
| 328 |
41 29 0:35 / /sys/fs/cgroup/net_cls,net_prio rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,net_cls,net_prio |
| 329 |
58 25 7:1 / /mnt/disks/blkvol1 rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordere |
| 330 |
` |
| 331 |
|
| 332 |
testcases := []struct { |
| 333 |
name string |
| 334 |
source string |
| 335 |
mountInfos string |
| 336 |
expectedRefs []string |
| 337 |
expectedErr error |
| 338 |
}{ |
| 339 |
{ |
| 340 |
"dir", |
| 341 |
"/mnt/disks/vol1", |
| 342 |
base, |
| 343 |
nil, |
| 344 |
nil, |
| 345 |
}, |
| 346 |
{ |
| 347 |
"dir-used", |
| 348 |
"/mnt/disks/vol1", |
| 349 |
base + ` |
| 350 |
56 25 252:0 /mnt/disks/vol1 /var/lib/kubelet/pods/1890aef5-5a60-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test rw,relatime shared:1 - ext4 /dev/mapper/ubuntu--vg-root rw,errors=remount-ro,data=ordered |
| 351 |
57 25 0:45 / /mnt/disks/vol rw,relatime shared:36 - tmpfs tmpfs rw |
| 352 |
`, |
| 353 |
[]string{"/var/lib/kubelet/pods/1890aef5-5a60-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test"}, |
| 354 |
nil, |
| 355 |
}, |
| 356 |
{ |
| 357 |
"tmpfs-vol", |
| 358 |
"/mnt/disks/vol1", |
| 359 |
base + `120 25 0:76 / /mnt/disks/vol1 rw,relatime shared:41 - tmpfs vol1 rw,size=10000k |
| 360 |
`, |
| 361 |
nil, |
| 362 |
nil, |
| 363 |
}, |
| 364 |
{ |
| 365 |
"tmpfs-vol-used-by-two-pods", |
| 366 |
"/mnt/disks/vol1", |
| 367 |
base + `120 25 0:76 / /mnt/disks/vol1 rw,relatime shared:41 - tmpfs vol1 rw,size=10000k |
| 368 |
196 25 0:76 / /var/lib/kubelet/pods/ade3ac21-5a5b-11e8-8559-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-8f263585 rw,relatime shared:41 - tmpfs vol1 rw,size=10000k |
| 369 |
228 25 0:76 / /var/lib/kubelet/pods/ac60532d-5a5b-11e8-8559-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-8f263585 rw,relatime shared:41 - tmpfs vol1 rw,size=10000k |
| 370 |
`, |
| 371 |
[]string{ |
| 372 |
"/var/lib/kubelet/pods/ade3ac21-5a5b-11e8-8559-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-8f263585", |
| 373 |
"/var/lib/kubelet/pods/ac60532d-5a5b-11e8-8559-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-8f263585", |
| 374 |
}, |
| 375 |
nil, |
| 376 |
}, |
| 377 |
{ |
| 378 |
"tmpfs-subdir-used-indirectly-via-bindmount-dir-by-one-pod", |
| 379 |
"/mnt/vol1/foo", |
| 380 |
base + `177 25 0:46 / /mnt/data rw,relatime shared:37 - tmpfs data rw |
| 381 |
190 25 0:46 /vol1 /mnt/vol1 rw,relatime shared:37 - tmpfs data rw |
| 382 |
191 25 0:46 /vol2 /mnt/vol2 rw,relatime shared:37 - tmpfs data rw |
| 383 |
62 25 0:46 /vol1/foo /var/lib/kubelet/pods/e25f2f01-5b06-11e8-8694-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test rw,relatime shared:37 - tmpfs data rw |
| 384 |
`, |
| 385 |
[]string{"/var/lib/kubelet/pods/e25f2f01-5b06-11e8-8694-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test"}, |
| 386 |
nil, |
| 387 |
}, |
| 388 |
{ |
| 389 |
"dir-bindmounted", |
| 390 |
"/mnt/disks/vol2", |
| 391 |
base + `342 25 252:0 /mnt/disks/vol2 /mnt/disks/vol2 rw,relatime shared:1 - ext4 /dev/mapper/ubuntu--vg-root rw,errors=remount-ro,data=ordered |
| 392 |
`, |
| 393 |
nil, |
| 394 |
nil, |
| 395 |
}, |
| 396 |
{ |
| 397 |
"dir-bindmounted-used-by-one-pod", |
| 398 |
"/mnt/disks/vol2", |
| 399 |
base + `342 25 252:0 /mnt/disks/vol2 /mnt/disks/vol2 rw,relatime shared:1 - ext4 /dev/mapper/ubuntu--vg-root rw,errors=remount-ro,data=ordered |
| 400 |
77 25 252:0 /mnt/disks/vol2 /var/lib/kubelet/pods/f30dc360-5a5d-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-1fb30a1c rw,relatime shared:1 - ext4 /dev/mapper/ubuntu--vg-root rw,errors=remount-ro,data=ordered |
| 401 |
`, |
| 402 |
[]string{"/var/lib/kubelet/pods/f30dc360-5a5d-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-1fb30a1c"}, |
| 403 |
nil, |
| 404 |
}, |
| 405 |
{ |
| 406 |
"blockfs", |
| 407 |
"/mnt/disks/blkvol1", |
| 408 |
base + `58 25 7:1 / /mnt/disks/blkvol1 rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 409 |
`, |
| 410 |
nil, |
| 411 |
nil, |
| 412 |
}, |
| 413 |
{ |
| 414 |
"blockfs-used-by-one-pod", |
| 415 |
"/mnt/disks/blkvol1", |
| 416 |
base + `58 25 7:1 / /mnt/disks/blkvol1 rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 417 |
62 25 7:1 / /var/lib/kubelet/pods/f19fe4e2-5a63-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 418 |
`, |
| 419 |
[]string{"/var/lib/kubelet/pods/f19fe4e2-5a63-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test"}, |
| 420 |
nil, |
| 421 |
}, |
| 422 |
{ |
| 423 |
"blockfs-used-by-two-pods", |
| 424 |
"/mnt/disks/blkvol1", |
| 425 |
base + `58 25 7:1 / /mnt/disks/blkvol1 rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 426 |
62 25 7:1 / /var/lib/kubelet/pods/f19fe4e2-5a63-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 427 |
95 25 7:1 / /var/lib/kubelet/pods/4854a48b-5a64-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test rw,relatime shared:38 - ext4 /dev/loop1 rw,data=ordered |
| 428 |
`, |
| 429 |
[]string{ |
| 430 |
"/var/lib/kubelet/pods/f19fe4e2-5a63-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test", |
| 431 |
"/var/lib/kubelet/pods/4854a48b-5a64-11e8-962f-000c29bb0377/volumes/kubernetes.io~local-volume/local-pv-test", |
| 432 |
}, |
| 433 |
nil, |
| 434 |
}, |
| 435 |
} |
| 436 |
tmpFile, err := os.CreateTemp("", "test-get-filetype") |
| 437 |
if err != nil { |
| 438 |
t.Fatal(err) |
| 439 |
} |
| 440 |
defer os.Remove(tmpFile.Name()) |
| 441 |
defer tmpFile.Close() |
| 442 |
for _, v := range testcases { |
| 443 |
assert.NoError(t, tmpFile.Truncate(0)) |
| 444 |
_, err := tmpFile.Seek(0, 0) |
| 445 |
assert.NoError(t, err) |
| 446 |
_, err = tmpFile.WriteString(v.mountInfos) |
| 447 |
assert.NoError(t, err) |
| 448 |
assert.NoError(t, tmpFile.Sync()) |
| 449 |
refs, err := SearchMountPoints(v.source, tmpFile.Name()) |
| 450 |
if !reflect.DeepEqual(refs, v.expectedRefs) { |
| 451 |
t.Errorf("test %q: expected Refs: %#v, got %#v", v.name, v.expectedRefs, refs) |
| 452 |
} |
| 453 |
if err != v.expectedErr { |
| 454 |
t.Errorf("test %q: expected err: %v, got %v", v.name, v.expectedErr, err) |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
func TestSensitiveMountOptions(t *testing.T) { |
| 460 |
// Arrange |
| 461 |
testcases := []struct { |
| 462 |
source string |
| 463 |
target string |
| 464 |
fstype string |
| 465 |
options []string |
| 466 |
sensitiveOptions []string |
| 467 |
mountFlags []string |
| 468 |
}{ |
| 469 |
{ |
| 470 |
source: "mySrc", |
| 471 |
target: "myTarget", |
| 472 |
fstype: "myFS", |
| 473 |
options: []string{"o1", "o2"}, |
| 474 |
sensitiveOptions: []string{"s1", "s2"}, |
| 475 |
mountFlags: []string{}, |
| 476 |
}, |
| 477 |
{ |
| 478 |
source: "mySrc", |
| 479 |
target: "myTarget", |
| 480 |
fstype: "myFS", |
| 481 |
options: []string{}, |
| 482 |
sensitiveOptions: []string{"s1", "s2"}, |
| 483 |
mountFlags: []string{}, |
| 484 |
}, |
| 485 |
{ |
| 486 |
source: "mySrc", |
| 487 |
target: "myTarget", |
| 488 |
fstype: "myFS", |
| 489 |
options: []string{"o1", "o2"}, |
| 490 |
sensitiveOptions: []string{}, |
| 491 |
mountFlags: []string{}, |
| 492 |
}, |
| 493 |
{ |
| 494 |
source: "mySrc", |
| 495 |
target: "myTarget", |
| 496 |
fstype: "myFS", |
| 497 |
options: []string{"o1", "o2"}, |
| 498 |
sensitiveOptions: []string{"s1", "s2"}, |
| 499 |
mountFlags: []string{"--no-canonicalize"}, |
| 500 |
}, |
| 501 |
} |
| 502 |
|
| 503 |
for _, v := range testcases { |
| 504 |
// Act |
| 505 |
mountArgs, mountArgsLogStr := MakeMountArgsSensitiveWithMountFlags(v.source, v.target, v.fstype, v.options, v.sensitiveOptions, v.mountFlags) |
| 506 |
|
| 507 |
// Assert |
| 508 |
t.Logf("\r\nmountArgs =%q\r\nmountArgsLogStr=%q", mountArgs, mountArgsLogStr) |
| 509 |
for _, mountFlag := range v.mountFlags { |
| 510 |
if found := mountArgsContainString(t, mountArgs, mountFlag); !found { |
| 511 |
t.Errorf("Expected mountFlag (%q) to exist in returned mountArgs (%q), but it does not", mountFlag, mountArgs) |
| 512 |
} |
| 513 |
if !strings.Contains(mountArgsLogStr, mountFlag) { |
| 514 |
t.Errorf("Expected mountFlag (%q) to exist in returned mountArgsLogStr (%q), but it does", mountFlag, mountArgsLogStr) |
| 515 |
} |
| 516 |
} |
| 517 |
for _, option := range v.options { |
| 518 |
if found := mountArgsContainOption(t, mountArgs, option); !found { |
| 519 |
t.Errorf("Expected option (%q) to exist in returned mountArgs (%q), but it does not", option, mountArgs) |
| 520 |
} |
| 521 |
if !strings.Contains(mountArgsLogStr, option) { |
| 522 |
t.Errorf("Expected option (%q) to exist in returned mountArgsLogStr (%q), but it does", option, mountArgsLogStr) |
| 523 |
} |
| 524 |
} |
| 525 |
for _, sensitiveOption := range v.sensitiveOptions { |
| 526 |
if found := mountArgsContainOption(t, mountArgs, sensitiveOption); !found { |
| 527 |
t.Errorf("Expected sensitiveOption (%q) to exist in returned mountArgs (%q), but it does not", sensitiveOption, mountArgs) |
| 528 |
} |
| 529 |
if strings.Contains(mountArgsLogStr, sensitiveOption) { |
| 530 |
t.Errorf("Expected sensitiveOption (%q) to not exist in returned mountArgsLogStr (%q), but it does", sensitiveOption, mountArgsLogStr) |
| 531 |
} |
| 532 |
} |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
func TestHasSystemd(t *testing.T) { |
| 537 |
mounter := &Mounter{} |
| 538 |
_ = mounter.hasSystemd() |
| 539 |
if mounter.withSystemd == nil { |
| 540 |
t.Error("Failed to run detectSystemd()") |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
func mountArgsContainString(t *testing.T, mountArgs []string, wanted string) bool { |
| 545 |
for _, mountArg := range mountArgs { |
| 546 |
if mountArg == wanted { |
| 547 |
return true |
| 548 |
} |
| 549 |
} |
| 550 |
return false |
| 551 |
} |
| 552 |
|
| 553 |
func mountArgsContainOption(t *testing.T, mountArgs []string, option string) bool { |
| 554 |
optionsIndex := -1 |
| 555 |
for i, s := range mountArgs { |
| 556 |
if s == "-o" { |
| 557 |
optionsIndex = i + 1 |
| 558 |
break |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
if optionsIndex < 0 || optionsIndex >= len(mountArgs) { |
| 563 |
return false |
| 564 |
} |
| 565 |
|
| 566 |
return strings.Contains(mountArgs[optionsIndex], option) |
| 567 |
} |
| 568 |
|
| 569 |
func TestDetectSafeNotMountedBehavior(t *testing.T) { |
| 570 |
// Example output for umount from util-linux 2.30.2 |
| 571 |
notMountedOutput := "umount: /foo: not mounted." |
| 572 |
|
| 573 |
testcases := []struct { |
| 574 |
fakeCommandAction testexec.FakeCommandAction |
| 575 |
expectedSafe bool |
| 576 |
}{ |
| 577 |
{ |
| 578 |
fakeCommandAction: makeFakeCommandAction(notMountedOutput, errors.New("any error"), nil), |
| 579 |
expectedSafe: true, |
| 580 |
}, |
| 581 |
{ |
| 582 |
fakeCommandAction: makeFakeCommandAction(notMountedOutput, nil, nil), |
| 583 |
expectedSafe: false, |
| 584 |
}, |
| 585 |
{ |
| 586 |
fakeCommandAction: makeFakeCommandAction("any output", nil, nil), |
| 587 |
expectedSafe: false, |
| 588 |
}, |
| 589 |
{ |
| 590 |
fakeCommandAction: makeFakeCommandAction("any output", errors.New("any error"), nil), |
| 591 |
expectedSafe: false, |
| 592 |
}, |
| 593 |
} |
| 594 |
|
| 595 |
for _, v := range testcases { |
| 596 |
fakeexec := &testexec.FakeExec{ |
| 597 |
LookPathFunc: func(s string) (string, error) { |
| 598 |
return "fake-umount", nil |
| 599 |
}, |
| 600 |
CommandScript: []testexec.FakeCommandAction{v.fakeCommandAction}, |
| 601 |
} |
| 602 |
|
| 603 |
if detectSafeNotMountedBehaviorWithExec(fakeexec) != v.expectedSafe { |
| 604 |
var adj string |
| 605 |
if v.expectedSafe { |
| 606 |
adj = "safe" |
| 607 |
} else { |
| 608 |
adj = "unsafe" |
| 609 |
} |
| 610 |
t.Errorf("Expected to detect %s umount behavior, but did not", adj) |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
func TestCheckUmountError(t *testing.T) { |
| 616 |
target := "/test/path" |
| 617 |
withSafeNotMountedBehavior := true |
| 618 |
command := exec.Command("uname", "-r") // dummy command return status 0 |
| 619 |
|
| 620 |
if err := command.Run(); err != nil { |
| 621 |
t.Errorf("Faild to exec dummy command. err: %s", err) |
| 622 |
} |
| 623 |
|
| 624 |
testcases := []struct { |
| 625 |
output []byte |
| 626 |
err error |
| 627 |
expected bool |
| 628 |
}{ |
| 629 |
{ |
| 630 |
err: errors.New("wait: no child processes"), |
| 631 |
expected: true, |
| 632 |
}, |
| 633 |
{ |
| 634 |
output: []byte("umount: /test/path: not mounted."), |
| 635 |
err: errors.New("exit status 1"), |
| 636 |
expected: true, |
| 637 |
}, |
| 638 |
{ |
| 639 |
output: []byte("umount: /test/path: No such file or directory"), |
| 640 |
err: errors.New("exit status 1"), |
| 641 |
expected: false, |
| 642 |
}, |
| 643 |
} |
| 644 |
|
| 645 |
for _, v := range testcases { |
| 646 |
if err := checkUmountError(target, command, v.output, v.err, withSafeNotMountedBehavior); (err == nil) != v.expected { |
| 647 |
if v.expected { |
| 648 |
t.Errorf("Expected to return nil, but did not. err: %s", err) |
| 649 |
} else { |
| 650 |
t.Errorf("Expected to return error, but did not.") |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
// TODO https://github.com/kubernetes/kubernetes/pull/117539#discussion_r1181873355 |
| 657 |
func TestFormatConcurrency(t *testing.T) { |
| 658 |
const ( |
| 659 |
formatCount = 5 |
| 660 |
fstype = "ext4" |
| 661 |
output = "complete" |
| 662 |
defaultTimeout = 1 * time.Minute |
| 663 |
) |
| 664 |
|
| 665 |
tests := []struct { |
| 666 |
desc string |
| 667 |
max int |
| 668 |
timeout time.Duration |
| 669 |
}{ |
| 670 |
{ |
| 671 |
max: 2, |
| 672 |
}, |
| 673 |
{ |
| 674 |
max: 3, |
| 675 |
}, |
| 676 |
{ |
| 677 |
max: 4, |
| 678 |
}, |
| 679 |
} |
| 680 |
|
| 681 |
for _, tc := range tests { |
| 682 |
t.Run(fmt.Sprintf("max=%d,timeout=%s", tc.max, tc.timeout.String()), func(t *testing.T) { |
| 683 |
if tc.timeout == 0 { |
| 684 |
tc.timeout = defaultTimeout |
| 685 |
} |
| 686 |
|
| 687 |
var concurrent int |
| 688 |
var mu sync.Mutex |
| 689 |
witness := make(chan struct{}) |
| 690 |
|
| 691 |
exec := &testexec.FakeExec{} |
| 692 |
for i := 0; i < formatCount; i++ { |
| 693 |
exec.CommandScript = append(exec.CommandScript, makeFakeCommandAction(output, nil, func() { |
| 694 |
mu.Lock() |
| 695 |
concurrent++ |
| 696 |
mu.Unlock() |
| 697 |
|
| 698 |
<-witness |
| 699 |
|
| 700 |
mu.Lock() |
| 701 |
concurrent-- |
| 702 |
mu.Unlock() |
| 703 |
})) |
| 704 |
} |
| 705 |
mounter := NewSafeFormatAndMount(nil, exec, WithMaxConcurrentFormat(tc.max, tc.timeout)) |
| 706 |
|
| 707 |
// we run max+1 goroutines and block the command execution |
| 708 |
// only max goroutine should be running and the additional one should wait |
| 709 |
// for one to be released |
| 710 |
for i := 0; i < tc.max+1; i++ { |
| 711 |
go func() { |
| 712 |
_, err := mounter.format(fstype, nil) |
| 713 |
if err != nil { |
| 714 |
t.Errorf("format(%q): %v", fstype, err) |
| 715 |
} |
| 716 |
}() |
| 717 |
} |
| 718 |
|
| 719 |
// wait for all goorutines to be scheduled |
| 720 |
time.Sleep(100 * time.Millisecond) |
| 721 |
|
| 722 |
mu.Lock() |
| 723 |
if concurrent != tc.max { |
| 724 |
t.Errorf("SafeFormatAndMount.format() got concurrency: %d, want: %d", concurrent, tc.max) |
| 725 |
} |
| 726 |
mu.Unlock() |
| 727 |
|
| 728 |
// signal the commands to finish the goroutines, this will allow the command |
| 729 |
// that is pending to be executed |
| 730 |
for i := 0; i < tc.max; i++ { |
| 731 |
witness <- struct{}{} |
| 732 |
} |
| 733 |
|
| 734 |
// wait for all goroutines to acquire the lock and decrement the counter |
| 735 |
time.Sleep(100 * time.Millisecond) |
| 736 |
|
| 737 |
mu.Lock() |
| 738 |
if concurrent != 1 { |
| 739 |
t.Errorf("SafeFormatAndMount.format() got concurrency: %d, want: 1", concurrent) |
| 740 |
} |
| 741 |
mu.Unlock() |
| 742 |
|
| 743 |
// signal the pending command to finish, no more command should be running |
| 744 |
close(witness) |
| 745 |
|
| 746 |
// wait a few for the last goroutine to acquire the lock and decrements the counter down to zero |
| 747 |
time.Sleep(10 * time.Millisecond) |
| 748 |
|
| 749 |
mu.Lock() |
| 750 |
if concurrent != 0 { |
| 751 |
t.Errorf("SafeFormatAndMount.format() got concurrency: %d, want: 0", concurrent) |
| 752 |
} |
| 753 |
mu.Unlock() |
| 754 |
}) |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
// TODO https://github.com/kubernetes/kubernetes/pull/117539#discussion_r1181873355 |
| 759 |
func TestFormatTimeout(t *testing.T) { |
| 760 |
const ( |
| 761 |
formatCount = 5 |
| 762 |
fstype = "ext4" |
| 763 |
output = "complete" |
| 764 |
maxConcurrency = 4 |
| 765 |
timeout = 200 * time.Millisecond |
| 766 |
) |
| 767 |
|
| 768 |
var concurrent int |
| 769 |
var mu sync.Mutex |
| 770 |
witness := make(chan struct{}) |
| 771 |
|
| 772 |
exec := &testexec.FakeExec{} |
| 773 |
for i := 0; i < formatCount; i++ { |
| 774 |
exec.CommandScript = append(exec.CommandScript, makeFakeCommandAction(output, nil, func() { |
| 775 |
mu.Lock() |
| 776 |
concurrent++ |
| 777 |
mu.Unlock() |
| 778 |
|
| 779 |
<-witness |
| 780 |
|
| 781 |
mu.Lock() |
| 782 |
concurrent-- |
| 783 |
mu.Unlock() |
| 784 |
})) |
| 785 |
} |
| 786 |
mounter := NewSafeFormatAndMount(nil, exec, WithMaxConcurrentFormat(maxConcurrency, timeout)) |
| 787 |
|
| 788 |
for i := 0; i < maxConcurrency+1; i++ { |
| 789 |
go func() { |
| 790 |
_, err := mounter.format(fstype, nil) |
| 791 |
if err != nil { |
| 792 |
t.Errorf("format(%q): %v", fstype, err) |
| 793 |
} |
| 794 |
}() |
| 795 |
} |
| 796 |
|
| 797 |
// wait a bit more than the configured timeout |
| 798 |
time.Sleep(timeout + 100*time.Millisecond) |
| 799 |
|
| 800 |
mu.Lock() |
| 801 |
if concurrent != maxConcurrency+1 { |
| 802 |
t.Errorf("SafeFormatAndMount.format() got concurrency: %d, want: %d", concurrent, maxConcurrency+1) |
| 803 |
} |
| 804 |
mu.Unlock() |
| 805 |
|
| 806 |
// signal the pending commands to finish |
| 807 |
close(witness) |
| 808 |
// wait for all goroutines to acquire the lock and decrement the counter |
| 809 |
time.Sleep(100 * time.Millisecond) |
| 810 |
|
| 811 |
mu.Lock() |
| 812 |
if concurrent != 0 { |
| 813 |
t.Errorf("SafeFormatAndMount.format() got concurrency: %d, want: 0", concurrent) |
| 814 |
} |
| 815 |
mu.Unlock() |
| 816 |
} |
| 817 |
|
| 818 |
// Replicate some types found in "golang.org/x/exp/constraints" |
| 819 |
// to avoid a dependency on that package. |
| 820 |
type Signed interface { |
| 821 |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
| 822 |
} |
| 823 |
|
| 824 |
type Unsigned interface { |
| 825 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
| 826 |
} |
| 827 |
|
| 828 |
type Integer interface { |
| 829 |
Signed | Unsigned |
| 830 |
} |
| 831 |
|
| 832 |
// Some platforms define unix.Statfs_t.Flags differently. Our need here is |
| 833 |
// pretty constrained, so some aggressive type-conversion is OK. |
| 834 |
func mkStatfsFlags[T1 Integer, T2 Integer](orig T1, add T2) T1 { |
| 835 |
return orig | T1(add) |
| 836 |
} |
| 837 |
|
| 838 |
func TestGetBindMountOptions(t *testing.T) { |
| 839 |
var testCases = map[string]struct { |
| 840 |
flags int32 // smallest size used by any platform we care about |
| 841 |
mountoptions string |
| 842 |
}{ |
| 843 |
"ro": {flags: unix.MS_RDONLY, mountoptions: "ro"}, |
| 844 |
"nodev": {flags: unix.MS_NODEV, mountoptions: "nodev"}, |
| 845 |
"noexec": {flags: unix.MS_NOEXEC, mountoptions: "noexec"}, |
| 846 |
"nosuid": {flags: unix.MS_NOSUID, mountoptions: "nosuid"}, |
| 847 |
"noatime": {flags: unix.MS_NOATIME, mountoptions: "noatime"}, |
| 848 |
"relatime": {flags: unix.MS_RELATIME, mountoptions: "relatime"}, |
| 849 |
"nodiratime": {flags: unix.MS_NODIRATIME, mountoptions: "nodiratime"}, |
| 850 |
"ronodev": {flags: unix.MS_RDONLY | unix.MS_NODEV, mountoptions: "nodev,ro"}, |
| 851 |
"ronodevnoexec": {flags: unix.MS_RDONLY | unix.MS_NODEV | unix.MS_NOEXEC, mountoptions: "nodev,noexec,ro"}, |
| 852 |
} |
| 853 |
|
| 854 |
statfsMock := func(path string, buf *unix.Statfs_t) (err error) { |
| 855 |
*buf = unix.Statfs_t{} |
| 856 |
buf.Flags = mkStatfsFlags(buf.Flags, testCases[path].flags) |
| 857 |
return nil |
| 858 |
} |
| 859 |
|
| 860 |
testGetBindMountOptionsSingleCase := func(t *testing.T) { |
| 861 |
path := strings.Split(t.Name(), "/")[1] |
| 862 |
options, _ := getBindMountOptions(path, statfsMock) |
| 863 |
sort.Strings(options) |
| 864 |
optionString := strings.Join(options, ",") |
| 865 |
mountOptions := testCases[path].mountoptions |
| 866 |
if optionString != mountOptions { |
| 867 |
t.Fatalf(`Mountoptions differ. Wanted: %s, returned: %s`, mountOptions, optionString) |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
for k := range testCases { |
| 872 |
t.Run(k, testGetBindMountOptionsSingleCase) |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
func makeFakeCommandAction(stdout string, err error, cmdFn func()) testexec.FakeCommandAction { |
| 877 |
c := testexec.FakeCmd{ |
| 878 |
CombinedOutputScript: []testexec.FakeAction{ |
| 879 |
func() ([]byte, []byte, error) { |
| 880 |
if cmdFn != nil { |
| 881 |
cmdFn() |
| 882 |
} |
| 883 |
return []byte(stdout), nil, err |
| 884 |
}, |
| 885 |
}, |
| 886 |
} |
| 887 |
return func(cmd string, args ...string) utilexec.Cmd { |
| 888 |
return testexec.InitFakeCmd(&c, cmd, args...) |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
func TestIsLikelyNotMountPoint(t *testing.T) { |
| 893 |
mounter := Mounter{"fake/path", ptr.To(true), true, true} |
| 894 |
|
| 895 |
tests := []struct { |
| 896 |
fileName string |
| 897 |
targetLinkName string |
| 898 |
setUp func(base, fileName, targetLinkName string) error |
| 899 |
cleanUp func(base, fileName, targetLinkName string) error |
| 900 |
expectedResult bool |
| 901 |
expectError bool |
| 902 |
}{ |
| 903 |
{ |
| 904 |
"Dir", |
| 905 |
"", |
| 906 |
func(base, fileName, targetLinkName string) error { |
| 907 |
return os.Mkdir(filepath.Join(base, fileName), 0o750) |
| 908 |
}, |
| 909 |
func(base, fileName, targetLinkName string) error { |
| 910 |
return os.Remove(filepath.Join(base, fileName)) |
| 911 |
}, |
| 912 |
true, |
| 913 |
false, |
| 914 |
}, |
| 915 |
{ |
| 916 |
"InvalidDir", |
| 917 |
"", |
| 918 |
func(base, fileName, targetLinkName string) error { |
| 919 |
return nil |
| 920 |
}, |
| 921 |
func(base, fileName, targetLinkName string) error { |
| 922 |
return nil |
| 923 |
}, |
| 924 |
true, |
| 925 |
true, |
| 926 |
}, |
| 927 |
{ |
| 928 |
"ValidSymLink", |
| 929 |
"targetSymLink", |
| 930 |
func(base, fileName, targetLinkName string) error { |
| 931 |
targeLinkPath := filepath.Join(base, targetLinkName) |
| 932 |
if err := os.Mkdir(targeLinkPath, 0o750); err != nil { |
| 933 |
return err |
| 934 |
} |
| 935 |
|
| 936 |
filePath := filepath.Join(base, fileName) |
| 937 |
if err := os.Symlink(targeLinkPath, filePath); err != nil { |
| 938 |
return err |
| 939 |
} |
| 940 |
return nil |
| 941 |
}, |
| 942 |
func(base, fileName, targetLinkName string) error { |
| 943 |
if err := os.Remove(filepath.Join(base, fileName)); err != nil { |
| 944 |
return err |
| 945 |
} |
| 946 |
return os.Remove(filepath.Join(base, targetLinkName)) |
| 947 |
}, |
| 948 |
true, |
| 949 |
false, |
| 950 |
}, |
| 951 |
{ |
| 952 |
"InvalidSymLink", |
| 953 |
"targetSymLink2", |
| 954 |
func(base, fileName, targetLinkName string) error { |
| 955 |
targeLinkPath := filepath.Join(base, targetLinkName) |
| 956 |
if err := os.Mkdir(targeLinkPath, 0o750); err != nil { |
| 957 |
return err |
| 958 |
} |
| 959 |
|
| 960 |
filePath := filepath.Join(base, fileName) |
| 961 |
if err := os.Symlink(targeLinkPath, filePath); err != nil { |
| 962 |
return err |
| 963 |
} |
| 964 |
return os.Remove(targeLinkPath) |
| 965 |
}, |
| 966 |
func(base, fileName, targetLinkName string) error { |
| 967 |
return os.Remove(filepath.Join(base, fileName)) |
| 968 |
}, |
| 969 |
true, |
| 970 |
true, |
| 971 |
}, |
| 972 |
} |
| 973 |
|
| 974 |
for _, test := range tests { |
| 975 |
// test with absolute and relative path |
| 976 |
baseList := []string{t.TempDir(), "./"} |
| 977 |
for _, base := range baseList { |
| 978 |
if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil { |
| 979 |
t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err) |
| 980 |
} |
| 981 |
|
| 982 |
filePath := filepath.Join(base, test.fileName) |
| 983 |
result, err := mounter.IsLikelyNotMountPoint(filePath) |
| 984 |
if result != test.expectedResult { |
| 985 |
t.Errorf("Expect result not equal with IsLikelyNotMountPoint(%s) return: %t, expected: %t", filePath, result, test.expectedResult) |
| 986 |
} |
| 987 |
|
| 988 |
if base == "./" { |
| 989 |
if err := test.cleanUp(base, test.fileName, test.targetLinkName); err != nil { |
| 990 |
t.Fatalf("unexpected error in cleanUp(%s, %s): %v", test.fileName, test.targetLinkName, err) |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
if (err != nil) != test.expectError { |
| 995 |
if test.expectError { |
| 996 |
t.Errorf("Expect error during IsLikelyNotMountPoint(%s)", filePath) |
| 997 |
} else { |
| 998 |
t.Errorf("Expect error is nil during IsLikelyNotMountPoint(%s): %v", filePath, err) |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
} |