Debian Codegraph @

testing/failedplatform.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 from functools import reduce
6 from typing import Dict, List, Optional, Set
7
8
9 class FailedPlatform:
10 """
11 Stores all failures on different build types and test variants for a single platform.
12 This allows us to detect when a platform failed on all build types or all test variants to
13 generate a simpler skip-if condition.
14 """
15
16 def __init__(
17 self,
18 # Keys are build types, values are test variants for this build type
19 # Tests variants can be composite by using the "+" character
20 # eg: a11y_checks+swgl
21 # each build_type[test_variant] has a {'pass': x, 'fail': y}
22 # x and y represent number of times this was run in the last 30 days
23 # See examples in
24 # https://firefox-ci-tc.services.mozilla.com/api/index/v1/task/gecko.v2.mozilla-central.latest.source.test-info-all/artifacts/public%2Ftest-info-testrun-matrix.json
25 oop_permutations: Optional[
26 Dict[
27 str, # Build type
28 Dict[str, Dict[str, int]], # Test Variant # {'pass': x, 'fail': y}
29 ]
30 ],
31 ) -> None:
32 # Contains all test variants for each build type the task failed on
33 self.failures: Dict[str, Set[str]] = {}
34 self.oop_permutations = oop_permutations
35
36 def get_possible_build_types(self) -> List[str]:
37 return (
38 list(self.oop_permutations.keys())
39 if self.oop_permutations is not None
40 else []
41 )
42
43 def get_possible_test_variants(self, build_type: str) -> List[str]:
44 permutations = (
45 self.oop_permutations.get(build_type, {})
46 if self.oop_permutations is not None
47 else []
48 )
49 return [tv for tv in permutations]
50
51 def is_full_fail(self) -> bool:
52 """
53 Test if failed on every test variant of every build type
54 """
55 build_types = set(self.failures.keys())
56 possible_build_types = self.get_possible_build_types()
57 # If we do not have information on possible build types, do not consider it a full fail
58 # This avoids creating a too broad skip-if condition
59 if len(possible_build_types) == 0:
60 return False
61 return all(
62 [
63 bt in build_types and self.is_full_test_variants_fail(bt)
64 for bt in possible_build_types
65 ]
66 )
67
68 def is_full_test_variants_fail(self, build_type: str) -> bool:
69 """
70 Test if failed on every test variant of given build type
71 """
72 failed_variants = self.failures.get(build_type, [])
73 possible_test_variants = self.get_possible_test_variants(build_type)
74 # If we do not have information on possible test variants, do not consider it a full fail
75 # This avoids creating a too broad skip-if condition
76 if len(possible_test_variants) == 0:
77 return False
78 return all([t in failed_variants for t in possible_test_variants])
79
80 def get_negated_variant(self, test_variant: str):
81 if not test_variant.startswith("!"):
82 return "!" + test_variant
83 return test_variant.replace("!", "", 1)
84
85 def get_no_variant_conditions(self, and_str: str, build_type: str):
86 """
87 The no_variant test variant does not really exist and is only internal.
88 This function gets all available test variants for the given build type
89 and negates them to create a skip-if that handle tasks without test variants
90 """
91 variants = [
92 tv
93 for tv in self.get_possible_test_variants(build_type)
94 if tv != "no_variant"
95 ]
96 return_str = ""
97 for tv in variants:
98 return_str += and_str + self.get_negated_variant(tv)
99 return return_str
100
101 def get_test_variant_condition(
102 self, and_str: str, build_type: str, test_variant: str
103 ):
104 """
105 If the given test variant is part of another composite test variant, then add negations matching that composite
106 variant to prevent overlapping in skips.
107 eg: test variant "a11y_checks" is to be added while "a11y_checks+swgl" exists
108 the resulting condition will be "a11y_checks && !swgl"
109 """
110 all_test_variants_parts = [
111 tv.split("+")
112 for tv in self.get_possible_test_variants(build_type)
113 if tv not in ["no_variant", test_variant]
114 ]
115 test_variant_parts = test_variant.split("+")
116 # List of composite test variants more specific than the current one
117 matching_variants_parts = [
118 tv_parts
119 for tv_parts in all_test_variants_parts
120 if all(x in tv_parts for x in test_variant_parts)
121 ]
122 variants_to_negate = [
123 part
124 for tv_parts in matching_variants_parts
125 for part in tv_parts
126 if part not in test_variant_parts
127 ]
128
129 return_str = reduce((lambda x, y: x + and_str + y), test_variant_parts, "")
130 return_str = reduce(
131 (lambda x, y: x + and_str + self.get_negated_variant(y)),
132 variants_to_negate,
133 return_str,
134 )
135 return return_str
136
137 def get_test_variant_string(self, test_variant: str):
138 """
139 Some test variants strings need to be updated to match what is given in oop_permutations
140 """
141 if test_variant == "no-fission":
142 return "!fission"
143 if test_variant == "1proc":
144 return "!e10s"
145 return test_variant
146
147 def get_skip_string(self, and_str: str, build_type: str, test_variant: str) -> str:
148 if self.failures.get(build_type) is None:
149 self.failures[build_type] = {test_variant}
150 else:
151 self.failures[build_type].add(test_variant)
152
153 return_str = ""
154 # If every test variant of every build type failed, do not add anything
155 if not self.is_full_fail():
156 return_str += and_str + build_type
157 if not self.is_full_test_variants_fail(build_type):
158 if test_variant == "no_variant":
159 return_str += self.get_no_variant_conditions(and_str, build_type)
160 else:
161 return_str += self.get_test_variant_condition(
162 and_str, build_type, test_variant
163 )
164
165 return return_str