Skip to content

Commit 103b8e1

Browse files
test(e2e): diff-driven install selector select-install.sh (#2847)
Add hack/select-install.sh: given the E2E suites select-e2e.sh picked, emit the forward dependsOn closure over the PackageSource graph — the minimal set of packages to install for those suites. Companion to the reverse (test-selection) walk in select-e2e.sh; keeps the cozystack-engine edge since install needs it. Suite->source mapping follows the <suite>-application convention with a bare-<suite> fallback plus explicit cases for irregular names (kubernetes/vminstance/securitygroup) and core-platform features that have no separately-enabled package (serviceexposure). Includes a --validate mode (reachability + cycle check) and a 13-case unit suite auto-discovered by make unit-tests. Track A of #2847; tracked in #3278. Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent eea8b73 commit 103b8e1

2 files changed

Lines changed: 329 additions & 0 deletions

File tree

‎hack/select-install.sh‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#!/bin/sh
2+
# Given the E2E app suite(s) about to run, emit the minimal set of packages that
3+
# must be INSTALLED for those suites — the forward dependency closure over the
4+
# PackageSource graph. This is the install-side companion to select-e2e.sh:
5+
# select-e2e.sh picks WHICH Chainsaw suites run (reverse dependency walk);
6+
# select-install.sh picks WHAT must be up for them (forward dependency walk).
7+
#
8+
# Usage:
9+
# hack/select-install.sh <suites> [<sources-dir>]
10+
# hack/select-install.sh --validate [<sources-dir>]
11+
#
12+
# Defaults: sources-dir = packages/core/platform/sources
13+
#
14+
# <suites>: whitespace-separated suite names, i.e. the output of select-e2e.sh
15+
# (e.g. "postgres harbor"). "-" reads the list from stdin.
16+
#
17+
# Output (closure mode): space-separated PackageSource names (cozystack.*) that
18+
# the run must enable, seeds + all transitive dependsOn. Empty when <suites>
19+
# is empty.
20+
#
21+
# --validate: check the whole graph — every dependsOn target resolves to a real
22+
# PackageSource, and there are no dependency cycles. Exit non-zero on either.
23+
#
24+
# Unlike select-e2e.sh's reverse walk, the forward walk KEEPS the
25+
# cozystack.cozystack-engine edge: every *-application declares
26+
# `dependsOn: cozystack.cozystack-engine` because the app's *-rd HelmRelease
27+
# needs the engine to register the ApplicationDefinition CRD before it can
28+
# reconcile. For test SELECTION that edge is noise (it would fan every app out
29+
# to every other); for INSTALL it is a genuine prerequisite that must be up.
30+
set -eu
31+
32+
SOURCES_DIR="packages/core/platform/sources"
33+
MODE="closure"
34+
SUITES=""
35+
36+
if [ "${1:-}" = "--validate" ]; then
37+
MODE="validate"
38+
SOURCES_DIR="${2:-$SOURCES_DIR}"
39+
else
40+
suites_arg="${1?usage: select-install.sh <suites> [sources-dir] | --validate [sources-dir]}"
41+
SOURCES_DIR="${2:-$SOURCES_DIR}"
42+
if [ "$suites_arg" = "-" ]; then
43+
SUITES="$(cat)"
44+
else
45+
SUITES="$suites_arg"
46+
fi
47+
fi
48+
49+
# suite name -> owning PackageSource. Prints:
50+
# - a "cozystack.*" name the suite's primary install target
51+
# - "-" the suite targets a core-platform feature that
52+
# has no separately-enabled package (always
53+
# present via the base install), e.g.
54+
# serviceexposure (ExposureClass/ServiceExposure
55+
# live in packages/core/platform)
56+
# - nothing no mapping is known (caller warns)
57+
# Most suites follow the <suite>-application convention; a bare-<suite> fallback
58+
# and the explicit cases cover suites whose source is named differently. Kept in
59+
# lockstep with select-e2e.sh's src_to_suites() and the hack/e2e-chainsaw/ dirs.
60+
#
61+
# Note: a suite may exercise MORE than one package (e.g. securitygroup also
62+
# stands up a Kubernetes app). Only the primary package is mapped here; the
63+
# forward closure covers its own deps, and the full-suite fallback covers the
64+
# rest. Modelling multi-package suites is deferred to the test-minimal work.
65+
suite_to_source() {
66+
case "$1" in
67+
kubernetes-latest|kubernetes-previous|kubernetes-oidc-system|kubernetes-oidc-customconfig)
68+
echo cozystack.kubernetes-application ; return ;;
69+
vminstance) echo cozystack.vm-instance-application ; return ;;
70+
securitygroup) echo cozystack.securitygroup-controller ; return ;;
71+
serviceexposure) echo - ; return ;;
72+
esac
73+
for cand in "cozystack.$1-application" "cozystack.$1"; do
74+
if echo "$NODES" | grep -Fxq "$cand"; then echo "$cand"; return; fi
75+
done
76+
}
77+
78+
# yq: forward edges — "owner<TAB>dep", one per variant dependsOn entry.
79+
build_forward_deps() {
80+
yq -rN '.metadata.name as $n | .spec.variants[]?.dependsOn[]? | select(. != null and . != "") | $n + "\t" + .' "$SOURCES_DIR"/*.yaml
81+
}
82+
83+
NODES="$(yq -rN '.metadata.name | select(. != null and . != "")' "$SOURCES_DIR"/*.yaml | sort -u)"
84+
FORWARD="$(build_forward_deps | sort -u)"
85+
86+
# forward deps of a single node
87+
deps_of() {
88+
echo "$FORWARD" | awk -v s="$1" -F'\t' '$1==s {print $2}'
89+
}
90+
91+
if [ "$MODE" = "validate" ]; then
92+
rc=0
93+
94+
# 1. Reachability: every dependsOn target must be a real PackageSource.
95+
echo "$FORWARD" | awk -F'\t' '{print $2}' | sort -u | while IFS= read -r dep; do
96+
[ -z "$dep" ] && continue
97+
if ! echo "$NODES" | grep -Fxq "$dep"; then
98+
owners="$(echo "$FORWARD" | awk -v x="$dep" -F'\t' '$2==x {print $1}' | paste -sd ',' -)"
99+
echo "select-install: dangling dependency '$dep' (referenced by: $owners) has no PackageSource" >&2
100+
echo dangling
101+
fi
102+
done | grep -q dangling && rc=1
103+
104+
# 2. Cycles: from every node with outgoing edges, walk forward; if the walk
105+
# returns to its start, the graph has a cycle through it.
106+
owners_list="$(echo "$FORWARD" | awk -F'\t' '{print $1}' | sort -u)"
107+
for start in $owners_list; do
108+
visited=""
109+
frontier="$(deps_of "$start")"
110+
while [ -n "$frontier" ]; do
111+
next=""
112+
for f in $frontier; do
113+
if [ "$f" = "$start" ]; then
114+
echo "select-install: dependency cycle detected involving '$start'" >&2
115+
rc=1
116+
next=""
117+
break
118+
fi
119+
case " $visited " in *" $f "*) continue ;; esac
120+
visited="$visited $f"
121+
next="$next $(deps_of "$f")"
122+
done
123+
frontier="$next"
124+
done
125+
done
126+
127+
[ "$rc" = 0 ] && echo "select-install: graph OK ($(echo "$NODES" | grep -c .) sources)" >&2
128+
exit "$rc"
129+
fi
130+
131+
# Closure mode: map suites to seed sources, then take the forward closure.
132+
seeds=""
133+
for suite in $SUITES; do
134+
[ -z "$suite" ] && continue
135+
src="$(suite_to_source "$suite")"
136+
case "$src" in
137+
"-") continue ;; # core-platform feature — nothing separate to enable
138+
"") echo "select-install: warning: suite '$suite' has no known PackageSource mapping; skipping" >&2 ; continue ;;
139+
esac
140+
if echo "$NODES" | grep -Fxq "$src"; then
141+
case " $seeds " in *" $src "*) ;; *) seeds="$seeds $src" ;; esac
142+
else
143+
echo "select-install: warning: suite '$suite' -> '$src' not found; skipping" >&2
144+
fi
145+
done
146+
147+
[ -z "$seeds" ] && exit 0
148+
149+
all="$seeds"
150+
while :; do
151+
new=""
152+
for s in $all; do
153+
for d in $(deps_of "$s"); do
154+
case " $all $new " in *" $d "*) ;; *) new="$new $d" ;; esac
155+
done
156+
done
157+
[ -z "$new" ] && break
158+
all="$all $new"
159+
done
160+
161+
echo "$all" | tr ' ' '\n' | grep -v '^$' | sort -u | paste -sd ' ' -

‎hack/select-install_test.bats‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env bats
2+
# -----------------------------------------------------------------------------
3+
# Unit tests for hack/select-install.sh
4+
#
5+
# cozytest.sh's awk parser recognizes only @test blocks and a bare `}` on its
6+
# own line; there is no bats `run` or `$status`. Each test runs as a shell
7+
# function under `set -eu -x`, so assertions are direct shell tests that exit
8+
# non-zero on failure. setup()/teardown() are not honored — each test creates
9+
# and cleans its own scratch dir. To assert a NON-zero exit, invert with `if`
10+
# so `set -e` doesn't abort the test.
11+
#
12+
# Run with: hack/cozytest.sh hack/select-install_test.bats
13+
# -----------------------------------------------------------------------------
14+
15+
@test "single app selects its forward dependency closure" {
16+
tmp=$(mktemp -d)
17+
trap 'rm -rf "$tmp"' EXIT
18+
cp -r packages/core/platform/sources "$tmp/sources"
19+
output=$(hack/select-install.sh "postgres" "$tmp/sources")
20+
echo "$output" | grep -wq cozystack.postgres-application
21+
echo "$output" | grep -wq cozystack.postgres-operator
22+
echo "$output" | grep -wq cozystack.networking
23+
# engine edge is KEPT on the install walk (unlike select-e2e.sh)
24+
echo "$output" | grep -wq cozystack.cozystack-engine
25+
}
26+
27+
@test "closure is transitive (deps of deps are pulled in)" {
28+
tmp=$(mktemp -d)
29+
trap 'rm -rf "$tmp"' EXIT
30+
cp -r packages/core/platform/sources "$tmp/sources"
31+
output=$(hack/select-install.sh "postgres" "$tmp/sources")
32+
# cert-manager is a 2-hop dep (via postgres-operator and via the engine)
33+
echo "$output" | grep -wq cozystack.cert-manager
34+
# gateway-api-crds is a 3-hop dep (postgres-application -> networking -> gateway-api-crds)
35+
echo "$output" | grep -wq cozystack.gateway-api-crds
36+
}
37+
38+
@test "app pulls its direct operator dependencies" {
39+
tmp=$(mktemp -d)
40+
trap 'rm -rf "$tmp"' EXIT
41+
cp -r packages/core/platform/sources "$tmp/sources"
42+
output=$(hack/select-install.sh "harbor" "$tmp/sources")
43+
echo "$output" | grep -wq cozystack.harbor-application
44+
echo "$output" | grep -wq cozystack.postgres-operator
45+
echo "$output" | grep -wq cozystack.redis-operator
46+
echo "$output" | grep -wq cozystack.objectstorage-controller
47+
}
48+
49+
@test "kubernetes suites map back to the kubernetes application source" {
50+
tmp=$(mktemp -d)
51+
trap 'rm -rf "$tmp"' EXIT
52+
cp -r packages/core/platform/sources "$tmp/sources"
53+
output=$(hack/select-install.sh "kubernetes-latest" "$tmp/sources")
54+
echo "$output" | grep -wq cozystack.kubernetes-application
55+
}
56+
57+
@test "multiple suites union their closures" {
58+
tmp=$(mktemp -d)
59+
trap 'rm -rf "$tmp"' EXIT
60+
cp -r packages/core/platform/sources "$tmp/sources"
61+
output=$(hack/select-install.sh "postgres kafka" "$tmp/sources")
62+
echo "$output" | grep -wq cozystack.postgres-application
63+
echo "$output" | grep -wq cozystack.kafka-application
64+
}
65+
66+
@test "empty suites select nothing" {
67+
tmp=$(mktemp -d)
68+
trap 'rm -rf "$tmp"' EXIT
69+
cp -r packages/core/platform/sources "$tmp/sources"
70+
output=$(hack/select-install.sh "" "$tmp/sources")
71+
[ -z "$output" ]
72+
}
73+
74+
@test "unknown suite is skipped, not fatal" {
75+
tmp=$(mktemp -d)
76+
trap 'rm -rf "$tmp"' EXIT
77+
cp -r packages/core/platform/sources "$tmp/sources"
78+
output=$(hack/select-install.sh "definitely-not-an-app" "$tmp/sources" 2>/dev/null)
79+
[ -z "$output" ]
80+
}
81+
82+
@test "suite whose source omits the -application suffix resolves via fallback" {
83+
tmp=$(mktemp -d)
84+
trap 'rm -rf "$tmp"' EXIT
85+
cp -r packages/core/platform/sources "$tmp/sources"
86+
# kuberture's PackageSource is cozystack.kuberture (no -application suffix)
87+
output=$(hack/select-install.sh "kuberture" "$tmp/sources")
88+
echo "$output" | grep -wq cozystack.kuberture
89+
}
90+
91+
@test "suite with an irregular source name maps via explicit override" {
92+
tmp=$(mktemp -d)
93+
trap 'rm -rf "$tmp"' EXIT
94+
cp -r packages/core/platform/sources "$tmp/sources"
95+
output=$(hack/select-install.sh "securitygroup" "$tmp/sources")
96+
echo "$output" | grep -wq cozystack.securitygroup-controller
97+
}
98+
99+
@test "core-platform suite contributes no package and emits no warning" {
100+
tmp=$(mktemp -d)
101+
trap 'rm -rf "$tmp"' EXIT
102+
cp -r packages/core/platform/sources "$tmp/sources"
103+
# serviceexposure is a core-platform CRD; nothing to enable, no warning.
104+
err=$(hack/select-install.sh "serviceexposure" "$tmp/sources" 2>&1 1>/dev/null)
105+
[ -z "$err" ]
106+
output=$(hack/select-install.sh "serviceexposure" "$tmp/sources" 2>/dev/null)
107+
[ -z "$output" ]
108+
}
109+
110+
@test "validate passes on the real source graph" {
111+
tmp=$(mktemp -d)
112+
trap 'rm -rf "$tmp"' EXIT
113+
cp -r packages/core/platform/sources "$tmp/sources"
114+
hack/select-install.sh --validate "$tmp/sources"
115+
}
116+
117+
@test "validate detects a dangling dependency" {
118+
tmp=$(mktemp -d)
119+
trap 'rm -rf "$tmp"' EXIT
120+
mkdir -p "$tmp/sources"
121+
cat > "$tmp/sources/foo-application.yaml" <<'YAML'
122+
apiVersion: cozystack.io/v1alpha1
123+
kind: PackageSource
124+
metadata:
125+
name: cozystack.foo-application
126+
spec:
127+
variants:
128+
- name: default
129+
dependsOn:
130+
- cozystack.does-not-exist
131+
YAML
132+
if hack/select-install.sh --validate "$tmp/sources" 2>/dev/null; then
133+
echo "expected validation to fail on a dangling dependency" >&2
134+
exit 1
135+
fi
136+
}
137+
138+
@test "validate detects a dependency cycle" {
139+
tmp=$(mktemp -d)
140+
trap 'rm -rf "$tmp"' EXIT
141+
mkdir -p "$tmp/sources"
142+
cat > "$tmp/sources/a.yaml" <<'YAML'
143+
apiVersion: cozystack.io/v1alpha1
144+
kind: PackageSource
145+
metadata:
146+
name: cozystack.a
147+
spec:
148+
variants:
149+
- name: default
150+
dependsOn:
151+
- cozystack.b
152+
YAML
153+
cat > "$tmp/sources/b.yaml" <<'YAML'
154+
apiVersion: cozystack.io/v1alpha1
155+
kind: PackageSource
156+
metadata:
157+
name: cozystack.b
158+
spec:
159+
variants:
160+
- name: default
161+
dependsOn:
162+
- cozystack.a
163+
YAML
164+
if hack/select-install.sh --validate "$tmp/sources" 2>/dev/null; then
165+
echo "expected validation to fail on a dependency cycle" >&2
166+
exit 1
167+
fi
168+
}

0 commit comments

Comments
 (0)