summaryrefslogtreecommitdiff
path: root/libbtrfsutil
diff options
context:
space:
mode:
authorOmar Sandoval <osandov@fb.com>2018-11-13 23:46:59 -0800
committerDavid Sterba <dsterba@suse.com>2018-11-26 16:45:22 +0100
commit70126a1570f5c5b042620274a2a009effb8639a0 (patch)
tree1782a7d524ad9268c410f59ede25f56ffc39d4bc /libbtrfsutil
parentc62625046a40fe9cd184fde309bfbe9b95ef4de3 (diff)
libbtrfsutil: use SubvolumeIterator as context manager in tests
We're leaking file descriptors, which makes it impossible to clean up the temporary mount point created by the test. Signed-off-by: Omar Sandoval <osandov@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'libbtrfsutil')
-rw-r--r--libbtrfsutil/python/tests/test_subvolume.py51
1 files changed, 30 insertions, 21 deletions
diff --git a/libbtrfsutil/python/tests/test_subvolume.py b/libbtrfsutil/python/tests/test_subvolume.py
index f2a4cdb..4049b08 100644
--- a/libbtrfsutil/python/tests/test_subvolume.py
+++ b/libbtrfsutil/python/tests/test_subvolume.py
@@ -334,11 +334,13 @@ class TestSubvolume(BtrfsTestCase):
os.chdir(self.mountpoint)
btrfsutil.create_subvolume('foo')
- path, subvol = next(btrfsutil.SubvolumeIterator('.', info=True))
- self.assertEqual(path, 'foo')
- self.assertIsInstance(subvol, btrfsutil.SubvolumeInfo)
- self.assertEqual(subvol.id, 256)
- self.assertEqual(subvol.parent_id, 5)
+ with btrfsutil.SubvolumeIterator('.', info=True) as it:
+ path, subvol = next(it)
+ self.assertEqual(path, 'foo')
+ self.assertIsInstance(subvol, btrfsutil.SubvolumeInfo)
+ self.assertEqual(subvol.id, 256)
+ self.assertEqual(subvol.parent_id, 5)
+ self.assertRaises(StopIteration, next, it)
btrfsutil.create_subvolume('foo/bar')
btrfsutil.create_subvolume('foo/bar/baz')
@@ -350,30 +352,37 @@ class TestSubvolume(BtrfsTestCase):
]
for arg in self.path_or_fd('.'):
- with self.subTest(type=type(arg)):
- self.assertEqual(list(btrfsutil.SubvolumeIterator(arg)), subvols)
- self.assertEqual(list(btrfsutil.SubvolumeIterator('.', top=0)), subvols)
- self.assertEqual(list(btrfsutil.SubvolumeIterator('foo', top=5)), subvols)
-
- self.assertEqual(list(btrfsutil.SubvolumeIterator('.', post_order=True)),
- [('foo/bar/baz', 258),
- ('foo/bar', 257),
- ('foo', 256)])
+ with self.subTest(type=type(arg)), btrfsutil.SubvolumeIterator(arg) as it:
+ self.assertEqual(list(it), subvols)
+ with btrfsutil.SubvolumeIterator('.', top=0) as it:
+ self.assertEqual(list(it), subvols)
+ with btrfsutil.SubvolumeIterator('foo', top=5) as it:
+ self.assertEqual(list(it), subvols)
+
+ with btrfsutil.SubvolumeIterator('.', post_order=True) as it:
+ self.assertEqual(list(it),
+ [('foo/bar/baz', 258),
+ ('foo/bar', 257),
+ ('foo', 256)])
subvols = [
('bar', 257),
('bar/baz', 258),
]
- self.assertEqual(list(btrfsutil.SubvolumeIterator('.', top=256)), subvols)
- self.assertEqual(list(btrfsutil.SubvolumeIterator('foo')), subvols)
- self.assertEqual(list(btrfsutil.SubvolumeIterator('foo', top=0)), subvols)
+ with btrfsutil.SubvolumeIterator('.', top=256) as it:
+ self.assertEqual(list(it), subvols)
+ with btrfsutil.SubvolumeIterator('foo') as it:
+ self.assertEqual(list(it), subvols)
+ with btrfsutil.SubvolumeIterator('foo', top=0) as it:
+ self.assertEqual(list(it), subvols)
os.rename('foo/bar/baz', 'baz')
- self.assertEqual(sorted(btrfsutil.SubvolumeIterator('.')),
- [('baz', 258),
- ('foo', 256),
- ('foo/bar', 257)])
+ with btrfsutil.SubvolumeIterator('.') as it:
+ self.assertEqual(sorted(it),
+ [('baz', 258),
+ ('foo', 256),
+ ('foo/bar', 257)])
with btrfsutil.SubvolumeIterator('.') as it:
self.assertGreaterEqual(it.fileno(), 0)