| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2019-2022 Matthias Klumpp <[email protected]> |
| 4 |
# |
| 5 |
# Licensed under the GNU Lesser General Public License Version 3 |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU Lesser General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the license, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This software is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU Lesser General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU Lesser General Public License |
| 18 |
# along with this software. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import os |
| 21 |
import tempfile |
| 22 |
|
| 23 |
from debspawn.utils.misc import umount, bindmount, is_mountpoint, rmtree_mntsafe |
| 24 |
|
| 25 |
|
| 26 |
def test_bindmount_umount(gconfig): |
| 27 |
with tempfile.TemporaryDirectory() as src_tmpdir1: |
| 28 |
with tempfile.TemporaryDirectory() as src_tmpdir2: |
| 29 |
with tempfile.TemporaryDirectory() as dest_tmpdir: |
| 30 |
bindmount(src_tmpdir1, dest_tmpdir) |
| 31 |
assert is_mountpoint(dest_tmpdir) |
| 32 |
|
| 33 |
bindmount(src_tmpdir2, dest_tmpdir) |
| 34 |
assert is_mountpoint(dest_tmpdir) |
| 35 |
|
| 36 |
# sanity check |
| 37 |
open(os.path.join(src_tmpdir2, 'test'), 'a').close() |
| 38 |
assert os.path.isfile(os.path.join(dest_tmpdir, 'test')) |
| 39 |
|
| 40 |
# umount is supposed to unmount everything, even overmounted directories |
| 41 |
umount(dest_tmpdir) |
| 42 |
assert not is_mountpoint(dest_tmpdir) |
| 43 |
|
| 44 |
|
| 45 |
def test_rmtree_mntsafe(gconfig): |
| 46 |
mnt_tmpdir = tempfile.TemporaryDirectory().name |
| 47 |
dest_tmpdir = tempfile.TemporaryDirectory().name |
| 48 |
|
| 49 |
# create directory structure and files to delete |
| 50 |
mp_dir = os.path.join(dest_tmpdir, 'subdir', 'mountpoint') |
| 51 |
mount_subdir = os.path.join(mnt_tmpdir, 'subdir_in_mount') |
| 52 |
os.makedirs(mp_dir) |
| 53 |
os.makedirs(mount_subdir) |
| 54 |
open(os.path.join(dest_tmpdir, 'file1.txt'), 'a').close() |
| 55 |
open(os.path.join(mp_dir, 'file_below_mountpoint.txt'), 'a').close() |
| 56 |
open(os.path.join(mnt_tmpdir, 'file_in_mount.txt'), 'a').close() |
| 57 |
open(os.path.join(mount_subdir, 'file_in_mount_subdir.txt'), 'a').close() |
| 58 |
|
| 59 |
# create bindmount |
| 60 |
bindmount(mnt_tmpdir, mp_dir) |
| 61 |
assert is_mountpoint(mp_dir) |
| 62 |
|
| 63 |
# try to delete the directory structure containing bindmounts |
| 64 |
rmtree_mntsafe(dest_tmpdir) |
| 65 |
|
| 66 |
# verify |
| 67 |
assert not os.path.exists(dest_tmpdir) |
| 68 |
assert os.path.isfile(os.path.join(mnt_tmpdir, 'file_in_mount.txt')) |
| 69 |
assert os.path.isfile(os.path.join(mount_subdir, 'file_in_mount_subdir.txt')) |
| 70 |
|
| 71 |
# cleanup mounted dir |
| 72 |
rmtree_mntsafe(mnt_tmpdir) |
| 73 |
assert not os.path.exists(mnt_tmpdir) |