Skip to content

Commit

Permalink
btrfs: add subvolume_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
aplanas committed Nov 26, 2018
1 parent c476a21 commit 5de7ce4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions salt/modules/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/modules/test_btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5de7ce4

Please sign in to comment.