Commit 7bd75bfc authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Update stress test code

parent 106766ec
......@@ -35,7 +35,6 @@
# or implied, of GRNET S.A.
import os
import sys
from optparse import OptionParser
from time import sleep
import threading
......@@ -47,9 +46,12 @@ path = os.path.dirname(os.path.realpath(__file__))
os.environ['SYNNEFO_SETTINGS_DIR'] = path + '/settings'
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
from astakos.im.models import AstakosUser
from astakos.im.functions import get_chain_of_application_id
from astakos.im import quotas
from views import submit, approve, join, leave
from snf_django.lib.db.transaction import commit_on_success_strict
from django.core.exceptions import PermissionDenied
USERS = {}
PROJECTS = {}
......@@ -72,22 +74,32 @@ def random_email():
alphabet = u'abcdef'
length = randint(2, 4)
last = ''.join(choice(alphabet) for _ in xrange(length))
return first + '@' + last
return first + '@' + last + '.com'
def new_user():
email = random_email()
u = AstakosUser(email=random_email(), first_name=random_name(),
last_name=random_name(), is_active=True)
u.save()
return u.id, u.email
defaults = {'first_name': random_name(),
'last_name': random_name(),
'is_active': True,
}
u, created = AstakosUser.objects.get_or_create(
email=email, defaults=defaults)
if created:
quotas.qh_sync_user(u)
return u.id, u.email
return None
@commit_on_success_strict()
def new_users(count):
for i in range(count):
uid, email = new_user()
USERS[uid] = email
while True:
result = new_user()
if result is not None:
uid, email = result
USERS[uid] = email
break
class SubmitApproveT(threading.Thread):
......@@ -113,8 +125,11 @@ def submit_and_approve(name, user_id, prec, repeat, prefix=""):
% (prefix, now, prec))
app_id = submit(name, user_id, prec)
prec = app_id
except PermissionDenied as e:
logger.info('Limit reached')
except Exception as e:
logger.exception(e)
continue
try:
now = datetime.datetime.now()
pid = get_chain_of_application_id(app_id)
......@@ -152,6 +167,8 @@ def join_and_leave(proj_id, user_id, repeat, prefix=""):
logger.info('%s%s: user %s joining project %s'
% (prefix, now, user_id, proj_id))
join(proj_id, user_id)
except PermissionDenied as e:
logger.info('Membership already exists')
except Exception as e:
logger.exception(e)
......@@ -160,6 +177,8 @@ def join_and_leave(proj_id, user_id, repeat, prefix=""):
logger.info('%s%s: user %s leaving project %s'
% (prefix, now, user_id, proj_id))
leave(proj_id, user_id)
except IOError as e:
logger.info('No such membership')
except Exception as e:
logger.exception(e)
......
......@@ -33,41 +33,54 @@
from datetime import datetime, timedelta
from astakos.im.models import AstakosUser
from django.core.exceptions import PermissionDenied
from astakos.im.models import AstakosUser, ProjectApplication
from astakos.im.functions import (join_project, leave_project,
submit_application, approve_application)
submit_application, approve_application,
get_user_by_id, qh_add_pending_app)
from snf_django.lib.db.transaction import commit_on_success_strict
@commit_on_success_strict()
def join(proj_id, user_id, ctx=None):
join_project(proj_id, user_id)
def join(proj_id, user_id):
join_project(proj_id, get_user_by_id(user_id))
@commit_on_success_strict()
def leave(proj_id, user_id, ctx=None):
leave_project(proj_id, user_id)
def leave(proj_id, user_id):
leave_project(proj_id, get_user_by_id(user_id))
@commit_on_success_strict()
def submit(name, user_id, prec, ctx=None):
def submit(name, user_id, prec):
try:
owner = AstakosUser.objects.get(id=user_id)
except AstakosUser.DoesNotExist:
raise AttributeError('user does not exist')
resource_policies = [{'service': 'cyclades',
'resource': 'network.private',
'uplimit': 5}]
precursor = (ProjectApplication.objects.get(id=prec)
if prec is not None
else None)
ok, limit = qh_add_pending_app(owner, precursor=precursor, dry_run=True)
if not ok:
raise PermissionDenied('Limit %s reached', limit)
resource_policies = [('cyclades.network.private', 5)]
data = {'owner': owner,
'name': name,
'precursor_application': prec,
'precursor_id': prec,
'end_date': datetime.now() + timedelta(days=1),
'member_join_policy': 1,
'member_leave_policy': 1,
'resource_policies': resource_policies,
'request_user': owner
}
app = submit_application(data, request_user=owner)
app = submit_application(**data)
return app.id
@commit_on_success_strict()
def approve(app_id, ctx=None):
def approve(app_id):
approve_application(app_id)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment