[Kimchi-devel] [PATCH V7 7/7] template supports networks: update test case

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Fri Jan 3 09:32:59 UTC 2014


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 | 82 +++++++++++++++++++++++++++++++++++++++++------------
 tests/test_rest.py  | 58 +++++++++++++++++++++++++++++++++++++
 2 files changed, 122 insertions(+), 18 deletions(-)

diff --git a/tests/test_model.py b/tests/test_model.py
index c03cc3f..0cb0fec 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -260,6 +260,7 @@ 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)
@@ -272,28 +273,73 @@ class ModelTests(unittest.TestCase):
         params['cdrom'] = os.path.abspath(__file__)
         self.assertRaises(InvalidParameter, inst.templates_create, params)
 
+        with 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])
+
+            # create template with non-existent network
+            params['name'] = 'new-test'
+            params['networks'] = ["no-exist"]
+            self.assertRaises(InvalidParameter, inst.templates_create, params)
+
+            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])
+
     @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)
-
-        params = {'name': 'new-test'}
-        self.assertEquals('new-test', inst.template_update('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])
-
-        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])
+        with 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 = {'name': 'new-test', 'memory': 512, 'cpus': 2}
+            inst.template_update('new-test', params)
+            rollback.prependDefer(inst.template_delete, 'new-test')
+
+            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 non-existent network
+            params = {'networks': ["no-exist"]}
+            self.assertRaises(InvalidParameter, inst.template_update,
+                              'new-test', params)
 
     def test_vm_edit(self):
         inst = kimchi.model.Model('qemu:///system',
diff --git a/tests/test_rest.py b/tests/test_rest.py
index e626d2f..a960868 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -351,6 +351,64 @@ class RestTests(unittest.TestCase):
         # Verify the volume was deleted
         self.assertHTTPStatus(404, vol_uri)
 
+    def test_template_customise_network(self):
+        with RollbackContext() as rollback:
+            tmpl = {'name': 'test', 'cdrom': '/nonexistent.iso',
+                    'disks': [{'size': 1}]}
+            req = json.dumps(tmpl)
+            resp = self.request('/templates', req, 'POST')
+            self.assertEquals(201, resp.status)
+            # Delete the template
+            rollback.prependDefer(self.request,
+                                  '/templates/test', '{}', 'DELETE')
+            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 non-existent 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 network
+            req = json.dumps({'name': 'test-network',
+                              'connection': 'nat',
+                              'net': '127.0.1.0/24'})
+            resp = self.request('/networks', req, 'POST')
+            self.assertEquals(201, resp.status)
+            # Delete the network
+            rollback.prependDefer(self.request,
+                                  '/networks/test-network', '{}', 'DELETE')
+
+            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 non-existent 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 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'])
+
     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