[Kimchi-devel] [PATCH 5/5] Async vm creation test updates

Christy Perez christy at linux.vnet.ibm.com
Wed Feb 25 16:53:26 UTC 2015


Signed-off-by: Christy Perez <christy at linux.vnet.ibm.com>
---
 tests/test_authorization.py | 23 +++++++++------
 tests/test_mockmodel.py     | 12 ++++++--
 tests/test_model.py         | 54 ++++++++++++++++++++++++----------
 tests/test_rest.py          | 71 ++++++++++++++++++++++++++++++++++-----------
 4 files changed, 115 insertions(+), 45 deletions(-)

diff --git a/tests/test_authorization.py b/tests/test_authorization.py
index 4fcc496..eae837c 100644
--- a/tests/test_authorization.py
+++ b/tests/test_authorization.py
@@ -26,7 +26,7 @@
 import kimchi.mockmodel
 from iso_gen import construct_fake_iso
 from utils import get_free_port, patch_auth, request
-from utils import run_server
+from utils import run_server, wait_task
 
 
 test_server = None
@@ -118,19 +118,24 @@ def test_nonroot_access(self):
         # Non-root users can only get vms authorized to them
         model.templates_create({'name': u'test', 'cdrom': fake_iso})
 
-        model.vms_create({'name': u'test-me', 'template': '/templates/test'})
+        task_info = model.vms_create({'name': u'test-me',
+                                      'template': '/templates/test'})
+        wait_task(model.task_lookup, task_info['id'])
+
         model.vm_update(u'test-me',
                         {'users': [kimchi.mockmodel.fake_user.keys()[0]],
                          'groups': []})
 
-        model.vms_create({'name': u'test-usera',
-                          'template': '/templates/test'})
+        task_info = model.vms_create({'name': u'test-usera',
+                                      'template': '/templates/test'})
+        wait_task(model.task_lookup, task_info['id'])
 
         non_root = list(set(model.users_get_list()) - set(['root']))[0]
         model.vm_update(u'test-usera', {'users': [non_root], 'groups': []})
 
-        model.vms_create({'name': u'test-groupa',
-                          'template': '/templates/test'})
+        task_info = model.vms_create({'name': u'test-groupa',
+                                      'template': '/templates/test'})
+        wait_task(model.task_lookup, task_info['id'])
         a_group = model.groups_get_list()[0]
         model.vm_update(u'test-groupa', {'groups': [a_group]})
 
@@ -143,9 +148,9 @@ def test_nonroot_access(self):
         self.assertEquals(403, resp.status)
 
         # Create a vm using mockmodel directly to test Resource access
-        model.vms_create({'name': 'kimchi-test',
-                          'template': '/templates/test'})
-
+        task_info = model.vms_create({'name': 'kimchi-test',
+                                      'template': '/templates/test'})
+        wait_task(model.task_lookup, task_info['id'])
         resp = self.request('/vms/kimchi-test', '{}', 'PUT')
         self.assertEquals(403, resp.status)
         resp = self.request('/vms/kimchi-test', '{}', 'DELETE')
diff --git a/tests/test_mockmodel.py b/tests/test_mockmodel.py
index 9fca012..e4378c4 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -169,7 +169,9 @@ def test_screenshot_refresh(self):
         req = json.dumps({'name': 'test', 'cdrom': fake_iso})
         request(host, ssl_port, '/templates', req, 'POST')
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
-        request(host, ssl_port, '/vms', req, 'POST')
+        resp = request(host, ssl_port, '/vms', req, 'POST')
+        task = json.loads(resp.read())
+        wait_task(model.task_lookup, task['id'])
 
         # Test screenshot refresh for running vm
         request(host, ssl_port, '/vms/test-vm/start', '{}', 'POST')
@@ -197,7 +199,9 @@ def test_vm_list_sorted(self):
         def add_vm(name):
             # Create a VM
             req = json.dumps({'name': name, 'template': '/templates/test'})
-            request(host, ssl_port, '/vms', req, 'POST')
+            task = json.loads(request(host, ssl_port, '/vms', req,
+                              'POST').read())
+            wait_task(model.task_lookup, task['id'])
 
         vms = [u'abc', u'bca', u'cab', u'xba']
         for vm in vms:
@@ -209,7 +213,9 @@ def add_vm(name):
     def test_vm_info(self):
         model.templates_create({'name': u'test',
                                 'cdrom': fake_iso})
