dropshell/runner/test.sh
Your Name a3914f8167
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 21s
.
2025-05-10 13:01:24 +12:00

147 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
set -e
# Simple test script to verify that the runner works properly
echo "Testing Runner - Dropshell Command Execution Library"
echo "=================================================="
echo
# Ensure the binary is built
if [ ! -f "build/runner" ]; then
echo "Building the project first..."
./build.sh
fi
# Run a simple echo command
echo "Test 1: Basic command execution"
JSON=$(cat <<'EOF'
{"command":"echo","args":["Hello from runner test!"]}
EOF
)
BASE64=$(echo -n "$JSON" | base64 -w0)
echo "JSON: $JSON"
echo "Running command..."
build/runner "$BASE64"
if [ $? -eq 0 ]; then
echo "✅ Test 1 passed"
else
echo "❌ Test 1 failed"
exit 1
fi
echo
# Test with environment variables
echo "Test 2: Environment variables"
JSON=$(cat <<'EOF'
{"command":"bash","args":["-c","echo Value of TEST_VAR: $TEST_VAR"],"env":{"TEST_VAR":"This is a test environment variable"}}
EOF
)
BASE64=$(echo -n "$JSON" | base64 -w0)
echo "JSON: $JSON"
echo "Running command..."
build/runner "$BASE64"
if [ $? -eq 0 ]; then
echo "✅ Test 2 passed"
else
echo "❌ Test 2 failed"
exit 1
fi
echo
# Test silent mode
echo "Test 3: Silent mode"
JSON=$(cat <<'EOF'
{"command":"echo","args":["This should not be displayed"],"options":{"silent":true}}
EOF
)
BASE64=$(echo -n "$JSON" | base64 -w0)
echo "JSON: $JSON"
echo "Running command (no output expected)..."
OUTPUT=$(build/runner "$BASE64")
if [ $? -eq 0 ] && [ -z "$OUTPUT" ]; then
echo "✅ Test 3 passed"
else
echo "❌ Test 3 failed, unexpected output: '$OUTPUT'"
exit 1
fi
echo
# Test return code handling
echo "Test 4: Error return code"
JSON=$(cat <<'EOF'
{"command":"false"}
EOF
)
BASE64=$(echo -n "$JSON" | base64)
echo "JSON: $JSON"
echo "Running command (should fail)..."
build/runner "$BASE64" || true
RC=$?
if [ $RC -ne 0 ]; then
echo "✅ Test 4 passed (command correctly reported failure)"
else
echo "❌ Test 4 failed (command should have returned non-zero)"
exit 1
fi
echo
# Test working directory feature
echo "Test 5: Working directory"
JSON=$(cat <<'EOF'
{"command":"pwd","working_directory":"/tmp"}
EOF
)
BASE64=$(echo -n "$JSON" | base64)
echo "JSON: $JSON"
echo "Running command (should show /tmp)..."
OUTPUT=$(build/runner "$BASE64")
if [ $? -eq 0 ] && [[ "$OUTPUT" == "/tmp"* ]]; then
echo "✅ Test 5 passed (command ran in the correct directory)"
else
echo "❌ Test 5 failed, unexpected output: '$OUTPUT'"
exit 1
fi
echo
# Optional SSH test (disabled by default)
# Set ENABLE_SSH_TEST=1 to enable this test
if [ "${ENABLE_SSH_TEST}" = "1" ]; then
echo "Test 6: SSH to localhost (make sure SSH server is running and you can connect to localhost)"
if [ -f "examples/local_ssh.json" ]; then
echo "Using examples/local_ssh.json for the test..."
./run.sh examples/local_ssh.json || {
echo "❌ Test 6 failed"
echo "Note: This test requires SSH server running on localhost and proper key-based authentication."
echo "Check your SSH configuration or disable this test."
exit 1
}
echo "✅ Test 6 passed"
else
echo "❌ Test 6 failed: examples/local_ssh.json not found"
exit 1
fi
echo
echo "Test 7: SSH with working directory (make sure SSH server is running and you can connect to localhost)"
if [ -f "examples/ssh_working_dir.json" ]; then
echo "Using examples/ssh_working_dir.json for the test..."
OUTPUT=$(./run.sh examples/ssh_working_dir.json | grep "/tmp")
if [[ "$OUTPUT" == *"/tmp"* ]]; then
echo "✅ Test 7 passed (SSH command ran in the correct directory)"
else
echo "❌ Test 7 failed, directory was not changed correctly"
echo "Note: This test requires SSH server running on localhost and proper key-based authentication."
exit 1
fi
else
echo "❌ Test 7 failed: examples/ssh_working_dir.json not found"
exit 1
fi
echo
else
echo "Test 6-7: SSH tests disabled (set ENABLE_SSH_TEST=1 to enable)"
echo
fi
echo "All tests passed! 🎉"