-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
locustfile.py
153 lines (124 loc) · 4.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import time
import random
from locust import HttpUser, events, task, run_single_user, tag, between
import greenlet
from locust.env import Environment
from django.conf import settings
# START DEBUG SETTINGS
# import logging
# from http.client import HTTPConnection
# HTTPConnection.debuglevel = 1
# logging.basicConfig()
# logging.getLogger().setLevel(logging.DEBUG)
# requests_log = logging.getLogger("requests.packages.urllib3")
# requests_log.setLevel(logging.DEBUG)
# requests_log.propagate = True
# END DEBUG SETTINGS
# Test account credentials and target printer_id
TEST_ADMIN_CREDENTIALS = ('test@test.com', 'test')
TEST_PRINTER_ID = 1
# Init global variables
IMAGE_CONTENT = None
AUTH_TOKEN = None
@events.test_start.add_listener
def get_image_content(environment: Environment, **kwargs):
with open(f'/backend/static_build/media/{settings.PICS_CONTAINER}/snapshots/1/latest_unrotated.jpg', 'rb') as file:
global IMAGE_CONTENT
IMAGE_CONTENT = file.read()
@events.test_start.add_listener
def set_printer_auth_token(environment: Environment, **kwargs):
"""Log in as admin user so we can query the web view APIs"""
print("Logging in admin")
WebUser.host = environment.host
admin_user = WebUser(environment=environment)
admin_user.login()
response = admin_user.client.get(f"/api/v1/printers/{TEST_PRINTER_ID}/")
response.raise_for_status()
global AUTH_TOKEN
AUTH_TOKEN = response.json()['auth_token']
print(f"Finished setting printer auth token: {AUTH_TOKEN}")
@events.test_start.add_listener
def on_test_start(environment: Environment, **kwargs):
print("Test started!")
@events.test_stop.add_listener
def on_test_stop(environment, **kwargs):
print("Test has ended!")
class PrinterUser(HttpUser):
auth_token = None
image_content = None
def on_start(self):
global AUTH_TOKEN
global IMAGE_CONTENT
self.auth_token = AUTH_TOKEN
self.image_content = IMAGE_CONTENT
if None in [self.auth_token, self.image_content]:
if self.auth_token is None:
print(f"AUTH_TOKEN: {self.auth_token}")
if self.image_content is None:
print(f"IMAGE_CONTENT: {IMAGE_CONTENT}")
raise greenlet.error("Must set a valid auth_token and image_content before proceeding")
@tag('octoprint')
@task(10)
def octo_pic(self):
self.client.post(
url='/api/v1/octo/pic/',
files={'pic': self.image_content},
headers={'Authorization': f'Token {self.auth_token}'},
cookies=None
).raise_for_status()
class WebUserBase(HttpUser):
auth_token = ''
environment: Environment
abstract = True
def on_start(self):
self.login()
def login(self):
retries = 0
while not self.client.cookies.get_dict().get('tsd_sessionid'):
response = self.client.get("/accounts/login/")
csrf_token = response.cookies.get('csrftoken', None)
if csrf_token is None:
raise ValueError("Could not get a valid csrftoken")
if retries:
print(f"Retrying login {retries}")
response = self.client.post(
url="/accounts/login/",
data={
'login': TEST_ADMIN_CREDENTIALS[0],
'password': TEST_ADMIN_CREDENTIALS[1],
'csrfmiddlewaretoken': csrf_token
},
allow_redirects=False
)
response.raise_for_status()
tsd_sessionid = response.cookies.get('tsd_sessionid', None)
self.client.cookies['tsd_sessionid'] = tsd_sessionid
retries += 1
if tsd_sessionid is None:
time.sleep(random.randint(1, 5))
if retries > 1:
print(f"Finally got a session after {retries} retries")
def get_internal_image_url(self, web_host='http://web:3334', printer_id=TEST_PRINTER_ID):
response = self.client.get(f'{web_host}/api/v1/printers/{printer_id}/')
response.raise_for_status()
resp_json: dict = response.json()
print(resp_json)
if 'pic' not in resp_json.keys():
return None
if 'img_url' not in resp_json['pic'].keys():
return None
return resp_json['pic']['img_url']
class WebUser(WebUserBase):
abstract = False
@tag('web')
@task(1)
def printers(self):
response = self.client.get('/api/v1/printers/')
response.raise_for_status()
@tag('web')
@task(2)
def printer(self):
response = self.client.get(f'/api/v1/printers/{TEST_PRINTER_ID}/')
response.raise_for_status()
if __name__ == "__main__":
run_single_user(PrinterUser)