-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtclient.py
executable file
·230 lines (196 loc) · 7.03 KB
/
tclient.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/local/bin/python3
#
# talk to remote client of eaitest
import json
import requests
import configparser
# hack a doodle
products = {
"Hotmail": "MS Outlook.com",
"Exchange": "MS Exchange Server (hosted)",
"Yandex": "Yandex Mail"
}
class TClient:
def __init__(self, config="config.txt", debug=False):
"""
pass config file or dict if it's already been read
"""
if type(config) is configparser.ConfigParser:
self.config = config
else:
self.config = configparser.ConfigParser()
self.config.read_file(open(config, "r"))
if debug:
self.debug = True
self.dbconfig = self.config['DebugDatabase']
else:
self.debug = False
self.dbconfig = self.config['Database']
def deprod(self, product):
"""
translate product hack
"""
if product in products:
return products[product]
return product
def getresult(self, product, testid):
"""
returns tid, pid, ttid, status, comments
"""
product = self.deprod(product)
d = { "apikey": self.dbconfig.get('apikey'), "request": "getresult", "product": product, "testid": testid }
try:
r = requests.post(self.dbconfig.get('url'), json=d)
except requests.exceptions.ConnectionError as err:
print("cannot contact server", err)
return None
if r.status_code != 200:
print("failed code ",r.status_code)
return None
js = r.json()
if js and js['answer'] == "yes":
return js['result']
else:
return None
def setresult(self, product, testid, status, comments=None):
"""
returns (True, None) or (False, reason)
"""
if status.upper() == "N/A":
status = "NA"
if status.upper() not in ("NA","PASS","FAIL","PENDING"):
return (False, "Bad status")
product = self.deprod(product)
d = { "apikey": self.dbconfig.get('apikey'), "request": "setresult", "product": product, "testid": testid,
"status": status, "comments": comments }
try:
r = requests.post(self.dbconfig.get('url'), json=d)
except requests.exceptions.ConnectionError as err:
print("cannot contact server", err)
return None
if r.status_code != 200:
print("failed code ",r.status_code)
return (False, f"Request failed {r.status_code}")
js = r.json()
if js and js['answer'] == "yes":
return js['result']
else:
return (False, "No answer")
def getresults(self, product, testtype, tester=None, done=False):
"""
returns list of [(tid, status, comments, testid, summary, description, action, expected, class, phase), ...]
tester defaults to current user
"""
product = self.deprod(product)
d = { "apikey": self.dbconfig.get('apikey'), "request": "getresults",
"product": product, "testtype": testtype, "done": done, "ttid": None }
if tester:
d['ttid'] = tester
try:
r = requests.post(self.dbconfig.get('url'), json=d)
except requests.exceptions.ConnectionError as err:
print("cannot contact server", err)
return None
if r.status_code != 200:
print("failed code ",r.status_code)
return None
js = r.json()
if js and js['answer'] == 'yes':
return js['result']
else:
return None
def gettasks(self, product=None, testtype=None):
"""
get tasks for a product
"""
if not product:
return None
product = self.deprod(product)
d = { "apikey": self.dbconfig.get('apikey'), "request": "tasks",
"product": product, "testtype": testtype }
try:
r = requests.post(self.dbconfig.get('url'), json=d)
except requests.exceptions.ConnectionError as err:
print("cannot contact server", err)
return None
if r.status_code != 200:
print("failed code ",r.status_code)
return None
js = r.json()
if js and js['answer'] == 'yes':
return js['result']
else:
return None
def getproducts(self):
"""
get list of products with active tasks
"""
d = { "apikey": self.dbconfig.get('apikey'), "request": "products" }
try:
r = requests.post(self.dbconfig.get('url'), json=d)
except requests.exceptions.ConnectionError as err:
print("cannot contact server", err)
return None
if r.status_code != 200:
print("failed code ",r.status_code)
return None
js = r.json()
if js and js['answer'] == 'yes':
return js['result']
else:
return None
if __name__=="__main__":
import argparse
parser = argparse.ArgumentParser(description='EAI test client')
parser.add_argument('-d', action='store_true', help="Use debug server")
parser.add_argument("-p", help="Product name")
parser.add_argument("-t", help="Test type")
parser.add_argument("-i", help="Test ID")
parser.add_argument("-s", help="Status")
parser.add_argument("-c", help="Comment")
parser.add_argument("-a", action='store_true', help="tAsks")
parser.add_argument('-q', action='store_true', help="Get list of products")
parser.add_argument('-r', action='store_true', help="Get one result")
parser.add_argument('-u', action='store_true', help="Set one result")
parser.add_argument('-z', help="Tester ID")
parser.add_argument('-l', action='store_true', help="get list of results")
parser.add_argument('-f', action='store_true', help="finished tests in list of results")
args = parser.parse_args()
t = TClient("config.txt", debug=args.d)
if args.q:
r = t.getproducts()
for l in r:
print(l)
if args.l or args.f:
if not args.p:
print("Need -p product")
elif not args.t:
print("Need -t testtype")
else:
r = t.getresults(args.p, args.t, args.z, done=args.f)
for l in r:
print(l)
if args.s:
if not args.p:
print("Need -p product")
elif not args.i:
print("Need -i TestID")
elif not args.s:
print("Need -s Status")
else:
r = t.setresult(args.p, args.i, args.s, args.c)
print(r)
if args.r:
if not args.p:
print("Need -p product")
elif not args.i:
print("Need -i TestID")
print("Need -s Status")
else:
r = t.getresult(args.p, args.i)
print(r)
if args.a:
if not args.p:
print("Need -p product")
r = t.gettasks(product=args.p, testtype=args.t)
print(r)