-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloadTests.py
151 lines (114 loc) · 6.74 KB
/
loadTests.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
from locust import HttpUser, TaskSet, between, task, events
import os
import json
import requests
#import time
# Change these variables if you wish to personalize the script
# These variables hold the codes of the errors that occur and their frequencies
# getFailures = {}
# postFailures = {}
# class UserBehavior(TaskSet):
# def __init__(self):
# super().__init__()
# self.host = "http://localhost"
# self.totalUsers = 0
# # maxUsers = int(os.environ["USERS"])
# self.maxUsers = 10
# parameters={'response_type': 'code','grant_type': 'password', 'client_id': 'portal', 'client_secret': 'portal', 'username': 'admin', 'password': 'admin'}
# response = self.client.post(host+"/tenant/oauth/token", headers={"Content-Type": "application/json"}, data=parameters)
# self.adminToken=response.json["access_token"]
# @task
# def get(self):
# if self.totalUsers == self.maxUsers:
# return
# self.totalUsers+=1
# #print("Total Purchase Attempts: "+str(UserBehavior.totalPurchaseAttempts))
# #create user
# createUser={"group": "user","username": "user"+self.totalUsers,"password": "test","role": "TENANT","storage": 100,"memory": 100,"vcpu": 100}
# response = self.client.post(host+"/lcm/vs", headers={"Authorization": "Bearer " + self.adminToken, "Content-Type": "application/json"}, data=json.dumps(createUser))
# #login user
# parameters={'response_type': 'code','grant_type': 'password', 'client_id': 'portal', 'client_secret': 'portal', 'username': 'user'+self.totalUsers, 'password': 'test'}
# response = self.client.post(host+"/tenant/oauth/token", headers={"Content-Type": "application/json"}, data=parameters)
# token=response.json["access_token"]
# #create VSI
# vsiData={"name":"test","vsdId":"60ca601412002d67ff294e67","domainId":"ITAV","vsiId":self.totalUsers}
# response = self.client.post(host+"/lcm/vs", headers={"Authorization": "Bearer " + token, "Content-Type": "application/json"}, data=json.dumps(vsiData))
# response_code = int(str(response).split("[")[1].split("]")[0])
# while response_code == 409: #"201" not in str(response):
# attempts = attempts-1
# if attempts == 0:
# #raise InterruptedError
# if response_code in postFailures.keys():
# postFailures[response_code] = postFailures[response_code] + 1
# else:
# postFailures[response_code] = 1
# print(postFailures)
# break
# response = self.client.post(r[len(r)-1][0], headers={"Authorization": "Token " + token, "Content-Type": "application/json"}, data=body)
# response_code = int(str(response).split("[")[1].split("]")[0])
# if response_code > 299:
# if response_code in postFailures.keys():
# postFailures[response_code] = postFailures[response_code] + 1
# else:
# postFailures[response_code] = 1
# print(postFailures)
# response = self.client.get(r[i][j], headers={"Authorization": "Token " + token})
# response_code = int(str(response).split("[")[1].split("]")[0])
# while response_code == 409: #"200" not in str(response):
# attempts = attempts-1
# if attempts == 0:
# #raise InterruptedError
# if response_code in getFailures.keys():
# getFailures[response_code] = getFailures[response_code] + 1
# else:
# getFailures[response_code] = 1
# print(getFailures)
# break
# response = self.client.get(r[i][j], headers={"Authorization": "Token " + token})
# response_code = int(str(response).split("[")[1].split("]")[0])
# if response_code > 299:
# if response_code in getFailures.keys():
# getFailures[response_code] = getFailures[response_code] + 1
# else:
# getFailures[response_code] = 1
# print(getFailures)
ids=[i for i in range(1000,0,-1)]
parameters={'response_type': 'code','grant_type': 'password', 'client_id': 'portal', 'client_secret': 'portal', 'username': 'admin', 'password': 'admin'}
response = requests.post("http://localhost/tenant/oauth/token", params=parameters)
adminToken=response.json()["access_token"]
class WebsiteUser(HttpUser):
wait_time = between(1,2)
def on_start(self):
self.identifier=ids.pop()
print(self.identifier)
createUser={"role":"TENANT","group": "user","username": "user_"+str(self.identifier),"password": "test","role": "TENANT","storage": 100,"memory": 100,"vcpu": 100}
response = self.client.post("/tenant/tenant", headers={"Authorization": "Bearer " + adminToken, "Content-Type": "application/json"}, data=json.dumps(createUser))
if response.status_code!=200:
print(response.text)
#login user
parameters={'response_type': 'code','grant_type': 'password', 'client_id': 'portal', 'client_secret': 'portal', 'username': 'user_'+str(self.identifier), 'password': 'test'}
response = self.client.post("/tenant/oauth/token", headers={"Content-Type": "application/json"}, params=parameters)
if response.status_code!=200:
print(response.text)
self.token=response.json()["access_token"]
#create VSI
vsiData={"name":"test","description":"test","vsdId":"60ca601412002d67ff294e67","domainId":"ITAV","vsiId":str(self.identifier)}
response = self.client.post("/lcm/vs", headers={"Authorization": "Bearer " + self.token, "Content-Type": "application/json"}, data=json.dumps(vsiData))
if response.status_code!=200:
print(response.text)
def on_stop(self):
print(self.token)
parameters={'force': True}
response = requests.delete("http://localhost/lcm/vs/"+str(self.identifier), headers={"Authorization": "Bearer " + self.token, "Content-Type": "application/json"}, params=parameters)
if response.status_code!=200:
print(response.text)
response = requests.delete("http://localhost/tenant/tenant/user_"+str(self.identifier), headers={"Authorization": "Bearer " + adminToken, "Content-Type": "application/json"})
if response.status_code!=200:
print(response.text)
@task
def get(self):
# if self.token:
# response = self.client.get("/lcm/vs", headers={"Authorization": "Bearer " + self.token, "Content-Type": "application/json"})
# if response.status_code!=200:
# print(response.text)
return