|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# Colors for output |
| 6 | +RED='\033[0;31m' |
| 7 | +GREEN='\033[0;32m' |
| 8 | +YELLOW='\033[1;33m' |
| 9 | +NC='\033[0m' # No Color |
| 10 | + |
| 11 | +# Docker image name |
| 12 | +IMAGE_NAME="falco-github-action-test" |
| 13 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 14 | + |
| 15 | +# Build Docker image if it doesn't exist |
| 16 | +if ! docker image inspect "$IMAGE_NAME" &>/dev/null; then |
| 17 | + echo -e "${YELLOW}Building Docker image...${NC}" |
| 18 | + docker build -t "$IMAGE_NAME" "$SCRIPT_DIR" |
| 19 | +fi |
| 20 | + |
| 21 | +# Test counter |
| 22 | +PASSED=0 |
| 23 | +FAILED=0 |
| 24 | +TOTAL=0 |
| 25 | + |
| 26 | +# Create tmp directory if it doesn't exist |
| 27 | +TMP_DIR="$SCRIPT_DIR/tmp" |
| 28 | +mkdir -p "$TMP_DIR" |
| 29 | + |
| 30 | +# Function to run a test |
| 31 | +run_test() { |
| 32 | + local file=$1 |
| 33 | + local options=$2 |
| 34 | + local should_pass=$3 |
| 35 | + local description=$4 |
| 36 | + |
| 37 | + TOTAL=$((TOTAL + 1)) |
| 38 | + |
| 39 | + echo -n "Testing $description... " |
| 40 | + |
| 41 | + # Run falco lint in Docker container |
| 42 | + # Arguments: $1=subcommand, $2=options, $3=target (as per entrypoint.sh and action.yml) |
| 43 | + if docker run --rm \ |
| 44 | + -v "$SCRIPT_DIR:/workspace" \ |
| 45 | + -w /workspace \ |
| 46 | + "$IMAGE_NAME" \ |
| 47 | + lint "$options" "$file" > "$TMP_DIR/falco_test_output.txt" 2>&1; then |
| 48 | + exit_code=0 |
| 49 | + else |
| 50 | + exit_code=$? |
| 51 | + fi |
| 52 | + |
| 53 | + # Check if test result matches expectation |
| 54 | + if [ "$should_pass" = "true" ]; then |
| 55 | + if [ $exit_code -eq 0 ]; then |
| 56 | + echo -e "${GREEN}PASSED${NC}" |
| 57 | + PASSED=$((PASSED + 1)) |
| 58 | + return 0 |
| 59 | + else |
| 60 | + echo -e "${RED}FAILED${NC} (expected success, got exit code $exit_code)" |
| 61 | + echo "Output:" |
| 62 | + cat "$TMP_DIR/falco_test_output.txt" | sed 's/^/ /' |
| 63 | + FAILED=$((FAILED + 1)) |
| 64 | + return 1 |
| 65 | + fi |
| 66 | + else |
| 67 | + if [ $exit_code -ne 0 ]; then |
| 68 | + echo -e "${GREEN}PASSED${NC}" |
| 69 | + PASSED=$((PASSED + 1)) |
| 70 | + return 0 |
| 71 | + else |
| 72 | + echo -e "${RED}FAILED${NC} (expected failure, got exit code 0)" |
| 73 | + echo "Output:" |
| 74 | + cat "$TMP_DIR/falco_test_output.txt" | sed 's/^/ /' |
| 75 | + FAILED=$((FAILED + 1)) |
| 76 | + return 1 |
| 77 | + fi |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +echo "Running Falco VCL/ACL tests..." |
| 82 | +echo "" |
| 83 | + |
| 84 | +# Test valid VCL file |
| 85 | +run_test "test/vcl/valid.vcl" "" "true" "valid.vcl" |
| 86 | + |
| 87 | +# Test valid VCL file with include |
| 88 | +run_test "test/vcl/valid_with_include.vcl" "-I test/vcl/includes" "true" "valid_with_include.vcl (with -I option)" |
| 89 | + |
| 90 | +# Test invalid VCL file |
| 91 | +run_test "test/vcl/invalid_syntax.vcl" "" "false" "invalid_syntax.vcl" |
| 92 | + |
| 93 | +# Test valid ACL file |
| 94 | +run_test "test/acl/valid.acl" "" "true" "valid.acl" |
| 95 | + |
| 96 | +# Test invalid ACL files |
| 97 | +run_test "test/acl/invalid_syntax.acl" "" "false" "invalid_syntax.acl" |
| 98 | +run_test "test/acl/invalid_ip.acl" "" "false" "invalid_ip.acl" |
| 99 | +run_test "test/acl/invalid_range.acl" "" "false" "invalid_range.acl" |
| 100 | + |
| 101 | +echo "" |
| 102 | +echo "==========================================" |
| 103 | +echo "Test Results: $PASSED passed, $FAILED failed, $TOTAL total" |
| 104 | + |
| 105 | +if [ $FAILED -eq 0 ]; then |
| 106 | + echo -e "${GREEN}All tests passed!${NC}" |
| 107 | + exit 0 |
| 108 | +else |
| 109 | + echo -e "${RED}Some tests failed!${NC}" |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | + |
0 commit comments