forked from sasvirco/julep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulep.py
153 lines (121 loc) · 5.38 KB
/
julep.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
#!/usr/bin/python
import getpass
import json
import logging
import sys
import argparse
import yaml
import time
import re
import hpoo as oo
from junit_xml import TestSuite, TestCase
def main():
levels = {
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}
parser = argparse.ArgumentParser(description='HP Operation Orchestration testing tool')
parser.add_argument('--askpass', action='store_true', help='Ignore password from config file and prompt for it.')
parser.add_argument('--configfile', default='julep.yaml', help='Configfile with hpoo flow testcases')
parser.add_argument('--loglevel', default='INFO', help='FATAL, ERROR, WARNING, INFO, DEBUG')
parser.add_argument('--logfile', default='julep.log', help='Logfile to store messages (Default: julep.log)')
parser.add_argument('--timeout', default=3600, type=int,
help='The time to wait for flow completion in seconds (Default: 3600 - 1hour)')
parser.add_argument('--heartbeat', default=120, type=int,
help='Operation Orchestration polling interval (Default: 120 secs)')
parser.add_argument('--quiet', action='store_true', help='Do not print logging to stdout')
parser.add_argument('--trustcert', action='store_true', help='Trust self-signed certs')
parser.add_argument('--configfmt', default='yaml', help="Configfile format - json or yaml. Default json.")
parser.add_argument('--delay', default=15, type=int, help="Delay in seconds to wait between starting flows")
parser.add_argument('--junitoutput', default='julepout.xml',
help="The location of the junit xml output. Default julepout.xml")
args = parser.parse_args()
loglevel = levels.get(args.loglevel, logging.NOTSET)
logging.basicConfig(
level=args.loglevel,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename=args.logfile,
filemode='a')
root = logging.getLogger()
if args.quiet is False:
console = logging.StreamHandler()
console.setLevel(args.loglevel)
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
console.setFormatter(formatter)
root.addHandler(console)
logging.info("Want some blacksea julep?")
config = parse_config(args.configfile, args.configfmt)
config['general']['trustcert'] = args.trustcert
if args.askpass is True:
config['general']['password'] = getpass.getpass()
testcases = {
'running': [],
'finished': [],
}
for flow in config['flows']:
test = oo.hpoo(config['general'], flow)
name = test.run()
if args.delay is not None:
logging.info("sleeping between runs for %s secs", args.delay)
time.sleep(args.delay)
testcases['running'].append(test)
root.debug(testcases)
timeout = int(args.timeout)
heartbeat = int(args.heartbeat)
while timeout >= heartbeat:
logging.info('Tracking testcases in running state')
for test in testcases['running']:
if test.get_status() == 'RUNNING':
continue
else:
testcases['finished'].append(test)
testcases['running'].remove(test)
logging.debug(testcases)
if len(testcases['running']) == 0:
root.info("Running testcases list is zero, we are done")
break
logging.info('Waiting %s seconds for next heartbeat', str(heartbeat))
timeout = timeout - heartbeat
time.sleep(heartbeat)
testresults = []
logging.info("Generating junit xml output")
for test in testcases['finished']:
result = test.collect()
flow = test.get_flow()
testname = flow['name'] + " " + test.get_run_id()
logging.info("Asserts for " + flow['name'])
errors = []
for assertType in flow['assert']:
for key in flow['assert'][assertType]:
if key not in result[assertType]:
errors.append("Failed to assert " + assertType + ", " + key + " doesn't exists in results.")
continue
if not re.search(flow['assert'][assertType][key], result[assertType][key]):
errors.append("Failed to assert " + assertType + ", " + key + " doesn't match")
if errors:
tc = TestCase(testname, flow['uuid'], '', errors)
tc.add_failure_info(errors)
logging.info("Adding failed test")
testresults.append(tc)
else:
logging.info("Adding succesfull test")
duration = int(result['executionSummary']['endTime'] - result['executionSummary']['startTime'])
tc = TestCase(testname, flow['uuid'], duration / 1000.0, result['executionSummary']['resultStatusType'], '')
testresults.append(tc)
ts = TestSuite('ootests', testresults)
with open(args.junitoutput, 'w') as f:
TestSuite.to_file(f, [ts], prettyprint=True)
logging.info("Writing output to " + args.junitoutput)
def parse_config(configfile, fmt):
f = open(configfile, 'r')
txt = f.read()
logging.debug(txt)
if fmt == "json":
return json.loads(txt)
return yaml.load(txt)
if __name__ == "__main__":
main()