-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocustfile.py
134 lines (115 loc) · 4.3 KB
/
locustfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import sys
import random
from lxml import etree
from locust import HttpLocust, TaskSet, task
IMG_DIR = os.path.join(os.path.dirname(__file__), 'locust_images')
SITE_ID = 'test-site'
USER = 'admin'
PASS = 'admin'
ALL_IMAGES = []
VIEW_URLS = ('', '/view', '/@@images/image/thumb', '/@@images/image/mini',
'/@@images/image/preview', '/@@images/image/large',
'/@@images/image/icon')
def select_image():
files = os.listdir(IMG_DIR)
fname = random.choice(files)
path = os.path.join(IMG_DIR, fname)
return fname, open(path, 'r')
class ImageCreator(TaskSet):
created_images = []
def login(self):
with self.client.post("/" + SITE_ID + "/login_form", {
"__ac_name": USER,
"__ac_password": PASS,
"came_from": "",
"next": "",
"target": "",
"form.submitted": "1",
"js_enabled": "0",
"cookies_enabled": "1",
"login_name": USER,
"pwd_empty": "0",
"submit": "Log in"},
catch_response=True) as resp:
if 'You are now logged in' not in resp.content:
resp.failure('Could not login')
def logout(self):
self.client.get('/' + SITE_ID + '/logout')
def _add_image(self, img_id, title, img_file):
with self.client.post(
"/" + SITE_ID +
"/portal_factory/Image/image.{}/atct_edit".format(
title
), data={
'id': img_id,
'title': title,
'description': 'Some Description',
'description_text_format': 'text/plain',
'form.button.save': 'Save',
'form.submitted': '1',
'last_referer': '/' + SITE_ID + '/'
}, files={'image_file': img_file},
catch_response=True) as resp:
if resp.status_code != 200:
resp.raise_for_status()
if 'Changes saved.' not in resp.content:
html = etree.HTML(resp.content)
errors = html.xpath('//div[@class="fieldErrorBox"]/text()')
errors = '; '.join(e for e in errors if e)
resp.failure(errors)
return
return resp.url.split('/')[-2]
@task
def add_image(self):
self.login()
number = random.randint(1, sys.maxint)
fname, img_file = select_image()
img_id = '{}-{}.jpg'.format(fname, number)
title = fname
with img_file as img:
new_id = self._add_image(img_id, title, img)
if new_id:
ALL_IMAGES.append(new_id)
self.logout()
self.interrupt(True)
class SiteBrowser(TaskSet):
tasks = {ImageCreator: 1}
login = ImageCreator.login.im_func
logout = ImageCreator.logout.im_func
@task(10)
def view_image(self):
if ALL_IMAGES:
img_id = random.choice(ALL_IMAGES)
more = random.choice(VIEW_URLS)
self.client.get('/' + SITE_ID + '/' + img_id + more)
else:
self.client.get("/" + SITE_ID + '/')
@task(1)
def index(self):
self.client.get("/" + SITE_ID + '/')
def create_site(self):
with self.client.get('/' + SITE_ID + '/', catch_response=True) as resp:
if resp.status_code == 200:
return
self.client.auth = (USER, PASS)
with self.client.post('/@@plone-addsite', data={
'site_id': SITE_ID,
'title': 'Test Site',
'default_language': 'en',
'setup_content:boolean': 'true',
'form.submitted:boolean': 'True',
'extension_ids:list': 'plonetheme.classic:default',
'extension_ids:list': 'plonetheme.sunburst:default',
'submit': 'Create Plone Site'}, catch_response=True) as resp:
if resp.status_code != 200:
resp.raise_for_status()
if 'Welcome to Plone' not in resp.content:
resp.failure('Site not created')
self.client.auth = False
def on_start(self):
self.create_site()
class CMSUser(HttpLocust):
task_set = SiteBrowser
min_wait = 10000
max_wait = 30000