Skip to content

Commit

Permalink
tests: Add a simple unit test for listing btrfs subvolumes
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechtrefny committed Mar 27, 2024
1 parent 2698bad commit edb2319
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tests/unit_tests/devices_test/btrfs_test.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import unittest

try:
from unittest.mock import patch
from unittest.mock import patch, PropertyMock
except ImportError:
from mock import patch
from mock import patch, PropertyMock

import blivet

from blivet.devices import StorageDevice
from blivet.devices import BTRFSVolumeDevice
from blivet.devices import BTRFSSubVolumeDevice
from blivet.size import Size
from blivet.formats.fs import BTRFS


DEVICE_CLASSES = [
Expand Down Expand Up @@ -65,3 +66,28 @@ def test_device_id(self):

sub = BTRFSSubVolumeDevice("testsub", parents=[vol])
self.assertEqual(sub.device_id, "BTRFS-" + vol.uuid + "-testsub")

def test_btrfs_list_subvolumes(self):
bd = StorageDevice("bd1", fmt=blivet.formats.get_format("btrfs"),
size=Size("2 GiB"), exists=True)

vol = BTRFSVolumeDevice("testvolume", parents=[bd])

with patch("blivet.devices.btrfs.blockdev.btrfs") as blockdev:
# not mounted and flags.auto_dev_updates is not set
vol.list_subvolumes()
blockdev.list_subvolumes.assert_not_called()
blockdev.get_default_subvolume_id.assert_not_called()

# mounted
with patch.object(BTRFS, "system_mountpoint", new=PropertyMock(return_value='/fake/mountpoint')):
vol.list_subvolumes()
blockdev.list_subvolumes.assert_called_with("/fake/mountpoint", snapshots_only=False)
blockdev.get_default_subvolume_id.assert_called_with("/fake/mountpoint")

# mounted but libblockdev btrfs plugin not available
blockdev.reset_mock()
with patch("blivet.devices.btrfs.missing_plugs", new={"btrfs"}):
vol.list_subvolumes()
blockdev.list_subvolumes.assert_not_called()
blockdev.get_default_subvolume_id.assert_not_called()

0 comments on commit edb2319

Please sign in to comment.