Torrent-Manager/test-installation.sh

192 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Print section header
print_section() {
echo -e "\n${GREEN}====== $1 ======${NC}"
}
# Error handling
set -e
trap 'echo -e "${RED}An error occurred. Test failed.${NC}"; exit 1' ERR
# Setup test environment
print_section "Setting up test environment"
CURRENT_DIR=$(pwd)
TEST_DIR="$CURRENT_DIR/test-install"
mkdir -p "$TEST_DIR"
CONFIG_DIR="$TEST_DIR/config"
mkdir -p "$CONFIG_DIR"
# Copy necessary files
print_section "Copying files"
mkdir -p "$TEST_DIR"
# Copy only essential files (not the test directory itself)
find "$CURRENT_DIR" -maxdepth 1 -not -path "*test-install*" -not -path "$CURRENT_DIR" -exec cp -r {} "$TEST_DIR/" \;
cd "$TEST_DIR"
# Check .NET SDK installation
print_section "Checking .NET SDK"
dotnet --version
if [ $? -ne 0 ]; then
echo -e "${RED}.NET SDK is not installed or not in PATH. Please install .NET SDK 7.0.${NC}"
exit 1
fi
# Build the application
print_section "Building application"
dotnet build -c Release
if [ $? -ne 0 ]; then
echo -e "${RED}Build failed. See errors above.${NC}"
exit 1
fi
echo -e "${GREEN}Build successful.${NC}"
# Create database configuration
print_section "Creating database configuration"
echo '{
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=torrentmanager_test;Username=postgres;Password=postgres"
}
}' > "$CONFIG_DIR/appsettings.json"
cp "$CONFIG_DIR/appsettings.json" "$TEST_DIR/appsettings.json"
echo -e "${GREEN}Database configuration created at $CONFIG_DIR/appsettings.json${NC}"
# Check if PostgreSQL is running
print_section "Checking PostgreSQL"
if command -v pg_isready >/dev/null 2>&1; then
pg_isready
PG_READY=$?
else
echo -e "${YELLOW}PostgreSQL client tools not found.${NC}"
PG_READY=1
fi
if [ $PG_READY -ne 0 ]; then
echo -e "${YELLOW}PostgreSQL is not running or not installed.${NC}"
echo -e "${YELLOW}This test expects PostgreSQL to be available with a 'postgres' user.${NC}"
echo -e "${YELLOW}Either install PostgreSQL or modify the connection string in appsettings.json.${NC}"
# Continue anyway for testing other aspects
echo -e "${YELLOW}Continuing test without database functionality.${NC}"
# Update appsettings.json to use SQLite instead
echo '{
"ConnectionStrings": {
"DefaultConnection": "Data Source=torrentmanager_test.db"
}
}' > "$TEST_DIR/appsettings.json"
echo -e "${YELLOW}Using SQLite database instead.${NC}"
# Install Entity Framework Core SQLite provider
dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 7.0.17
# Update DatabaseProvider to use SQLite
if [ -f "$TEST_DIR/src/Data/TorrentManagerContextFactory.cs" ]; then
# Replace Npgsql with SQLite in DbContext factory
sed -i 's/UseNpgsql/UseSqlite/g' "$TEST_DIR/src/Data/TorrentManagerContextFactory.cs"
echo -e "${YELLOW}Modified database provider to use SQLite.${NC}"
fi
else
# Create test database
echo -e "${GREEN}Creating test database...${NC}"
psql -U postgres -c "DROP DATABASE IF EXISTS torrentmanager_test;" || true
psql -U postgres -c "CREATE DATABASE torrentmanager_test;"
fi
# Install Entity Framework CLI if needed
if ! dotnet tool list -g | grep "dotnet-ef" > /dev/null; then
echo -e "${GREEN}Installing Entity Framework Core tools...${NC}"
dotnet tool install --global dotnet-ef --version 7.0.15
fi
# Apply migrations
print_section "Applying database migrations"
dotnet ef database update || true
if [ $? -eq 0 ]; then
echo -e "${GREEN}Migrations applied successfully.${NC}"
else
echo -e "${YELLOW}Failed to apply migrations, but continuing test.${NC}"
fi
# Test run the application (with timeout)
print_section "Testing application startup"
timeout 30s dotnet run --urls=http://localhost:5555 &
APP_PID=$!
# Wait for the app to start
echo -e "${GREEN}Waiting for application to start...${NC}"
sleep 5
# Try to access the API
print_section "Testing API endpoints"
MAX_ATTEMPTS=30
ATTEMPT=0
API_STATUS=1
echo -e "${GREEN}Waiting for API to become available...${NC}"
while [ $ATTEMPT -lt $MAX_ATTEMPTS ] && [ $API_STATUS -ne 0 ]; do
if command -v curl >/dev/null 2>&1; then
curl -s http://localhost:5555/swagger/index.html > /dev/null
API_STATUS=$?
else
# Try using wget if curl is not available
if command -v wget >/dev/null 2>&1; then
wget -q --spider http://localhost:5555/swagger/index.html
API_STATUS=$?
else
echo -e "${YELLOW}Neither curl nor wget found. Cannot test API.${NC}"
break
fi
fi
if [ $API_STATUS -ne 0 ]; then
echo -n "."
sleep 1
ATTEMPT=$((ATTEMPT+1))
fi
done
echo "" # New line after progress dots
# Kill the app
kill $APP_PID 2>/dev/null || true
if [ $API_STATUS -eq 0 ]; then
echo -e "${GREEN}API successfully responded.${NC}"
else
echo -e "${YELLOW}API did not respond within the timeout period.${NC}"
echo -e "${YELLOW}This may be normal if database migrations are slow or there are other startup delays.${NC}"
fi
# Check file permissions and structure
print_section "Checking build outputs"
if [ -f "$TEST_DIR/bin/Release/net7.0/TransmissionRssManager.dll" ]; then
echo -e "${GREEN}Application was built successfully.${NC}"
else
echo -e "${RED}Missing application binaries.${NC}"
fi
# Clean up test resources
print_section "Test completed"
if [ "$1" != "--keep" ]; then
echo -e "${GREEN}Cleaning up test resources...${NC}"
# Drop test database if PostgreSQL is available
pg_isready > /dev/null && psql -U postgres -c "DROP DATABASE IF EXISTS torrentmanager_test;" || true
# Remove test directory
rm -rf "$TEST_DIR"
echo -e "${GREEN}Test directory removed.${NC}"
else
echo -e "${GREEN}Test resources kept as requested.${NC}"
echo -e "${GREEN}Test directory: $TEST_DIR${NC}"
fi
echo -e "${GREEN}Installation test completed.${NC}"