#!/bin/bash # Test script for slim - boots containers and verifies CMD inference/override. # Requires: podman, qemu-system-x86_64, KVM, and a kernel at # $XDG_DATA_HOME/slim-rs/vmlinuz. set -u SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SLIM_BIN="$SCRIPT_DIR/../target/debug/slim" TIMEOUT=120 pass=0 fail=0 report() { if [ "$1" = "pass" ]; then echo " PASS: $2" pass=$((pass + 1)) else echo " FAIL: $2" fail=$((fail + 1)) fi } check_output() { output="$1" marker="$2" label="$3" if echo "$output" | grep -q "$marker"; then report pass "$label" else report fail "$label (expected marker: $marker)" fi } cleanup() { "$SLIM_BIN" image rm "$1" >/dev/null 2>&1 || true podman image rm "$2" >/dev/null 2>&1 || true } test_distro() { distro="$1" from="$2" extra_setup="$3" echo "=== Testing $distro ===" work="$(mktemp -d)" # === Test 1: CMD-inferred === echo "-- Test 1: CMD-inferred" cat > "$work/Containerfile.infer" </dev/null 2>&1 && echo PING_OK; ping -c1 example.org >/dev/null 2>&1 && echo DNS_OK; poweroff -f"] EOF img="slim-test-$distro-infer" if podman build -t "$img" -f "$work/Containerfile.infer" >/dev/null 2>&1; then "$SLIM_BIN" build qcow2 "$img" >/dev/null 2>&1 output=$(timeout "$TIMEOUT" "$SLIM_BIN" run "$img" 2>&1 || true) check_output "$output" "CMD_INFERRED_OK" "$distro CMD-inferred" check_output "$output" "PING_OK" "$distro ping 1.1.1.1" check_output "$output" "DNS_OK" "$distro DNS lookup" else report fail "$distro CMD-inferred (podman build failed)" fi cleanup "$img" "$img" # === Test 2: slim run --cmd override === echo "-- Test 2: slim run --cmd override" cat > "$work/Containerfile.run" </dev/null 2>&1; then "$SLIM_BIN" build qcow2 "$img" >/dev/null 2>&1 output=$(timeout "$TIMEOUT" "$SLIM_BIN" run "$img" --cmd 'echo RUN_OVERRIDE_OK; poweroff -f' 2>&1 || true) check_output "$output" "RUN_OVERRIDE_OK" "$distro run --cmd override" else report fail "$distro run --cmd override (podman build failed)" fi cleanup "$img" "$img" # === Test 3: slim build --cmd override === echo "-- Test 3: slim build --cmd override" cat > "$work/Containerfile.build" </dev/null 2>&1; then "$SLIM_BIN" build qcow2 "$img" --cmd 'echo BUILD_OVERRIDE_OK; poweroff -f' >/dev/null 2>&1 output=$(timeout "$TIMEOUT" "$SLIM_BIN" run "$img" 2>&1 || true) check_output "$output" "BUILD_OVERRIDE_OK" "$distro build --cmd override" else report fail "$distro build --cmd override (podman build failed)" fi cleanup "$img" "$img" rm -rf "$work" } echo "Building slim..." cargo build 2>&1 test_distro "alpine" "alpine:latest" "" test_distro "archlinux" "archlinux:latest" "RUN pacman -Sy --noconfirm iputils iproute2 util-linux; pacman -Sc --noconfirm" echo "" echo "=== Results: $pass passed, $fail failed ===" [ "$fail" -eq 0 ]