Skip to content

Commit

Permalink
btrfs: add option to not set subvolumes as default
Browse files Browse the repository at this point in the history
When we want to create a subvolume and set is as default, we can
use the set_default parameter. The current code will check, for
already created subvolumes, if it is actually marked as default,
and if not it will be set inside the state.

For composability with other states, we sometimes want to avoid
the re-set of the default flag for already present subvolumes, as
can be that this was change in a later action by another state.
  • Loading branch information
aplanas committed Dec 10, 2018
1 parent c4110b4 commit 067482e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
12 changes: 10 additions & 2 deletions salt/states/btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def wrapper(*args, **kwargs):

@__mount_device
def subvolume_created(name, device, qgroupids=None, set_default=False,
copy_on_write=True, __dest=None):
copy_on_write=True, force_set_default=True,
__dest=None):
'''
Makes sure that a btrfs subvolume is present.
Expand All @@ -150,6 +151,10 @@ def subvolume_created(name, device, qgroupids=None, set_default=False,
copy_on_write
If false, set the subvolume with chattr +C
force_set_default
If false and the subvolume is already present, it will not
force it as default if ``set_default`` is True
'''
ret = {
'name': name,
Expand Down Expand Up @@ -190,7 +195,10 @@ def subvolume_created(name, device, qgroupids=None, set_default=False,

ret['changes'][name] = 'Created subvolume {}'.format(name)

if set_default and not _is_default(path, __dest, name):
# If the volume was already present, we can opt-out the check for
# default subvolume.
if (not exists or (exists and force_set_default)) and \
set_default and not _is_default(path, __dest, name):
ret['changes'][name + '_default'] = _set_default(path, __dest, name)

if not copy_on_write and _is_cow(path):
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/states/test_btrfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,42 @@ def test_subvolume_created_exists_set_default(self, mount, umount,
mount.assert_called_once()
umount.assert_called_once()

@patch('salt.states.btrfs._set_default')
@patch('salt.states.btrfs._is_default')
@patch('salt.states.btrfs._umount')
@patch('salt.states.btrfs._mount')
def test_subvolume_created_exists_set_default_no_force(self,
mount,
umount,
is_default,
set_default):
'''
Test creating a subvolume.
'''
mount.return_value = '/tmp/xxx'
is_default.return_value = False
set_default.return_value = True
salt_mock = {
'btrfs.subvolume_exists': MagicMock(return_value=True),
}
opts_mock = {
'test': False,
}
with patch.dict(btrfs.__salt__, salt_mock), \
patch.dict(btrfs.__opts__, opts_mock):
assert btrfs.subvolume_created(name='@/var',
device='/dev/sda1',
set_default=True,
force_set_default=False) == {
'name': '@/var',
'result': True,
'changes': {},
'comment': ['Subvolume @/var already present'],
}
salt_mock['btrfs.subvolume_exists'].assert_called_with('/tmp/xxx/@/var')
mount.assert_called_once()
umount.assert_called_once()

@patch('salt.states.btrfs._is_cow')
@patch('salt.states.btrfs._umount')
@patch('salt.states.btrfs._mount')
Expand Down

0 comments on commit 067482e

Please sign in to comment.