From: Christy Perez <christy(a)linux.vnet.ibm.com>
Signed-off-by: Christy Perez <christy(a)linux.vnet.ibm.com>
---
tests/test_authorization.py | 25 +++++++++++--------
tests/test_mockmodel.py | 14 +++++++----
tests/test_model.py | 45 ++++++++++++++++++++++++-----------
tests/test_rest.py | 58 ++++++++++++++++++++++++++++++++++-----------
tests/test_server.py | 26 +++++++++-----------
5 files changed, 111 insertions(+), 57 deletions(-)
diff --git a/tests/test_authorization.py b/tests/test_authorization.py
index 4fcc496..959dcf8 100644
--- a/tests/test_authorization.py
+++ b/tests/test_authorization.py
@@ -1,7 +1,7 @@
#
# Project Kimchi
#
-# Copyright IBM, Corp. 2014
+# Copyright IBM, Corp. 2014-2015
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -26,7 +26,7 @@ from functools import partial
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 @@ class AuthorizationTests(unittest.TestCase):
# 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 @@ class AuthorizationTests(unittest.TestCase):
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 aa48dd1..52972f0 100644
--- a/tests/test_mockmodel.py
+++ b/tests/test_mockmodel.py
@@ -25,7 +25,7 @@ import unittest
import kimchi.mockmodel
-from utils import get_free_port, patch_auth, request, run_server
+from utils import get_free_port, patch_auth, request, run_server, wait_task
from kimchi.osinfo import get_template_default
@@ -66,7 +66,9 @@ class MockModelTests(unittest.TestCase):
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')
@@ -94,7 +96,9 @@ class MockModelTests(unittest.TestCase):
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:
@@ -106,7 +110,9 @@ class MockModelTests(unittest.TestCase):
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 ad0dccd..88c020e 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -129,8 +129,11 @@ class ModelTests(unittest.TestCase):
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)
@@ -267,7 +270,8 @@ class ModelTests(unittest.TestCase):
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()
@@ -286,7 +290,8 @@ class ModelTests(unittest.TestCase):
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')
@@ -296,7 +301,8 @@ class ModelTests(unittest.TestCase):
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')
@@ -325,7 +331,8 @@ class ModelTests(unittest.TestCase):
for vm_name in ['kimchi-ifaces', 'kimchi-ifaces-running']:
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)
ifaces = inst.vmifaces_get_list(vm_name)
@@ -422,7 +429,8 @@ class ModelTests(unittest.TestCase):
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))
@@ -465,7 +473,8 @@ class ModelTests(unittest.TestCase):
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
@@ -486,7 +495,8 @@ class ModelTests(unittest.TestCase):
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))
@@ -575,7 +585,8 @@ class ModelTests(unittest.TestCase):
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'])
@@ -596,10 +607,12 @@ class ModelTests(unittest.TestCase):
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')
@@ -778,11 +791,13 @@ class ModelTests(unittest.TestCase):
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ī-∨м')
@@ -801,7 +816,8 @@ class ModelTests(unittest.TestCase):
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()
@@ -873,7 +889,8 @@ class ModelTests(unittest.TestCase):
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 65c3db5..8826424 100644
--- a/tests/test_rest.py
+++ b/tests/test_rest.py
@@ -112,7 +112,9 @@ class RestTests(unittest.TestCase):
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))
@@ -130,7 +132,9 @@ class RestTests(unittest.TestCase):
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'])
@@ -247,7 +251,9 @@ class RestTests(unittest.TestCase):
# 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())
@@ -429,7 +435,9 @@ class RestTests(unittest.TestCase):
# 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'])
@@ -443,7 +451,9 @@ class RestTests(unittest.TestCase):
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'])
@@ -457,7 +467,9 @@ class RestTests(unittest.TestCase):
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'])
@@ -471,7 +483,9 @@ class RestTests(unittest.TestCase):
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'])
@@ -513,7 +527,9 @@ class RestTests(unittest.TestCase):
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')
@@ -658,7 +674,9 @@ class RestTests(unittest.TestCase):
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')
@@ -738,7 +756,10 @@ class RestTests(unittest.TestCase):
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
@@ -791,7 +812,9 @@ class RestTests(unittest.TestCase):
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')
@@ -816,8 +839,10 @@ class RestTests(unittest.TestCase):
# 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)
@@ -849,7 +874,10 @@ class RestTests(unittest.TestCase):
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(
@@ -939,6 +967,8 @@ class RestTests(unittest.TestCase):
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')
diff --git a/tests/test_server.py b/tests/test_server.py
index 42bebbe..a4d31d0 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -263,29 +263,25 @@ class ServerTests(unittest.TestCase):
mockiso = '/tmp/mock.iso'
open('/tmp/mock.iso', 'w').close()
- req = json.dumps({'name': 'test', 'cdrom': mockiso})
+ # Create 2 different templates
+ req = json.dumps({'name': 'test-tmpl1', 'cdrom':
mockiso})
self.request('/templates', req, 'POST')
- # Create a VM
- req = json.dumps({'name': 'test-vm1', 'template':
'/templates/test'})
- resp = self.request('/vms', req, 'POST')
- self.assertEquals(201, resp.status)
- req = json.dumps({'name': 'test-vm2', 'template':
'/templates/test'})
- resp = self.request('/vms', req, 'POST')
- self.assertEquals(201, resp.status)
+ req = json.dumps({'name': 'test-tmpl2', 'cdrom':
mockiso})
+ self.request('/templates', req, 'POST')
# Remove mock iso
os.unlink(mockiso)
- resp = self.request('/vms')
+ # Get the templates
+ resp = self.request('/templates')
self.assertEquals(200, resp.status)
res = json.loads(resp.read())
- self.assertEquals(3, len(res))
+ self.assertEquals(2, len(res))
- # FIXME: control/base.py also allows filter by regex so it is returning
- # 2 vms when querying for 'test-vm1': 'test' and
'test-vm1'
- resp = self.request('/vms?name=test-vm1')
+ # Get a specific template
+ resp = self.request('/templates?name=test-tmpl1')
self.assertEquals(200, resp.status)
res = json.loads(resp.read())
- self.assertEquals(2, len(res))
- self.assertIn('test-vm1', [r['name'] for r in res])
+ self.assertEquals(1, len(res))
+ self.assertEquals('test-tmpl1', res[0]['name'])
--
2.1.0