xref: /aosp_15_r20/external/cronet/testing/scripts/skia_gold_infra/finch_skia_gold_utils.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1# Copyright 2022 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import sys
7
8from skia_gold_common.skia_gold_properties import SkiaGoldProperties
9from skia_gold_common.skia_gold_session_manager import SkiaGoldSessionManager
10
11# This is the corpus used by skia gold to identify the data set.
12# We are not using the same corpus as the rest of the skia gold chromium tests.
13# This corpus is a dedicated one for finch smoke tests.
14CORPUS = 'finch-smoke-tests'
15
16
17class FinchSkiaGoldUtil:
18  def __init__(self, temp_dir, args):
19    self._skia_gold_properties = SkiaGoldProperties(args)
20    self._skia_gold_session_manager = SkiaGoldSessionManager(
21        temp_dir, self._skia_gold_properties)
22    self._skia_gold_session = self._GetSkiaGoldSession()
23    self._retry_without_patch = False
24    if args.isolated_script_test_filter:
25      self._retry_without_patch = True
26
27  @property
28  def SkiaGoldProperties(self):
29    return self._skia_gold_properties
30
31  @property
32  def SkiaGoldSessionManager(self):
33    return self._skia_gold_session_manager
34
35  @property
36  def SkiaGoldSession(self):
37    return self._skia_gold_session
38
39  @property
40  def IsTryjobRun(self):
41    return self._skia_gold_properties.IsTryjobRun()
42
43  @property
44  def IsRetryWithoutPatch(self):
45    return self._retry_without_patch
46
47  def _GetSkiaGoldSession(self):
48    """Returns a SkiaGoldSession from the given session_manager.
49
50    Returns:
51      a SkiaGoldSession object.
52    """
53    key_input = {}
54    key_input['platform'] = _get_platform()
55    return self._skia_gold_session_manager.GetSkiaGoldSession(
56        key_input, CORPUS)
57
58
59def _get_platform():
60  """Returns the host platform.
61
62  Returns:
63    One of 'linux', 'win' and 'mac'.
64  """
65  if sys.platform == 'win32' or sys.platform == 'cygwin':
66    return 'win'
67  if sys.platform.startswith('linux'):
68    return 'linux'
69  if sys.platform == 'darwin':
70    return 'mac'
71
72  raise RuntimeError(
73    'Unsupported platform: %s. Only Linux (linux*) and Mac (darwin) and '
74    'Windows (win32 or cygwin) are supported' % sys.platform)
75
76
77def _output_local_diff_files(skia_gold_session, image_name):
78  """Logs the local diff image files from the given SkiaGoldSession
79
80  Args:
81    skia_gold_session: A SkiaGoldSession instance to pull files
82        from.
83    image_name: A string containing the name of the image/test that was
84        compared.
85
86  Returns:
87    None
88  """
89  given_file = skia_gold_session.GetGivenImageLink(image_name)
90  closest_file = skia_gold_session.GetClosestImageLink(image_name)
91  diff_file = skia_gold_session.GetDiffImageLink(image_name)
92  failure_message = 'Unable to retrieve link'
93  logging.error('Generated image: %s', given_file or failure_message)
94  logging.error('Closest image: %s', closest_file or failure_message)
95  logging.error('Diff image: %s', diff_file or failure_message)
96
97
98def log_skia_gold_status_code(skia_gold_session, image_name, status, error):
99  """Checks the skia gold status code and logs more detailed message
100
101  Args:
102    skia_gold_session: A SkiaGoldSession object.
103    image_name: The name of the image file.
104    status: A StatusCodes returned from RunComparison.
105    error: An error message describing the status if not successful
106
107  Returns:
108    A link to a triage page if there are images to triage, otherwise None
109  """
110  triage_link = None
111  status_codes = skia_gold_session.StatusCodes
112  if status in (status_codes.AUTH_FAILURE, status_codes.INIT_FAILURE):
113    logging.error('Gold failed with code %d output %s', status, error)
114  elif status == status_codes.COMPARISON_FAILURE_REMOTE:
115    _, triage_link = skia_gold_session.GetTriageLinks(image_name)
116    if not triage_link:
117      logging.error('Failed to get triage link for %s, raw output: %s',
118                    image_name, error)
119      logging.error('Reason for no triage link: %s',
120                    skia_gold_session.GetTriageLinkOmissionReason(image_name))
121    else:
122      logging.warning('triage link: %s', triage_link)
123  elif status == status_codes.COMPARISON_FAILURE_LOCAL:
124    logging.error('Local comparison failed. Local diff files:')
125    _output_local_diff_files(skia_gold_session, image_name)
126  elif status == status_codes.LOCAL_DIFF_FAILURE:
127    logging.error(
128        'Local comparison failed and an error occurred during diff '
129        'generation: %s', error)
130    # There might be some files, so try outputting them.
131    logging.error('Local diff files:')
132    _output_local_diff_files(skia_gold_session, image_name)
133  else:
134    logging.error(
135        'Given unhandled SkiaGoldSession StatusCode %s with error %s', status,
136        error)
137  return triage_link
138