[Kimchi-devel] [PATCH 5/6] Create VMs Asynchronously: Tests

Christy Perez christy at linux.vnet.ibm.com
Mon Apr 27 17:28:12 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         | 45 ++++++++++++++++++++++++-----------
 tests/test_rest.py          | 58 ++++++++++++++++++++++++++++++++++-----------
 4 files changed, 98 insertions(+), 40 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 c7e733e..528c084 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -67,7 +67,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')
@@ -95,7 +97,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:
@@ -107,7 +111,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 16ec239..7c9285b 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -133,8 +133,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)
@@ -271,7 +274,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()
@@ -290,7 +294,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')
@@ -300,7 +305,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')
@@ -317,7 +323,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
@@ -424,7 +431,8 @@ def _attach_disk(expect_bus=modern_disk_bus):
             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))
@@ -467,7 +475,8 @@ def _attach_disk(expect_bus=modern_disk_bus):
             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)
 
             # Need to check the right disk_bus for old distro
@@ -488,7 +497,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))
@@ -584,7 +594,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'])
@@ -605,10 +616,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')
 
@@ -818,11 +831,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ī-∨м')
 
@@ -841,7 +856,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()
@@ -913,7 +929,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 6b37b9b..ab7a783 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -125,7 +125,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))
@@ -143,7 +145,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'])
@@ -260,7 +264,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())
@@ -442,7 +448,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'])
@@ -456,7 +464,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'])
@@ -470,7 +480,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'])
@@ -484,7 +496,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'])
@@ -526,7 +540,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')
@@ -671,7 +687,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')
@@ -746,7 +764,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
@@ -799,7 +820,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')
@@ -824,8 +847,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)
 
@@ -857,7 +882,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(
@@ -947,6 +975,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')
-- 
2.1.0




More information about the Kimchi-devel mailing list