Skip to content

Commit 60a000c

Browse files
fix(hack): validate selective install graphs before output
Reject dangling dependencies and cycles in both output modes before emitting an install selection. Make graph regressions exercise valid suite controls and whole-inventory failures. Assisted-By: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 45b3608 commit 60a000c

2 files changed

Lines changed: 183 additions & 44 deletions

File tree

‎hack/select-install.sh‎

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,70 @@ deps_of() {
159159
echo "$FORWARD" | awk -v s="$1" -F'\t' '$1==s {print $2}'
160160
}
161161

162+
# Validate the complete PackageSource graph, including branches unrelated to
163+
# the requested suites. Any emitted closure or complement describes this
164+
# inventory as a whole, so accepting a dangling edge or cycle elsewhere would
165+
# make the result depend on an invalid input snapshot.
166+
validate_graph() {
167+
# One awk pass checks every reference and walks every node once. The former
168+
# shell cycle walk restarted from each owner and the dangling check spawned a
169+
# grep per edge, both too expensive once ordinary output shared validation.
170+
if graph_errors=$(printf '%s\n' "$FORWARD" | awk -v known_nodes="$NODES" -F'\t' '
171+
function visit(node, i, next_node) {
172+
if (state[node] == 1) {
173+
cycle = node
174+
return 1
175+
}
176+
if (state[node] == 2)
177+
return 0
178+
state[node] = 1
179+
for (i = 1; i <= degree[node]; i++) {
180+
next_node = edge[node, i]
181+
if (visit(next_node))
182+
return 1
183+
}
184+
state[node] = 2
185+
return 0
186+
}
187+
BEGIN {
188+
count = split(known_nodes, known_list, "\n")
189+
for (i = 1; i <= count; i++)
190+
known[known_list[i]] = 1
191+
}
192+
NF == 2 {
193+
if (references[$2] == "")
194+
references[$2] = $1
195+
else
196+
references[$2] = references[$2] "," $1
197+
edge[$1, ++degree[$1]] = $2
198+
nodes[$1] = 1
199+
nodes[$2] = 1
200+
}
201+
END {
202+
rc = 0
203+
for (dependency in references) {
204+
if (!(dependency in known)) {
205+
print "select-install: dangling dependency \047" dependency \
206+
"\047 (referenced by: " references[dependency] ") has no PackageSource"
207+
rc = 1
208+
}
209+
}
210+
for (node in nodes) {
211+
if (visit(node)) {
212+
print "select-install: dependency cycle detected involving \047" cycle "\047"
213+
rc = 1
214+
break
215+
}
216+
}
217+
exit rc
218+
}
219+
'); then
220+
return 0
221+
fi
222+
printf '%s\n' "$graph_errors" >&2
223+
return 1
224+
}
225+
162226
# all Chainsaw suite names under a suites-dir (dirs holding chainsaw-test.yaml),
163227
# discovered exactly like select-e2e.sh.
164228
discover_suites() {
@@ -176,40 +240,9 @@ discover_suites() {
176240
if [ "$MODE" = "validate" ]; then
177241
rc=0
178242

179-
# 1. Reachability: every dependsOn target must be a real PackageSource.
180-
echo "$FORWARD" | awk -F'\t' '{print $2}' | sort -u | while IFS= read -r dep; do
181-
[ -z "$dep" ] && continue
182-
if ! echo "$NODES" | grep -Fxq "$dep"; then
183-
owners="$(echo "$FORWARD" | awk -v x="$dep" -F'\t' '$2==x {print $1}' | paste -sd ',' -)"
184-
echo "select-install: dangling dependency '$dep' (referenced by: $owners) has no PackageSource" >&2
185-
echo dangling
186-
fi
187-
done | grep -q dangling && rc=1
243+
validate_graph || rc=1
188244

189-
# 2. Cycles: from every node with outgoing edges, walk forward; if the walk
190-
# returns to its start, the graph has a cycle through it.
191-
owners_list="$(echo "$FORWARD" | awk -F'\t' '{print $1}' | sort -u)"
192-
for start in $owners_list; do
193-
visited=""
194-
frontier="$(deps_of "$start")"
195-
while [ -n "$frontier" ]; do
196-
next=""
197-
for f in $frontier; do
198-
if [ "$f" = "$start" ]; then
199-
echo "select-install: dependency cycle detected involving '$start'" >&2
200-
rc=1
201-
next=""
202-
break
203-
fi
204-
case " $visited " in *" $f "*) continue ;; esac
205-
visited="$visited $f"
206-
next="$next $(deps_of "$f")"
207-
done
208-
frontier="$next"
209-
done
210-
done
211-
212-
# 3. Suite mapping: every Chainsaw suite dir must resolve to a real source.
245+
# Suite mapping: every Chainsaw suite dir must resolve to a real source.
213246
# The suite universe is auto-discovered while suite_to_source() is
214247
# hand-maintained, so this is the part that actually drifts. A missing
215248
# suites dir is itself a failure — otherwise a moved/misspelled path would
@@ -276,6 +309,11 @@ if [ -z "$seeds" ]; then
276309
exit 0
277310
fi
278311

312+
# Do not emit an answer from an invalid inventory. Suite-directory validation
313+
# remains exclusive to --validate because ordinary selection must not depend on
314+
# scanning an unrelated test tree.
315+
validate_graph || exit 1
316+
279317
# The standard E2E install has unconditional runtime requirements which are not
280318
# owned by any selected application. Seed those direct requirements here rather
281319
# than inventing PackageSource edges: graph edges also drive select-e2e.sh's
@@ -351,14 +389,21 @@ cozystack.gateway-application
351389
cozystack.info-application
352390
cozystack.kamaji
353391
cozystack.kubevirt"
354-
missing=""
392+
missing_sources=""
393+
missing_reachable=""
355394
for required in $derived_baseline; do
356-
if ! echo "$closure" | grep -Fxq "$required"; then
357-
missing="${missing:+$missing }$required"
395+
if ! echo "$NODES" | grep -Fxq "$required"; then
396+
missing_sources="${missing_sources:+$missing_sources }$required"
397+
elif ! echo "$closure" | grep -Fxq "$required"; then
398+
missing_reachable="${missing_reachable:+$missing_reachable }$required"
358399
fi
359400
done
360-
if [ -n "$missing" ]; then
361-
echo "select-install: error: runtime baseline dependency closure is missing: $missing" >&2
401+
if [ -n "$missing_sources" ]; then
402+
echo "select-install: error: required runtime PackageSource(s) not found in $SOURCES_DIR: $missing_sources" >&2
403+
exit 1
404+
fi
405+
if [ -n "$missing_reachable" ]; then
406+
echo "select-install: error: runtime baseline dependency closure is missing: $missing_reachable" >&2
362407
exit 1
363408
fi
364409

‎hack/select-install_test.bats‎

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ assert_contains_package() {
2121
printf '%s\n' "$1" | tr ' ' '\n' | grep -Fxq "$2"
2222
}
2323

24+
assert_output_graph_error() {
25+
mode=$1
26+
sources=$2
27+
expected=$3
28+
out=$(mktemp)
29+
err=$(mktemp)
30+
if [ "$mode" = disabled ]; then
31+
if hack/select-install.sh --disabled postgres "$sources" >"$out" 2>"$err"; then
32+
echo "expected disabled mode to reject invalid graph $sources" >&2
33+
exit 1
34+
fi
35+
elif hack/select-install.sh postgres "$sources" >"$out" 2>"$err"; then
36+
echo "expected closure mode to reject invalid graph $sources" >&2
37+
exit 1
38+
fi
39+
[ ! -s "$out" ]
40+
grep -Fq "$expected" "$err"
41+
rm -f "$out" "$err"
42+
}
43+
2444
@test "single app selects its forward dependency closure" {
2545
output=$(hack/select-install.sh "postgres")
2646
assert_contains_package "$output" cozystack.postgres-application
@@ -135,7 +155,8 @@ assert_contains_package() {
135155

136156
@test "validate detects a dangling dependency" {
137157
tmp=$(mktemp -d)
138-
mkdir -p "$tmp/sources" "$tmp/suites"
158+
mkdir -p "$tmp/sources" "$tmp/suites/foo"
159+
: > "$tmp/suites/foo/chainsaw-test.yaml"
139160
cat > "$tmp/sources/foo-application.yaml" <<'YAML'
140161
apiVersion: cozystack.io/v1alpha1
141162
kind: PackageSource
@@ -147,17 +168,24 @@ spec:
147168
dependsOn:
148169
- cozystack.does-not-exist
149170
YAML
150-
# empty suites-dir isolates this to the graph check
151-
if hack/select-install.sh --validate "$tmp/sources" "$tmp/suites" 2>/dev/null; then
171+
out="$tmp/out"; err="$tmp/err"
172+
if hack/select-install.sh --validate "$tmp/sources" "$tmp/suites" >"$out" 2>"$err"; then
152173
echo "expected validation to fail on a dangling dependency" >&2
153174
exit 1
154175
fi
176+
[ ! -s "$out" ]
177+
grep -Fq "dangling dependency 'cozystack.does-not-exist'" "$err"
178+
if grep -Fq "found no chainsaw-test.yaml" "$err"; then
179+
echo "positive-control suite discovery failed instead of graph validation" >&2
180+
exit 1
181+
fi
155182
rm -rf "$tmp"
156183
}
157184

158185
@test "validate detects a dependency cycle" {
159186
tmp=$(mktemp -d)
160-
mkdir -p "$tmp/sources" "$tmp/suites"
187+
mkdir -p "$tmp/sources" "$tmp/suites/a"
188+
: > "$tmp/suites/a/chainsaw-test.yaml"
161189
cat > "$tmp/sources/a.yaml" <<'YAML'
162190
apiVersion: cozystack.io/v1alpha1
163191
kind: PackageSource
@@ -180,11 +208,64 @@ spec:
180208
dependsOn:
181209
- cozystack.a
182210
YAML
183-
# empty suites-dir isolates this to the graph check
184-
if hack/select-install.sh --validate "$tmp/sources" "$tmp/suites" 2>/dev/null; then
211+
out="$tmp/out"; err="$tmp/err"
212+
if hack/select-install.sh --validate "$tmp/sources" "$tmp/suites" >"$out" 2>"$err"; then
185213
echo "expected validation to fail on a dependency cycle" >&2
186214
exit 1
187215
fi
216+
[ ! -s "$out" ]
217+
grep -Fq "dependency cycle detected involving 'cozystack.a'" "$err"
218+
if grep -Fq "found no chainsaw-test.yaml" "$err"; then
219+
echo "positive-control suite discovery failed instead of graph validation" >&2
220+
exit 1
221+
fi
222+
rm -rf "$tmp"
223+
}
224+
225+
@test "output modes reject a removed runtime source" {
226+
tmp=$(mktemp -d)
227+
cp -r packages/core/platform/sources "$tmp/sources"
228+
mv "$tmp/sources/reloader.yaml" "$tmp/reloader.yaml"
229+
for mode in closure disabled; do
230+
assert_output_graph_error "$mode" "$tmp/sources" \
231+
"dangling dependency 'cozystack.reloader'"
232+
done
233+
234+
yq -i '(.spec.variants[].dependsOn) -= ["cozystack.reloader"]' \
235+
"$tmp/sources/linstor.yaml"
236+
for mode in closure disabled; do
237+
assert_output_graph_error "$mode" "$tmp/sources" \
238+
"required runtime PackageSource(s) not found in $tmp/sources: cozystack.reloader"
239+
done
240+
rm -rf "$tmp"
241+
}
242+
243+
@test "output modes reject a dangling dependency in an unselected package" {
244+
tmp=$(mktemp -d)
245+
cp -r packages/core/platform/sources "$tmp/sources"
246+
yq -i '.spec.variants[0].dependsOn += ["cozystack.does-not-exist"]' \
247+
"$tmp/sources/kafka-application.yaml"
248+
for mode in closure disabled; do
249+
assert_output_graph_error "$mode" "$tmp/sources" \
250+
"dangling dependency 'cozystack.does-not-exist' (referenced by: cozystack.kafka-application)"
251+
done
252+
rm -rf "$tmp"
253+
}
254+
255+
@test "output modes reject selected and unselected dependency cycles" {
256+
tmp=$(mktemp -d)
257+
cp -r packages/core/platform/sources "$tmp/selected"
258+
cp -r packages/core/platform/sources "$tmp/unselected"
259+
yq -i '.spec.variants[0].dependsOn += ["cozystack.postgres-application"]' \
260+
"$tmp/selected/postgres-application.yaml"
261+
yq -i '.spec.variants[0].dependsOn += ["cozystack.kafka-application"]' \
262+
"$tmp/unselected/kafka-application.yaml"
263+
for mode in closure disabled; do
264+
assert_output_graph_error "$mode" "$tmp/selected" \
265+
"dependency cycle detected involving 'cozystack.postgres-application'"
266+
assert_output_graph_error "$mode" "$tmp/unselected" \
267+
"dependency cycle detected involving 'cozystack.kafka-application'"
268+
done
188269
rm -rf "$tmp"
189270
}
190271

@@ -331,6 +412,19 @@ YAML
331412
[ "$drop" -eq 68 ]
332413
}
333414

415+
@test "disabled mode accepts a valid inventory with an empty complement" {
416+
tmp=$(mktemp -d)
417+
mkdir -p "$tmp/sources"
418+
keep=$(hack/select-install.sh postgres)
419+
for source in $keep; do
420+
name=${source#cozystack.}
421+
cp "packages/core/platform/sources/$name.yaml" "$tmp/sources/"
422+
done
423+
output=$(hack/select-install.sh --disabled postgres "$tmp/sources")
424+
[ -z "$output" ]
425+
rm -rf "$tmp"
426+
}
427+
334428
@test "disabled mode: never lists a package the closure keeps" {
335429
keep=$(hack/select-install.sh "postgres kafka")
336430
drop=$(hack/select-install.sh --disabled "postgres kafka")

0 commit comments

Comments
 (0)