123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- # -*- coding: utf-8 -*-
- """
- Tests for salt.states.zpool
- :codeauthor: Jorge Schrauwen <sjorge@blackdot.be>
- :maintainer: Jorge Schrauwen <sjorge@blackdot.be>
- :maturity: new
- :depends: salt.utils.zfs, salt.modules.zpool
- :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.zpool as zpool
- # 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 ZpoolTestCase(TestCase, LoaderModuleMockMixin):
- """
- Test cases for salt.states.zpool
- """
- 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"])
- zpool_obj = {
- zpool: {
- "__opts__": opts,
- "__grains__": {"kernel": "SunOS"},
- "__utils__": utils,
- }
- }
- return zpool_obj
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_without_pool(self):
- """
- Test zpool absent without a pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool is absent",
- "changes": {},
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__utils__, self.utils_patch
- ):
- self.assertEqual(zpool.absent("myzpool"), ret)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_destroy_pool(self):
- """
- Test zpool absent destroying pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool was destroyed",
- "changes": {"myzpool": "destroyed"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("destroyed", True)]))
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.destroy": mock_destroy}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(zpool.absent("myzpool"), ret)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_absent_exporty_pool(self):
- """
- Test zpool absent exporting pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool was exported",
- "changes": {"myzpool": "exported"},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(return_value=OrderedDict([("exported", True)]))
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.export": mock_destroy}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(zpool.absent("myzpool", export=True), ret)
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_absent_busy(self):
- """
- Test zpool absent on a busy pool
- """
- ret = {
- "name": "myzpool",
- "result": False,
- "comment": "\n".join(
- [
- "cannot unmount '/myzpool': Device busy",
- "cannot export 'myzpool': pool is busy",
- ]
- ),
- "changes": {},
- }
- mock_exists = MagicMock(return_value=True)
- mock_destroy = MagicMock(
- return_value=OrderedDict(
- [
- ("exported", False),
- (
- "error",
- "\n".join(
- [
- "cannot unmount '/myzpool': Device busy",
- "cannot export 'myzpool': pool is busy",
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.export": mock_destroy}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(zpool.absent("myzpool", export=True), ret)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_import_success(self):
- """
- Test zpool present with import allowed and unimported pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool was imported",
- "changes": {"myzpool": "imported"},
- }
- config = {
- "import": True,
- }
- mock_exists = MagicMock(return_value=False)
- mock_import = MagicMock(return_value=OrderedDict([("imported", True)]))
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.import": mock_import}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(zpool.present("myzpool", config=config), ret)
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_present_import_fail(self):
- """
- Test zpool present with import allowed and no unimported pool or layout
- """
- ret = {
- "name": "myzpool",
- "result": False,
- "comment": "storage pool myzpool was not imported, no (valid) layout specified for creation",
- "changes": {},
- }
- config = {
- "import": True,
- }
- mock_exists = MagicMock(return_value=False)
- mock_import = MagicMock(return_value=OrderedDict([("imported", False)]))
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.import": mock_import}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(zpool.present("myzpool", config=config), ret)
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_create_success(self):
- """
- Test zpool present with non existing pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool was created",
- "changes": {"myzpool": "created"},
- }
- config = {
- "import": False,
- }
- layout = [
- OrderedDict([("mirror", ["disk0", "disk1"])]),
- OrderedDict([("mirror", ["disk2", "disk3"])]),
- ]
- properties = {
- "autoexpand": True,
- }
- filesystem_properties = {
- "quota": "5G",
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(
- return_value=OrderedDict(
- [
- ("created", True),
- (
- "vdevs",
- OrderedDict(
- [
- ("mirror-0", ["/dev/dsk/disk0", "/dev/dsk/disk1"]),
- ("mirror-1", ["/dev/dsk/disk2", "/dev/dsk/disk3"]),
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.create": mock_create}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(
- zpool.present(
- "myzpool",
- config=config,
- layout=layout,
- properties=properties,
- filesystem_properties=filesystem_properties,
- ),
- ret,
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_create_fail(self):
- """
- Test zpool present with non existing pool (without a layout)
- """
- ret = {
- "name": "myzpool",
- "result": False,
- "comment": "storage pool myzpool was not imported, no (valid) layout specified for creation",
- "changes": {},
- }
- config = {
- "import": False,
- }
- mock_exists = MagicMock(return_value=False)
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__utils__, self.utils_patch
- ):
- self.assertEqual(zpool.present("myzpool", config=config), ret)
- @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
- def test_present_create_passthrough_fail(self):
- """
- Test zpool present with non existing pool (without a layout)
- """
- ret = {
- "name": "myzpool",
- "result": False,
- "comment": "\n".join(
- [
- "invalid vdev specification",
- "use 'force=True' to override the following errors:",
- "/data/salt/vdisk0 is part of exported pool 'zsalt'",
- "/data/salt/vdisk1 is part of exported pool 'zsalt'",
- ]
- ),
- "changes": {},
- }
- config = {
- "force": False,
- "import": False,
- }
- layout = [
- OrderedDict([("mirror", ["disk0", "disk1"])]),
- OrderedDict([("mirror", ["disk2", "disk3"])]),
- ]
- properties = {
- "autoexpand": True,
- }
- filesystem_properties = {
- "quota": "5G",
- }
- mock_exists = MagicMock(return_value=False)
- mock_create = MagicMock(
- return_value=OrderedDict(
- [
- ("created", False),
- (
- "error",
- "\n".join(
- [
- "invalid vdev specification",
- "use 'force=True' to override the following errors:",
- "/data/salt/vdisk0 is part of exported pool 'zsalt'",
- "/data/salt/vdisk1 is part of exported pool 'zsalt'",
- ]
- ),
- ),
- ]
- )
- )
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.create": mock_create}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(
- zpool.present(
- "myzpool",
- config=config,
- layout=layout,
- properties=properties,
- filesystem_properties=filesystem_properties,
- ),
- ret,
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_update_success(self):
- """
- Test zpool present with an existing pool that needs an update
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "properties updated",
- "changes": {"myzpool": {"autoexpand": False}},
- }
- config = {
- "import": False,
- }
- layout = [
- OrderedDict([("mirror", ["disk0", "disk1"])]),
- OrderedDict([("mirror", ["disk2", "disk3"])]),
- ]
- properties = {
- "autoexpand": False,
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- ("comment", "salt managed pool"),
- ("freeing", 0),
- ("listsnapshots", False),
- ("leaked", 0),
- ("feature@obsolete_counts", "enabled"),
- ("feature@sha512", "enabled"),
- ("delegation", True),
- ("dedupditto", "0"),
- ("dedupratio", "1.00x"),
- ("autoexpand", True),
- ("feature@bookmarks", "enabled"),
- ("allocated", 115712),
- ("guid", 1591906802560842214),
- ("feature@large_blocks", "enabled"),
- ("size", 2113929216),
- ("feature@enabled_txg", "active"),
- ("feature@hole_birth", "active"),
- ("capacity", 0),
- ("feature@multi_vdev_crash_dump", "enabled"),
- ("feature@extensible_dataset", "enabled"),
- ("cachefile", "-"),
- ("bootfs", "-"),
- ("autoreplace", True),
- ("readonly", False),
- ("version", "-"),
- ("health", "ONLINE"),
- ("expandsize", "-"),
- ("feature@embedded_data", "active"),
- ("feature@lz4_compress", "active"),
- ("feature@async_destroy", "enabled"),
- ("feature@skein", "enabled"),
- ("feature@empty_bpobj", "enabled"),
- ("feature@spacemap_histogram", "active"),
- ("bootsize", "-"),
- ("free", 2113813504),
- ("feature@device_removal", "enabled"),
- ("failmode", "wait"),
- ("feature@filesystem_limits", "enabled"),
- ("feature@edonr", "enabled"),
- ("altroot", "-"),
- ("fragmentation", "0%"),
- ]
- )
- )
- mock_set = MagicMock(return_value=OrderedDict([("set", True)]))
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.get": mock_get}
- ), patch.dict(zpool.__salt__, {"zpool.set": mock_set}), patch.dict(
- zpool.__utils__, self.utils_patch
- ):
- self.assertEqual(
- zpool.present(
- "myzpool", config=config, layout=layout, properties=properties,
- ),
- ret,
- )
- @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
- def test_present_update_nochange_success(self):
- """
- Test zpool present with non existing pool
- """
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "no update needed",
- "changes": {},
- }
- config = {
- "import": False,
- }
- layout = [
- OrderedDict([("mirror", ["disk0", "disk1"])]),
- OrderedDict([("mirror", ["disk2", "disk3"])]),
- ]
- properties = {
- "autoexpand": True,
- }
- mock_exists = MagicMock(return_value=True)
- mock_get = MagicMock(
- return_value=OrderedDict(
- [
- ("comment", "salt managed pool"),
- ("freeing", 0),
- ("listsnapshots", False),
- ("leaked", 0),
- ("feature@obsolete_counts", "enabled"),
- ("feature@sha512", "enabled"),
- ("delegation", True),
- ("dedupditto", "0"),
- ("dedupratio", "1.00x"),
- ("autoexpand", True),
- ("feature@bookmarks", "enabled"),
- ("allocated", 115712),
- ("guid", 1591906802560842214),
- ("feature@large_blocks", "enabled"),
- ("size", 2113929216),
- ("feature@enabled_txg", "active"),
- ("feature@hole_birth", "active"),
- ("capacity", 0),
- ("feature@multi_vdev_crash_dump", "enabled"),
- ("feature@extensible_dataset", "enabled"),
- ("cachefile", "-"),
- ("bootfs", "-"),
- ("autoreplace", True),
- ("readonly", False),
- ("version", "-"),
- ("health", "ONLINE"),
- ("expandsize", "-"),
- ("feature@embedded_data", "active"),
- ("feature@lz4_compress", "active"),
- ("feature@async_destroy", "enabled"),
- ("feature@skein", "enabled"),
- ("feature@empty_bpobj", "enabled"),
- ("feature@spacemap_histogram", "active"),
- ("bootsize", "-"),
- ("free", 2113813504),
- ("feature@device_removal", "enabled"),
- ("failmode", "wait"),
- ("feature@filesystem_limits", "enabled"),
- ("feature@edonr", "enabled"),
- ("altroot", "-"),
- ("fragmentation", "0%"),
- ]
- )
- )
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.get": mock_get}
- ), patch.dict(zpool.__utils__, self.utils_patch):
- self.assertEqual(
- zpool.present(
- "myzpool", config=config, layout=layout, properties=properties,
- ),
- ret,
- )
- # Run state with test=true
- ret = {
- "name": "myzpool",
- "result": True,
- "comment": "storage pool myzpool is uptodate",
- "changes": {},
- }
- with patch.dict(zpool.__salt__, {"zpool.exists": mock_exists}), patch.dict(
- zpool.__salt__, {"zpool.get": mock_get}
- ), patch.dict(zpool.__utils__, self.utils_patch), patch.dict(
- zpool.__opts__, {"test": True}
- ):
- self.assertEqual(
- zpool.present(
- "myzpool", config=config, layout=layout, properties=properties,
- ),
- ret,
- )
|