|
5 | 5 | "net" |
6 | 6 | "os" |
7 | 7 | "path/filepath" |
| 8 | + "reflect" |
| 9 | + "runtime" |
| 10 | + "strings" |
8 | 11 | "testing" |
9 | 12 |
|
10 | 13 | "golang.org/x/sys/unix" |
@@ -276,8 +279,14 @@ func tryOpenat2() error { |
276 | 279 | } |
277 | 280 |
|
278 | 281 | func TestMountedBy(t *testing.T) { |
279 | | - openat2Supported := tryOpenat2() == nil |
280 | 282 | checked := false |
| 283 | + |
| 284 | + // List of individual implementations to check. |
| 285 | + toCheck := []func(string) (bool, error){mountedByMountinfo, mountedByStat} |
| 286 | + if tryOpenat2() == nil { |
| 287 | + toCheck = append(toCheck, mountedByOpenat2) |
| 288 | + } |
| 289 | + |
281 | 290 | for _, tc := range testMounts { |
282 | 291 | tc := tc |
283 | 292 | t.Run(tc.desc, func(t *testing.T) { |
@@ -316,45 +325,29 @@ func TestMountedBy(t *testing.T) { |
316 | 325 | t.Fatalf("normalizePath: %v", err) |
317 | 326 | } |
318 | 327 |
|
319 | | - mounted, err = mountedByMountinfo(m) |
320 | | - if err != nil { |
321 | | - t.Errorf("mountedByMountinfo error: %v", err) |
322 | | - // Check false is returned in error case. |
323 | | - if mounted != false { |
324 | | - t.Errorf("MountedByMountinfo: expected false on error, got %v", mounted) |
325 | | - } |
326 | | - } else if mounted != exp { |
327 | | - t.Errorf("mountedByMountinfo: expected %v, got %v", exp, mounted) |
328 | | - } |
329 | | - checked = true |
330 | | - |
331 | | - mounted, err = mountedByStat(m) |
332 | | - if err != nil { |
333 | | - t.Errorf("mountedByStat error: %v", err) |
334 | | - // Check false is returned in error case. |
335 | | - if mounted != false { |
336 | | - t.Errorf("MountedByStat: expected false on error, got %v", mounted) |
337 | | - } |
338 | | - } else if mounted != exp && !tc.isBind { // mountedByStat can not detect bind mounts |
339 | | - t.Errorf("mountedByStat: expected %v, got %v", exp, mounted) |
340 | | - } |
341 | | - |
342 | | - if !openat2Supported { |
343 | | - return |
344 | | - } |
345 | | - mounted, err = mountedByOpenat2(m) |
346 | | - if err != nil { |
347 | | - t.Errorf("mountedByOpenat2 error: %v", err) |
348 | | - // Check false is returned in error case. |
349 | | - if mounted != false { |
350 | | - t.Errorf("MountedByOpenat2: expected false on error, got %v", mounted) |
| 328 | + for _, fn := range toCheck { |
| 329 | + // Figure out function name. |
| 330 | + name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() |
| 331 | + |
| 332 | + mounted, err = fn(m) |
| 333 | + if err != nil { |
| 334 | + t.Errorf("%s: %v", name, err) |
| 335 | + // Check false is returned in error case. |
| 336 | + if mounted != false { |
| 337 | + t.Errorf("%s: expected false on error, got %v", name, mounted) |
| 338 | + } |
| 339 | + } else if mounted != exp { |
| 340 | + if tc.isBind && strings.HasSuffix(name, "mountedByStat") { |
| 341 | + // mountedByStat can not detect bind mounts. |
| 342 | + } else { |
| 343 | + t.Errorf("%s: expected %v, got %v", name, exp, mounted) |
| 344 | + } |
351 | 345 | } |
352 | | - } else if mounted != exp { |
353 | | - t.Errorf("mountedByOpenat2: expected %v, got %v", exp, mounted) |
| 346 | + checked = true |
354 | 347 | } |
355 | 348 | }) |
356 | | - |
357 | 349 | } |
| 350 | + |
358 | 351 | if !checked { |
359 | 352 | t.Skip("no mounts to check") |
360 | 353 | } |
|
0 commit comments