[Kimchi-devel] [PATCH V2 6/6] template supports networks: update test case

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Fri Dec 27 09:21:06 UTC 2013


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

update test_rest and test_model

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 tests/test_model.py | 87 +++++++++++++++++++++++++++++++++++++++++------------
 tests/test_rest.py  | 68 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 136 insertions(+), 19 deletions(-)

diff --git a/tests/test_model.py b/tests/test_model.py
index fb7d6dd..d1e516e 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -253,8 +253,10 @@ class ModelTests(unittest.TestCase):
             disk_path = '/tmp/kimchi-images/%s-0.img' % vm_info['uuid']
             self.assertTrue(os.access(disk_path, os.F_OK))
 
+    @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
     def test_template_create(self):
-        inst = kimchi.model.Model('test:///default', objstore_loc=self.tmp_store)
+        inst = kimchi.model.Model('qemu:///system',
+                                  objstore_loc=self.tmp_store)
         # Test non-exist path raises InvalidParameter
         params = {'name': 'test',
                   'cdrom': '/non-exsitent.iso'}
@@ -264,27 +266,74 @@ class ModelTests(unittest.TestCase):
         params['cdrom'] = os.path.abspath(__file__)
         self.assertRaises(InvalidParameter, inst.templates_create, params)
 
-    @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
-    def test_template_update(self):
-        inst = kimchi.model.Model('qemu:///system', objstore_loc=self.tmp_store)
-
-        orig_params = {'name': 'test', 'memory': '1024', 'cpus': '1'}
-        inst.templates_create(orig_params)
+        with utils.RollbackContext() as rollback:
+            net_name = 'test-network'
+            net_args = {'name': net_name,
+                        'connection': 'nat',
+                        'subnet': '127.0.100.0/24'}
+            inst.networks_create(net_args)
+            rollback.prependDefer(inst.network_delete, net_name)
+
+            params = {'name': 'test', 'memory': '1024', 'cpus': '1'}
+            inst.templates_create(params)
+            rollback.prependDefer(inst.template_delete, 'test')
+            info = inst.template_lookup('test')
+            for key in params.keys():
+                self.assertEquals(params[key], info[key])
+            self.assertEquals("default", info["networks"][0])
 
-        params = {'name': 'new-test'}
-        self.assertEquals('new-test', inst.template_update('test', params))
+            # create template with with non-existent network
+            params['name'] = 'new-test'
+            params['networks'] = ["no-exist"]
+            self.assertRaises(NotFoundError, inst.templates_create, params)
 
-        params = {'name': 'new-test', 'memory': '512', 'cpus': '2'}
-        inst.template_update('new-test', params)
-        info = inst.template_lookup('new-test')
-        for key in params.keys():
-            self.assertEquals(params[key], info[key])
+            params['networks'] = ['default', 'test-network']
+            inst.templates_create(params)
+            rollback.prependDefer(inst.template_delete, params['name'])
+            info = inst.template_lookup(params['name'])
+            for key in params.keys():
+                self.assertEquals(params[key], info[key])
 
-        params = {'name': 'new-test', 'memory': 1024, 'cpus': 1}
-        inst.template_update('new-test', params)
-        info = inst.template_lookup('new-test')
-        for key in params.keys():
-            self.assertEquals(params[key], info[key])
+    @unittest.skipUnless(utils.running_as_root(), 'Must be run as root')
+    def test_template_update(self):
+        inst = kimchi.model.Model('qemu:///system',
+                                  objstore_loc=self.tmp_store)
+        with utils.RollbackContext() as rollback:
+            net_name = 'test-network'
+            net_args = {'name': net_name,
+                        'connection': 'nat',
+                        'subnet': '127.0.100.0/24'}
+            inst.networks_create(net_args)
+            rollback.prependDefer(inst.network_delete, net_name)
+
+            orig_params = {'name': 'test', 'memory': '1024', 'cpus': '1'}
+            inst.templates_create(orig_params)
+
+            params = {'name': 'new-test'}
+            self.assertEquals('new-test', inst.template_update('test', params))
+            self.assertRaises(NotFoundError, inst.template_delete,
+                              'test', params)
+
+            params = {'name': 'new-test', 'memory': '512', 'cpus': '2'}
+            inst.template_update('new-test', params)
+            info = inst.template_lookup('new-test')
+            for key in params.keys():
+                self.assertEquals(params[key], info[key])
+            self.assertEquals("default", info["networks"][0])
+
+            params = {'name': 'new-test', 'memory': 1024, 'cpus': 1,
+                      'networks': ['default', 'test-network']}
+            inst.template_update('new-test', params)
+            info = inst.template_lookup('new-test')
+            for key in params.keys():
+                self.assertEquals(params[key], info[key])
+
+            # test update with with non-existent network
+            params = {'networks': ["no-exist"]}
+            self.assertRaises(NotFoundError, inst.template_update,
+                              'new-test', params)
+
+            inst.template_delete('new-test')
 
     def test_vm_edit(self):
         inst = kimchi.model.Model('qemu:///system', objstore_loc=self.tmp_store)