-        model.vms_create({'name': u'test-vm', 'template': '/templates/test'})
+        task = model.vms_create({'name': u'test-vm',
+                                 'template': '/templates/test'})
+        wait_task(model.task_lookup, task['id'])
         vms = model.vms_get_list()
         self.assertEquals(2, len(vms))
         self.assertIn(u'test-vm', vms)
diff --git a/tests/test_model.py b/tests/test_model.py
index 1045f2d..74fc4a7 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -117,8 +117,11 @@ def test_vm_lifecycle(self):
             rollback.prependDefer(inst.template_delete, 'test')
 
             params = {'name': 'kimchi-vm', 'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
             rollback.prependDefer(inst.vm_delete, 'kimchi-vm')
+            inst.task_wait(task['id'], 10)
+            task = inst.task_lookup(task['id'])
+            self.assertEquals('finished', task['status'])
 
             vms = inst.vms_get_list()
             self.assertTrue('kimchi-vm' in vms)
@@ -235,7 +238,8 @@ def test_image_based_template(self):
                 session.store('template', tmpl_name, tmpl_info)
 
             params = {'name': 'kimchi-vm', 'template': '/templates/img-tmpl'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-vm')
 
             vms = inst.vms_get_list()
@@ -254,7 +258,8 @@ def test_vm_graphics(self):
         inst.templates_create(params)
         with RollbackContext() as rollback:
             params = {'name': 'kimchi-vnc', 'template': '/templates/test'}
-            inst.vms_create(params)
+            task1 = inst.vms_create(params)
+            inst.task_wait(task1['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-vnc')
 
             info = inst.vm_lookup('kimchi-vnc')
@@ -264,7 +269,8 @@ def test_vm_graphics(self):
             graphics = {'type': 'spice', 'listen': '127.0.0.1'}
             params = {'name': 'kimchi-spice', 'template': '/templates/test',
                       'graphics': graphics}
-            inst.vms_create(params)
+            task2 = inst.vms_create(params)
+            inst.task_wait(task2['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-spice')
 
             info = inst.vm_lookup('kimchi-spice')
@@ -281,7 +287,8 @@ def test_vm_ifaces(self):
             inst.templates_create(params)
             rollback.prependDefer(inst.template_delete, 'test')
             params = {'name': 'kimchi-ifaces', 'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-ifaces')
 
             # Create a network
@@ -387,7 +394,8 @@ def _attach_disk(expect_bus='virtio'):
             inst.templates_create(params)
             rollback.prependDefer(inst.template_delete, 'test')
             params = {'name': vm_name, 'template': '/templates/test'}
-            inst.vms_create(params)
+            task1 = inst.vms_create(params)
+            inst.task_wait(task1['id'])
             rollback.prependDefer(inst.vm_delete, vm_name)
 
             prev_count = len(inst.vmstorages_get_list(vm_name))
@@ -430,7 +438,8 @@ def _attach_disk(expect_bus='virtio'):
             rollback.prependDefer(inst.template_delete, 'old_distro_template')
             params = {'name': vm_name,
                       'template': '/templates/old_distro_template'}
-            inst.vms_create(params)
+            task2 = inst.vms_create(params)
+            inst.task_wait(task2['id'])
             rollback.prependDefer(inst.vm_delete, vm_name)
 
             # Attach will choose IDE bus for old distro
@@ -451,7 +460,8 @@ def test_vm_cdrom(self):
             inst.templates_create(params)
             rollback.prependDefer(inst.template_delete, 'test')
             params = {'name': vm_name, 'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, vm_name)
 
             prev_count = len(inst.vmstorages_get_list(vm_name))
@@ -549,7 +559,8 @@ def test_vm_storage_provisioning(self):
             rollback.prependDefer(inst.template_delete, 'test')
 
             params = {'name': 'test-vm-1', 'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'test-vm-1')
 
             vm_info = inst.vm_lookup(params['name'])
@@ -601,12 +612,14 @@ def test_template_storage_customise(self):
             inst.template_update('test', params)
 
             params = {'name': 'test-vm-1', 'template': '/templates/test'}
+            # @TODO: What is invalid here? Rewrite test for that.
             self.assertRaises(InvalidParameter, inst.vms_create, params)
 
             inst.storagepool_activate(pool)
             rollback.prependDefer(inst.storagepool_deactivate, pool)
 
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'test-vm-1')
             vm_info = inst.vm_lookup(params['name'])
             disk_path = '/tmp/kimchi-images/%s-0.img' % vm_info['uuid']
@@ -789,7 +802,10 @@ def test_template_update(self):
                               'new-test', params)
 
             params = {'name': 'some-vm', 'template': '/templates/new-test'}
-            self.assertEquals('some-vm', inst.vms_create(params))
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
+            vm_name = task['target_uri'].split('/')[-1]
+            self.assertEquals('some-vm', vm_name)
             rollback.prependDefer(inst.vm_delete, 'some-vm')
 
             iface_args = {'type': 'network', 'network': u'kīмсhī-пet'}
@@ -808,10 +824,12 @@ def test_vm_edit(self):
         with RollbackContext() as rollback:
             params_1 = {'name': 'kimchi-vm1', 'template': '/templates/test'}
             params_2 = {'name': 'kimchi-vm2', 'template': '/templates/test'}
-            inst.vms_create(params_1)
+            task1 = inst.vms_create(params_1)
+            inst.task_wait(task1['id'])
             rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
                                   'kimchi-vm1')
-            inst.vms_create(params_2)
+            task2 = inst.vms_create(params_2)
+            inst.task_wait(task2['id'])
             rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
                                   'kimchi-vm2')
 
@@ -1091,11 +1109,13 @@ def test_delete_running_vm(self):
             rollback.prependDefer(inst.template_delete, 'test')
 
             params = {'name': u'kīмсhī-∨м', 'template': u'/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(utils.rollback_wrapper, inst.vm_delete,
                                   u'kīмсhī-∨м')
 
             inst.vm_start(u'kīмсhī-∨м')
+            self.assertEquals(inst.vm_lookup(u'kīмсhī-∨м')['state'], 'running')
             rollback.prependDefer(utils.rollback_wrapper, inst.vm_poweroff,
                                   u'kīмсhī-∨м')
 
@@ -1114,7 +1134,8 @@ def test_vm_list_sorted(self):
             rollback.prependDefer(inst.template_delete, 'test')
 
             params = {'name': 'kimchi-vm', 'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-vm')
 
             vms = inst.vms_get_list()
@@ -1186,7 +1207,8 @@ def test_use_test_host(self):
 
             params = {'name': 'kimchi-vm',
                       'template': '/templates/test'}
-            inst.vms_create(params)
+            task = inst.vms_create(params)
+            inst.task_wait(task['id'])
             rollback.prependDefer(inst.vm_delete, 'kimchi-vm')
 
             vms = inst.vms_get_list()
diff --git a/tests/test_rest.py b/tests/test_rest.py
index cb1ea7c..c497791 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -190,7 +190,9 @@ def test_get_vms(self):
             req = json.dumps({'name': name, 'template': '/templates/test',
                              'users': test_users, 'groups': test_groups})
             resp = self.request('/vms', req, 'POST')
-            self.assertEquals(201, resp.status)
+            self.assertEquals(202, resp.status)
+            task = json.loads(resp.read())
+            wait_task(self._task_lookup, task['id'])
 
         vms = json.loads(self.request('/vms').read())
         self.assertEquals(11, len(vms))
@@ -208,7 +210,9 @@ def test_edit_vm(self):
 
         req = json.dumps({'name': 'vm-1', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
 
         vm = json.loads(self.request('/vms/vm-1').read())
         self.assertEquals('vm-1', vm['name'])
@@ -325,7 +329,9 @@ def test_vm_lifecycle(self):
         # Create a VM
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
+        self.assertEquals(202, resp.status)
 
         # Verify the VM
         vm = json.loads(self.request('/vms/test-vm').read())
@@ -479,7 +485,9 @@ def test_vm_graphics(self):
         # Create a VM with default args
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
         # Verify the VM
         vm = json.loads(self.request('/vms/test-vm').read())
         self.assertEquals('127.0.0.1', vm['graphics']['listen'])
@@ -493,7 +501,9 @@ def test_vm_graphics(self):
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test',
                           'graphics': graphics})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
         # Verify the VM
         vm = json.loads(self.request('/vms/test-vm').read())
         self.assertEquals('127.0.0.1', vm['graphics']['listen'])
@@ -507,7 +517,9 @@ def test_vm_graphics(self):
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test',
                           'graphics': graphics})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
         # Verify the VM
         vm = json.loads(self.request('/vms/test-vm').read())
         self.assertEquals('fe00::0', vm['graphics']['listen'])
