[Kimchi-devel] [PATCH v3 2/5] storagepool: rename and consolidate arguments of creating (back end)
Zhou Zheng Sheng
zhshzhou at linux.vnet.ibm.com
Fri Dec 27 10:20:06 UTC 2013
As we are adding support to new type of storage pool, the current naming
scheme of the storage pool creating arguments should be rearranged to be
more extendable. This patch renames some arguments and consolidates the
argument of the same purposes as follow. This patch moves storage source
related arguments under "source" key, while "source" itself is a
dictionary.
nfsserver -> source.host
This is because in future patches, iSCSI pool can use this host info as
well. Other network backed storage pool can also make use of this
argument.
nfspath -> source.path
This is because other netfs pool can also make use of this argument.
devices -> source.devices
To differentiate source arguments from the target arguments, we move
this argument under "source" key.
The patch also adds unit test to check the generated XML is correct.
v2 -> v3
test_storagepool.py: fix typo, reorganize import.
Signed-off-by: Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
---
Makefile.am | 1 +
docs/API.md | 20 ++++++----
src/kimchi/model.py | 39 +++++++++++---------
tests/Makefile.am | 1 +
tests/test_storagepool.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 128 insertions(+), 26 deletions(-)
create mode 100644 tests/test_storagepool.py
diff --git a/Makefile.am b/Makefile.am
index 0fd92c8..33059a7 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -49,6 +49,7 @@ PEP8_WHITELIST = \
plugins/sample/__init__.py \
plugins/sample/model.py \
tests/test_plugin.py \
+ tests/test_storagepool.py \
$(NULL)
check-local:
diff --git a/docs/API.md b/docs/API.md
index 9edc551..a8b9ea0 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -180,15 +180,19 @@ Represents a snapshot of the Virtual Machine's primary monitor.
* **GET**: Retrieve a summarized list of all defined Storage Pools
* **POST**: Create a new Storage Pool
- * name: The name of the Storage Pool
- * path: The path of the defined Storage Pool,
+ * name: The name of the Storage Pool.
+ * type: The type of the defined Storage Pool.
+ Supported types: 'dir', 'kimchi-iso', 'netfs', 'logical'
+ * path: The path of the defined Storage Pool.
For 'kimchi-iso' pool refers to targeted deep scan path.
- * type: The type of the defined Storage Pool,
- Supported types: 'dir', 'kimchi-iso', 'netfs'
- * nfsserver: IP or hostname of NFS server to create NFS pool.
- * nfspath: export path on nfs server for NFS pool.
- * devices: Array of devices to be used in the Storage Pool
- Exclusive to the 'logical' storage pool type.
+ Pool types: 'dir', 'kimchi-iso'.
+ * source: Dictionary containing source information of the pool.
+ * host: IP or hostname of server for a pool backed from a remote host.
+ Pool types: 'netfs'.
+ * path: Export path on NFS server for NFS pool.
+ Pool types: 'netfs'.
+ * devices: Array of devices to be used in the Storage Pool
+ Pool types: 'logical'.
### Resource: Storage Pool
diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index 4d7f9f2..b6213e9 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -1458,6 +1458,8 @@ class StoragePoolDef(object):
def _get_xml_imp(self, poolArgs):
''' Subclasses have to override this method to actually generate the
storage pool XML definition. '''
+ # REMIND: When add new pool type, also add the related test in
+ # tests/test_storagepool.py
raise OperationFailed('get_xml is not implemented: %s' % self)
@@ -1471,12 +1473,12 @@ class DirPoolDef(StoragePoolDef):
# path:
xml = """
<pool type='dir'>
- <name>%(name)s</name>
+ <name>{name}</name>
<target>
- <path>%(path)s</path>
+ <path>{path}</path>
</target>
</pool>
- """ % poolArgs
+ """.format(**poolArgs)
return xml
@@ -1495,21 +1497,21 @@ class NetfsPoolDef(StoragePoolDef):
# Required parameters
# name:
# type:
- # nfsserver:
- # nfspath:
+ # source[host]:
+ # source[path]:
poolArgs['path'] = self.path
xml = """
<pool type='netfs'>
- <name>%(name)s</name>
+ <name>{name}</name>
<source>
- <host name='%(nfsserver)s'/>
- <dir path='%(nfspath)s'/>
+ <host name='{source[host]}'/>
+ <dir path='{source[path]}'/>
</source>
<target>
- <path>%(path)s</path>
+ <path>{path}</path>
</target>
</pool>
- """ % poolArgs
+ """.format(**poolArgs)
return xml
@@ -1524,24 +1526,25 @@ class LogicalPoolDef(StoragePoolDef):
# Required parameters
# name:
# type:
- # devices:
+ # source[devices]:
devices = []
- for device_path in poolArgs['devices']:
+ for device_path in poolArgs['source']['devices']:
devices.append('<device path="%s" />' % device_path)
- poolArgs.update({'devices': ''.join(devices),
- 'path': self.path})
+ poolArgs['source']['devices'] = ''.join(devices)
+ poolArgs['path'] = self.path
+
xml = """
<pool type='logical'>
- <name>%(name)s</name>
+ <name>{name}</name>
<source>
- %(devices)s
+ {source[devices]}
</source>
<target>
- <path>%(path)s</path>
+ <path>{path}</path>
</target>
</pool>
- """ % poolArgs
+ """.format(**poolArgs)
return xml
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5fb00c0..55d3ab4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -32,6 +32,7 @@ EXTRA_DIST = \
test_plugin.py \
test_rest.py \
test_server.py \
+ test_storagepool.py \
test_vmtemplate.py \
utils.py \
$(NULL)
diff --git a/tests/test_storagepool.py b/tests/test_storagepool.py
new file mode 100644
index 0000000..beecdb6
--- /dev/null
+++ b/tests/test_storagepool.py
@@ -0,0 +1,93 @@
+#
+# Project Kimchi
+#
+# Copyright IBM, Corp. 2013
+#
+# Authors:
+# Zhou Zheng Sheng <zhshzhou at linux.vnet.ibm.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+import libxml2
+import unittest
+
+
+import kimchi.model
+import utils
+
+
+class storagepoolTests(unittest.TestCase):
+ def test_get_storagepool_xml(self):
+ poolDefs = [
+ {'def':
+ {'type': 'dir',
+ 'name': 'unitTestDirPool',
+ 'path': '/var/temp/images'},
+ 'xml':
+ """
+ <pool type='dir'>
+ <name>unitTestDirPool</name>
+ <target>
+ <path>/var/temp/images</path>
+ </target>
+ </pool>
+ """},
+ {'def':
+ {'type': 'netfs',
+ 'name': 'unitTestNFSPool',
+ 'source': {'host': '127.0.0.1',
+ 'path': '/var/export'}},
+ 'xml':
+ """
+ <pool type='netfs'>
+ <name>unitTestNFSPool</name>
+ <source>
+ <host name='127.0.0.1'/>
+ <dir path='/var/export'/>
+ </source>
+ <target>
+ <path>/var/lib/kimchi/nfs_mount/unitTestNFSPool</path>
+ </target>
+ </pool>
+ """},
+ {'def':
+ {'type': 'logical',
+ 'name': 'unitTestLogicalPool',
+ 'source': {'devices': ['/dev/hda', '/dev/hdb']}},
+ 'xml':
+ """
+ <pool type='logical'>
+ <name>unitTestLogicalPool</name>
+ <source>
+ <device path="/dev/hda" />
+ <device path="/dev/hdb" />
+ </source>
+ <target>
+ <path>/var/lib/kimchi/logical_mount/unitTestLogicalPool</path>
+ </target>
+ </pool>
+ """}]
+
+ for poolDef in poolDefs:
+ defObj = kimchi.model.StoragePoolDef.create(poolDef['def'])
+ xmlStr = defObj.get_xml()
+ with utils.RollbackContext() as rollback:
+ t1 = libxml2.readDoc(xmlStr, URL='', encoding='UTF-8',
+ options=libxml2.XML_PARSE_NOBLANKS)
+ rollback.prependDefer(t1.freeDoc)
+ t2 = libxml2.readDoc(poolDef['xml'], URL='', encoding='UTF-8',
+ options=libxml2.XML_PARSE_NOBLANKS)
+ rollback.prependDefer(t2.freeDoc)
+ self.assertEquals(t1.serialize(), t2.serialize())
--
1.7.11.7
More information about the Kimchi-devel
mailing list