#!/usr/bin/env bash # Copyright 2026 The KCP Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -euo pipefail # Builds the kcp WASM shard, runs it through Deno, and verifies the Fetch CRD # end to end with kubectl: # * the CRD is served by the WASM kcp # * a Fetch starts Unresolved # * adding a URL makes the controller fetch it via Deno's fetch() # * the response body shows up in `kubectl get fetches` / `-o yaml` repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" port="${PORT:-18080}" host="${HOST:-127.0.0.1}" workdir="$(mktemp -d "${TMPDIR:-/tmp}/kcp-wasm-verify.XXXXXX")" server_pid="" cleanup() { if [[ -n "${server_pid}" ]] && kill -0 "${server_pid}" 2>/dev/null; then kill "${server_pid}" 2>/dev/null || true wait "${server_pid}" 2>/dev/null || true fi if [[ "${KEEP_WORKDIR:-false}" == "true" ]]; then echo "artifacts kept in ${workdir}" else rm -rf "${workdir}" fi } trap cleanup EXIT fail() { echo "FAIL: $*" >&2 echo "--- server log ---" >&2 cat "${workdir}/server.log" >&2 2>/dev/null || true exit 1 } assert_contains() { local haystack="$1" needle="$2" what="$3" if [[ "${haystack}" != *"${needle}"* ]]; then fail "${what}: expected to find '${needle}' in:\n${haystack}" fi } for bin in deno kubectl curl; do command -v "${bin}" >/dev/null || fail "${bin} is required" done echo "== building kcp-wasm ==" "${repo_root}/scripts/build-kcp-wasm.sh" "${workdir}/bin" >/dev/null state_file="${workdir}/state.json" start_server() { echo "== starting Deno host on http://${host}:${port} ==" deno run --allow-read --allow-net --allow-env --allow-write \ "${repo_root}/scripts/kcp_wasm.ts" \ --wasm="${workdir}/bin/kcp.wasm" \ --wasm-exec="${workdir}/bin/wasm_exec.js" \ --state="${state_file}" \ --host="${host}" --port="${port}" --verbose \ >>"${workdir}/server.log" 2>&1 & server_pid=$! local ready=false for _ in $(seq 1 120); do if ! kill -0 "${server_pid}" 2>/dev/null; then fail "Deno host exited during startup" fi code="$(curl -s -o /dev/null -w '%{http_code}' "http://${host}:${port}/readyz" 2>/dev/null || true)" if [[ "${code}" == "200" ]]; then ready=true break fi sleep 0.25 done [[ "${ready}" == "true" ]] || fail "kcp-wasm did not become ready" } stop_server() { if [[ -n "${server_pid}" ]] && kill -0 "${server_pid}" 2>/dev/null; then kill "${server_pid}" 2>/dev/null || true wait "${server_pid}" 2>/dev/null || true fi server_pid="" } start_server cat >"${workdir}/kubeconfig" </dev/null | tee "${workdir}/version.txt" assert_contains "$(cat "${workdir}/version.txt")" "Server Version" "server version" echo "== CRD is served by the WASM kcp ==" crds="$(kubectl get crds)" echo "${crds}" assert_contains "${crds}" "fetches.fetch.kcp.io" "CRD discovery" echo "== apply a Fetch with a URL; it should resolve and store the body ==" cat >"${workdir}/demo.yaml" <"${workdir}/later.yaml" <