123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941 |
- # -*- coding: utf-8 -*-
- """
- Tests for salt.states.zfs
- :codeauthor: Jorge Schrauwen <sjorge@blackdot.be>
- :maintainer: Jorge Schrauwen <sjorge@blackdot.be>
- :maturity: new
- :depends: salt.utils.zfs, salt.modules.zfs
- :platform: illumos,freebsd,linux
- """
- # Import Python libs
- from __future__ import absolute_import, print_function, unicode_literals
- import pytest
- # Import Salt Utils
- import salt.loader
- import salt.states.zfs as zfs
- # Import Salt Execution module to test
- import salt.utils.zfs
- from salt.utils.odict import OrderedDict
- from tests.support.mixins import LoaderModuleMockMixin
- from tests.support.mock import MagicMock, patch
- from tests.support.unit import TestCase
- # Import Salt Testing Libs
- from tests.support.zfs import ZFSMockData
- class ZfsTestCase(TestCase, LoaderModuleMockMixin):
- """
- Test cases for salt.states.zfs
- """
- def setup_loader_modules(self):
- self.opts = opts = salt.config.DEFAULT_MINION_OPTS.copy()
- self.utils_patch = ZFSMockData().get_patched_utils()
- for key in ("opts", "utils_patch"):
- self.addCleanup(delattr, self, key)
- utils = salt.loader.utils(opts, whitelist=["zfs"])
- zfs_obj = {
- zfs: {
- "__opts__": opts,
- "__grains__": {"kernel": "SunOS"},
- "__utils__": utils,
- }
- }
- return zfs_obj
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_filesystem_absent_nofs(self):
- """
- Test if filesystem is absent (non existing filesystem)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "filesystem myzpool/filesystem is absent",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.filesystem_absent("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_filesystem_absent_removed(self):
- """
- Test if filesystem is absent
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "filesystem myzpool/filesystem was destroyed",
- "changes": {"myzpool/filesystem": "destroyed"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("destroyed", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.filesystem_absent("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_filesystem_absent_fail(self):
- """
- Test if filesystem is absent (with snapshots)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": False,
- "comment": "\n".join(
- [
- "cannot destroy 'myzpool/filesystem': filesystem has children",
- "use 'recursive=True' to destroy the following datasets:",
- "myzpool/filesystem@snap",
- ]
- ),
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(
- return_value=OrderedDict(
- [
- ("destroyed", False),
- (
- "error",
- "\n".join(
- [
- "cannot destroy 'myzpool/filesystem': filesystem has children",
- "use 'recursive=True' to destroy the following datasets:",
- "myzpool/filesystem@snap",
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.filesystem_absent("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_absent_novol(self):
- """
- Test if volume is absent (non existing volume)
- """
- ret = {
- "name": "myzpool/volume",
- "result": True,
- "comment": "volume myzpool/volume is absent",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.volume_absent("myzpool/volume"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_absent_removed(self):
- """
- Test if volume is absent
- """
- ret = {
- "name": "myzpool/volume",
- "result": True,
- "comment": "volume myzpool/volume was destroyed",
- "changes": {"myzpool/volume": "destroyed"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("destroyed", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.volume_absent("myzpool/volume"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_absent_fail(self):
- """
- Test if volume is absent (with snapshots)
- """
- ret = {
- "name": "myzpool/volume",
- "result": False,
- "comment": "\n".join(
- [
- "cannot destroy 'myzpool/volume': volume has children",
- "use 'recursive=True' to destroy the following datasets:",
- "myzpool/volume@snap",
- ]
- ),
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(
- return_value=OrderedDict(
- [
- ("destroyed", False),
- (
- "error",
- "\n".join(
- [
- "cannot destroy 'myzpool/volume': volume has children",
- "use 'recursive=True' to destroy the following datasets:",
- "myzpool/volume@snap",
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.volume_absent("myzpool/volume"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_snapshot_absent_nosnap(self):
- """
- Test if snapshot is absent (non existing snapshot)
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": True,
- "comment": "snapshot myzpool/filesystem@snap is absent",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.snapshot_absent("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_snapshot_absent_removed(self):
- """
- Test if snapshot is absent
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": True,
- "comment": "snapshot myzpool/filesystem@snap was destroyed",
- "changes": {"myzpool/filesystem@snap": "destroyed"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("destroyed", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.snapshot_absent("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_snapshot_absent_fail(self):
- """
- Test if snapshot is absent (with snapshots)
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": False,
- "comment": "cannot destroy snapshot myzpool/filesystem@snap: dataset is busy",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(
- return_value=OrderedDict(
- [
- ("destroyed", False),
- (
- "error",
- "cannot destroy snapshot myzpool/filesystem@snap: dataset is busy",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.snapshot_absent("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_bookmark_absent_nobook(self):
- """
- Test if bookmark is absent (non existing bookmark)
- """
- ret = {
- "name": "myzpool/filesystem#book",
- "result": True,
- "comment": "bookmark myzpool/filesystem#book is absent",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.bookmark_absent("myzpool/filesystem#book"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_bookmark_absent_removed(self):
- """
- Test if bookmark is absent
- """
- ret = {
- "name": "myzpool/filesystem#book",
- "result": True,
- "comment": "bookmark myzpool/filesystem#book was destroyed",
- "changes": {"myzpool/filesystem#book": "destroyed"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("destroyed", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.destroy": mock_destroy}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.bookmark_absent("myzpool/filesystem#book"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_hold_absent_nohold(self):
- """
- Test if hold is absent (non existing hold)
- """
- ret = {
- "name": "myhold",
- "result": True,
- "comment": "hold myhold is absent",
- "changes": {},
- }
- mock_holds = MagicMock(return_value=OrderedDict([]))
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.hold_absent("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_hold_absent_removed(self):
- """
- Test if hold is absent
- """
- ret = {
- "name": "myhold",
- "result": True,
- "comment": "hold myhold released",
- "changes": OrderedDict(
- [("myzpool/filesystem@snap", OrderedDict([("myhold", "released")]))]
- ),
- }
- mock_holds = MagicMock(
- return_value=OrderedDict([("myhold", "Thu Feb 15 16:24 2018")])
- )
- mock_release = MagicMock(return_value=OrderedDict([("released", True)]))
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__salt__, {"zfs.release": mock_release}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.hold_absent("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_hold_absent_fail(self):
- """
- Test if hold is absent (non existing snapshot)
- """
- ret = {
- "name": "myhold",
- "result": False,
- "comment": "cannot open 'myzpool/filesystem@snap': dataset does not exist",
- "changes": {},
- }
- mock_holds = MagicMock(
- return_value=OrderedDict(
- [
- (
- "error",
- "cannot open 'myzpool/filesystem@snap': dataset does not exist",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.hold_absent("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_hold_present(self):
- """
- Test if hold is present (hold already present)
- """
- ret = {
- "name": "myhold",
- "result": True,
- "comment": "hold myhold is present for myzpool/filesystem@snap",
- "changes": {},
- }
- mock_holds = MagicMock(
- return_value=OrderedDict([("myhold", "Thu Feb 15 16:24 2018")])
- )
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.hold_present("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_hold_present_new(self):
- """
- Test if hold is present (new)
- """
- ret = {
- "name": "myhold",
- "result": True,
- "comment": "hold myhold added to myzpool/filesystem@snap",
- "changes": {"myzpool/filesystem@snap": {"myhold": "held"}},
- }
- mock_holds = MagicMock(return_value=OrderedDict([]))
- mock_hold = MagicMock(return_value=OrderedDict([("held", True)]))
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__salt__, {"zfs.hold": mock_hold}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.hold_present("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_hold_present_fail(self):
- """
- Test if hold is present (using non existing snapshot)
- """
- ret = {
- "name": "myhold",
- "result": False,
- "comment": "cannot hold snapshot 'zsalt/filesystem@snap': dataset does not exist",
- "changes": {},
- }
- mock_holds = MagicMock(return_value=OrderedDict([]))
- mock_hold = MagicMock(
- return_value=OrderedDict(
- [
- ("held", False),
- (
- "error",
- "cannot hold snapshot 'zsalt/filesystem@snap': dataset does not exist",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.holds": mock_holds}), patch.dict(
- zfs.__salt__, {"zfs.hold": mock_hold}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.hold_present("myhold", "myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_filesystem_present(self):
- """
- Test if filesystem is present (existing filesystem)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "filesystem myzpool/filesystem is uptodate",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/filesystem",
- OrderedDict(
- [
- ("type", OrderedDict([("value", "filesystem")])),
- ("compression", OrderedDict([("value", False)])),
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.filesystem_present("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_filesystem_present_new(self):
- """
- Test if filesystem is present (non existing filesystem)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "filesystem myzpool/filesystem was created",
- "changes": {"myzpool/filesystem": "created"},
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(return_value=OrderedDict([("created", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.create": mock_create}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.filesystem_present("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_filesystem_present_update(self):
- """
- Test if filesystem is present (non existing filesystem)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "filesystem myzpool/filesystem was updated",
- "changes": {"myzpool/filesystem": {"compression": "lz4"}},
- }
- mock_exists = MagicMock(return_value=True)
- mock_set = MagicMock(return_value=OrderedDict([("set", True)]))
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/filesystem",
- OrderedDict(
- [
- ("type", OrderedDict([("value", "filesystem")])),
- ("compression", OrderedDict([("value", False)])),
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__salt__, {"zfs.set": mock_set}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(
- ret,
- zfs.filesystem_present(
- name="myzpool/filesystem", properties={"compression": "lz4"},
- ),
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_filesystem_present_fail(self):
- """
- Test if filesystem is present (non existing pool)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": False,
- "comment": "cannot create 'myzpool/filesystem': no such pool 'myzpool'",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(
- return_value=OrderedDict(
- [
- ("created", False),
- (
- "error",
- "cannot create 'myzpool/filesystem': no such pool 'myzpool'",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.create": mock_create}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.filesystem_present("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_volume_present(self):
- """
- Test if volume is present (existing volume)
- """
- ret = {
- "name": "myzpool/volume",
- "result": True,
- "comment": "volume myzpool/volume is uptodate",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/volume",
- OrderedDict(
- [
- ("type", OrderedDict([("value", "volume")])),
- ("compression", OrderedDict([("value", False)])),
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(
- ret, zfs.volume_present("myzpool/volume", volume_size="1G")
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_present_new(self):
- """
- Test if volume is present (non existing volume)
- """
- ret = {
- "name": "myzpool/volume",
- "result": True,
- "comment": "volume myzpool/volume was created",
- "changes": {"myzpool/volume": "created"},
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(return_value=OrderedDict([("created", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.create": mock_create}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(
- ret, zfs.volume_present("myzpool/volume", volume_size="1G")
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_present_update(self):
- """
- Test if volume is present (non existing volume)
- """
- ret = {
- "name": "myzpool/volume",
- "result": True,
- "comment": "volume myzpool/volume was updated",
- "changes": {"myzpool/volume": {"compression": "lz4"}},
- }
- mock_exists = MagicMock(return_value=True)
- mock_set = MagicMock(return_value=OrderedDict([("set", True)]))
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/volume",
- OrderedDict(
- [
- ("type", OrderedDict([("value", "volume")])),
- ("compression", OrderedDict([("value", False)])),
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__salt__, {"zfs.set": mock_set}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(
- ret,
- zfs.volume_present(
- name="myzpool/volume",
- volume_size="1G",
- properties={"compression": "lz4"},
- ),
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_volume_present_fail(self):
- """
- Test if volume is present (non existing pool)
- """
- ret = {
- "name": "myzpool/volume",
- "result": False,
- "comment": "cannot create 'myzpool/volume': no such pool 'myzpool'",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(
- return_value=OrderedDict(
- [
- ("created", False),
- ("error", "cannot create 'myzpool/volume': no such pool 'myzpool'"),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.create": mock_create}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(
- ret, zfs.volume_present("myzpool/volume", volume_size="1G")
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_bookmark_present(self):
- """
- Test if bookmark is present (bookmark already present)
- """
- ret = {
- "name": "myzpool/filesystem#mybookmark",
- "result": True,
- "comment": "bookmark is present",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(
- ret, zfs.bookmark_present("mybookmark", "myzpool/filesystem@snap")
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_bookmark_present_new(self):
- """
- Test if bookmark is present (new)
- """
- ret = {
- "name": "myzpool/filesystem#mybookmark",
- "result": True,
- "comment": "myzpool/filesystem@snap bookmarked as myzpool/filesystem#mybookmark",
- "changes": {"myzpool/filesystem#mybookmark": "myzpool/filesystem@snap"},
- }
- mock_exists = MagicMock(return_value=False)
- mock_bookmark = MagicMock(return_value=OrderedDict([("bookmarked", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.bookmark": mock_bookmark}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(
- ret, zfs.bookmark_present("mybookmark", "myzpool/filesystem@snap")
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_bookmark_present_fail(self):
- """
- Test if bookmark is present (using non existing snapshot)
- """
- ret = {
- "name": "myzpool/filesystem#mybookmark",
- "result": False,
- "comment": "cannot bookmark snapshot 'zsalt/filesystem@snap': dataset does not exist",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- mock_bookmark = MagicMock(
- return_value=OrderedDict(
- [
- ("bookmarked", False),
- (
- "error",
- "cannot bookmark snapshot 'zsalt/filesystem@snap': dataset does not exist",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.bookmark": mock_bookmark}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(
- ret, zfs.bookmark_present("mybookmark", "myzpool/filesystem@snap")
- )
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_snapshot_present(self):
- """
- Test if snapshot is present (snapshot already present)
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": True,
- "comment": "snapshot is present",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.snapshot_present("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_snapshot_present_new(self):
- """
- Test if snapshot is present (new)
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": True,
- "comment": "snapshot myzpool/filesystem@snap was created",
- "changes": {"myzpool/filesystem@snap": "snapshotted"},
- }
- mock_exists = MagicMock(return_value=False)
- mock_snapshot = MagicMock(return_value=OrderedDict([("snapshotted", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.snapshot": mock_snapshot}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.snapshot_present("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_snapshot_present_fail(self):
- """
- Test if snapshot is present (using non existing snapshot)
- """
- ret = {
- "name": "myzpool/filesystem@snap",
- "result": False,
- "comment": "cannot open 'myzpool/filesystem': dataset does not exist",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- mock_snapshot = MagicMock(
- return_value=OrderedDict(
- [
- ("snapshotted", False),
- (
- "error",
- "cannot open 'myzpool/filesystem': dataset does not exist",
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.snapshot": mock_snapshot}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.snapshot_present("myzpool/filesystem@snap"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_propmoted(self):
- """
- Test promotion of clone (already promoted)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "myzpool/filesystem already promoted",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/filesystem",
- OrderedDict([("origin", OrderedDict([("value", "-")]))]),
- ),
- ]
- )
- )
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__utils__, self.utils_patch):
- self.assertEqual(ret, zfs.promoted("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_propmoted_clone(self):
- """
- Test promotion of clone
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": True,
- "comment": "myzpool/filesystem promoted",
- "changes": {"myzpool/filesystem": "promoted"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- (
- "myzpool/filesystem",
- OrderedDict(
- [
- (
- "origin",
- OrderedDict(
- [("value", "myzool/filesystem_source@clean")]
- ),
- ),
- ]
- ),
- ),
- ]
- )
- )
- mock_promote = MagicMock(return_value=OrderedDict([("promoted", True)]))
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__salt__, {"zfs.get": mock_get}
- ), patch.dict(zfs.__salt__, {"zfs.promote": mock_promote}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.promoted("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_propmoted_fail(self):
- """
- Test promotion of clone (unknown dataset)
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": False,
- "comment": "dataset myzpool/filesystem does not exist",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(ret, zfs.promoted("myzpool/filesystem"))
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_scheduled_snapshot_fail(self):
- """
- Test scheduled_snapshot of unknown dataset
- """
- ret = {
- "name": "myzpool/filesystem",
- "result": False,
- "comment": "dataset myzpool/filesystem does not exist",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zfs.__salt__, {"zfs.exists": mock_exists}), patch.dict(
- zfs.__utils__, self.utils_patch
- ):
- self.assertEqual(
- ret,
- zfs.scheduled_snapshot(
- "myzpool/filesystem", "shadow", schedule={"hour": 6}
- ),
- )
|