diff --git a/test.sh b/test.sh index 198076a..e94388e 100755 --- a/test.sh +++ b/test.sh @@ -6,7 +6,7 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" TEST_DIR="${SCRIPT_DIR}/test_tmp" TEST_TOKEN="test-token-$(date +%s)" CONTAINER_NAME="sos-test-$(date +%s)" -TEST_PORT=$((7700 + RANDOM % 100)) +TEST_PORT="" # Will be set dynamically after container starts CLEANUP_NEEDED=false # Colors for output @@ -85,7 +85,6 @@ function setup_test_environment() { # Create test directory structure rm -rf "${TEST_DIR}" - mkdir -p "${TEST_DIR}/storage" mkdir -p "${TEST_DIR}/test_files" mkdir -p "${TEST_DIR}/config" @@ -118,19 +117,18 @@ EOF } function start_test_server() { - log_info "Starting SOS test server on port ${TEST_PORT}..." + log_info "Starting SOS test server..." # Ensure the config file exists and is readable if [ ! -f "${TEST_DIR}/config/sos_config.json" ]; then die "Config file ${TEST_DIR}/config/sos_config.json does not exist" fi - # Start the container without mounting config file (will use docker cp instead) - # Only mount the storage directory + # Start the container without specifying host port (Docker will assign one) + # No volume mounts - will use docker cp/exec for file operations local container_id=$(docker run -d \ --name "${CONTAINER_NAME}" \ - -p "${TEST_PORT}:80" \ - -v "${TEST_DIR}/storage:/data/storage" \ + -p 80 \ gitea.jde.nz/public/simple-object-server 2>&1) if [ $? -ne 0 ]; then @@ -140,6 +138,14 @@ function start_test_server() { log_info "Container started with ID: ${container_id:0:12}" + # Get the dynamically assigned port + TEST_PORT=$(docker port "${CONTAINER_NAME}" 80 | cut -d: -f2) + if [ -z "${TEST_PORT}" ]; then + log_error "Failed to get container port" + die "Failed to get container port mapping" + fi + log_info "Container mapped to port: ${TEST_PORT}" + # Copy the config file into the container using docker cp log_info "Copying config file into container..." docker cp "${TEST_DIR}/config/sos_config.json" "${CONTAINER_NAME}:/data/sos_config.json" @@ -149,6 +155,12 @@ function start_test_server() { die "Failed to copy config file" fi + # Ensure storage directory exists in container + docker exec "${CONTAINER_NAME}" mkdir -p /data/storage + if [ $? -ne 0 ]; then + log_warning "Could not create storage directory (may already exist)" + fi + # Restart the container to pick up the config file log_info "Restarting container to load config..." docker restart "${CONTAINER_NAME}" >/dev/null 2>&1 @@ -160,6 +172,18 @@ function start_test_server() { # Give container more time to initialize after restart sleep 5 + # Verify port mapping after restart (it might change) + local new_port=$(docker port "${CONTAINER_NAME}" 80 | cut -d: -f2) + if [ -z "${new_port}" ]; then + log_error "Lost port mapping after restart" + docker logs "${CONTAINER_NAME}" 2>&1 | tail -20 + die "Container port mapping lost after restart" + fi + if [ "${new_port}" != "${TEST_PORT}" ]; then + log_info "Port changed after restart: ${TEST_PORT} -> ${new_port}" + TEST_PORT="${new_port}" + fi + # Check if container is still running if ! docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then log_error "Container stopped unexpectedly. Logs:" @@ -184,9 +208,16 @@ function start_test_server() { # The SOS server should respond to a basic GET request local http_code=$(curl -s --max-time 2 -o /dev/null -w "%{http_code}" "http://localhost:${TEST_PORT}/" 2>/dev/null || echo "000") - # Debug: Show HTTP code on first few attempts + # Debug: Show HTTP code and container status on first few attempts if [ $attempt -lt 3 ]; then log_info "Health check attempt $((attempt + 1)): HTTP code ${http_code}" + # Check if container is actually running + if docker ps --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then + log_info "Container is running, checking logs..." + docker logs "${CONTAINER_NAME}" 2>&1 | tail -5 + else + log_error "Container is not running!" + fi fi if [ "$http_code" = "200" ] || [ "$http_code" = "204" ] || [ "$http_code" = "404" ]; then