From 5de7ce4c09cf37ad4b20e03e8a77792a0a27e929 Mon Sep 17 00:00:00 2001 From: Alberto Planas Date: Tue, 20 Nov 2018 11:48:42 +0100 Subject: [PATCH] btrfs: add subvolume_exists --- salt/modules/btrfs.py | 18 ++++++++++++++++++ tests/unit/modules/test_btrfs.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/salt/modules/btrfs.py b/salt/modules/btrfs.py index 576eabd385e8..eb6e022e8999 100644 --- a/salt/modules/btrfs.py +++ b/salt/modules/btrfs.py @@ -675,6 +675,24 @@ def properties(obj, type=None, set=None): return ret +def subvolume_exists(path): + ''' + Check if a subvolume is present in the filesystem. + + path + Mount point for the subvolume (full path) + + CLI Example: + + .. code-block:: bash + + salt '*' btrfs.subvolume_exists /mnt/var + + ''' + cmd = ['btrfs', 'subvolume', 'show', path] + return __salt__['cmd.retcode'](cmd, ignore_retcode=True) == 0 + + def subvolume_create(name, dest=None, qgroupids=None): ''' Create subvolume `name` in `dest`. diff --git a/tests/unit/modules/test_btrfs.py b/tests/unit/modules/test_btrfs.py index ef31e68554a3..a56c3d8a198b 100644 --- a/tests/unit/modules/test_btrfs.py +++ b/tests/unit/modules/test_btrfs.py @@ -365,6 +365,26 @@ def test_properties_error(self): self.assertRaises(CommandExecutionError, btrfs.properties, '/dev/sda1', 'subvol', True) + def test_subvolume_exists(self): + ''' + Test subvolume_exists + ''' + salt_mock = { + 'cmd.retcode': MagicMock(return_value=0), + } + with patch.dict(btrfs.__salt__, salt_mock): + assert btrfs.subvolume_exists('/mnt/one') + + def test_subvolume_not_exists(self): + ''' + Test subvolume_exists + ''' + salt_mock = { + 'cmd.retcode': MagicMock(return_value=1), + } + with patch.dict(btrfs.__salt__, salt_mock): + assert not btrfs.subvolume_exists('/mnt/nowhere') + def test_subvolume_create_fails_parameters(self): ''' Test btrfs subvolume create