@@ -300,3 +300,34 @@ func TestIgnoreCgroup2Mount(t *testing.T) {
300300 }
301301 }
302302}
303+
304+ const fakeMountInfo = ` 18 24 0:17 / /sys rw,nosuid,nodev,noexec,relatime - sysfs sysfs rw
305+ 100 99 1:31 / /foo/bar rw,relatime - fake fake rw,fake
306+ 100 99 1:31 / /foo/bar/baz2 rw,relatime - fake fake rw,fake
307+ 100 99 1:31 / /foo/bar/baz rw,relatime - fake fake rw,fake
308+ 100 99 1:31 / /foo/bar/bazza rw,relatime - fake fake rw,fake
309+ 100 99 1:31 / /foo/bar/baz3 rw,relatime - fake fake rw,fake
310+ 100 99 1:31 / /foo rw,relatime - fake fake rw,fake
311+ 100 99 1:31 / /unrelated rw,relatime - fake fake rw,fake
312+ 100 99 1:31 / / rw,relatime - fake fake rw,fake
313+ `
314+
315+ func TestGetClosestMountpointAncestor (t * testing.T ) {
316+ testCases := []struct {
317+ input string
318+ mountinfos string
319+ output string
320+ }{
321+ {input : "/foo/bar/baz/a/b/c" , mountinfos : fakeMountInfo , output : "/foo/bar/baz" },
322+ {input : "/foo/bar/baz" , mountinfos : fakeMountInfo , output : "/foo/bar/baz" },
323+ {input : "/foo/bar/bazza" , mountinfos : fakeMountInfo , output : "/foo/bar/bazza" },
324+ {input : "/a/b/c/d" , mountinfos : fakeMountInfo , output : "/" },
325+ }
326+
327+ for _ , c := range testCases {
328+ mountpoint := GetClosestMountpointAncestor (c .input , c .mountinfos )
329+ if mountpoint != c .output {
330+ t .Errorf ("expected %s, got %s" , c .output , mountpoint )
331+ }
332+ }
333+ }
0 commit comments