[Kimchi-devel] [PATCHv4 7/7] Add tests for image based template

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Thu Jul 24 09:02:24 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 tests/test_model.py | 25 +++++++++++++++++++++++++
 tests/test_rest.py  | 30 ++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/tests/test_model.py b/tests/test_model.py
index 30daafa..3bc0f7f 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -107,6 +107,31 @@ class ModelTests(unittest.TestCase):
         self.assertFalse('kimchi-vm' in vms)
 
     @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
+    def test_image_based_template(self):
+        disk_path = '/tmp/existent2.img'
+        open(disk_path, 'w').close()
+
+        inst = model.Model(objstore_loc=self.tmp_store)
+
+        with RollbackContext() as rollback:
+            params = {'name': 'test', 'disks': [{'base': disk_path}]}
+            inst.templates_create(params)
+            rollback.prependDefer(inst.template_delete, 'test')
+
+            params = {'name': 'kimchi-vm', 'template': '/templates/test'}
+            inst.vms_create(params)
+            rollback.prependDefer(inst.vm_delete, 'kimchi-vm')
+
+            vms = inst.vms_get_list()
+            self.assertTrue('kimchi-vm' in vms)
+
+            inst.vm_start('kimchi-vm')
+            rollback.prependDefer(inst.vm_poweroff, 'kimchi-vm')
+
+            info = inst.vm_lookup('kimchi-vm')
+            self.assertEquals('running', info['state'])
+
+    @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
     def test_vm_graphics(self):
         inst = model.Model(objstore_loc=self.tmp_store)
         params = {'name': 'test', 'disks': [], 'cdrom': self.kimchi_iso}
diff --git a/tests/test_rest.py b/tests/test_rest.py
index 2dde32d..bb654a5 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -854,6 +854,27 @@ class RestTests(unittest.TestCase):
         resp = json.loads(resp.read())
         self.assertIn(u"KCHVM0012E", resp['reason'])
 
+    def test_create_vm_with_img_based_template(self):
+        resp = json.loads(
+            self.request('/storagepools/default/storagevolumes').read())
+        self.assertEquals(0, len(resp))
+
+        # Create a Template
+        mock_base = '/tmp/mock.img'
+        open(mock_base, 'w').close()
+        req = json.dumps({'name': 'test', 'disks': [{'base':mock_base}]})
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(201, resp.status)
+
+        req = json.dumps({'template': '/templates/test'})
+        json.loads(self.request('/vms', req, 'POST').read())
+
+        # Test storage volume created with backing store of base file
+        resp = json.loads(
+            self.request('/storagepools/default/storagevolumes').read())
+        self.assertEquals(1, len(resp))
+        self.assertEquals(mock_base, resp[0]['base']['path'])
+
     def test_get_storagepools(self):
         storagepools = json.loads(self.request('/storagepools').read())
         self.assertEquals(2, len(storagepools))
@@ -1076,6 +1097,15 @@ class RestTests(unittest.TestCase):
         resp = self.request('/templates', req, 'POST')
         self.assertEquals(400, resp.status)
 
+        # Create an image based template
+        open('/tmp/mock.img', 'w').close()
+        t = {'name': 'test_img_template', 'os_distro': 'ImagineOS',
+             'os_version': '1.0', 'memory': 1024, 'cpus': 1,
+             'storagepool': '/storagepools/alt', 'disks':[{'base':'/tmp/mock.img'}]}
+        req = json.dumps(t)
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(201, resp.status)
+
         # Create a template
         open('/tmp/mock.iso', 'w').close()
         graphics = {'type': 'spice', 'listen': '127.0.0.1'}
-- 
1.8.3.2




More information about the Kimchi-devel mailing list