libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id()
[platform/upstream/btrfs-progs.git] / libbtrfsutil / python / tests / test_subvolume.py
1 # Copyright (C) 2018 Facebook
2 #
3 # This file is part of libbtrfsutil.
4 #
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.
9 #
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.
14 #
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/>.
17
18 import fcntl
19 import errno
20 import os
21 import os.path
22 from pathlib import PurePath
23 import traceback
24
25 import btrfsutil
26 from tests import BtrfsTestCase
27
28
29 class TestSubvolume(BtrfsTestCase):
30     def test_is_subvolume(self):
31         dir = os.path.join(self.mountpoint, 'foo')
32         os.mkdir(dir)
33
34         for arg in self.path_or_fd(self.mountpoint):
35             with self.subTest(type=type(arg)):
36                 self.assertTrue(btrfsutil.is_subvolume(arg))
37         for arg in self.path_or_fd(dir):
38             with self.subTest(type=type(arg)):
39                 self.assertFalse(btrfsutil.is_subvolume(arg))
40
41         with self.assertRaises(btrfsutil.BtrfsUtilError) as e:
42             btrfsutil.is_subvolume(os.path.join(self.mountpoint, 'bar'))
43         # This is a bit of an implementation detail, but really this is testing
44         # that the exception is initialized correctly.
45         self.assertEqual(e.exception.btrfsutilerror, btrfsutil.ERROR_STATFS_FAILED)
46         self.assertEqual(e.exception.errno, errno.ENOENT)
47
48     def test_subvolume_id(self):
49         dir = os.path.join(self.mountpoint, 'foo')
50         os.mkdir(dir)
51
52         for arg in self.path_or_fd(self.mountpoint):
53             with self.subTest(type=type(arg)):
54                 self.assertEqual(btrfsutil.subvolume_id(arg), 5)
55         for arg in self.path_or_fd(dir):
56             with self.subTest(type=type(arg)):
57                 self.assertEqual(btrfsutil.subvolume_id(arg), 5)