Added a time request parameter, and raising an exception when requests are sent longer than 5 seconds ago

This commit is contained in:
Anton Beloglazov 2012-10-12 11:20:21 +11:00
parent 5b001e16ae
commit 259fe240e7
2 changed files with 51 additions and 8 deletions

View File

@ -92,9 +92,8 @@ ERRORS = {
'specified in the configuration file',
405: 'Method not allowed: the request is made with ' +
'a method other than the only supported PUT',
422: 'Unprocessable entity: one or more VMs could not ' +
'be found using the list of UUIDs specified in ' +
'the vm_uuids parameter'}
412: 'Precondition failed: some VMs possibly have been in ' +
'migration when the request has been sent - retry'}
@contract
@ -136,9 +135,13 @@ def validate_params(user, password, params):
return False
if 'reason' not in params or \
params['reason'] == 1 and 'vm_uuids' not in params or \
params['reason'] == 0 and 'host' not in params:
params['reason'] == 0 and 'host' not in params or \
'time' not in params:
raise_error(400)
return False
if params['time'] + 5 < time.time():
raise_error(412)
return False
log.debug('Request parameters validated')
return True
@ -176,6 +179,8 @@ def get_params(request):
:rtype: dict(str: *)
"""
params = dict(request.forms)
if 'time' in params:
params['time'] = float(params['time'])
if 'reason' in params:
params['reason'] = int(params['reason'])
if 'vm_uuids' in params:

View File

@ -18,6 +18,7 @@ from pyqcy import *
import bottle
from hashlib import sha1
from novaclient.v1_1 import client
import time
import neat.globals.manager as manager
import neat.common as common
@ -29,7 +30,7 @@ logging.disable(logging.CRITICAL)
class GlobalManager(TestCase):
def test_raise_error(self):
for error_code in [400, 401, 403, 405, 422]:
for error_code in [400, 401, 403, 405, 412]:
try:
manager.raise_error(error_code)
except bottle.HTTPResponse as e:
@ -80,6 +81,7 @@ class GlobalManager(TestCase):
sha1('test2').hexdigest(),
{'username': sha1('test1').hexdigest(),
'password': sha1('test2').hexdigest(),
'time': time.time(),
'reason': 1,
'vm_uuids': ['qwe', 'asd']})
@ -88,27 +90,63 @@ class GlobalManager(TestCase):
sha1('test2').hexdigest(),
{'username': sha1('test1').hexdigest(),
'password': sha1('test2').hexdigest(),
'time': time.time(),
'reason': 0,
'host': 'test'})
with MockTransaction:
expect(manager).raise_error(400).exactly(5).times()
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test'})
expect(manager).raise_error(400).exactly(6).times()
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time()})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time(),
'reason': 1})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time(),
'reason': 0})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time(),
'reason': 1,
'host': 'test'})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time(),
'reason': 0,
'vm_uuids': []})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'reason': 0,
'vm_uuids': []})
with MockTransaction:
expect(manager).raise_error(412).exactly(2).times()
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': 1.,
'reason': 0,
'host': 'test'})
manager.validate_params('test', 'test', {'username': 'test',
'password': 'test',
'time': time.time() - 6,
'reason': 0,
'host': 'test'})
assert manager.validate_params('test', 'test',
{'username': 'test',
'password': 'test',
'time': time.time(),
'reason': 0,
'host': 'test'})
assert manager.validate_params('test', 'test',
{'username': 'test',
'password': 'test',
'time': time.time() - 4,
'reason': 0,
'host': 'test'})
def test_start(self):
with MockTransaction: