2014-12-19 15:29:33 +00:00
# -*- coding: utf-8 -*-
'''
: codeauthor : Nitin Madhok < nmadhok @clemson.edu > `
tests . unit . modules . zfs_test
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
'''
2015-01-08 02:33:12 +00:00
# Import Python libs
2018-01-24 20:47:14 +00:00
from __future__ import absolute_import , unicode_literals , print_function
2015-01-08 02:33:12 +00:00
2014-12-19 15:29:33 +00:00
# Import Salt Testing libs
2017-03-21 23:56:24 +00:00
from tests . support . mixins import LoaderModuleMockMixin
2017-02-27 13:58:07 +00:00
from tests . support . unit import skipIf , TestCase
from tests . support . mock import (
2014-12-19 15:29:33 +00:00
MagicMock ,
patch ,
NO_MOCK ,
NO_MOCK_REASON ,
)
2014-12-21 16:44:57 +00:00
# Import Salt Execution module to test
2017-03-21 17:15:36 +00:00
import salt . modules . zfs as zfs
2015-12-23 15:20:51 +00:00
from salt . utils . odict import OrderedDict
2014-12-21 16:44:57 +00:00
2014-12-19 15:29:33 +00:00
# Skip this test case if we don't have access to mock!
@skipIf ( NO_MOCK , NO_MOCK_REASON )
2017-03-21 23:56:24 +00:00
class ZfsTestCase ( TestCase , LoaderModuleMockMixin ) :
2014-12-19 15:29:33 +00:00
'''
This class contains a set of functions that test salt . modules . zfs module
'''
2017-03-22 12:12:36 +00:00
def setup_loader_modules ( self ) :
2017-04-10 13:00:57 +00:00
patcher = patch ( ' salt.modules.zfs._check_zfs ' , MagicMock ( return_value = ' /sbin/zfs ' ) )
patcher . start ( )
self . addCleanup ( patcher . stop )
2017-03-22 12:12:36 +00:00
return { zfs : { } }
2014-12-19 15:29:33 +00:00
def test_exists_success ( self ) :
'''
Tests successful return of exists function
'''
2015-12-23 12:47:41 +00:00
ret = { }
ret [ ' stdout ' ] = " NAME USED AVAIL REFER MOUNTPOINT \n myzpool/mydataset 30K 157G 30K /myzpool/mydataset "
ret [ ' stderr ' ] = ' '
ret [ ' retcode ' ] = 0
2014-12-19 15:29:33 +00:00
mock_cmd = MagicMock ( return_value = ret )
2015-12-23 12:47:41 +00:00
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
2014-12-19 16:45:16 +00:00
self . assertTrue ( zfs . exists ( ' myzpool/mydataset ' ) )
2014-12-19 15:29:33 +00:00
2014-12-19 15:36:28 +00:00
def test_exists_failure_not_exists ( self ) :
2014-12-19 15:29:33 +00:00
'''
2014-12-19 15:36:28 +00:00
Tests unsuccessful return of exists function if dataset does not exist
2014-12-19 15:29:33 +00:00
'''
2015-12-23 12:47:41 +00:00
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot open ' myzpool/mydataset ' : dataset does not exist "
ret [ ' retcode ' ] = 1
2014-12-19 15:29:33 +00:00
mock_cmd = MagicMock ( return_value = ret )
2015-12-23 12:47:41 +00:00
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
2014-12-19 16:45:16 +00:00
self . assertFalse ( zfs . exists ( ' myzpool/mydataset ' ) )
2014-12-19 15:29:33 +00:00
2014-12-19 15:36:28 +00:00
def test_exists_failure_invalid_name ( self ) :
'''
Tests unsuccessful return of exists function if dataset name is invalid
'''
2015-12-23 12:47:41 +00:00
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot open ' myzpool/ ' : invalid dataset name "
ret [ ' retcode ' ] = 1
2014-12-19 15:36:28 +00:00
mock_cmd = MagicMock ( return_value = ret )
2015-12-23 12:47:41 +00:00
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
2014-12-19 16:45:16 +00:00
self . assertFalse ( zfs . exists ( ' myzpool/ ' ) )
2014-12-19 15:36:28 +00:00
2014-12-19 16:04:19 +00:00
def test_create_success ( self ) :
'''
2014-12-20 17:51:19 +00:00
Tests successful return of create function on ZFS file system creation
2014-12-19 16:04:19 +00:00
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/mydataset ' : ' created ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " "
ret [ ' retcode ' ] = 0
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool/mydataset ' ) , res )
2014-12-19 16:04:19 +00:00
2014-12-20 18:04:53 +00:00
def test_create_success_with_create_parent ( self ) :
'''
Tests successful return of create function when ` ` create_parent = True ` `
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/mydataset/mysubdataset ' : ' created ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " "
ret [ ' retcode ' ] = 0
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool/mydataset/mysubdataset ' , create_parent = True ) , res )
2014-12-20 18:04:53 +00:00
2014-12-20 17:51:19 +00:00
def test_create_success_with_properties ( self ) :
'''
Tests successful return of create function on ZFS file system creation ( with properties )
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/mydataset ' : ' created ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " "
ret [ ' retcode ' ] = 0
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
2014-12-20 18:02:21 +00:00
self . assertEqual (
zfs . create (
' myzpool/mydataset ' ,
properties = {
' mountpoint ' : ' /export/zfs ' ,
' sharenfs ' : ' on '
}
2015-12-23 12:47:41 +00:00
) , res
2014-12-20 18:02:21 +00:00
)
2014-12-20 17:51:19 +00:00
2014-12-19 18:46:33 +00:00
def test_create_error_missing_dataset ( self ) :
'''
Tests unsuccessful return of create function if dataset name is missing
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool ' : ' cannot create \' myzpool \' : missing dataset name ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot create ' myzpool ' : missing dataset name "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool ' ) , res )
2014-12-19 18:46:33 +00:00
2014-12-19 18:58:53 +00:00
def test_create_error_trailing_slash ( self ) :
'''
Tests unsuccessful return of create function if trailing slash in name is present
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/ ' : ' cannot create \' myzpool/ \' : trailing slash in name ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot create ' myzpool/ ' : trailing slash in name "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool/ ' ) , res )
2014-12-19 18:58:53 +00:00
2014-12-19 19:07:40 +00:00
def test_create_error_no_such_pool ( self ) :
'''
Tests unsuccessful return of create function if the pool is not present
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/mydataset ' : ' cannot create \' myzpool/mydataset \' : no such pool \' myzpool \' ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot create ' myzpool/mydataset ' : no such pool ' myzpool ' "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool/mydataset ' ) , res )
2014-12-19 19:07:40 +00:00
2014-12-19 19:12:12 +00:00
def test_create_error_missing_parent ( self ) :
'''
Tests unsuccessful return of create function if the parent datasets do not exist
'''
2015-12-23 12:47:41 +00:00
res = { ' myzpool/mydataset/mysubdataset ' : ' cannot create \' myzpool/mydataset/mysubdataset \' : parent does not exist ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot create ' myzpool/mydataset/mysubdataset ' : parent does not exist "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . create ( ' myzpool/mydataset/mysubdataset ' ) , res )
2014-12-19 19:12:12 +00:00
2015-12-23 15:20:51 +00:00
def test_list_success ( self ) :
'''
Tests zfs list
'''
2017-12-02 12:37:51 +00:00
res = OrderedDict ( [ ( ' myzpool ' , { ' avail ' : ' 954G ' , ' mountpoint ' : ' /myzpool ' , ' used ' : ' 844G ' , ' refer ' : ' 96K ' } ) ] )
ret = { ' pid ' : 31817 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' myzpool \t 844G \t 954G \t 96K \t /myzpool ' }
2015-12-23 15:20:51 +00:00
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . list_ ( ' myzpool ' ) , res )
2017-12-02 12:37:51 +00:00
def test_list_parsable_success ( self ) :
'''
Tests zfs list with parsable output
'''
res = OrderedDict ( [ ( ' myzpool ' , { ' avail ' : 1024795238400 , ' mountpoint ' : ' /myzpool ' , ' used ' : 905792561152 , ' refer ' : 98304 } ) ] )
ret = { ' pid ' : 31817 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' myzpool \t 905792561152 \t 1024795238400 \t 98304 \t /myzpool ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . list_ ( ' myzpool ' , parsable = True ) , res )
2015-12-23 15:14:45 +00:00
def test_mount_success ( self ) :
'''
Tests zfs mount of filesystem
'''
res = { ' myzpool/mydataset ' : ' mounted ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " "
ret [ ' retcode ' ] = 0
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . mount ( ' myzpool/mydataset ' ) , res )
def test_mount_failure ( self ) :
'''
Tests zfs mount of already mounted filesystem
'''
res = { ' myzpool/mydataset ' : " cannot mount ' myzpool/mydataset ' : filesystem already mounted " }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot mount ' myzpool/mydataset ' : filesystem already mounted "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . mount ( ' myzpool/mydataset ' ) , res )
2015-12-23 15:36:33 +00:00
def test_unmount_success ( self ) :
'''
Tests zfs unmount of filesystem
'''
res = { ' myzpool/mydataset ' : ' unmounted ' }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " "
ret [ ' retcode ' ] = 0
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . unmount ( ' myzpool/mydataset ' ) , res )
def test_unmount_failure ( self ) :
'''
Tests zfs unmount of already mounted filesystem
'''
res = { ' myzpool/mydataset ' : " cannot mount ' myzpool/mydataset ' : not currently mounted " }
ret = { }
ret [ ' stdout ' ] = " "
ret [ ' stderr ' ] = " cannot mount ' myzpool/mydataset ' : not currently mounted "
ret [ ' retcode ' ] = 1
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . unmount ( ' myzpool/mydataset ' ) , res )
2015-12-23 15:59:50 +00:00
def test_inherit_success ( self ) :
'''
Tests zfs inherit of compression property
'''
res = { ' myzpool/mydataset ' : { ' compression ' : ' cleared ' } }
ret = { ' pid ' : 45193 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . inherit ( ' compression ' , ' myzpool/mydataset ' ) , res )
def test_inherit_failure ( self ) :
'''
Tests zfs inherit of canmount
'''
res = {
' myzpool/mydataset ' : {
' canmount ' : " ' canmount ' property cannot be inherited, use revert=True to try and reset it to it ' s default value. "
}
}
ret = { ' pid ' : 43898 , ' retcode ' : 1 , ' stderr ' : " ' canmount ' property cannot be inherited " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . inherit ( ' canmount ' , ' myzpool/mydataset ' ) , res )
2015-12-23 16:20:32 +00:00
def test_diff ( self ) :
'''
Tests zfs diff
'''
res = [ ' M \t / \t /myzpool/mydataset/ ' , ' + \t F \t /myzpool/mydataset/hello ' ]
ret = { ' pid ' : 51495 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' M \t / \t /myzpool/mydataset/ \n + \t F \t /myzpool/mydataset/hello ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . diff ( ' myzpool/mydataset@yesterday ' , ' myzpool/mydataset ' ) , res )
2015-12-23 16:48:24 +00:00
def test_rollback_success ( self ) :
'''
Tests zfs rollback success
'''
res = { ' myzpool/mydataset ' : ' rolledback to snapshot: yesterday ' }
ret = { ' pid ' : 56502 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . rollback ( ' myzpool/mydataset@yesterday ' ) , res )
def test_rollback_failure ( self ) :
'''
Tests zfs rollback failure
'''
2017-04-10 13:00:57 +00:00
res = { ' myzpool/mydataset ' : " cannot rollback to ' myzpool/mydataset@yesterday ' : more recent snapshots "
" or bookmarks exist \n use ' -r ' to force deletion of the following snapshots "
" and bookmarks: \n myzpool/mydataset@today " }
2015-12-23 16:48:24 +00:00
ret = {
' pid ' : 57471 ,
' retcode ' : 1 ,
2017-04-10 13:00:57 +00:00
' stderr ' : " cannot rollback to ' myzpool/mydataset@yesterday ' : more recent snapshots or bookmarks "
" exist \n use ' -r ' to force deletion of the following snapshots and "
" bookmarks: \n myzpool/mydataset@today " ,
2015-12-23 16:48:24 +00:00
' stdout ' : ' '
}
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . rollback ( ' myzpool/mydataset@yesterday ' ) , res )
2015-12-23 17:06:22 +00:00
def test_clone_success ( self ) :
'''
Tests zfs clone success
'''
res = { ' myzpool/yesterday ' : ' cloned from myzpool/mydataset@yesterday ' }
ret = { ' pid ' : 64532 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . clone ( ' myzpool/mydataset@yesterday ' , ' myzpool/yesterday ' ) , res )
def test_clone_failure ( self ) :
'''
Tests zfs clone failure
'''
res = { ' myzpool/archive/yesterday ' : " cannot create ' myzpool/archive/yesterday ' : parent does not exist " }
ret = { ' pid ' : 64864 , ' retcode ' : 1 , ' stderr ' : " cannot create ' myzpool/archive/yesterday ' : parent does not exist " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . clone ( ' myzpool/mydataset@yesterday ' , ' myzpool/archive/yesterday ' ) , res )
2015-12-23 17:19:34 +00:00
def test_promote_success ( self ) :
'''
Tests zfs promote success
'''
res = { ' myzpool/yesterday ' : ' promoted ' }
ret = { ' pid ' : 69075 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . promote ( ' myzpool/yesterday ' ) , res )
def test_promote_failure ( self ) :
'''
Tests zfs promote failure
'''
res = { ' myzpool/yesterday ' : " cannot promote ' myzpool/yesterday ' : not a cloned filesystem " }
ret = { ' pid ' : 69209 , ' retcode ' : 1 , ' stderr ' : " cannot promote ' myzpool/yesterday ' : not a cloned filesystem " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . promote ( ' myzpool/yesterday ' ) , res )
2015-12-23 20:12:07 +00:00
def test_bookmark_success ( self ) :
'''
Tests zfs bookmark success
'''
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
with patch ( ' salt.utils.path.which ' , MagicMock ( return_value = ' /usr/bin/man ' ) ) :
2017-04-10 13:00:57 +00:00
res = { ' myzpool/mydataset@yesterday ' : ' bookmarked as myzpool/mydataset#important ' }
ret = { ' pid ' : 20990 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . bookmark ( ' myzpool/mydataset@yesterday ' , ' myzpool/mydataset#important ' ) , res )
2015-12-23 20:12:07 +00:00
2015-12-23 21:16:45 +00:00
def test_holds_success ( self ) :
'''
Tests zfs holds success
'''
res = { ' myzpool/mydataset@baseline ' : { ' important ' : ' Wed Dec 23 21:06 2015 ' , ' release-1.0 ' : ' Wed Dec 23 21:08 2015 ' } }
ret = { ' pid ' : 40216 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' myzpool/mydataset@baseline \t important \t Wed Dec 23 21:06 2015 \n myzpool/mydataset@baseline \t release-1.0 \t Wed Dec 23 21:08 2015 ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . holds ( ' myzpool/mydataset@baseline ' ) , res )
def test_holds_failure ( self ) :
'''
Tests zfs holds failure
'''
res = { ' myzpool/mydataset@baseline ' : " cannot open ' myzpool/mydataset@baseline ' : dataset does not exist " }
ret = { ' pid ' : 40993 , ' retcode ' : 1 , ' stderr ' : " cannot open ' myzpool/mydataset@baseline ' : dataset does not exist " , ' stdout ' : ' no datasets available ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . holds ( ' myzpool/mydataset@baseline ' ) , res )
2015-12-23 21:50:01 +00:00
def test_hold_success ( self ) :
'''
Tests zfs hold success
'''
2015-12-26 10:28:25 +00:00
res = { ' myzpool/mydataset@baseline ' : { ' important ' : ' held ' } , ' myzpool/mydataset@release-1.0 ' : { ' important ' : ' held ' } }
2015-12-23 21:50:01 +00:00
ret = { ' pid ' : 50876 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . hold ( ' important ' , ' myzpool/mydataset@baseline ' , ' myzpool/mydataset@release-1.0 ' ) , res )
def test_hold_failure ( self ) :
'''
Tests zfs hold failure
'''
2015-12-26 10:28:25 +00:00
res = { ' myzpool/mydataset@baseline ' : { ' important ' : ' tag already exists on this dataset ' } }
2015-12-23 21:50:01 +00:00
ret = { ' pid ' : 51006 , ' retcode ' : 1 , ' stderr ' : " cannot hold snapshot ' myzpool/mydataset@baseline ' : tag already exists on this dataset " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . hold ( ' important ' , ' myzpool/mydataset@baseline ' ) , res )
2015-12-23 22:06:44 +00:00
def test_release_success ( self ) :
'''
Tests zfs release success
'''
2015-12-26 10:28:25 +00:00
res = { ' myzpool/mydataset@baseline ' : { ' important ' : ' released ' } , ' myzpool/mydataset@release-1.0 ' : { ' important ' : ' released ' } }
2015-12-23 22:06:44 +00:00
ret = { ' pid ' : 50876 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . release ( ' important ' , ' myzpool/mydataset@baseline ' , ' myzpool/mydataset@release-1.0 ' ) , res )
def test_release_failure ( self ) :
'''
Tests zfs release failure
'''
2015-12-26 10:28:25 +00:00
res = { ' myzpool/mydataset@baseline ' : { ' important ' : ' no such tag on this dataset ' } }
2015-12-23 22:06:44 +00:00
ret = { ' pid ' : 51006 , ' retcode ' : 1 , ' stderr ' : " cannot release hold from snapshot ' myzpool/mydataset@baseline ' : no such tag on this dataset " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . release ( ' important ' , ' myzpool/mydataset@baseline ' ) , res )
2015-12-23 22:44:19 +00:00
def test_snapshot_success ( self ) :
'''
Tests zfs snapshot success
'''
res = { ' myzpool/mydataset@baseline ' : ' snapshotted ' }
ret = { ' pid ' : 69125 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . snapshot ( ' myzpool/mydataset@baseline ' ) , res )
def test_snapshot_failure ( self ) :
'''
Tests zfs snapshot failure
'''
res = { ' myzpool/mydataset@baseline ' : ' dataset already exists ' }
ret = { ' pid ' : 68526 , ' retcode ' : 1 , ' stderr ' : " cannot create snapshot ' myzpool/mydataset@baseline ' : dataset already exists " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . snapshot ( ' myzpool/mydataset@baseline ' ) , res )
def test_snapshot_failure2 ( self ) :
'''
Tests zfs snapshot failure
'''
res = { ' myzpool/mydataset@baseline ' : ' dataset does not exist ' }
ret = { ' pid ' : 69256 , ' retcode ' : 2 , ' stderr ' : " cannot open ' myzpool/mydataset ' : dataset does not exist \n usage: \n \t snapshot [-r] [-o property=value] ... <filesystem|volume>@<snap> ... \n \n For the property list, run: zfs set|get \n \n For the delegated permission list, run: zfs allow|unallow " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . snapshot ( ' myzpool/mydataset@baseline ' ) , res )
2015-12-23 23:22:09 +00:00
def test_set_success ( self ) :
'''
Tests zfs set success
'''
res = { ' myzpool/mydataset ' : { ' compression ' : ' set ' } }
ret = { ' pid ' : 79736 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . set ( ' myzpool/mydataset ' , compression = ' lz4 ' ) , res )
def test_set_failure ( self ) :
'''
Tests zfs set failure
'''
res = { ' myzpool/mydataset ' : { ' canmount ' : " ' canmount ' must be one of ' on | off | noauto ' " } }
ret = { ' pid ' : 79887 , ' retcode ' : 1 , ' stderr ' : " cannot set property for ' myzpool/mydataset ' : ' canmount ' must be one of ' on | off | noauto ' " , ' stdout ' : ' ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . set ( ' myzpool/mydataset ' , canmount = ' lz4 ' ) , res )
2015-12-24 00:07:36 +00:00
def test_get_success ( self ) :
'''
Tests zfs get success
'''
2017-12-02 12:37:51 +00:00
res = OrderedDict ( [ ( ' myzpool ' , { ' used ' : { ' value ' : ' 844G ' } } ) ] )
ret = { ' pid ' : 562 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' myzpool \t used \t 844G ' }
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
self . assertEqual ( zfs . get ( ' myzpool ' , properties = ' used ' , fields = ' value ' ) , res )
def test_get_parsable_success ( self ) :
'''
Tests zfs get with parsable output
'''
res = OrderedDict ( [ ( ' myzpool ' , { ' used ' : { ' value ' : 905792561152 } } ) ] )
ret = { ' pid ' : 562 , ' retcode ' : 0 , ' stderr ' : ' ' , ' stdout ' : ' myzpool \t used \t 905792561152 ' }
2015-12-24 00:07:36 +00:00
mock_cmd = MagicMock ( return_value = ret )
with patch . dict ( zfs . __salt__ , { ' cmd.run_all ' : mock_cmd } ) :
2017-12-02 12:37:51 +00:00
self . assertEqual ( zfs . get ( ' myzpool ' , properties = ' used ' , fields = ' value ' , parsable = True ) , res )