[Kimchi-devel] [PATCH 2/2 v3] Unit tests for the new disk image format

Daniel Henrique Barboza danielhb at linux.vnet.ibm.com
Fri Oct 31 01:32:23 UTC 2014


The tests covered the case where the template is created and the
format of the disk is changed.

Signed-off-by: Daniel Henrique Barboza <danielhb at linux.vnet.ibm.com>
---
 tests/test_mockmodel.py | 75 +++++++++++++++++++++++++++++++++++++++++--------
 tests/test_model.py     | 14 +++++++++
 2 files changed, 77 insertions(+), 12 deletions(-)

diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index eeb6715..db0915e 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -91,29 +91,80 @@ class MockModelTests(unittest.TestCase):
             else:
                 self.fail("Expected exception not raised")
 
-    def test_template_cpu_info(self):
+    def _create_default_template(self):
         # Create default template
         req = json.dumps({'name': 'test', 'cdrom': fake_iso})
         resp = request(host, ssl_port, '/templates', req, 'POST')
         rsp_body = resp.read()
-        template_data = json.loads(rsp_body)
+        return json.loads(rsp_body)
+
+    def test_template_cpu_info(self):
+        template = self._create_default_template()
         # GET of cpu_info will be {}
-        cpu_info = template_data['cpu_info']
+        cpu_info = template['cpu_info']
         self.assertEquals(cpu_info, {})
         self.assertEquals(cpu_info.get('topology'), None)
 
         # Update topology
         # GET of cpu_info will contain 'topology'
-        req = json.dumps({'cpu_info': {'topology': {'sockets': 1,
-                                                    'cores': 1,
-                                                    'threads': 1}}})
-        resp = request(host, ssl_port, '/templates/test', req, 'PUT')
+        cpu_info_data = {'cpu_info': {'topology': {'sockets': 1,
+                                                   'cores': 1,
+                                                   'threads': 1}}}
+        _, resp_code = self._send_url_request('PUT', '/templates/test',
+                                              cpu_info_data)
+        self.assertEquals(200, resp_code)
+
+        updated_template, resp_code = \
+            self._send_url_request('GET', '/templates/test')
+        self.assertEquals(200, resp_code)
+        self.assertEquals(updated_template['cpu_info'],
+                          cpu_info_data['cpu_info'])
+
+    def test_template_update_disk_type(self):
+        def _get_default_disk_data(disk_type):
+            return {'disks': [{'index': 0, 'format': disk_type, 'size': 10}]}
+
+        template = self._create_default_template()
+        # Default template is created with 1 disk without any declared
+        # type.
+        disk_data = template['disks']
+        self.assertEquals(disk_data, [{'index': 0, 'size': 10}])
+
+        # For all supported types, edit the template and check if
+        # the change was made.
+        disk_types = ['bochs', 'cloop', 'cow', 'dmg', 'qcow', 'qcow2',
+                      'qed', 'raw', 'vmdk', 'vpc']
+        for disk_type in disk_types:
+            disk_data = _get_default_disk_data(disk_type)
+            _, resp_code = self._send_url_request('PUT', '/templates/test',
+                                                  disk_data)
+            self.assertEquals(200, resp_code)
+
+            updated_template, resp_code = \
+                self._send_url_request('GET', '/templates/test')
+            self.assertEquals(200, resp_code)
+            self.assertEquals(updated_template['disks'], disk_data['disks'])
+
+        # Check Bad Request when type is invalid
+        bad_disk_data = _get_default_disk_data('invalid_disk_type')
+        _, resp_code = self._send_url_request('PUT', '/templates/test',
+                                              bad_disk_data)
+        self.assertEquals(400, resp_code)
+
+    def _create_default_template(self):
+        params = {'name': 'test', 'cdrom': fake_iso}
+        template, resp_code = self._send_url_request('POST', '/templates',
+                                                     params)
+        self.assertEquals(201, resp_code)
+        return template
+
+    def _send_url_request(self, method, url, data=None):
+        req = None
+        if data:
+            req = json.dumps(data)
+        resp = request(host, ssl_port, url, req, method)
         rsp_body = resp.read()
-        template_data = json.loads(rsp_body)
-        cpu_info = template_data['cpu_info']
-        self.assertEquals(cpu_info, {'topology': {'sockets': 1,
-                                                  'cores': 1,
-                                                  'threads': 1}})
+        return json.loads(rsp_body), resp.status
 
     def test_screenshot_refresh(self):
         # Create a VM
diff --git a/tests/test_model.py b/tests/test_model.py
index 563e80a..dab5890 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -639,6 +639,20 @@ class ModelTests(unittest.TestCase):
             self.assertRaises(InvalidParameter, inst.template_update,
                               'test', params)
 
+            # For all supported formats, edit the template and check if
+            # the change was made.
+            disk_formats = ['bochs', 'cloop', 'cow', 'dmg', 'qcow', 'qcow2',
+                          'qed', 'raw', 'vmdk', 'vpc']
+            for disk_format in disk_formats:
+                disk_data = {'disks': [{'index': 0, 'format': disk_format,
+                                       'size': 1}]}
+                inst.template_update('test', disk_data)
+                updated_template = inst.template_lookup('test')
+                self.assertEquals(updated_template['disks'],
+                                  disk_data['disks'])
+            # Restore disk data to default value
+            inst.template_update('test', {'disks': [{'index': 0, 'size': 1}]})
+
             args = {'name': pool,
                     'path': path,
                     'type': 'dir'}
-- 
1.8.3.1




More information about the Kimchi-devel mailing list