-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
193 lines (173 loc) · 6.65 KB
/
app.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import pathlib
import json
import argparse
import xml.etree.ElementTree as ET
from utils.display import print_message, Colors
def main():
"""
The main context of the application.
Purpose: Converts LoadRunner correlation rules to Blazemeter Correlation Recorder Plugin format
@Input: LoadRunner Correlation file (*.cor)
@Output: Blazemeter Correlation Recorder Rules Template (*.json)
Author: NaveenKumar Namachivayam | QAInsights.com
"""
try:
parser = argparse.ArgumentParser(
description='Convert LoadRunner Correlation Rules to JMeter'
)
required_named = parser.add_argument_group('mandatory arguments')
required_named.add_argument(
"-f",
"--file",
dest="corfile",
help="Add Correlation file path"
)
args = parser.parse_args()
cor = args.corfile
# File Rename from *.cor to *.xml
lr_cor_file = cor
base = os.path.splitext(lr_cor_file)[0]
lr_cor_file_renamed = base + ".xml"
os.rename(lr_cor_file, lr_cor_file_renamed)
# Get Inputs from user
get_id = str(input("Enter id - [default: 1.0]: ") or "myrules")
get_description = str(input("Enter description - [default: Inception version for my rules]: ") or "1.0")
get_version = str(input("Enter the version - [default: 1.0]: ") or "1.0")
get_components = str(input(
"Enter the components - [default: com.blazemeter.jmeter.correlation.siebel.SiebelCounterCorrelationReplacement]:")
or "com.blazemeter.jmeter.correlation.siebel.SiebelCounterCorrelationReplacement"
)
get_response_filters = str(input("Enter the version - [default: text/html]: ") or "text/html")
# Root element of cor file
root = ET.parse(lr_cor_file_renamed).getroot()
# Iterate through each group
# Loops only one time
for group in root.iter('Group'):
group_name = group.attrib['Name']
break
# Boiler Plate
create_jmeter_rule(group_name,get_id, get_description, get_version, get_components, get_response_filters)
# Create Rules
fetch_rules(root, group_name,get_id, get_version)
# Create Repository File
create_repository(get_id, get_version, group_name)
# Reverting the file name
lr_cor_file = lr_cor_file_renamed
base = os.path.splitext(lr_cor_file)[0]
lr_cor_file_renamed = base + ".cor"
os.rename(lr_cor_file, lr_cor_file_renamed)
except FileNotFoundError as e:
print_message(e, message_color="red")
def create_repository(get_id, get_version, group_name):
"""
Creating a repository for the rules
"""
data = {
get_id : {
"versions": [
get_version
]
}
}
repository_file_name = group_name + '/' + get_id + '-repository.json'
os.makedirs(os.path.dirname(repository_file_name), exist_ok=True)
with open(repository_file_name,"w+") as f:
json.dump(data, f)
f.close()
message = "Repository file has been created: " + repository_file_name
print_message(message, message_color="green")
#print_message(f"{message}","green")
def create_jmeter_rule(group_name,get_id, get_description, get_version, get_components, get_response_filters):
"""
Create JSON file structure
@param group_name: the name of rules group
@param get_id: the id of the rules
@param get_description: the description of the rules
@param get_version: the version of the rules
@param get_components: the components of the rules
@param get_response_filters: the response filter for the rules
@return json file structure
"""
data = {
'id': get_id,
'description': get_description,
'version': get_version,
"components": get_components,
"responseFilters": get_response_filters,
"rules": [
],
"repositoryId": "local"
}
json_file_name = group_name + '/' + get_id + '-' + get_version + '-template' + '.json'
os.makedirs(os.path.dirname(json_file_name))
# Create JSON File
with open(json_file_name,"w+") as f:
json.dump(data, f)
f.close()
#print("Rules has been created: ", json_file_name)
message = "Rules has been created: " + json_file_name
print_message(message, message_color="green")
return
def fetch_rules(root, group_name, get_id, get_version):
"""
@param root: the root element in cor file
@param group_name: the name of rules group
@param get_id: the id of the rules
@param get_version: the version of the rules
@return each rule, left boundary, right boundary from the cor file
"""
#print(f"Group name is {group_name}")
for rule in root.iter('Rule'):
# Rule Name
rule_name = rule.attrib['Name']
# Left Boundary
corr_extractor_lb = rule.attrib['LeftBoundText']
# Right Boundary
corr_extractor_rb = rule.attrib['RightBoundText']
# Constant Regex
regex = "(.+?)"
# Complete Regex
full_regex = corr_extractor_lb + regex + corr_extractor_rb
json_file_name = group_name + '/' + get_id + '-' + get_version + '-template' + '.json'
# Adding each rule to the json
add_rules_to_json(json_file_name, rule_name, full_regex)
#print(full_regex)
return
def add_rules_to_json(json_file_name, rule_name, full_regex):
"""
@param json_file_name: the json file name
@param rule_name: the rule name
@param full_regex: the full regex
@retuen append rules to json
"""
with open(json_file_name) as json_file:
data = json.load(json_file)
# Adding Rules Ref names
rules_data = {
'referenceName': rule_name,
'correlationExtractor' : {
'type' : 'com.blazemeter.jmeter.correlation.core.extractors.RegexCorrelationExtractor',
'regex' : full_regex,
'matchNr': 1
},
'correlationReplacement' : {
'type': 'com.blazemeter.jmeter.correlation.core.replacements.RegexCorrelationReplacement',
'regex' : ''
}
}
temp = data['rules']
temp.append(rules_data)
# Appends each rule to the JSON
write_json(data, json_file_name)
return
def write_json(data, json_file_name):
"""
@param data: boilerplate for the rules
@param json_file_name: json file name
@return complete json file
"""
with open(json_file_name,'w') as f:
json.dump(data, f, indent=4)
if __name__ == "__main__":
main()