@@ -521,7 +533,9 @@ def test_vm_graphics(self):
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test',
                           'graphics': graphics})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
         # Verify the VM
         vm = json.loads(self.request('/vms/test-vm').read())
         self.assertEquals('127.0.0.1', vm['graphics']['listen'])
@@ -563,7 +577,9 @@ def test_vm_storage_devices(self):
             req = json.dumps({'name': 'test-vm',
                               'template': '/templates/test'})
             resp = self.request('/vms', req, 'POST')
-            self.assertEquals(201, resp.status)
+            self.assertEquals(202, resp.status)
+            task = json.loads(resp.read())
+            wait_task(self._task_lookup, task['id'])
             # Delete the VM
             rollback.prependDefer(self.request,
                                   '/vms/test-vm', '{}', 'DELETE')
@@ -708,7 +724,9 @@ def test_vm_iface(self):
             req = json.dumps({'name': 'test-vm',
                               'template': '/templates/test'})
             resp = self.request('/vms', req, 'POST')
-            self.assertEquals(201, resp.status)
+            self.assertEquals(202, resp.status)
+            task = json.loads(resp.read())
+            wait_task(self._task_lookup, task['id'])
             # Delete the VM
             rollback.prependDefer(self.request,
                                   '/vms/test-vm', '{}', 'DELETE')
