blob: 2b28d5b4ac72548f60fafb367437681a0fefd304 [file] [edit]
#!/usr/bin/env vpython3
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""The purpose of this CLI tool is to help you manage changes to screenshots in
tests across multiple platforms.
For more information, see test/README.md.
If you've made changes that impact the screenshots, you'll need to update them
for all supported platforms. Assuming you've committed your changes and
uploaded the CL, you'll need to trigger a dry run in Gerrit or execute the
command:
\x1b[32m git cl try \x1b[0m
After waiting for the dry run to complete, you can execute the command:
\x1b[32m update_goldens.py \x1b[0m
Any failing test will generate updated screenshots, which will be downloaded and
applied to your local change. Inspect the status of your working copy for any
such screenshot updates. If you have new screenshots and they look as expected,
add, commit, and upload the changes. If you repeat the steps above without
making any additional changes to the code, you should not have any more
screenshot tests failing.
"""
import argparse
import json
import os
import re
import shutil
import ssl
import subprocess
import sys
import time
import urllib.request
ssl._create_default_https_context = ssl._create_unverified_context # pylint: disable=protected-access
class ProjectConfig:
def __init__(self,
name='devtools-frontend',
gs_root='gs://devtools-frontend-screenshots',
builder_prefix='devtools_screenshot',
platforms=None,
ignore_failed_builders=False):
self.name = name
self.gs_root = gs_root
self.gs_folder = self.gs_root + '/screenshots'
self.builder_prefix = builder_prefix
self.platforms = platforms or ['linux', 'mac', 'win']
platforms_re = '|'.join(self.platforms)
self.builder_pattern = f'{self.builder_prefix}_({platforms_re})_(rel|dbg)'
self.ignore_failed_builders = ignore_failed_builders
TOOLS_DIR = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.normpath(os.path.join(TOOLS_DIR, '..', '..'))
DEPOT_TOOLS_DIR = os.path.join(BASE_DIR, 'third_party', 'depot_tools')
VPYTHON = os.path.join(DEPOT_TOOLS_DIR, 'vpython3')
GOLDENS_DIR = os.path.join(BASE_DIR, 'test', 'goldens')
WARNING_BUILDERS_STILL_RUNNING = (
'Patchset %s has builders that are still running.\n'
'Builders in progress:\n %s\n')
WARNING_BUILDERS_FAILED = 'Patchset %s has builders that failed:\n %s\n'
WARNING_BUILDERS_MISSING = (
'Patchset %s does not have screenshot tests for all platforms.\n'
'Only these builders found:\n %s')
WARNING_GCS_CONNECTIVITY = (
'Ups! "gcloud storage" seems to not work for you right '
'now.\nThis is either a connectivity problem or a configuration issue.\n'
'Make sure you are logged in with your Google account and you are included '
'in the devtools-dev@google.com group.\n'
'Try running "gcloud config set core/project v8-infra" command.\n')
WARNING_GIT_DIRTY = (
'Before attempting to apply screenshot patches, please '
'make sure your local repo is clean.\nFolder %s seems to contain '
'un-committed changes.' % GOLDENS_DIR)
WARNING_RESULTS_EXIST = (
'Screenshot builders were already triggered for the current patch!')
INFO_BUILDERS_TRIGGERED = (
'Screenshot builders were triggered for the current patchset.')
INFO_PATCHES_APPLIED = (
'Patches containing screenshot updates were applied to your local repo.\n'
' To quickly see what is new just run "git status".')
def main(project_config, *args):
parser = build_parser()
options = parser.parse_args(*args)
update(project_config, options)
def build_parser():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter, epilog=__doc__)
parser.add_argument(
'--wait-sec',
type=int,
help='Wait and retry update every specified number of seconds. '
'Minimum value is 30s to avoid overwhelming Gerrit.')
parser.set_defaults(func=update)
parser.add_argument('--verbose',
action='store_true',
help='Show more debugging info')
parser.add_argument('--cl',
type=int,
help='Gerrit CL number to inspect (optional).')
parser.add_argument('--patchset',
type=int,
help='Patchset number to inspect (optional).')
parser.add_argument('--ignore-failed',
help='Deprecated. Not used by this tool.')
parser.add_argument('--retry', help='Deprecated. Not used by this tool.')
return parser
def update(project_config, options):
test_clean_git()
wait_sec = options.wait_sec
if wait_sec:
wait_sec = max(wait_sec, 30)
query_rdb_for_screenshots(project_config,
options.patchset,
wait_sec,
options.ignore_failed,
options.retry,
options.verbose,
cl=options.cl)
def query_rdb_for_screenshots(project_config,
patchset,
wait_sec,
ignore_failed,
retry,
verbose,
cl=None):
"""Download and apply the patches from the builders."""
results = screenshot_results(project_config, patchset, cl=cl)
check_not_empty(results)
check_all_platforms(project_config, results)
retry, should_wait = check_all_success(project_config, results, wait_sec,
ignore_failed, retry)
if retry:
trigger_screenshots(project_config, retry)
sys.exit(0)
if not should_wait:
download_generated_imgs(results)
print(INFO_PATCHES_APPLIED)
sys.exit(0)
print(f'Waiting {wait_sec} seconds ...')
time.sleep(wait_sec)
query_rdb_for_screenshots(project_config,
patchset,
wait_sec,
ignore_failed,
retry,
verbose,
cl=cl)
def download_generated_imgs(try_results):
for _, try_result in try_results.items():
if try_result['status'] == 'FAILURE':
unexpected_results = get_unexpected_results(try_result['id'])
artifacts_by_result = get_unexpected_artifacts(try_result['id'])
for result in unexpected_results.get('testResults', []):
screenshot_path = get_screenshot_path(result)
if not screenshot_path:
continue
for artifact in artifacts_by_result.get(result['name'], []):
if artifact['artifactId'] in ['actual_image', 'generated']:
download_individual_screenshot(screenshot_path,
artifact['fetchUrl'])
def get_unexpected_results(invocation_suffix):
"""Queries all unexpected test results with full metadata (readMask='*')."""
invocation_id = f'build-{invocation_suffix}'
all_results = []
page_token = None
while True:
payload = {
'invocations': [f'invocations/{invocation_id}'],
'predicate': {
'excludeExonerated': True,
'expectancy': 'VARIANTS_WITH_ONLY_UNEXPECTED_RESULTS',
},
'readMask': '*',
'pageSize': 1000,
}
if page_token:
payload['pageToken'] = page_token
resp = rdb_rpc('QueryTestResults', payload)
all_results.extend(resp.get('testResults', []))
page_token = resp.get('nextPageToken')
if not page_token:
break
return {'testResults': all_results}
def get_unexpected_artifacts(invocation_suffix):
"""Queries all unexpected test result artifacts in a build, indexed by parent result name."""
invocation_id = f'build-{invocation_suffix}'
artifacts_by_result = {}
page_token = None
while True:
payload = {
'invocations': [f'invocations/{invocation_id}'],
'predicate': {
'testResultPredicate': {
'expectancy': 'VARIANTS_WITH_ONLY_UNEXPECTED_RESULTS',
}
},
'pageSize': 1000,
}
if page_token:
payload['pageToken'] = page_token
resp = rdb_rpc('QueryArtifacts', payload)
for artifact in resp.get('artifacts', []):
name = artifact.get('name', '')
if '/artifacts/' in name:
parent_result_name = name.split('/artifacts/')[0]
artifacts_by_result.setdefault(parent_result_name,
[]).append(artifact)
page_token = resp.get('nextPageToken')
if not page_token:
break
return artifacts_by_result
def get_result_with_tags(name):
return rdb_rpc('GetTestResult', {'name': name})
def list_artifacts(name):
return rdb_rpc('ListArtifacts', {
'parent': name,
})
def download_individual_screenshot(screenshot_path, fetch_url):
with urllib.request.urlopen(fetch_url) as response:
parent_dir = os.path.dirname(screenshot_path)
if parent_dir:
os.makedirs(parent_dir, exist_ok=True)
with open(screenshot_path, 'w+b') as screenshot_file:
shutil.copyfileobj(response, screenshot_file)
def rdb_rpc(service, request_payload):
results_command = ['rdb', 'rpc', 'luci.resultdb.v1.ResultDB', service]
with subprocess.Popen(results_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True) as proc:
stdout, stderr = proc.communicate(json.dumps(request_payload))
if proc.returncode != 0:
if 'interactive login is required' in stderr:
print('Authentication is required to fetch test metadata.\n'
'Please run:\n\trdb auth-login\nand try again')
else:
print(f'rdb rpc {service} failed with: {stderr}')
sys.exit(1)
return json.loads(stdout)
def get_screenshot_path(individual_result):
for tag in individual_result.get('tags', []):
if tag['key'] == 'run_phase' and tag['value'] != 'default':
return None
if tag['key'] == 'screenshot_path':
return tag['value'].replace('\\', '/')
return None
def get_help(parser, subparsers):
def _help(options):
if options.name:
subparsers.choices[options.name].print_help()
else:
parser.print_help()
return _help
def check_results_exist(project_config, ignore_triggered):
"""Verify the existence of previously triggered builders."""
results = screenshot_results(project_config)
if results:
print(WARNING_RESULTS_EXIST)
if not ignore_triggered:
sys.exit(1)
print('Ignoring ...')
def trigger_screenshots(project_config, verbose, builders=None):
"""Trigger screenshot builders for the current patch."""
if not builders:
builders = [
f'{project_config.builder_prefix}_{p}_rel'
for p in project_config.platforms
]
try_command = f'git cl try -B {project_config.name}/try ' + ' '.join(
[f'-b {b}' for b in builders])
run_command(try_command.split(), verbose)
print(INFO_BUILDERS_TRIGGERED)
def test_clean_git():
"""Test if the local repo can accept a patch with screenshots."""
stdout = subprocess.check_output(
['git', 'status', '--porcelain', '--', GOLDENS_DIR])
if stdout:
print(stdout.decode('utf-8'))
print(WARNING_GIT_DIRTY)
sys.exit(0)
def screenshot_results(project_config, patchset=None, cl=None):
"""Select only screenshot builders results."""
results = read_try_results(patchset, cl=cl)
screenshots = filter_screenshots(project_config, results)
return filter_last_results(screenshots)
def read_try_results(patchset=None, cl=None):
"""Collect results from existing try-jobs via git cl try-results."""
results_command = ['git', 'cl', 'try-results', '--json=-']
if cl:
results_command.append(f'--issue={cl}')
if patchset:
results_command.extend(['-p', str(patchset)])
stdout = subprocess.check_output(results_command)
if stdout:
try:
return json.loads(stdout)
except Exception as exc: # pylint: disable=broad-except
print(f'Unable to parse try-results output. \n{str(exc)}\n')
print('Usually this goes away if you set SKIP_GCE_AUTH_FOR_GIT=1.')
sys.exit(1)
return []
def filter_screenshots(project_config, results):
"""Remove results coming from other builders."""
sht_results = []
for r in results:
if re.fullmatch(project_config.builder_pattern,
r['builder']['builder']):
sht_results.append(r)
return sht_results
def filter_last_results(results):
"""Select only the last results for each builder on the current patch."""
last_results = {}
for result in results:
builder = result['builder']['builder']
abbreviated_result = abbreviate_result(result)
if not is_newer(abbreviated_result, last_results, builder):
continue
last_results[builder] = abbreviated_result
return last_results
def abbreviate_result(result):
cl, patch = find_buildset(result)
build_id = int(result['id'])
status = result['status']
summary_markdown = result.get('summaryMarkdown', '')
return {
'id': build_id,
'status': status,
'cl': cl,
'patch': patch,
'summaryMarkdown': summary_markdown,
}
def is_newer(abbreviated_result, last_results, builder):
maybe_older = last_results.get(builder)
if maybe_older:
if abbreviated_result['patch'] != maybe_older['patch']:
return abbreviated_result['patch'] > maybe_older['patch']
if maybe_older['id'] > abbreviated_result['id']:
return False
return True
def find_buildset(result):
"""Select the CL number and the patch number from the result."""
for tag in result.get('tags', []):
if tag['key'] == 'buildset':
components = tag['value'].split('/')
return int(components[-2]), int(components[-1])
raise RuntimeError('Cannot find tag buildset in a try-job result')
def check_not_empty(results):
if results:
return
print('No screenshot test results found! '
'Make sure to run CQ against your change first.')
sys.exit(1)
def check_all_platforms(project_config, results):
"""Warn if any platform was not covered."""
patchset = list(results.values())[0]['patch']
if len(results) < len(project_config.platforms):
print(WARNING_BUILDERS_MISSING %
(patchset, '\n '.join(results.keys())))
def check_all_success(project_config, results, wait_sec, ignore_failed, retry):
"""Verify and react to the presence of in progress or failed builds."""
in_progress, failed = find_exceptions(results)
if in_progress:
warn_on_exceptions(results, in_progress,
WARNING_BUILDERS_STILL_RUNNING)
return builders_in_progress(wait_sec)
if failed and not project_config.ignore_failed_builders:
warn_on_exceptions(results, failed, WARNING_BUILDERS_FAILED)
return builders_failed(results, failed, ignore_failed, retry)
return ([], False)
def find_exceptions(results):
"""Find builds in progress and failed builds."""
assert results
in_progress = []
failed = []
for builder, result in results.items():
if result['status'] in ['STARTED', 'SCHEDULED']:
in_progress.append(builder)
elif result['status'] != 'SUCCESS':
failed.append(builder)
return in_progress, failed
def warn_on_exceptions(results, exceptions, warning):
patchset = list(results.values())[0]['patch']
status_lines = builder_status(results, exceptions)
print(warning % (patchset, status_lines))
def builders_in_progress(wait_sec):
if wait_sec:
return ([], True)
sys.exit(1)
def builders_failed(results, exceptions, ignore_failed, retry):
if ignore_failed:
for f in exceptions:
results.pop(f)
return ([], False)
if retry:
return (exceptions, False)
sys.exit(1)
def builder_status(results, builders):
return '\n '.join(f'{b}: {results[b]["status"]}' for b in builders)
def run_command(command, verbose, message=None):
"""Run command and deal with return code and output from the subprocess."""
with subprocess.Popen(command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) as process:
stdout, stderr = process.communicate()
if verbose:
print(stdout.decode('utf-8'))
if process.returncode != 0:
print(stderr.decode('utf-8'))
if message:
print(message)
else:
print('Ups! Something went wrong.')
print('Try --verbose to debug.')
sys.exit(1)
if __name__ == '__main__':
main(
ProjectConfig(platforms=['linux'],
builder_prefix='dtf',
ignore_failed_builders=True), sys.argv[1:])