Skip to content

Commit b5ee540

Browse files
committed
bugfix: handling multiple ops
1 parent 5ecdedc commit b5ee540

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

template/scripts/run-tests

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
9595
parser.add_argument(
9696
"--operator",
9797
help="Patch operator version in release.yaml. Format <operator>=<version>",
98-
nargs="*",
98+
action="append",
9999
type=cli_parse_operator_args,
100100
default=[],
101101
)
102102

103103
parser.add_argument(
104104
"--skip-operator",
105105
help="Skip given operator(s) when installing a release.",
106-
nargs="*",
106+
action="append",
107107
default=[],
108108
)
109109

@@ -145,7 +145,7 @@ def cli_parse_operator_args(args: str) -> tuple[str, str]:
145145
f"Invalid operator argument: {args}. Must be in format <operator>=<version>"
146146
)
147147
op, version = args.split("=", maxsplit=1)
148-
return (op, version)
148+
return op, version
149149

150150

151151
def cli_log_level(cli_arg: str) -> int:
@@ -186,12 +186,13 @@ def have_requirements() -> None:
186186

187187
@contextlib.contextmanager
188188
def release_file(
189-
operators: list[tuple[str, str]] = [],
190-
skip_ops: list[str] = [],
189+
operators: list[tuple[str, str]], skip_ops: list[str]
191190
) -> collections.abc.Generator[str, None, None]:
192-
"""Patch release.yaml with operator versions if needed.
191+
"""Generate a (possibly modified) copy of the release.yaml file.
193192
194-
If no --operator is set, the default release file is used.
193+
Operator versions passed as --operator take precedence over the release.yaml contents.
194+
195+
Operators passed as --skip-operator are excluded from the resulting release.yaml contents.
195196
196197
If an invalid operator name is provided (i.e. one that doesn't exist in the
197198
original release file), a TestRunnerException is raised.
@@ -202,51 +203,60 @@ def release_file(
202203

203204
def _patch():
204205
release_file = os.path.join("tests", "release.yaml")
205-
# Make a copy so we can mutate it without affecting the original
206-
ops_copy = operators.copy()
206+
# A marker to validate that all ops were patched
207207
patched_release = []
208208
with open(release_file, "r") as f:
209+
patched_ops = []
209210
patch_version = ""
210211
for line in f:
211212
if patch_version:
212213
line = re.sub(":.+$", f": {patch_version}", line)
213214
patch_version = ""
214215
else:
215-
for op, version in ops_copy:
216+
for op, version in operators:
216217
if op in line:
217218
patch_version = version
218-
ops_copy.remove((op, version)) # found an operator to patch
219+
patched_ops.append(op)
219220
break
220221
patched_release.append(line.rstrip("\n"))
221222

223+
# Sanity test that cli didn't contain garbage that is silently discarded
224+
ops_not_patched = set([op for op, _ in operators]) - set(patched_ops)
225+
if ops_not_patched:
226+
logging.error(
227+
f"Patched operators [{', '.join(ops_not_patched)}] not found in {release_file}"
228+
)
229+
raise TestRunnerException()
230+
222231
# Filter out skip operators
223-
skipped_release = []
232+
release_contents = []
233+
skip = False
234+
valid_skip_ops = []
224235
for line in patched_release:
225-
skip = False
226236
if skip:
227237
skip = False
228-
else:
229-
if any((op in line for op in skip_ops)):
238+
continue
239+
for op in skip_ops:
240+
if op in line:
230241
skip = True
242+
valid_skip_ops.append(op)
231243
break
232-
else:
233-
skipped_release.append(line)
234-
patched_release = skipped_release
235-
236-
if ops_copy:
237-
# Some --operator args were not found in the release file. This is
238-
# most likely a typo and CI pipelines should terminate early in such
239-
# cases.
244+
else:
245+
release_contents.append(line)
246+
# Sanity test that cli didn't contain garbage that is silently discarded
247+
ops_not_skipped = set(skip_ops) - set(valid_skip_ops)
248+
if ops_not_skipped:
240249
logging.error(
241-
f"Operators {', '.join([op for op, _ in ops_copy])} not found in {release_file}"
250+
f"Skipped operators [{', '.join(ops_not_skipped)}] not found in {release_file}"
242251
)
243252
raise TestRunnerException()
253+
244254
with tempfile.NamedTemporaryFile(
245255
mode="w",
246256
delete=False,
247257
prefix="patched",
248258
) as f:
249-
pcontents = "\n".join(patched_release)
259+
pcontents = "\n".join(release_contents)
250260
logging.debug(f"Writing patched release to {f.name}: {pcontents}\n")
251261
f.write(pcontents)
252262
return f.name

0 commit comments

Comments
 (0)