|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Bash-specific e2e test for docker model list command with fixture validation |
| 4 | +# This script validates the output against the expected fixture |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +TEST_MODEL="ai/smollm2" |
| 9 | +FAILED_TESTS=0 |
| 10 | +TOTAL_TESTS=0 |
| 11 | + |
| 12 | +# Colors for output |
| 13 | +RED='\033[0;31m' |
| 14 | +GREEN='\033[0;32m' |
| 15 | +NC='\033[0m' # No Color |
| 16 | + |
| 17 | +log_info() { |
| 18 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 19 | +} |
| 20 | + |
| 21 | +log_success() { |
| 22 | + echo -e "${GREEN}[PASS]${NC} $1" |
| 23 | +} |
| 24 | + |
| 25 | +log_error() { |
| 26 | + echo -e "${RED}[FAIL]${NC} $1" |
| 27 | + FAILED_TESTS=$((FAILED_TESTS + 1)) |
| 28 | +} |
| 29 | + |
| 30 | +# Load fixture file and normalize lines |
| 31 | +load_fixture() { |
| 32 | + local fixture_path="$1" |
| 33 | + if [ ! -f "$fixture_path" ]; then |
| 34 | + log_error "Fixture file not found: $fixture_path" |
| 35 | + return 1 |
| 36 | + fi |
| 37 | + # Remove trailing whitespace |
| 38 | + sed 's/[[:space:]]*$//' "$fixture_path" |
| 39 | +} |
| 40 | + |
| 41 | +run_test_with_fixture_validation() { |
| 42 | + local test_name="$1" |
| 43 | + local command="$2" |
| 44 | + local fixture_path="$3" |
| 45 | + local expected_exit_code="${4:-0}" |
| 46 | + |
| 47 | + TOTAL_TESTS=$((TOTAL_TESTS + 1)) |
| 48 | + log_info "Running test: $test_name" |
| 49 | + |
| 50 | + # Load expected output from fixture |
| 51 | + expected_output=$(load_fixture "$fixture_path") |
| 52 | + |
| 53 | + if [ $? -ne 0 ]; then |
| 54 | + log_error "$test_name - Failed to load fixture" |
| 55 | + return 1 |
| 56 | + fi |
| 57 | + |
| 58 | + # Run command and capture output and exit code |
| 59 | + set +e |
| 60 | + output=$(eval "$command" 2>&1) |
| 61 | + exit_code=$? |
| 62 | + set -e |
| 63 | + |
| 64 | + # Normalize output: remove leading/trailing blank lines and trailing spaces |
| 65 | + normalized_output=$(echo "$output" | sed 's/[[:space:]]*$//') |
| 66 | + |
| 67 | + if [ $exit_code -eq $expected_exit_code ] && [ "$normalized_output" = "$expected_output" ]; then |
| 68 | + log_success "$test_name" |
| 69 | + return 0 |
| 70 | + else |
| 71 | + log_error "$test_name" |
| 72 | + echo "Command: $command" |
| 73 | + echo "Expected exit code: $expected_exit_code, got: $exit_code" |
| 74 | + echo "Expected output:" |
| 75 | + echo "$expected_output" |
| 76 | + echo "Actual normalized output:" |
| 77 | + echo "$normalized_output" |
| 78 | + return 1 |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +# Check prerequisites |
| 83 | +log_info "Checking prerequisites..." |
| 84 | + |
| 85 | +# Ensure we're running from the correct directory |
| 86 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 87 | +PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" |
| 88 | +cd "$PROJECT_ROOT" |
| 89 | + |
| 90 | +# Check if docker model is available |
| 91 | +if ! command -v docker &> /dev/null; then |
| 92 | + log_error "docker command not found" |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +# Check if model runner is running |
| 97 | +if ! docker model status | grep -q "Docker Model Runner is running"; then |
| 98 | + log_error "Docker Model Runner is not running" |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +# Check if test model is available |
| 103 | +if ! docker model list | grep -q "$TEST_MODEL"; then |
| 104 | + log_info "Test model $TEST_MODEL not found, pulling..." |
| 105 | + docker model pull "$TEST_MODEL" |
| 106 | +fi |
| 107 | + |
| 108 | +log_info "Starting fixture validation test..." |
| 109 | + |
| 110 | +# Test: Fixture format validation |
| 111 | +run_test_with_fixture_validation "Fixture format validation" \ |
| 112 | + "docker model list" \ |
| 113 | + "e2e/fixtures/table_format/with_models.txt" |
| 114 | + |
| 115 | +# Summary |
| 116 | +echo |
| 117 | +log_info "Test Summary:" |
| 118 | +echo "Total tests: $TOTAL_TESTS" |
| 119 | +echo "Failed tests: $FAILED_TESTS" |
| 120 | +echo "Passed tests: $((TOTAL_TESTS - FAILED_TESTS))" |
| 121 | + |
| 122 | +if [ $FAILED_TESTS -eq 0 ]; then |
| 123 | + log_success "Fixture validation test passed!" |
| 124 | + exit 0 |
| 125 | +else |
| 126 | + log_error "$FAILED_TESTS tests failed" |
| 127 | + exit 1 |
| 128 | +fi |
0 commit comments