@@ -782,7 +800,10 @@ def test_vm_customise_storage(self):
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test',
                           'storagepool': '/storagepools/alt'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
+        resp = self.request('/vms/test-vm', {}, 'GET')
         vm_info = json.loads(resp.read())
 
         # Test template not changed after vm customise its pool
@@ -835,7 +856,9 @@ def test_scsi_fc_storage(self):
         req = json.dumps({'name': 'test-vm',
                           'template': '/templates/test_fc_pool'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
 
         # Start the VM
         resp = self.request('/vms/test-vm/start', '{}', 'POST')
@@ -886,8 +909,11 @@ def test_template_customise_storage(self):
         # Create a VM
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
+        resp = self.request('/vms/test-vm', {}, 'GET')
         vm = json.loads(resp.read())
-        self.assertEquals(201, resp.status)
+        self.assertEquals(200, resp.status)
 
         # Verify the volume was created
         vol_uri = '/storagepools/alt/storagevolumes/%s-0.img' % vm['uuid']
@@ -969,8 +995,10 @@ def test_unnamed_vms(self):
         # Create 5 unnamed vms from this template
         for i in xrange(1, 6):
             req = json.dumps({'template': '/templates/test'})
-            vm = json.loads(self.request('/vms', req, 'POST').read())
-            self.assertEquals('test-vm-%i' % i, vm['name'])
+            task = json.loads(self.request('/vms', req, 'POST').read())
+            wait_task(self._task_lookup, task['id'])
+            resp = self.request('/vms/test-vm-%i' % i, {}, 'GET')
+            self.assertEquals(resp.status, 200)
         count = len(json.loads(self.request('/vms').read()))
         self.assertEquals(6, count)
 
@@ -1002,7 +1030,10 @@ def test_create_vm_with_img_based_template(self):
         self.assertEquals(201, resp.status)
 
         req = json.dumps({'template': '/templates/test'})
-        json.loads(self.request('/vms', req, 'POST').read())
+        resp = self.request('/vms', req, 'POST')
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
 
         # Test storage volume created with backing store of base file
         resp = json.loads(
@@ -1318,6 +1349,8 @@ def test_screenshot_refresh(self):
         resp = self.request('/templates', req, 'POST')
         req = json.dumps({'name': 'test-vm', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
 
         # Test screenshot for shut-off state vm
         resp = self.request('/vms/test-vm/screenshot')
@@ -1648,10 +1681,14 @@ def test_get_param(self):
         # Create a VM
         req = json.dumps({'name': 'test-vm1', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
+        self.assertEquals(202, resp.status)
         req = json.dumps({'name': 'test-vm2', 'template': '/templates/test'})
         resp = self.request('/vms', req, 'POST')
-        self.assertEquals(201, resp.status)
+        self.assertEquals(202, resp.status)
+        task = json.loads(resp.read())
+        wait_task(self._task_lookup, task['id'])
 
         resp = request(host, ssl_port, '/vms')
         self.assertEquals(200, resp.status)
-- 
2.1.0




More information about the Kimchi-devel mailing list