diff --git a/tests/test_rest.py b/tests/test_rest.py
index f597796..3602aae 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -345,6 +345,74 @@ class RestTests(unittest.TestCase):
         # Verify the volume was deleted
         self.assertHTTPStatus(404, vol_uri)
 
+    def test_template_customise_network(self):
+        tmpl = {'name': 'test', 'cdrom': '/nonexistent.iso',
+                'disks': [{'size': 1}]}
+        req = json.dumps(tmpl)
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(201, resp.status)
+        tmpl_res = json.loads(resp.read())
+        self.assertTrue(type(tmpl_res['networks']) is list)
+        self.assertEquals("default", tmpl_res['networks'][0])
+
+        tmpl['name'] = "failed_tmpl"
+        # Create a Template with non-array network fails with 400
+        tmpl['networks'] = "test-network"
+        req = json.dumps(tmpl)
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(400, resp.status)
+
+        # Create a Template with empty-array network fails with 400
+        tmpl['networks'] = []
+        req = json.dumps(tmpl)
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(400, resp.status)
+
+        # Create a Template with non-existent network fails with 404
+        tmpl['networks'] = ["test-network"]
+        req = json.dumps(tmpl)
+        resp = self.request('/templates', req, 'POST')
+        self.assertEquals(404, resp.status)
+
+        # Create a network
+        req = json.dumps({'name': 'test-network',
+                          'connection': 'nat',
+                          'net': '127.0.1.0/24'})
+        resp = request(host, port, '/networks', req, 'POST')
+        self.assertEquals(201, resp.status)
+
+        tmpl['name'] = "test"
+        # Update a Template with non-array network fails with 400
+        tmpl['networks'] = "bad-network"
+        req = json.dumps(tmpl)
+        resp = self.request('/templates/test', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        # Update a Template with empty-array network fails with 400
+        tmpl['networks'] = []
+        req = json.dumps(tmpl)
+        resp = self.request('/templates/test', req, 'PUT')
+        self.assertEquals(400, resp.status)
+
+        # Update a Template with non-existent network fails with 404
+        tmpl['networks'] = ["bad-network"]
+        req = json.dumps(tmpl)
+        resp = self.request('/templates/test', req, 'PUT')
+        self.assertEquals(404, resp.status)
+
+        # Update a Template with existent network, successful
+        tmpl['networks'] = ["default", "test-network"]
+        req = json.dumps(tmpl)
+        resp = self.request('/templates/test', req, 'PUT')
+        self.assertEquals(200, resp.status)
+        tmpl_res = json.loads(resp.read())
+        self.assertTrue(type(tmpl_res['networks']) is list)
+        self.assertEquals(tmpl['networks'], tmpl_res['networks'])
+
+        # Delete the network
+        resp = request(host, port, '/networks/test-network', '{}', 'DELETE')
+        self.assertEquals(204, resp.status)
+
     def test_unnamed_vms(self):
         # Create a Template
         req = json.dumps({'name': 'test', 'cdrom': '/nonexistent.iso'})
-- 
1.8.4.2




More information about the Kimchi-devel mailing list