-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_tests.py
139 lines (106 loc) · 4.16 KB
/
start_tests.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
'''
Mini adhoc test library for all the types of requests we have
'''
import httplib
import json
import os
import fnmatch
import time
import requests
import xml.dom.minidom
from dateutil import parser
HOST = "localhost:8401"
API_URL = "/ows"
JOBURL = '''
http://localhost:8400/ows?service=wps&request=execute&
version=1.0.0&identifier=listJobs&RawDataOutput=job_list&json
'''
WPSHOST = "http://localhost:8400"
passed_reqs = 0
failed_reqs = 0
dash = '=' * 90
def do_request(xml_location):
"""HTTP XML Post request"""
global passed_reqs, failed_reqs
request = open(xml_location, "r").read()
webservice = httplib.HTTP(HOST)
webservice.putrequest("POST", API_URL)
webservice.putheader("Host", HOST)
webservice.putheader("User-Agent", "Python post")
webservice.putheader("Content-type", "text/xml; charset=\"UTF-8\"")
webservice.putheader("Content-length", "%d" % len(request))
webservice.endheaders()
webservice.send(request)
statuscode, statusmessage, header = webservice.getreply()
result = webservice.getfile().read()
if("download" in xml_location):
if statuscode != 200:
failed_reqs += 1
print '{:<70s}{:^20s}'.format("Requesting %s"%xml_location, 'FAILED')
else:
# Check where output is generated and loop until request is finished
#resultxml = xml.dom.minidom.parseString(result)
# Get list of jobs
resp = requests.get(url=JOBURL)
jsresp = resp.json()
latest = parser.parse("1970-05-09T14:41:41.288970+00:00")
curr_job = None
for process in jsresp:
for job in xrange (len(jsresp[process])):
cur_job = jsresp[process][job]
curtime = parser.parse(cur_job["created"])
if latest < curtime:
latest = curtime
curr_job = cur_job["url"]
cnt=0
max_retry=3
# Try to get info on job
while cnt < max_retry:
try:
response = requests.get(WPSHOST+curr_job)
xmlresp = xml.dom.minidom.parseString(response.content)
#import pdb; pdb.set_trace()
#wps:ProcessStarted
#print response
if xmlresp.getElementsByTagName('wps:ProcessSucceeded'):
passed_reqs += 1
print '{:<70s}{:^20s}'.format("Requesting %s"%xml_location, 'PASSED')
cnt = max_retry
elif xmlresp.getElementsByTagName('wps:ProcessFailed'):
failed_reqs += 1
print '{:<70s}{:^20s}'.format("Requesting %s"%xml_location, 'FAILED')
cnt = max_retry
# return True
else:
pass
#raise Exception("Job not finished")
except requests.exceptions.RequestException as e:
time.sleep(5)
cnt += 1
if cnt >= max_retry:
raise e
else:
result = webservice.getfile().read()
if statuscode != 200:
failed_reqs += 1
print '{:<70s}{:^20s}'.format("Requesting %s"%xml_location, 'FAILED')
else:
# TODO further tests with received data, unpack and check for content
passed_reqs += 1
print '{:<70s}{:^20s}'.format("Requesting %s"%xml_location, 'PASSED')
#print statuscode, statusmessage, header
#print resultxml.toprettyxml()
# with open("output-%s" % xml_location, "w") as xmlfile:
# xmlfile.write(resultxml.toprettyxml())
os.chdir(".")
print "Starting tests:"
print dash
testfiles = []
for root, dirnames, filenames in os.walk('.'):
for filename in fnmatch.filter(filenames, '*.xml'):
testfiles.append(os.path.join(root, filename))
for file in testfiles:
do_request(file)
print (dash)
print "PASSED requests %s/%s"%(passed_reqs,len(testfiles))
print "FAILED requests %s/%s"%(failed_reqs,len(testfiles))