From 0313e643e30cfe202a100a0b69e7ff3c3875fff5 Mon Sep 17 00:00:00 2001 From: Thais Siqueira Date: Fri, 11 Mar 2016 10:57:34 -0300 Subject: Update locust test to run after xsrf token implementation. - Adds port 8089 to vagrant file to be able to run inside vm. - Update requests version to 2.4.1, the minimum required version by locust. - Adds the xsrf token on locust post request headers. Issue #213 --- service/test/load/locustfile.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'service/test/load') diff --git a/service/test/load/locustfile.py b/service/test/load/locustfile.py index 68e39433..23142eee 100644 --- a/service/test/load/locustfile.py +++ b/service/test/load/locustfile.py @@ -1,6 +1,5 @@ import os import json -import time from random import randint from leap.auth import SRPAuth @@ -11,7 +10,7 @@ from pixelated.resources.login_resource import LoginResource LEAP_PROVIDER = os.environ.get('LEAP_PROVIDER', 'dev.pixelated-project.org') LEAP_SERVER_HOST = os.environ.get('LEAP_SERVER_HOST', 'https://api.%s:4430' % LEAP_PROVIDER) LEAP_VERIFY_CERTIFICATE = os.environ.get('LEAP_VERIFY_CERTIFICATE', '~/.leap/ca.crt') -MAX_NUMBER_USER = os.environ.get('MAX_NUMBER_USER', 10000) +MAX_NUMBER_USER = os.environ.get('MAX_NUMBER_USER', 100) INVITES_FILENAME = os.environ.get('INVITES_FILENAME', '/tmp/invite_codes.txt') INVITES_ENABLED = os.environ.get('INVITES_ENABLED', 'true') == 'true' @@ -23,6 +22,10 @@ def load_invite_from_number(number): class UserBehavior(TaskSet): + def __init__(self, *args, **kwargs): + super(UserBehavior, self).__init__(*args, **kwargs) + self.cookies = {} + def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ self.login() @@ -40,9 +43,11 @@ class UserBehavior(TaskSet): def login(self): number = randint(1, int(MAX_NUMBER_USER)) username, password = self._get_or_create_user(number) - self.client.post("/%s" % LoginResource.BASE_URL, {"username": username, "password": password}) + response = self.client.post("/%s" % LoginResource.BASE_URL, {"username": username, "password": password}) + self.cookies.update(response.cookies.get_dict()) + resp = self.client.get("/") + self.cookies.update(resp.cookies.get_dict()) self.username = username - time.sleep(5) @task(1) def index(self): @@ -56,7 +61,9 @@ class UserBehavior(TaskSet): def send_mail(self): payload = {"tags": ["drafts"], "body": "some text lorem ipsum", "attachments": [], "ident": "", "header": {"to": ["%s@%s" % (self.username, LEAP_PROVIDER)], "cc": [], "bcc": [], "subject": "load testing"}} - with self.client.post('/mails', json=payload, catch_response=True) as email_response: + self.cookies.update(self.client.get("/").cookies.get_dict()) + print(self.cookies) + with self.client.post('/mails', json=payload, catch_response=True, cookies=self.cookies, headers={'X-Requested-With': 'XMLHttpRequest', 'X-XSRF-TOKEN':self.cookies['XSRF-TOKEN']}) as email_response: if email_response.status_code == 201: email_id = json.loads(email_response.content)['ident'] print email_id @@ -66,10 +73,10 @@ class UserBehavior(TaskSet): def delete_mail(self, ident): payload = {"idents": [ident]} - self.client.post('/mails/delete', json=payload) + self.client.post('/mails/delete', json=payload, cookies=self.cookies, headers={'X-Requested-With': 'XMLHttpRequest', 'X-XSRF-TOKEN':self.cookies['XSRF-TOKEN']}) class WebsiteUser(HttpLocust): task_set = UserBehavior - min_wait = 3000 + min_wait = 5000 max_wait = 15000 -- cgit v1.2.3 From 918ae543d94ea2c359c8608807fc251ac16ef1cc Mon Sep 17 00:00:00 2001 From: Thais Siqueira Date: Fri, 11 Mar 2016 14:46:06 -0300 Subject: Fixes pep8 errors and update requests to 2.9.1. Issue #213 --- service/test/load/locustfile.py | 63 ++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 14 deletions(-) (limited to 'service/test/load') diff --git a/service/test/load/locustfile.py b/service/test/load/locustfile.py index 23142eee..138f13db 100644 --- a/service/test/load/locustfile.py +++ b/service/test/load/locustfile.py @@ -8,8 +8,12 @@ from locust import HttpLocust, TaskSet, task from pixelated.resources.login_resource import LoginResource LEAP_PROVIDER = os.environ.get('LEAP_PROVIDER', 'dev.pixelated-project.org') -LEAP_SERVER_HOST = os.environ.get('LEAP_SERVER_HOST', 'https://api.%s:4430' % LEAP_PROVIDER) -LEAP_VERIFY_CERTIFICATE = os.environ.get('LEAP_VERIFY_CERTIFICATE', '~/.leap/ca.crt') +LEAP_SERVER_HOST = os.environ.get( + 'LEAP_SERVER_HOST', + 'https://api.%s:4430' % LEAP_PROVIDER) +LEAP_VERIFY_CERTIFICATE = os.environ.get( + 'LEAP_VERIFY_CERTIFICATE', + '~/.leap/ca.crt') MAX_NUMBER_USER = os.environ.get('MAX_NUMBER_USER', 100) INVITES_FILENAME = os.environ.get('INVITES_FILENAME', '/tmp/invite_codes.txt') INVITES_ENABLED = os.environ.get('INVITES_ENABLED', 'true') == 'true' @@ -23,27 +27,33 @@ def load_invite_from_number(number): class UserBehavior(TaskSet): def __init__(self, *args, **kwargs): - super(UserBehavior, self).__init__(*args, **kwargs) - self.cookies = {} + super(UserBehavior, self).__init__(*args, **kwargs) + self.cookies = {} def on_start(self): - """ on_start is called when a Locust start before any task is scheduled """ self.login() def _get_or_create_user(self, number): - srp_auth = SRPAuth(LEAP_SERVER_HOST, os.path.expanduser(LEAP_VERIFY_CERTIFICATE)) + srp_auth = SRPAuth( + LEAP_SERVER_HOST, + os.path.expanduser(LEAP_VERIFY_CERTIFICATE)) username, password = ('loadtest%d' % number), ('password_%d' % number) try: srp_auth.authenticate(username, password) except SRPAuthenticationError: - invite_code = load_invite_from_number(number) if INVITES_ENABLED else None + invite_code = None + if INVITES_ENABLED: + invite_code = load_invite_from_number(number) + srp_auth.register(username, password, invite_code) return username, password def login(self): number = randint(1, int(MAX_NUMBER_USER)) username, password = self._get_or_create_user(number) - response = self.client.post("/%s" % LoginResource.BASE_URL, {"username": username, "password": password}) + response = self.client.post( + "/%s" % LoginResource.BASE_URL, + {"username": username, "password": password}) self.cookies.update(response.cookies.get_dict()) resp = self.client.get("/") self.cookies.update(resp.cookies.get_dict()) @@ -59,21 +69,46 @@ class UserBehavior(TaskSet): @task(3) def send_mail(self): - payload = {"tags": ["drafts"], "body": "some text lorem ipsum", "attachments": [], "ident": "", - "header": {"to": ["%s@%s" % (self.username, LEAP_PROVIDER)], "cc": [], "bcc": [], "subject": "load testing"}} - self.cookies.update(self.client.get("/").cookies.get_dict()) + payload = { + "tags": ["drafts"], + "body": "some text lorem ipsum", + "attachments": [], + "ident": "", + "header": { + "to": ["%s@%s" % (self.username, LEAP_PROVIDER)], + "cc": [], + "bcc": [], + "subject": "load testing"}} + + self.cookies.update( + self.client.get("/").cookies.get_dict()) print(self.cookies) - with self.client.post('/mails', json=payload, catch_response=True, cookies=self.cookies, headers={'X-Requested-With': 'XMLHttpRequest', 'X-XSRF-TOKEN':self.cookies['XSRF-TOKEN']}) as email_response: + with self.client.post( + '/mails', + json=payload, + catch_response=True, + cookies=self.cookies, + headers={ + 'X-Requested-With': 'XMLHttpRequest', + 'X-XSRF-TOKEN': self.cookies['XSRF-TOKEN']}) as email_response: if email_response.status_code == 201: email_id = json.loads(email_response.content)['ident'] print email_id self.delete_mail(email_id) else: - email_response.failure('Error: email not Sent, status code: %s' % email_response.status_code) + email_response.failure( + 'Error: email not Sent, status code: %s' % ( + email_response.status_code)) def delete_mail(self, ident): payload = {"idents": [ident]} - self.client.post('/mails/delete', json=payload, cookies=self.cookies, headers={'X-Requested-With': 'XMLHttpRequest', 'X-XSRF-TOKEN':self.cookies['XSRF-TOKEN']}) + self.client.post( + '/mails/delete', + json=payload, + cookies=self.cookies, + headers={ + 'X-Requested-With': 'XMLHttpRequest', + 'X-XSRF-TOKEN': self.cookies['XSRF-TOKEN']}) class WebsiteUser(HttpLocust): -- cgit v1.2.3 From cf32471caf75b817b23339166002987726d3d6d8 Mon Sep 17 00:00:00 2001 From: Thais Siqueira Date: Fri, 11 Mar 2016 16:30:45 -0300 Subject: Sets SSL certifications to false. Issue #213 --- service/test/load/locustfile.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'service/test/load') diff --git a/service/test/load/locustfile.py b/service/test/load/locustfile.py index 138f13db..0c2ed518 100644 --- a/service/test/load/locustfile.py +++ b/service/test/load/locustfile.py @@ -53,19 +53,20 @@ class UserBehavior(TaskSet): username, password = self._get_or_create_user(number) response = self.client.post( "/%s" % LoginResource.BASE_URL, - {"username": username, "password": password}) + {"username": username, "password": password}, + verify=False) self.cookies.update(response.cookies.get_dict()) - resp = self.client.get("/") + resp = self.client.get("/", verify=False) self.cookies.update(resp.cookies.get_dict()) self.username = username @task(1) def index(self): - self.client.get("/") + self.client.get("/", verify=False) @task(2) def mail_box(self): - self.client.get("/mails?q=tag:'inbox'&p=1&w=25") + self.client.get("/mails?q=tag:'inbox'&p=1&w=25", verify=False) @task(3) def send_mail(self): @@ -81,7 +82,7 @@ class UserBehavior(TaskSet): "subject": "load testing"}} self.cookies.update( - self.client.get("/").cookies.get_dict()) + self.client.get("/", verify=False).cookies.get_dict()) print(self.cookies) with self.client.post( '/mails', @@ -106,6 +107,7 @@ class UserBehavior(TaskSet): '/mails/delete', json=payload, cookies=self.cookies, + verify=False, headers={ 'X-Requested-With': 'XMLHttpRequest', 'X-XSRF-TOKEN': self.cookies['XSRF-TOKEN']}) -- cgit v1.2.3