From cf7bd11eeb822a12ea9300b50410b1691e800e75 Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Wed, 21 Oct 2026 17:36:24 +0200 Subject: [PATCH] test(e2e): prepare and check the backend the install deploys The e2e scripts assumed LINSTOR throughout: the prep created pools through linstor-controller and the replicated suite's pool guard read them from it. COZY_STORAGE_BACKEND is now set once by the install step and reaches the platform values, the prep and the Chainsaw suites alike. The prep refuses a missing or unknown value instead of guessing, which would race the install and fail open to linstor. Under blockstor the prep waits for blockstor's own Deployments and for every LinstorSatellite to apply, registers a Node per Kubernetes node at its InternalIP, creates the data zpool and its StoragePool on each satellite, and waits until every satellite has reported its pool, since placement does not count a pool before that. The pool guard reads StoragePool objects and lists datasets through blockstor-satellite. The backend is validated before the guard starts polling. Inside the loop every failed read is retried: the remote jq exits 2 on the output a restarting controller can print, so no read status can mean "give up". blockstor needs DRBD and the QEMU lane's private disk, so the pair with COZY_LINSTOR_DRBD_ENABLED=false is refused rather than installed into a prep that cannot finish. No CI lane sets the variable, so the default runs are unchanged. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- hack/e2e-chainsaw/_lib/run-kubernetes.sh | 115 ++++++++--- hack/e2e-install-cozystack.bats | 5 + hack/e2e-platform-packages.sh | 29 ++- hack/e2e-post-install-prep.sh | 231 +++++++++++++++++++++++ hack/linstor-storage-mode_test.bats | 184 ++++++++++++++++++ hack/platform-packages_test.bats | 41 ++++ hack/post-install-prep.bats | 194 +++++++++++++++++++ packages/core/testing/Makefile | 6 +- 8 files changed, 774 insertions(+), 31 deletions(-) diff --git a/hack/e2e-chainsaw/_lib/run-kubernetes.sh b/hack/e2e-chainsaw/_lib/run-kubernetes.sh index d13ec5a31..32ffc61f1 100644 --- a/hack/e2e-chainsaw/_lib/run-kubernetes.sh +++ b/hack/e2e-chainsaw/_lib/run-kubernetes.sh @@ -493,6 +493,52 @@ cozy_wait_schedulable_node() { done } +# The storage control plane the cluster under test runs, as the install was +# told (COZY_STORAGE_BACKEND, forwarded into the sandbox). Unset means linstor, +# the default every lane installs; anything else unknown is refused rather than +# read as linstor, which would poll a controller that does not exist. +_cozy_storage_backend() { + case "${COZY_STORAGE_BACKEND:-linstor}" in + linstor | blockstor) printf '%s\n' "${COZY_STORAGE_BACKEND:-linstor}" ;; + *) + echo "» ERROR: COZY_STORAGE_BACKEND must be linstor or blockstor, got '${COZY_STORAGE_BACKEND}'" >&2 + return 2 + ;; + esac +} + +# Print `:` for every ZFS pool that reports a free capacity. +# The LINSTOR path asks the controller, whose `sp l` serves one outer array of +# pool objects with free_capacity in KiB; OFFLINE satellites omit it, and the +# DISKLESS placeholder carries a Long.MAX_VALUE sentinel, so both are skipped. +# The blockstor path reads the StoragePool objects, whose status.freeCapacity +# is in KiB too; there is no controller pod to exec into. +_cozy_pool_free_rows() { + _backend=$(_cozy_storage_backend) || return 2 + if [ "$_backend" = blockstor ]; then + # Read first and filter second: piped, the read's status is jq's, and jq + # on empty input succeeds. + _pools_json=$(kubectl get storagepools.blockstor.cozystack.io --request-timeout=20s -o json) || return 1 + printf '%s\n' "$_pools_json" | + jq -r '.items[] | select((.spec.providerKind | test("^ZFS")) and .status.freeCapacity != null) | "\(.status.freeCapacity):\(.spec.nodeName)"' + return + fi + # jq lives inside the controller pod (Debian-bookworm base, `sh` is dash, + # so the script stays POSIX). + kubectl -n cozy-linstor exec deploy/linstor-controller -- sh -c ' + linstor --machine-readable sp l 2>/dev/null | + jq -r "first | .[] | select((.provider_kind | test(\"^ZFS\")) and .free_capacity != null) | \"\(.free_capacity):\(.node_name)\"" + ' +} + +_cozy_pool_diagnostics() { + if [ "$(_cozy_storage_backend 2>/dev/null)" = blockstor ]; then + kubectl get storagepools.blockstor.cozystack.io -o wide --request-timeout=20s 2>&1 + return + fi + kubectl -n cozy-linstor exec deploy/linstor-controller -- linstor --no-color sp l 2>&1 +} + # Block until every ZFS storage pool on every LINSTOR satellite reports at # least _min_free_gib of FreeCapacity. Motivation is proven from a # cozyreport artefact captured by hack/cozyreport.sh (see PR #3044 run @@ -524,36 +570,34 @@ cozy_wait_linstor_pool_free() { _timeout="${2:-300}" _min_free_kib=$(( _min_free_gib * 1024 * 1024 )) _deadline=$(( $(date +%s) + _timeout )) + # Settled once, before polling. Inside the loop every non-zero status is a + # read that failed and is retried: the remote jq exits 2 on the truncated or + # error output a restarting controller can print, so no status of the read + # can double as "give up". + _cozy_storage_backend >/dev/null || return 2 while :; do - # jq lives inside the controller pod (Debian-bookworm base, `sh` is - # dash — keep the heredoc POSIX-safe). LINSTOR's `--machine-readable` - # output for `sp l` on LINSTOR 1.33.x is a one-element outer array - # whose sole element is a flat array of storage-pool objects; each - # pool object exposes free_capacity at the top level in KiB. Filter - # to ZFS variants (both `ZFS` and `ZFS_THIN`) so DISKLESS - # placeholders (whose free_capacity is a Long.MAX_VALUE sentinel) - # and any future non-ZFS driver are skipped. Also guard against - # OFFLINE satellites, whose pool objects omit free_capacity entirely - # (StoragePool schema marks it optional) — without the null guard - # `sort -n` would rank the string "null" ahead of real numbers and - # the loop would silently poll to timeout. Emit - # `:` lines so a single sort yields the - # smallest pool and its owner in one round-trip. - _min_line=$(kubectl -n cozy-linstor exec deploy/linstor-controller -- sh -c ' - linstor --machine-readable sp l 2>/dev/null | - jq -r "first | .[] | select((.provider_kind | test(\"^ZFS\")) and .free_capacity != null) | \"\(.free_capacity):\(.node_name)\"" | - sort -n | head -n 1 - ' 2>/dev/null) || _min_line="" + # Filter to ZFS variants (both `ZFS` and `ZFS_THIN`) so DISKLESS + # placeholders and any future non-ZFS driver are skipped, and to pools + # that report a free capacity at all, or `sort -n` would rank a missing + # one ahead of real numbers. The read keeps its own status: piped + # straight into `head` it took head's, so a failed read, a 403 and a + # pool that is genuinely full all looked alike. + _rows_rc=0 + _rows=$(_cozy_pool_free_rows 2>/dev/null) || _rows_rc=$? + _min_line="" + if [ "$_rows_rc" -eq 0 ]; then + _min_line=$(printf '%s\n' "$_rows" | grep . | sort -n | head -n 1) + fi _min_kib="${_min_line%%:*}" _min_node="${_min_line#*:}" if [ -n "$_min_kib" ] && [ "$_min_kib" -ge "$_min_free_kib" ] 2>/dev/null; then - echo "» LINSTOR ZFS pool free: smallest satellite ${_min_node} has $(( _min_kib / 1024 / 1024 )) GiB (>= ${_min_free_gib} GiB threshold)" + echo "» ZFS pool free: smallest satellite ${_min_node} has $(( _min_kib / 1024 / 1024 )) GiB (>= ${_min_free_gib} GiB threshold)" return 0 fi if [ "$(date +%s)" -ge "$_deadline" ]; then - echo "» ERROR: LINSTOR ZFS pool free did not reach ${_min_free_gib} GiB on every satellite within ${_timeout}s (smallest observed: ${_min_kib:-unknown} KiB on ${_min_node:-unknown})" >&2 + echo "» ERROR: ZFS pool free did not reach ${_min_free_gib} GiB on every satellite within ${_timeout}s (smallest observed: ${_min_kib:-unknown} KiB on ${_min_node:-unknown}; last read exited ${_rows_rc})" >&2 _pool_diag_rc=0 - _pool_diag=$(kubectl -n cozy-linstor exec deploy/linstor-controller -- linstor --no-color sp l 2>&1) || _pool_diag_rc=$? + _pool_diag=$(_cozy_pool_diagnostics) || _pool_diag_rc=$? if [ "$_pool_diag_rc" -ne 0 ]; then echo " linstor-pool: diagnostic read failed with exit $_pool_diag_rc" >&2 fi @@ -582,19 +626,34 @@ _cozy_linstor_pool_baseline_file() { # `data-srvN` in container mode, where every node's pool is imported into one # shared kernel. Every read carries its own ceiling, so a wedged controller or # satellite fails the poll within 35s instead of holding the caller for good. +# +# On the blockstor backend the pools are StoragePool objects rather than +# controller rows, the zpool is whichever StorDriver property names it, and the +# satellite is blockstor's own DaemonSet. _cozy_linstor_pool_datasets() { - _pools=$(timeout -k 5 30 kubectl -n cozy-linstor exec deploy/linstor-controller -- sh -c ' - linstor --machine-readable sp l 2>/dev/null | - jq -r "first | .[] | select(.provider_kind | test(\"^ZFS\")) | \"\(.node_name) \(.props[\"StorDriver/StorPoolName\"] // \"\")\"" - ' 2>/dev/null) || return 1 + _backend=$(_cozy_storage_backend) || return 1 + if [ "$_backend" = blockstor ]; then + _pools_json=$(timeout -k 5 30 kubectl get storagepools.blockstor.cozystack.io --request-timeout=20s -o json 2>/dev/null) || return 1 + _pools=$(printf '%s\n' "$_pools_json" | + jq -r '.items[] | select(.spec.providerKind | test("^ZFS")) | "\(.spec.nodeName) \(.spec.props["StorDriver/StorPoolName"] // .spec.props["StorDriver/ZPoolThin"] // .spec.props["StorDriver/ZPool"] // "")"') || return 1 + _satellite_selector=app=blockstor-satellite + _satellite_container=satellite + else + _pools=$(timeout -k 5 30 kubectl -n cozy-linstor exec deploy/linstor-controller -- sh -c ' + linstor --machine-readable sp l 2>/dev/null | + jq -r "first | .[] | select(.provider_kind | test(\"^ZFS\")) | \"\(.node_name) \(.props[\"StorDriver/StorPoolName\"] // \"\")\"" + ' 2>/dev/null) || return 1 + _satellite_selector=app.kubernetes.io/component=linstor-satellite + _satellite_container=linstor-satellite + fi [ -n "$_pools" ] || return 1 - _satellites=$(timeout -k 5 30 kubectl -n cozy-linstor get pods -l app.kubernetes.io/component=linstor-satellite \ + _satellites=$(timeout -k 5 30 kubectl -n cozy-linstor get pods -l "$_satellite_selector" \ -o jsonpath='{range .items[*]}{.spec.nodeName} {.metadata.name}{"\n"}{end}' 2>/dev/null) || return 1 while read -r _node _zpool; do [ -n "$_node" ] && [ -n "$_zpool" ] || return 1 _pod=$(printf '%s\n' "$_satellites" | awk -v node="$_node" '$1 == node { print $2; exit }') [ -n "$_pod" ] || return 1 - _datasets=$(timeout -k 5 30 kubectl -n cozy-linstor exec "$_pod" --container=linstor-satellite -- \ + _datasets=$(timeout -k 5 30 kubectl -n cozy-linstor exec "$_pod" --container="$_satellite_container" -- \ zfs list -H -o name -t filesystem,volume,snapshot -r "$_zpool" /dev/null) || return 1 [ -n "$_datasets" ] || return 1 printf '%s\n' "$_datasets" | awk -v node="$_node" '{ print node " " $0 }' diff --git a/hack/e2e-install-cozystack.bats b/hack/e2e-install-cozystack.bats index f3724179b..e61feb786 100644 --- a/hack/e2e-install-cozystack.bats +++ b/hack/e2e-install-cozystack.bats @@ -181,6 +181,11 @@ # Wait for operator to create the platform PackageSource timeout 120 sh -ec 'until kubectl get packagesource cozystack.cozystack-platform >/dev/null 2>&1; do sleep 2; done' + # The storage control plane this install deploys. Set once here and read by + # both the generator, which passes it to the platform, and the prep, which + # needs it to take the matching branch and will not guess. + export COZY_STORAGE_BACKEND="${COZY_STORAGE_BACKEND:-linstor}" + # Stage the generated manifest instead of piping it into kubectl: cozytest # runs under /bin/sh without pipefail, so a generator failure in a pipeline # could be hidden by kubectl successfully applying empty input. diff --git a/hack/e2e-platform-packages.sh b/hack/e2e-platform-packages.sh index 5fe18739d..d232bdac2 100755 --- a/hack/e2e-platform-packages.sh +++ b/hack/e2e-platform-packages.sh @@ -3,6 +3,7 @@ set -eu api_server_endpoint=${COZY_APISERVER_ENDPOINT:-https://192.168.123.10:6443} linstor_drbd_enabled=${COZY_LINSTOR_DRBD_ENABLED:-true} +storage_backend=${COZY_STORAGE_BACKEND:-linstor} # Container-mode Talos shares the runner kernel and cannot load DRBD. Keep the # normal package graph for QEMU, and let that lane replace two Packages instead: @@ -24,6 +25,32 @@ case "$linstor_drbd_enabled" in ;; esac +# The storage control plane the cluster installs, passed to the platform as +# storage.backend. Blockstor creates its zpool on the QEMU lane's private +# /dev/vdc, which the container lane does not have, so the pair is refused +# rather than installed into a prep that cannot finish. +case "$storage_backend" in + linstor) ;; + blockstor) + if [ "$linstor_drbd_enabled" = false ]; then + echo "COZY_STORAGE_BACKEND=blockstor is not supported with COZY_LINSTOR_DRBD_ENABLED=false" >&2 + exit 2 + fi + ;; + *) + echo "COZY_STORAGE_BACKEND must be linstor or blockstor, got: $storage_backend" >&2 + exit 2 + ;; +esac + +# Only when asked: the linstor default renders the same Package it always has. +storage_values= +if [ "$storage_backend" = blockstor ]; then + storage_values=' storage: + backend: blockstor +' +fi + # Packages left out of this install. The container lane always drops the two it # replaces below; COZY_DISABLED_PACKAGES adds the complement computed by # hack/select-install.sh for the selected suites. Both land under one @@ -68,7 +95,7 @@ spec: publishing: host: "example.org" apiServerEndpoint: "$api_server_endpoint" - # Must match Tenant/root spec.monitoring, which the install sets to +${storage_values} # Must match Tenant/root spec.monitoring, which the install sets to # false. Left true, cozystack-basics renders Services into # cozy-monitoring, which a narrow suite selection never creates. monitoring: diff --git a/hack/e2e-post-install-prep.sh b/hack/e2e-post-install-prep.sh index 359f5cf75..52f79f437 100755 --- a/hack/e2e-post-install-prep.sh +++ b/hack/e2e-post-install-prep.sh @@ -57,6 +57,26 @@ validate_linstor_storage_mode() { esac } +# Which storage control plane the install deployed. The caller passes what it +# installed rather than this script guessing: a probe run here would race the +# install it runs alongside, since the namespace it would look in does not +# exist yet when the script starts, and it would fail open to linstor on any +# error. An unknown value is refused for the same reason the platform chart +# refuses one: it would otherwise take whichever branch is the fallback. +validate_storage_backend() { + case "${COZY_STORAGE_BACKEND:-}" in + linstor | blockstor) return 0 ;; + "") + echo "[post-install-prep] COZY_STORAGE_BACKEND must be set to linstor or blockstor" >&2 + return 1 + ;; + *) + echo "[post-install-prep] COZY_STORAGE_BACKEND must be linstor or blockstor, got '${COZY_STORAGE_BACKEND}'" >&2 + return 1 + ;; + esac +} + # KUBECTL_BOUND prefixes the cluster calls in the helpers below with a wall-clock # bound. The main body sets it; it stays empty when unit tests source the # helpers, because `timeout` execs a binary and would bypass the kubectl shell @@ -132,6 +152,45 @@ patch_local_cdi_storage_profile() { -p '{"spec":{"claimPropertySets":[{"accessModes":["ReadWriteOnce"],"volumeMode":"Block"}]}}' } +# count_true : how many lines of are exactly True. +count_true() { + printf '%s\n' "$1" | grep -c '^True$' || true +} + +# count_lines : how many non-empty lines has. +count_lines() { + printf '%s\n' "$1" | grep -c . || true +} + +render_blockstor_node() { + cat <.. +render_blockstor_storage_pool() { + cat </dev/null || exit 0 @@ -141,6 +200,17 @@ if ! validate_linstor_storage_mode; then exit 2 fi +if ! validate_storage_backend; then + exit 2 +fi + +# The blockstor branch creates its zpool on the QEMU lane's private /dev/vdc. +# The container lane has no such disk, and runs no DRBD for blockstor to drive. +if [ "$COZY_STORAGE_BACKEND" = blockstor ] && [ "${COZY_LINSTOR_DRBD_ENABLED:-true}" = false ]; then + echo "[post-install-prep] COZY_STORAGE_BACKEND=blockstor is not supported with COZY_LINSTOR_DRBD_ENABLED=false" >&2 + exit 2 +fi + # Every call that does work carries a bound, not only the waits: the caller # blocks in `wait` on this script, so one call that never returns holds the # install open until the job's own ceiling instead of failing the step. A @@ -281,6 +351,11 @@ wait_for_object() { wait_for_linstor "linstor HelmRelease to be Ready" \ helmrelease/linstor -n cozy-linstor --for=condition=Ready + +echo "[post-install-prep] storage backend: $COZY_STORAGE_BACKEND" + +if [ "$COZY_STORAGE_BACKEND" = linstor ]; then + wait_for_linstor "linstor-controller Deployment to be Available" \ deployment/linstor-controller -n cozy-linstor --for=condition=available @@ -331,6 +406,162 @@ for pid in $pids; do wait "$pid" done +else # COZY_STORAGE_BACKEND = blockstor + +# piraeus-operator runs in external mode here: there is no in-cluster +# linstor-controller to exec into. The zpool is made inside the +# blockstor-satellite pod, which carries the /dev, /run/udev and /lib/modules +# mounts libzfs needs, and the pool is then declared as a StoragePool. +wait_for_linstor "blockstor-apiserver Deployment to be Available" \ + deployment/blockstor-apiserver -n cozy-linstor --for=condition=available +wait_for_linstor "blockstor-controller Deployment to be Available" \ + deployment/blockstor-controller -n cozy-linstor --for=condition=available +wait_for_linstor "LinstorCluster to be Available" \ + linstorcluster/linstorcluster --for=condition=Available + +# Available says the operator reached the API once. It does not say a +# satellite reconcile finished, and the two came apart in the field: the +# operator probes /v1/controller/version before every LinstorSatellite +# reconcile, so a probe it cannot complete leaves each satellite at +# Applied=False with its DaemonSet frozen at a spec the chart no longer +# renders, while every HelmRelease reports Ready. That surfaces only when a pod +# is recreated, on a node that then loses its satellite for good, so the +# per-satellite condition is asserted here. +# +# Both reads are checked: a gate comparing two counts passes on no data when +# both reads fail and each count comes out zero. A cluster with no node is +# not a pass either. +echo "[post-install-prep] waiting for every LinstorSatellite to report Applied" +applied_deadline=$(( $(date +%s) + 300 )) +while :; do + sat_rc=0 + sat_applied=$(timeout -k 5 30 kubectl get linstorsatellite --request-timeout=10s \ + -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Applied")].status}{"\n"}{end}' \ + 2>/dev/null) || sat_rc=$? + node_rc=0 + node_names=$(timeout -k 5 30 kubectl get nodes --request-timeout=10s \ + -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' 2>/dev/null) || node_rc=$? + if [ "$sat_rc" -eq 0 ] && [ "$node_rc" -eq 0 ]; then + node_count=$(count_lines "$node_names") + applied_count=$(count_true "$sat_applied") + if [ "$node_count" -gt 0 ] && [ "$applied_count" -eq "$node_count" ]; then + break + fi + applied_state="$applied_count of $node_count LinstorSatellite objects Applied" + else + applied_state="LinstorSatellite read exited $sat_rc, Node read exited $node_rc" + fi + if [ "$(date +%s)" -ge "$applied_deadline" ]; then + echo "[post-install-prep] timed out after 300s waiting for every LinstorSatellite to report Applied: $applied_state" >&2 + timeout -k 5 30 kubectl get linstorsatellite --request-timeout=10s \ + -o jsonpath='{range .items[*]}{.metadata.name}{": "}{.status.conditions[?(@.type=="Applied")].message}{"\n"}{end}' \ + 2>&1 | tail -n 30 >&2 + exit 1 + fi + sleep 5 +done + +echo "[post-install-prep] waiting for a Ready blockstor-satellite pod on each of $node_count nodes" +sat_deadline=$(( $(date +%s) + 300 )) +while :; do + ready_rc=0 + sat_ready=$(timeout -k 5 30 kubectl get pods -n cozy-linstor -l app=blockstor-satellite \ + --request-timeout=10s \ + -o jsonpath='{range .items[*]}{.status.conditions[?(@.type=="Ready")].status}{"\n"}{end}' \ + 2>/dev/null) || ready_rc=$? + if [ "$ready_rc" -eq 0 ] && [ "$(count_true "$sat_ready")" -eq "$node_count" ]; then + break + fi + if [ "$(date +%s)" -ge "$sat_deadline" ]; then + echo "[post-install-prep] timed out after 300s waiting for $node_count Ready blockstor-satellite pods (read exited $ready_rc)" >&2 + timeout -k 5 30 kubectl get pods -n cozy-linstor -l app=blockstor-satellite -o wide \ + --request-timeout=10s 2>&1 | tail -n 30 >&2 + exit 1 + fi + sleep 5 +done + +# Node CRs carrying each node's InternalIP must exist before replicated volumes +# can resolve their DRBD peers: the label-sync controller only patches Node CRs +# that already exist. The reads are assignments, not `for` lists, so a failed +# one stops the script under set -e instead of registering nothing and going on. +echo "[post-install-prep] registering blockstor Node CRs for DRBD peer resolution" +node_rows=$($KUBECTL_BOUND kubectl get nodes --request-timeout=60s \ + -o jsonpath='{range .items[*]}{.metadata.name} {.status.addresses[?(@.type=="InternalIP")].address}{"\n"}{end}') +if [ "$(count_lines "$node_rows")" -ne "$node_count" ]; then + echo "[post-install-prep] expected $node_count nodes with an address, read: $node_rows" >&2 + exit 1 +fi +while read -r node ip; do + [ -n "$node" ] || continue + if [ -z "$ip" ]; then + echo "[post-install-prep] node $node has no InternalIP" >&2 + exit 1 + fi + render_blockstor_node "$node" "$ip" | $KUBECTL_BOUND kubectl apply --request-timeout=60s -f - +done <&2 + exit 1 +fi +pids="" +while read -r pod node; do + [ -n "$pod" ] || continue + ( + # Partition first and hand zpool the partition: a whole-disk create needs + # a GPT rescan that fails inside the container's devtmpfs view. + $KUBECTL_BOUND kubectl exec -n cozy-linstor "$pod" -- sh -ec ' + if zpool list data >/dev/null 2>&1; then + echo "zpool data already exists on '"$node"'" + exit 0 + fi + wipefs -af /dev/vdc* 2>/dev/null || true + sgdisk --zap-all /dev/vdc 2>/dev/null || true + sgdisk --new=1:0:0 -t 1:bf01 /dev/vdc + partprobe /dev/vdc 2>/dev/null || true + sleep 1 + zpool create -f -o cachefile=none data /dev/vdc1 + echo "zpool data created on '"$node"'" + ' + render_blockstor_storage_pool "$node" | $KUBECTL_BOUND kubectl apply --request-timeout=60s -f - + ) & + pids="$pids $!" +done <&2 + timeout -k 5 30 kubectl get storagepools.blockstor.cozystack.io -o yaml --request-timeout=20s 2>&1 | tail -n 80 >&2 + exit 1 + fi + sleep 5 +done + +fi # COZY_STORAGE_BACKEND + echo "[post-install-prep] applying StorageClasses" render_linstor_storageclasses | $KUBECTL_BOUND kubectl apply --request-timeout=60s -f - diff --git a/hack/linstor-storage-mode_test.bats b/hack/linstor-storage-mode_test.bats index a4eeb7e77..7ade7160f 100644 --- a/hack/linstor-storage-mode_test.bats +++ b/hack/linstor-storage-mode_test.bats @@ -704,3 +704,187 @@ idle_pool_datasets() { return 1 fi } + +# The blockstor backend has no controller pod to exec into: the pool guard +# reads StoragePool objects and lists datasets through blockstor's own +# satellite. These pin that it does, and that the LINSTOR path is not touched. +blockstor_storage_pools_json() { + printf '%s' '{"items":[ + {"spec":{"nodeName":"srv1","providerKind":"ZFS_THIN","props":{"StorDriver/ZPoolThin":"data"}},"status":{"freeCapacity":104857600}}, + {"spec":{"nodeName":"srv1","providerKind":"DISKLESS","props":{}},"status":{}}, + {"spec":{"nodeName":"srv2","providerKind":"ZFS_THIN","props":{"StorDriver/ZPoolThin":"data"}},"status":{"freeCapacity":'"${1:-104857600}"'}}, + {"spec":{"nodeName":"srv3","providerKind":"ZFS_THIN","props":{"StorDriver/ZPoolThin":"data"}},"status":{"freeCapacity":104857600}} + ]}' +} + +@test "the blockstor pool floor is read from StoragePool objects, not a controller" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + COZY_STORAGE_BACKEND=blockstor + calls="_out/tmp/blockstor-calls-$$" + mkdir -p _out/tmp + : >"$calls" + kubectl() { + printf '%s\n' "$*" >>"$calls" + case "$*" in + 'get storagepools.blockstor.cozystack.io '*-o\ json*) blockstor_storage_pools_json ;; + *) return 1 ;; + esac + } + fake_linstor_clock + + rc=0 + output=$(cozy_wait_linstor_pool_free 90 300 2>&1) || rc=$? + rm -f "$clock_file" + unset COZY_STORAGE_BACKEND + + if [ "$rc" -ne 0 ]; then + echo "the blockstor floor failed on pools that all hold 100 GiB: $output" >&2 + return 1 + fi + if grep -q 'linstor-controller' "$calls"; then + echo "the blockstor floor reached for the LINSTOR controller" >&2 + return 1 + fi + rm -f "$calls" +} + +@test "a blockstor pool below the floor fails the guard and names the node" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + COZY_STORAGE_BACKEND=blockstor + kubectl() { + case "$*" in + 'get storagepools.blockstor.cozystack.io '*-o\ json*) blockstor_storage_pools_json 87127923 ;; + *) printf 'STUB-POOL-TABLE\n' ;; + esac + } + fake_linstor_clock + + rc=0 + output=$(cozy_wait_linstor_pool_free 90 300 2>&1) || rc=$? + rm -f "$clock_file" + unset COZY_STORAGE_BACKEND + + if [ "$rc" -eq 0 ]; then + echo "the blockstor floor passed with srv2 at 83 GiB" >&2 + return 1 + fi + if ! printf '%s\n' "$output" | grep -q '87127923 KiB on srv2'; then + echo "the blockstor floor failure does not name the short node: $output" >&2 + return 1 + fi +} + +@test "a pool read that fails is told apart from a full pool" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + COZY_STORAGE_BACKEND=blockstor + kubectl() { + case "$*" in + 'get storagepools.blockstor.cozystack.io '*-o\ json*) + echo 'Error from server (Forbidden): storagepools is forbidden' >&2 + return 1 ;; + esac + } + fake_linstor_clock + + rc=0 + output=$(cozy_wait_linstor_pool_free 90 300 2>&1) || rc=$? + rm -f "$clock_file" + unset COZY_STORAGE_BACKEND + + if [ "$rc" -eq 0 ]; then + echo "the floor passed on a read that failed" >&2 + return 1 + fi + if printf '%s\n' "$output" | grep -q 'last read exited 0'; then + echo "a failed read was reported as a successful one: $output" >&2 + return 1 + fi +} + +@test "the blockstor dataset listing goes through blockstor's satellite" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + COZY_STORAGE_BACKEND=blockstor + calls="_out/tmp/blockstor-calls-$$" + mkdir -p _out/tmp + : >"$calls" + kubectl() { + printf '%s\n' "$*" >>"$calls" + case "$*" in + 'get storagepools.blockstor.cozystack.io '*) blockstor_storage_pools_json ;; + *'-l app=blockstor-satellite'*) + printf 'srv1 blockstor-satellite-a\nsrv2 blockstor-satellite-b\nsrv3 blockstor-satellite-c\n' ;; + *'--container=satellite -- zfs list'*) printf 'data\n' ;; + *) return 1 ;; + esac + } + timeout() { + [ "${1:-}" = -k ] && [ "${3:-0}" -gt 0 ] || return 97 + shift 3 + "$@" + } + + rc=0 + rows=$(_cozy_linstor_pool_datasets) || rc=$? + unset COZY_STORAGE_BACKEND + + if [ "$rc" -ne 0 ] || [ "$rows" != "$(printf 'srv1 data\nsrv2 data\nsrv3 data')" ]; then + echo "the blockstor dataset listing returned rc=$rc rows=$rows" >&2 + cat "$calls" >&2 + return 1 + fi + if grep -q 'linstor-controller\|linstor-satellite' "$calls"; then + echo "the blockstor listing reached for LINSTOR's own pods" >&2 + return 1 + fi + rm -f "$calls" +} + +@test "an unknown storage backend stops the pool guard before it reads anything" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + COZY_STORAGE_BACKEND=blokstor + kubectl() { printf 'UNEXPECTED_READ\n'; } + fake_linstor_clock + + rc=0 + output=$(cozy_wait_linstor_pool_free 90 300 2>&1) || rc=$? + rm -f "$clock_file" + unset COZY_STORAGE_BACKEND + + if [ "$rc" -ne 2 ] || printf '%s\n' "$output" | grep -q UNEXPECTED_READ; then + echo "an unknown backend was read as some backend: rc=$rc $output" >&2 + return 1 + fi +} + +@test "a pool read that exits 2 keeps the guard polling instead of giving up" { + # shellcheck source=/dev/null + . "$HACK_DIR/e2e-chainsaw/_lib/run-kubernetes.sh" + reads="_out/tmp/linstor-reads-$$" + mkdir -p _out/tmp + : >"$reads" + # jq inside the controller pod exits 2 on the error banner a restarting + # controller prints, and kubectl exec passes that status through. + kubectl() { + printf 'read\n' >>"$reads" + case "$*" in + *'exec deploy/linstor-controller'*) return 2 ;; + esac + } + fake_linstor_clock + + rc=0 + output=$(cozy_wait_linstor_pool_free 90 300 2>&1) || rc=$? + rm -f "$clock_file" + count=$(grep -c read "$reads") + rm -f "$reads" + + if [ "$rc" -ne 1 ] || [ "$count" -lt 2 ]; then + echo "a read exiting 2 ended the guard: rc=$rc after $count read(s): $output" >&2 + return 1 + fi +} diff --git a/hack/platform-packages_test.bats b/hack/platform-packages_test.bats index 693f4d056..986b5f15f 100644 --- a/hack/platform-packages_test.bats +++ b/hack/platform-packages_test.bats @@ -201,3 +201,44 @@ load test_helper fi done } + +@test "the linstor default leaves the platform Package without a storage block" { + manifest=$(hack/e2e-platform-packages.sh) + if printf '%s\n' "$manifest" | grep -q '^ storage:'; then + echo "the default install names a storage backend it never used to" >&2 + return 1 + fi +} + +@test "blockstor reaches the platform as storage.backend" { + manifest=$(COZY_STORAGE_BACKEND=blockstor hack/e2e-platform-packages.sh) + backend=$(printf '%s\n' "$manifest" | yq 'select(.metadata.name == "cozystack.cozystack-platform") | .spec.components.platform.values.storage.backend') + if [ "$backend" != blockstor ]; then + echo "storage.backend is '$backend', want blockstor" >&2 + return 1 + fi + # The block sits in the values, not inside bundles. + bundles=$(printf '%s\n' "$manifest" | yq 'select(.metadata.name == "cozystack.cozystack-platform") | .spec.components.platform.values.bundles | keys | join(",")') + if [ "$bundles" != enabledPackages ]; then + echo "bundles carries '$bundles'" >&2 + return 1 + fi +} + +@test "an unknown storage backend fails before emitting YAML" { + if manifest=$(COZY_STORAGE_BACKEND=blokstor hack/e2e-platform-packages.sh); then + echo "an unknown backend unexpectedly succeeded" >&2 + return 1 + fi + if [ -n "$manifest" ]; then + echo "an unknown backend emitted a partial manifest" >&2 + return 1 + fi +} + +@test "blockstor on the container lane is refused" { + if manifest=$(COZY_STORAGE_BACKEND=blockstor COZY_LINSTOR_DRBD_ENABLED=false hack/e2e-platform-packages.sh); then + echo "blockstor without a block device unexpectedly succeeded" >&2 + return 1 + fi +} diff --git a/hack/post-install-prep.bats b/hack/post-install-prep.bats index cc34a4a6b..d0d6e3065 100644 --- a/hack/post-install-prep.bats +++ b/hack/post-install-prep.bats @@ -129,6 +129,39 @@ case "$*" in 'get hr -A '*) echo "cozy-linstor linstor False dependency 'cozy-system/piraeus-operator' is not ready" echo "STUB-HR cozy-kubeovn kubeovn False install retries exhausted" ;; + 'get linstorsatellite '*) + if [ -n "${STUB_SAT_READ_FAIL:-}" ]; then + echo 'Error from server (Forbidden): linstorsatellites is forbidden' >&2 + exit 1 + fi + printf '%s\n' "${STUB_SAT_APPLIED:-True True True}" | tr ' ' '\n' ;; + 'get nodes '*InternalIP*) + if [ -n "${STUB_NODE_ROWS_FAIL:-}" ]; then + echo 'Error from server (Forbidden): nodes is forbidden' >&2 + exit 1 + fi + for n in ${STUB_NODES:-srv1 srv2 srv3}; do echo "$n 192.0.2.$((${#n} + 10))"; done ;; + 'get nodes '*) + if [ -n "${STUB_NODES_READ_FAIL:-}" ]; then + echo 'Error from server (Forbidden): nodes is forbidden' >&2 + exit 1 + fi + for n in ${STUB_NODES:-srv1 srv2 srv3}; do echo "$n"; done ;; + 'get storagepools.blockstor.cozystack.io '*freeCapacity*) + # STUB_POOLS_UNREPORTED names nodes whose satellite has not reported its + # pool yet: the object exists, its free capacity does not. + for n in ${STUB_NODES:-srv1 srv2 srv3}; do + case " ${STUB_POOLS_UNREPORTED:-} " in + *" $n "*) echo "$n " ;; + *) echo "$n 104857600" ;; + esac + done ;; + 'get pods -n cozy-linstor -l app=blockstor-satellite '*spec.nodeName*) + for n in ${STUB_NODES:-srv1 srv2 srv3}; do echo "blockstor-satellite-$n $n"; done ;; + 'get pods -n cozy-linstor -l app=blockstor-satellite '*Ready*) + for n in ${STUB_NODES:-srv1 srv2 srv3}; do echo True; done ;; + 'apply '*) + cat >> "${STUB_APPLIED:-/dev/null}" ;; 'get pods '*) if [ -n "${STUB_PODS_FAIL:-}" ]; then echo "Error from server (Forbidden): pods is forbidden" >&2 @@ -187,6 +220,8 @@ run_prep() { STUB_CLOCK="$d/clock" STUB_CALLS="$d/calls" \ STUB_HR_READY_AT=$2 STUB_DEPLOY_READY_AT=$3 \ STUB_EVENTS_FAIL="${4:-}" STUB_PODS_FAIL="${5:-}" \ + STUB_APPLIED="$d/applied" \ + COZY_STORAGE_BACKEND="${STUB_BACKEND-linstor}" \ PATH="$d/bin:$PATH" \ "$SCRIPT" > "$d/out" 2> "$d/err" && echo 0 > "$d/rc" || echo $? > "$d/rc" } @@ -516,3 +551,162 @@ run_prep() { fi rm -rf "$tmp" } + +# run_prep_blockstor +# The blockstor branch. Every wait before the satellite gate is answered as +# ready, so what a test sees is the gate and what follows it. +run_prep_blockstor() { + STUB_BACKEND=blockstor run_prep "$1" 0 0 +} + +@test "the prep refuses to run without being told the storage backend" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + STUB_BACKEND="" run_prep "$tmp" 0 0 + + # A guess here races the install it runs alongside and fails open to linstor. + [ "$(cat "$tmp/rc")" -eq 2 ] + grep -q 'COZY_STORAGE_BACKEND must be set' "$tmp/err" + if grep -q '^wait ' "$tmp/calls"; then + echo "the prep waited on the cluster before it knew the backend" >&2 + return 1 + fi + rm -rf "$tmp" +} + +@test "the prep refuses a storage backend it does not know" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + STUB_BACKEND=blokstor run_prep "$tmp" 0 0 + + [ "$(cat "$tmp/rc")" -eq 2 ] + grep -q "got 'blokstor'" "$tmp/err" + rm -rf "$tmp" +} + +@test "the blockstor prep registers a Node CR and a StoragePool for every node" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + run_prep_blockstor "$tmp" + + cat "$tmp/err" >&2 + [ "$(cat "$tmp/rc")" -eq 0 ] + grep -q '\[post-install-prep\] done' "$tmp/out" + for n in srv1 srv2 srv3; do + # The durable outcome, not whether a wait returned: the objects applied. + grep -A8 '^kind: Node$' "$tmp/applied" | grep -q "^ name: $n$" + grep -A8 '^kind: StoragePool$' "$tmp/applied" | grep -q "^ name: data.$n$" + grep -A8 '^kind: StoragePool$' "$tmp/applied" | grep -q "^ nodeName: $n$" + grep -q "^exec -n cozy-linstor blockstor-satellite-$n -- sh -ec" "$tmp/calls" + done + if grep '^UNBOUNDED ' "$tmp/calls" >&2; then + echo "unbounded kubectl calls on the blockstor path" >&2 + return 1 + fi + # Never the LINSTOR path's own controller. + if grep -q 'linstor-controller' "$tmp/calls"; then + echo "the blockstor prep reached for the LINSTOR controller" >&2 + return 1 + fi + rm -rf "$tmp" +} + +@test "the blockstor prep fails when a satellite never reports Applied" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + export STUB_SAT_APPLIED="True False True" + run_prep_blockstor "$tmp" + unset STUB_SAT_APPLIED + + [ "$(cat "$tmp/rc")" -ne 0 ] + grep -q '2 of 3 LinstorSatellite objects Applied' "$tmp/err" + if grep -q '^kind: StoragePool$' "$tmp/applied" 2>/dev/null; then + echo "pools were created past a satellite that never applied" >&2 + return 1 + fi + rm -rf "$tmp" +} + +@test "the satellite gate does not pass when both of its reads fail" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + # Two failed reads each count zero, and zero equals zero. + export STUB_SAT_READ_FAIL=1 STUB_NODES_READ_FAIL=1 + run_prep_blockstor "$tmp" + unset STUB_SAT_READ_FAIL STUB_NODES_READ_FAIL + + [ "$(cat "$tmp/rc")" -ne 0 ] + grep -q 'LinstorSatellite read exited 1, Node read exited 1' "$tmp/err" + rm -rf "$tmp" +} + +@test "the satellite gate does not pass on a cluster with no node" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + export STUB_NODES=" " STUB_SAT_APPLIED=" " + run_prep_blockstor "$tmp" + unset STUB_NODES STUB_SAT_APPLIED + + [ "$(cat "$tmp/rc")" -ne 0 ] + grep -q '0 of 0 LinstorSatellite objects Applied' "$tmp/err" + rm -rf "$tmp" +} + +@test "a failed node read stops the blockstor prep before it registers anything" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + # A read used as a for-list is not covered by set -e: it registers nothing + # and the script reports done. + export STUB_NODE_ROWS_FAIL=1 + run_prep_blockstor "$tmp" + unset STUB_NODE_ROWS_FAIL + + [ "$(cat "$tmp/rc")" -ne 0 ] + if grep -q '\[post-install-prep\] done' "$tmp/out"; then + echo "the prep reported done after a failed node read" >&2 + return 1 + fi + if grep -q '^kind: \(Node\|StoragePool\)$' "$tmp/applied" 2>/dev/null; then + echo "objects were registered after the node read failed" >&2 + return 1 + fi + rm -rf "$tmp" +} + +@test "the blockstor prep waits for every satellite to report its pool" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + run_prep_blockstor "$tmp" + + [ "$(cat "$tmp/rc")" -eq 0 ] + grep -q "waiting for every satellite to report its 'data' pool" "$tmp/out" + grep -q '^get storagepools.blockstor.cozystack.io .*freeCapacity' "$tmp/calls" + rm -rf "$tmp" +} + +@test "the blockstor prep fails when a satellite never reports its pool" { + tmp=$(mktemp -d) + prep_sandbox "$tmp" + + # A declared pool no satellite has opened is one placement cannot use, so + # the first three-replica volume would fail on it. + export STUB_POOLS_UNREPORTED=srv2 + run_prep_blockstor "$tmp" + unset STUB_POOLS_UNREPORTED + + [ "$(cat "$tmp/rc")" -ne 0 ] + grep -q "timed out after 300s waiting for 3 reported 'data' pools" "$tmp/err" + if grep -q 'applying StorageClasses' "$tmp/out"; then + echo "StorageClasses were applied over a pool nobody reported" >&2 + return 1 + fi + rm -rf "$tmp" +} diff --git a/packages/core/testing/Makefile b/packages/core/testing/Makefile index 861af1e44..c45d1c530 100644 --- a/packages/core/testing/Makefile +++ b/packages/core/testing/Makefile @@ -159,7 +159,7 @@ delete-cluster-container: # COZY_DISABLED_PACKAGES is the space-separated output of # hack/select-install.sh --disabled; empty installs the whole platform. install-cozystack: - docker exec -e COZY_APISERVER_ENDPOINT="${COZY_APISERVER_ENDPOINT}" -e COZY_LINSTOR_DRBD_ENABLED="${COZY_LINSTOR_DRBD_ENABLED}" -e COZY_E2E_STORAGE_CLASS="${COZY_E2E_STORAGE_CLASS}" -e COZY_DISABLED_PACKAGES="${COZY_DISABLED_PACKAGES}" "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozytest.sh hack/e2e-install-cozystack.bats' + docker exec -e COZY_APISERVER_ENDPOINT="${COZY_APISERVER_ENDPOINT}" -e COZY_LINSTOR_DRBD_ENABLED="${COZY_LINSTOR_DRBD_ENABLED}" -e COZY_STORAGE_BACKEND="${COZY_STORAGE_BACKEND}" -e COZY_E2E_STORAGE_CLASS="${COZY_E2E_STORAGE_CLASS}" -e COZY_DISABLED_PACKAGES="${COZY_DISABLED_PACKAGES}" "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozytest.sh hack/e2e-install-cozystack.bats' test-openapi: docker exec "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozytest.sh hack/e2e-test-openapi.bats' @@ -175,8 +175,10 @@ test-openapi: # reports into that same chainsaw-report.xml. # COZY_E2E_STORAGE_CLASS is passed both to script operations and as a Chainsaw # value for templated resource files; empty keeps the QEMU default, replicated. +# COZY_STORAGE_BACKEND reaches the install and the suite alike, so the pool +# guards read the backend the cluster was installed with; empty is linstor. test-chainsaw: - docker exec -e COZY_E2E_STORAGE_CLASS="${COZY_E2E_STORAGE_CLASS}" "${SANDBOX_NAME}" sh -c 'cd /workspace/hack/e2e-chainsaw && ../e2e-chainsaw-run.sh --set-string storageClass="$${COZY_E2E_STORAGE_CLASS:-replicated}" ${CHAINSAW_SUITES}' + docker exec -e COZY_E2E_STORAGE_CLASS="${COZY_E2E_STORAGE_CLASS}" -e COZY_STORAGE_BACKEND="${COZY_STORAGE_BACKEND}" "${SANDBOX_NAME}" sh -c 'cd /workspace/hack/e2e-chainsaw && ../e2e-chainsaw-run.sh --set-string storageClass="$${COZY_E2E_STORAGE_CLASS:-replicated}" ${CHAINSAW_SUITES}' collect-report: ## Collect the test report from the sandbox. docker exec "${SANDBOX_NAME}" sh -c 'cd /workspace && hack/cozyreport.sh cozyreport'