forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake_checker.py
183 lines (152 loc) · 5.76 KB
/
flake_checker.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
# Copyright 2020 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS-IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Python execution for checking whether the tests output is flaky."""
from __future__ import annotations
import datetime
import os
from core import python_utils
import requests
FLAKE_CHECK_AND_REPORT_URL = (
'https://oppia-e2e-test-results-logger.herokuapp.com'
'/check-flake-and-report')
PASS_REPORT_URL = (
'https://oppia-e2e-test-results-logger.herokuapp.com'
'/report-pass')
REPORT_API_KEY = '7Ccp062JVjv9LUYwnLMqcm5Eu5gYqqhpl3zQmcO3cDQ'
CI_INFO = {
'githubActions': {
'env': {
'identifier': 'GITHUB_ACTIONS',
'user_info': 'GITHUB_ACTOR',
'branch': 'GITHUB_REF',
'build_url_template_vars': ['GITHUB_REPOSITORY', 'GITHUB_RUN_ID'],
},
'build_url_template': 'https://github.com/%s/actions/runs/%s',
},
'circleCI': {
'env': {
'identifier': 'CIRCLECI',
'user_info': 'CIRCLE_USERNAME',
'branch': 'CIRCLE_BRANCH',
'build_url_template_vars': ['CIRCLE_BUILD_URL']
},
'build_url_template': '%s',
}
}
REQUEST_EXCEPTIONS = (
requests.RequestException, requests.ConnectionError,
requests.HTTPError, requests.TooManyRedirects, requests.Timeout)
def _print_color_message(message):
"""Prints the given message in red color.
Args:
message: str. The success message to print.
"""
# \033[91m is the ANSI escape sequences for green color.
python_utils.PRINT('\033[92m' + message + '\033[0m\n')
def check_if_on_ci():
"""Check if the script is running on a CI server.
Returns: bool. Whether we are running on a CI server.
"""
for info in CI_INFO.values():
ci_identifier = info['env']['identifier']
if os.getenv(ci_identifier):
return True
return False
def _get_build_info():
"""Returns the info related to the build container."""
build_info = {}
for info in CI_INFO.values():
ci_env = info['env']
if not os.getenv(ci_env['identifier']):
continue
template_values = []
for template_var in ci_env['build_url_template_vars']:
value = os.getenv(template_var)
if value is None:
raise RuntimeError(
'Expected environment variable %s missing' %
template_var)
template_values.append(value)
build_url = info['build_url_template'] % tuple(template_values)
timestamp = datetime.datetime.utcnow().isoformat() + '+00:00'
build_info['username'] = os.getenv(ci_env['user_info'])
build_info['build_url'] = build_url
build_info['timestamp'] = timestamp
build_info['branch'] = os.getenv(ci_env['branch'])
return build_info
raise Exception('Unknown build environment.')
def report_pass(suite_name):
"""Report a passing test to the logging server."""
metadata = _get_build_info()
payload = {
'suite': suite_name,
'metadata': metadata,
}
try:
requests.post(
PASS_REPORT_URL, json=payload,
allow_redirects=False,
headers={'report_key': REPORT_API_KEY})
except REQUEST_EXCEPTIONS as e:
_print_color_message((
'Failed to contact E2E test logging server at %s.'
'Please report to E2E team in case server is down.'
'Exception: %s') % (PASS_REPORT_URL, e))
_print_color_message(
'Reported pass to E2E logging server at {}.'.format(
PASS_REPORT_URL))
def is_test_output_flaky(output_lines, suite_name):
"""Returns whether the test output matches any flaky test log."""
build_info = _get_build_info()
payload = {
'suite': suite_name,
'output_lines': output_lines,
'metadata': build_info,
}
response = None
try:
response = requests.post(
FLAKE_CHECK_AND_REPORT_URL, json=payload,
allow_redirects=False,
headers={'report_key': REPORT_API_KEY})
except REQUEST_EXCEPTIONS as e:
_print_color_message((
'Failed to contact E2E test logging server at %s.'
'Please report to E2E team in case server is down.'
'Exception: %s') % (FLAKE_CHECK_AND_REPORT_URL, e))
return False
if not response.ok:
_print_color_message('Failed request with response code: %s (%s)' % (
response.status_code, response.reason))
return False
report = {}
try:
report = response.json()
except ValueError as e:
_print_color_message('Unable to convert json response: %s' % e)
if 'log' in report:
log_str = '\n'.join(report['log'])
_print_color_message(
'Logs from test result logging server:\n %s' % log_str)
flaky = report['result'] if 'result' in report else False
_print_color_message(
'E2E logging server says test flaky: {}.'.format(flaky))
if flaky:
flake = report['flake']
_print_color_message('Flake Detected:')
_print_color_message(' Suite: %s' % flake['suite'])
_print_color_message(' Test: %s' % flake['test'])
_print_color_message(
' Error Message: %s' % flake['flake_id'])
return flaky