mount: fix read-only bind (#1368)

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda
2017-08-17 02:53:25 +00:00
parent b4cc42d028
commit a560e5e0ef
6 changed files with 112 additions and 14 deletions

View File

@@ -38,6 +38,8 @@ func SnapshotterSuite(t *testing.T, name string, snapshotterFn func(ctx context.
// Rename test still fails on some kernels with overlay
//t.Run("Rename", makeTest(name, snapshotterFn, checkRename))
t.Run("ViewReadonly", makeTest(name, snapshotterFn, checkSnapshotterViewReadonly))
}
func makeTest(name string, snapshotterFn func(ctx context.Context, root string) (snapshot.Snapshotter, func() error, error), fn func(ctx context.Context, t *testing.T, snapshotter snapshot.Snapshotter, work string)) func(t *testing.T) {
@@ -651,3 +653,40 @@ func checkRemove(ctx context.Context, t *testing.T, snapshotter snapshot.Snapsho
t.Fatal(err)
}
}
// checkSnapshotterViewReadonly ensures a KindView snapshot to be mounted as a read-only filesystem.
// This function is called only when WithTestViewReadonly is true.
func checkSnapshotterViewReadonly(ctx context.Context, t *testing.T, snapshotter snapshot.Snapshotter, work string) {
preparing := filepath.Join(work, "preparing")
if _, err := snapshotter.Prepare(ctx, preparing, ""); err != nil {
t.Fatal(err)
}
committed := filepath.Join(work, "commited")
if err := snapshotter.Commit(ctx, committed, preparing); err != nil {
t.Fatal(err)
}
view := filepath.Join(work, "view")
m, err := snapshotter.View(ctx, view, committed)
if err != nil {
t.Fatal(err)
}
viewMountPoint := filepath.Join(work, "view-mount")
if err := os.MkdirAll(viewMountPoint, 0777); err != nil {
t.Fatal(err)
}
// Just checking the option string of m is not enough, we need to test real mount. (#1368)
if err := mount.MountAll(m, viewMountPoint); err != nil {
t.Fatal(err)
}
testfile := filepath.Join(viewMountPoint, "testfile")
if err := ioutil.WriteFile(testfile, []byte("testcontent"), 0777); err != nil {
t.Logf("write to %q failed with %v (EROFS is expected but can be other error code)", testfile, err)
} else {
t.Fatalf("write to %q should fail (EROFS) but did not fail", testfile)
}
testutil.Unmount(t, viewMountPoint)
assert.NoError(t, snapshotter.Remove(ctx, view))
assert.NoError(t, snapshotter.Remove(ctx, committed))
}