diff --git a/core/storage/__init__.py b/core/storage/__init__.py index 39f623d5..ef8b27e0 100644 --- a/core/storage/__init__.py +++ b/core/storage/__init__.py @@ -258,6 +258,32 @@ def get_pool(name, vm): return klass(vm, **kwargs) +def pool_exists(name): + """ Check if the specified pool exists """ + try: + _get_pool_klass(name) + return True + except StoragePoolException: + return False + +def add_pool(name, **kwargs): + """ Add a storage pool to config.""" + config = _get_storage_config_parser() + config.add_section(name) + for key, value in kwargs.iteritems(): + config.set(name, key, value) + _write_config(config) + +def remove_pool(name): + """ Remove a storage pool from config file. """ + config = _get_storage_config_parser() + config.remove_section(name) + _write_config(config) + +def _write_config(config): + with open(CONFIG_FILE, 'w') as configfile: + config.write(configfile) + def _get_storage_config_parser(): """ Instantiates a `ConfigParaser` for specified storage config file. @@ -297,14 +323,6 @@ def _get_pool_klass(name, config=None): return klass -def pool_exists(name): - """ Check if the specified pool exists """ - try: - _get_pool_klass(name) - return True - except StoragePoolException: - return False - class StoragePoolException(QubesException): pass diff --git a/core/storage/storage.conf b/core/storage/storage.conf index 8fa44c7e..45b6bda0 100644 --- a/core/storage/storage.conf +++ b/core/storage/storage.conf @@ -6,6 +6,7 @@ type=xen ; the default xen storage ; class name ; [pool-b] ; class = foo.bar.MyStorage -; - +; +; [test-dummy] +; type=dummy diff --git a/tests/storage.py b/tests/storage.py index b5a0defc..d016fb1a 100644 --- a/tests/storage.py +++ b/tests/storage.py @@ -17,11 +17,11 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -from qubes.tests import QubesTestCase, SystemTestsMixin -from qubes.qubes import defaults - import qubes.storage -from qubes.storage.xen import XenStorage, XenPool +from qubes.qubes import defaults +from qubes.tests import QubesTestCase, SystemTestsMixin + +from qubes.storage.xen import XenPool, XenStorage class TC_00_Storage(SystemTestsMixin, QubesTestCase): @@ -62,6 +62,19 @@ class TC_00_Storage(SystemTestsMixin, QubesTestCase): self.assertFalse( qubes.storage.pool_exists('asdh312096r832598213iudhas')) + def test_006_add_remove_pool(self): + """ Tries to adding and removing a pool. """ + pool_name = 'asdjhrp89132' + + # make sure it's really does not exist + qubes.storage.remove_pool(pool_name) + + qubes.storage.add_pool(pool_name, type='xen') + self.assertTrue(qubes.storage.pool_exists(pool_name)) + + qubes.storage.remove_pool(pool_name) + self.assertFalse(qubes.storage.pool_exists(pool_name)) + class TC_00_Pool(SystemTestsMixin, QubesTestCase):