-
Notifications
You must be signed in to change notification settings - Fork 74
/
validate_release.py
219 lines (181 loc) · 6.18 KB
/
validate_release.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
#!/usr/bin/python
#
# Copyright (C) Extensible Service Proxy Authors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
################################################################################
"""Look at the json files extracted and validate git commit."""
import collections
import fnmatch
import gflags
import json
import logging
import os
import prettytable
import sys
from datetime import date
COMMIT = 'headCommitHash'
DATE = 'date'
RUN_ID = 'runId'
STATUS = 'scriptStatus'
TEST_ID = 'testId'
# List of test that are expected to have run successfully at least once.
RUN_TESTS = [
'asan',
'check-files',
'flex-on-endpoints-on',
'gke-custom-http-bookstore',
'gke-tight-http-bookstore',
'gke-tight-http2-echo',
'gke-tight-http2-interop',
'gke-tight-https-bookstore',
'jenkins-perf-test-vm-esp',
'jenkins-post-submit-perf-test',
'long-run-test_flex-on-endpoints-on',
'long-run-test_gke-tight-http2-echo',
'long-run-test_gke-tight-http2-interop',
'long-run-test_gke-tight-https-bookstore',
'release',
]
FLAGS = gflags.FLAGS
gflags.DEFINE_string(
'commit_sha', '',
'The expected git commit sha'
)
gflags.DEFINE_string(
'path',
'',
'Path where all release information has been extracted'
)
gflags.DEFINE_boolean(
'detail',
False,
'Prints detailed output.'
)
class Error(Exception):
"""Base Error for this class"""
class JsonParsingError(Error):
"""Error for Json parsing."""
class CommitError(Error):
"""Commit found is not what was expected."""
def findFiles(path, pattern):
"""Find file in a directory with a given pattern."""
for root, dirs, files in os.walk(path):
for file in files:
if fnmatch.fnmatch(file, pattern):
yield os.path.join(root, file)
class ReleaseValidation(object):
"""Check Release."""
def __init__(self, reference_commit):
self._ref_commit = reference_commit
self._test_info = collections.defaultdict(dict)
self._successful_test = set()
def AddJsonData(self, json_file):
"""Parse Json file and add test info."""
logging.info('Adding information from %s' % json_file)
try:
with open(json_file) as log:
result = json.load(log)
except:
raise JsonParsingError('Unable to parse %s' % json_file)
status = result.get(STATUS, '')
test_id = result.get(TEST_ID, '')
run_id = result.get(RUN_ID, '')
commit = result.get(COMMIT, '')
unix_timestamp = result.get(DATE, '')
if '' in [commit, run_id, status, test_id]:
raise JsonParsingError('Unable to parse %s' % json_file)
if commit != self._ref_commit:
raise CommitError('%s != %s' % (commit, self._ref_commit))
self._test_info[test_id][run_id] = {
DATE: date.fromtimestamp(float(unix_timestamp)),
STATUS: int(status)
}
if int(status) == 0:
self._successful_test.add(test_id)
def ExtractTestInfoFromPath(self, path):
"""Extracts test information for a path."""
for json_file in findFiles(path, '*.json'):
try:
self.AddJsonData(json_file)
except Error:
logging.error('Could not parse %s', json_file)
def PrintAllTests(self):
"""Prints all test information."""
if not self._test_info:
return
table = prettytable.PrettyTable(
[TEST_ID, RUN_ID, DATE, STATUS])
for test_id, run in self._test_info.iteritems():
for run_id, values in run.iteritems():
date = values[DATE]
status = values[STATUS]
table.add_row([test_id, run_id, date, status])
table.align = 'l'
table.sortby = TEST_ID
print table
def PrintSummary(self):
"""Prints tests summary."""
if not self._test_info:
return
table = prettytable.PrettyTable(
[TEST_ID, 'Success', 'Failure'])
for test_id, run in self._test_info.iteritems():
failures, successes = 0, 0
for values in run.values():
status = values[STATUS]
if status == 0:
successes += 1
else:
failures +=1
table.add_row([test_id, successes, failures])
table.align = 'l'
table.sortby = TEST_ID
print table
def ValidateRelease(self):
missing_tests = set(RUN_TESTS).difference(self._successful_test)
if missing_tests:
logging.error(
'The following tests haven\'t been run successfully once: \n - %s',
'\n - '.join(sorted(missing_tests)))
return len(missing_tests)
def main(unused_argv):
releaseVal = ReleaseValidation(FLAGS.commit_sha)
releaseVal.ExtractTestInfoFromPath(FLAGS.path)
if FLAGS.detail:
releaseVal.PrintAllTests()
else:
releaseVal.PrintSummary()
sys.exit(releaseVal.ValidateRelease())
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout, level=logging.ERROR)
try:
argv = FLAGS(sys.argv) # Parse flags
except:
sys.exit('%s\nUsage: %s ARGS\n%s' % (e, sys.argv[0], FLAGS))
if not FLAGS.commit_sha:
sys.exit('Flag commit_sha is required')
if not FLAGS.path:
sys.exit('Flag path required.')
main(argv)