1 # Copyright (C) 2018 Facebook
3 # This file is part of libbtrfsutil.
5 # libbtrfsutil is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # libbtrfsutil is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
19 from pathlib import PurePath
25 HAVE_PATH_LIKE = hasattr(PurePath, '__fspath__')
28 @unittest.skipIf(os.geteuid() != 0, 'must be run as root')
29 class BtrfsTestCase(unittest.TestCase):
31 self.mountpoint = tempfile.mkdtemp()
33 with tempfile.NamedTemporaryFile(delete=False) as f:
34 os.truncate(f.fileno(), 1024 * 1024 * 1024)
36 except Exception as e:
37 os.rmdir(self.mountpoint)
41 subprocess.check_call(['mkfs.btrfs', '-q', self.image])
42 subprocess.check_call(['mount', '-o', 'loop', '--', self.image, self.mountpoint])
43 except Exception as e:
45 os.rmdir(self.mountpoint)
50 subprocess.check_call(['umount', self.mountpoint])
53 os.rmdir(self.mountpoint)
56 def path_or_fd(path, open_flags=os.O_RDONLY):
61 fd = os.open(path, open_flags)