|
| 1 | +#!/usr/bin/env bats |
| 2 | + |
| 3 | +# Tests for quickstart scripts across different operating systems |
| 4 | + |
| 5 | +setup() { |
| 6 | + # Get the script directory |
| 7 | + SCRIPT_DIR="$BATS_TEST_DIRNAME/../static/quickstart" |
| 8 | +} |
| 9 | + |
| 10 | +# Shellcheck validation tests |
| 11 | +@test "check.sh passes shellcheck" { |
| 12 | + shellcheck "$SCRIPT_DIR/check.sh" |
| 13 | +} |
| 14 | + |
| 15 | +@test "install.sh passes shellcheck" { |
| 16 | + shellcheck "$SCRIPT_DIR/install.sh" |
| 17 | +} |
| 18 | + |
| 19 | +# OS detection tests |
| 20 | +@test "check.sh detects OS on Ubuntu 22.04" { |
| 21 | + run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c " |
| 22 | + apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1 |
| 23 | + bash static/quickstart/check.sh |
| 24 | + " |
| 25 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] # Script may exit 1 if ports in use |
| 26 | +} |
| 27 | + |
| 28 | +@test "check.sh detects OS on Ubuntu 24.04" { |
| 29 | + run docker run --rm -v "$PWD:/work" -w /work ubuntu:24.04 bash -c " |
| 30 | + apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1 |
| 31 | + bash static/quickstart/check.sh |
| 32 | + " |
| 33 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] |
| 34 | +} |
| 35 | + |
| 36 | +@test "check.sh detects OS on Debian Bookworm" { |
| 37 | + run docker run --rm -v "$PWD:/work" -w /work debian:bookworm bash -c " |
| 38 | + apt-get update -qq && apt-get install -y -qq lsof curl >/dev/null 2>&1 |
| 39 | + bash static/quickstart/check.sh |
| 40 | + " |
| 41 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] |
| 42 | +} |
| 43 | + |
| 44 | +@test "check.sh detects OS on Fedora 39" { |
| 45 | + run docker run --rm -v "$PWD:/work" -w /work fedora:39 bash -c " |
| 46 | + dnf install -y -q lsof curl >/dev/null 2>&1 |
| 47 | + bash static/quickstart/check.sh |
| 48 | + " |
| 49 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] |
| 50 | +} |
| 51 | + |
| 52 | +@test "check.sh detects OS on Alpine Linux" { |
| 53 | + run docker run --rm -v "$PWD:/work" -w /work alpine:latest sh -c " |
| 54 | + apk add --no-cache bash lsof curl >/dev/null 2>&1 |
| 55 | + bash static/quickstart/check.sh |
| 56 | + " |
| 57 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] |
| 58 | +} |
| 59 | + |
| 60 | +# Script syntax validation |
| 61 | +@test "check.sh has valid bash syntax" { |
| 62 | + bash -n "$SCRIPT_DIR/check.sh" |
| 63 | +} |
| 64 | + |
| 65 | +@test "install.sh has valid bash syntax" { |
| 66 | + bash -n "$SCRIPT_DIR/install.sh" |
| 67 | +} |
| 68 | + |
| 69 | +# Dependency checking tests |
| 70 | +@test "check.sh detects missing docker" { |
| 71 | + run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c " |
| 72 | + apt-get update -qq >/dev/null 2>&1 |
| 73 | + PATH=/usr/bin:/bin bash static/quickstart/check.sh |
| 74 | + " |
| 75 | + echo "Output: $output" |
| 76 | + [[ "$output" =~ "docker" ]] || [[ "$output" =~ "Docker" ]] |
| 77 | +} |
| 78 | + |
| 79 | +@test "check.sh detects docker when installed" { |
| 80 | + skip "Requires docker-in-docker setup" |
| 81 | +} |
| 82 | + |
| 83 | +# Port checking tests |
| 84 | +@test "check.sh port validation works" { |
| 85 | + run bash -c "grep -q 'check_port.*8080' $SCRIPT_DIR/check.sh" |
| 86 | + [ "$status" -eq 0 ] |
| 87 | + |
| 88 | + run bash -c "grep -q 'check_port.*8443' $SCRIPT_DIR/check.sh" |
| 89 | + [ "$status" -eq 0 ] |
| 90 | + |
| 91 | + run bash -c "grep -q 'check_port.*9443' $SCRIPT_DIR/check.sh" |
| 92 | + [ "$status" -eq 0 ] |
| 93 | +} |
| 94 | + |
| 95 | +# Script structure tests |
| 96 | +@test "check.sh defines print_status function" { |
| 97 | + grep -q "print_status()" "$SCRIPT_DIR/check.sh" |
| 98 | +} |
| 99 | + |
| 100 | +@test "check.sh defines print_warning function" { |
| 101 | + grep -q "print_warning()" "$SCRIPT_DIR/check.sh" |
| 102 | +} |
| 103 | + |
| 104 | +@test "check.sh defines check_port function" { |
| 105 | + grep -q "check_port()" "$SCRIPT_DIR/check.sh" |
| 106 | +} |
| 107 | + |
| 108 | +@test "install.sh defines install_otdfctl function" { |
| 109 | + grep -q "install_otdfctl()" "$SCRIPT_DIR/install.sh" |
| 110 | +} |
| 111 | + |
| 112 | +# Variable quoting tests (shellcheck compliance) |
| 113 | +@test "check.sh properly quotes variables in print_status" { |
| 114 | + grep -q 'if \[ "$1" -eq 0 \]' "$SCRIPT_DIR/check.sh" |
| 115 | +} |
| 116 | + |
| 117 | +@test "check.sh properly quotes variables in check_port" { |
| 118 | + grep -q 'lsof -Pi :"$1"' "$SCRIPT_DIR/check.sh" |
| 119 | +} |
| 120 | + |
| 121 | +@test "install.sh properly declares TEMP_DIR variable" { |
| 122 | + grep -q 'local TEMP_DIR$' "$SCRIPT_DIR/install.sh" |
| 123 | + grep -q 'TEMP_DIR=$(mktemp -d)' "$SCRIPT_DIR/install.sh" |
| 124 | +} |
| 125 | + |
| 126 | +# Install script validation |
| 127 | +@test "install.sh can detect platform architecture" { |
| 128 | + run bash -c "source $SCRIPT_DIR/install.sh; detect_os_and_arch" |
| 129 | + echo "Output: $output" |
| 130 | + # Should not error out |
| 131 | + [ "$status" -eq 0 ] || [ "$status" -eq 1 ] |
| 132 | +} |
| 133 | + |
| 134 | +# Error handling tests |
| 135 | +@test "check.sh handles missing lsof gracefully on Ubuntu" { |
| 136 | + run docker run --rm -v "$PWD:/work" -w /work ubuntu:22.04 bash -c " |
| 137 | + bash static/quickstart/check.sh |
| 138 | + " |
| 139 | + # Should exit with error but not crash |
| 140 | + [[ "$output" =~ "lsof" ]] || [ "$status" -ne 127 ] |
| 141 | +} |
| 142 | + |
| 143 | +# Output format tests |
| 144 | +@test "check.sh produces readable output" { |
| 145 | + run bash "$SCRIPT_DIR/check.sh" 2>&1 || true |
| 146 | + # Should have some output |
| 147 | + [ -n "$output" ] |
| 148 | +} |
| 149 | + |
| 150 | +@test "check.sh uses color codes" { |
| 151 | + grep -q "GREEN=" "$SCRIPT_DIR/check.sh" |
| 152 | + grep -q "RED=" "$SCRIPT_DIR/check.sh" |
| 153 | + grep -q "YELLOW=" "$SCRIPT_DIR/check.sh" |
| 154 | +} |
0 commit comments