Skip to content

Commit 917a1b3

Browse files
author
secus
committed
Add Docker service start based on OS, update Rust dependencies, and enhance test requirements
1 parent dde6acd commit 917a1b3

12 files changed

Lines changed: 450 additions & 520 deletions

setup.sh

Lines changed: 124 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,82 @@ command_exists() {
8787
command -v "$1" >/dev/null 2>&1
8888
}
8989

90-
# Check if Docker service is running
90+
# Check if Docker service is running and try to start it if possible
9191
is_docker_running() {
9292
docker info >/dev/null 2>&1
9393
}
9494

95+
# Function to start Docker based on OS
96+
start_docker_service() {
97+
log_info "Attempting to start Docker service..."
98+
99+
case "$OSTYPE" in
100+
linux-gnu*)
101+
# Try systemctl first (for systemd-based systems)
102+
if command_exists systemctl; then
103+
sudo systemctl start docker || {
104+
log_warning "Failed to start with systemctl, trying service command..."
105+
sudo service docker start
106+
}
107+
# Try service command (for non-systemd systems)
108+
elif command_exists service; then
109+
sudo service docker start
110+
else
111+
log_error "Could not find systemctl or service command"
112+
return 1
113+
fi
114+
;;
115+
darwin*)
116+
# For macOS, try to start Docker.app
117+
if [ -d "/Applications/Docker.app" ]; then
118+
log_info "Found Docker Desktop, attempting to start it..."
119+
open -a Docker
120+
else
121+
log_error "Docker Desktop not found in /Applications"
122+
return 1
123+
fi
124+
;;
125+
msys*|cygwin*)
126+
# For Windows Git Bash/Cygwin
127+
if command_exists "/c/Program Files/Docker/Docker/Docker Desktop.exe"; then
128+
log_info "Found Docker Desktop, attempting to start it..."
129+
"/c/Program Files/Docker/Docker/Docker Desktop.exe"
130+
else
131+
log_error "Docker Desktop not found in standard Windows location"
132+
return 1
133+
fi
134+
;;
135+
*)
136+
log_error "Unsupported operating system: $OSTYPE"
137+
return 1
138+
;;
139+
esac
140+
141+
# Wait for Docker to start
142+
local max_retries=12 # 60 seconds total
143+
local retry_count=0
144+
local started=false
145+
146+
log_info "Waiting for Docker to become available..."
147+
while [ $retry_count -lt $max_retries ]; do
148+
if is_docker_running; then
149+
started=true
150+
break
151+
fi
152+
log_info "Waiting for Docker to start... ($((retry_count + 1))/$max_retries)"
153+
sleep 5
154+
retry_count=$((retry_count + 1))
155+
done
156+
157+
if [ "$started" = true ]; then
158+
log_success "Docker is now running!"
159+
return 0
160+
else
161+
log_error "Docker failed to start after 60 seconds"
162+
return 1
163+
fi
164+
}
165+
95166
# Function to detect OS
96167
detect_os() {
97168
if [[ -f /etc/os-release ]]; then
@@ -568,14 +639,27 @@ setup_env() {
568639
install_rust_deps() {
569640
log_info "Installing Rust dependencies..."
570641

642+
# Source cargo environment
643+
if [ -f "$HOME/.cargo/env" ]; then
644+
. "$HOME/.cargo/env"
645+
fi
646+
571647
# Install SQLx CLI if not present
572648
if ! command_exists sqlx; then
573649
log_info "Installing SQLx CLI..."
574650
cargo install sqlx-cli --no-default-features --features native-tls,postgres
651+
# Re-source cargo environment to get new binaries
652+
. "$HOME/.cargo/env"
575653
else
576654
log_info "SQLx CLI already installed"
577655
fi
578656

657+
# Verify SQLx installation
658+
if ! command_exists sqlx; then
659+
log_error "SQLx installation failed. Try running manually: cargo install sqlx-cli"
660+
return 1
661+
fi
662+
579663
log_success "Rust dependencies installed"
580664
}
581665

@@ -584,8 +668,31 @@ start_database() {
584668
log_info "Starting database services..."
585669

586670
if ! is_docker_running; then
587-
log_error "Docker is not running. Please start Docker first."
588-
return 1
671+
log_info "Docker is not running. Attempting to start Docker service..."
672+
if [ "$OSTYPE" == "linux-gnu"* ]; then
673+
# Try to start Docker service on Linux
674+
sudo systemctl start docker || {
675+
log_error "Failed to start Docker service. Please check Docker installation."
676+
return 1
677+
}
678+
# Wait for Docker to fully initialize
679+
local max_retries=6
680+
local retry_count=0
681+
while [ $retry_count -lt $max_retries ] && ! is_docker_running; do
682+
log_info "Waiting for Docker to start... ($((retry_count + 1))/$max_retries)"
683+
sleep 5
684+
retry_count=$((retry_count + 1))
685+
done
686+
687+
if ! is_docker_running; then
688+
log_error "Docker service failed to start after 30 seconds. Please check Docker status."
689+
return 1
690+
fi
691+
log_success "Docker service started successfully!"
692+
else
693+
log_error "Docker is not running. On this OS, please start Docker Desktop manually."
694+
return 1
695+
fi
589696
fi
590697

591698
if command_exists docker && docker compose version >/dev/null 2>&1; then
@@ -657,8 +764,21 @@ reset_database() {
657764
run_migrations() {
658765
log_info "Running database migrations..."
659766

767+
# Source cargo environment
768+
if [ -f "$HOME/.cargo/env" ]; then
769+
. "$HOME/.cargo/env"
770+
fi
771+
772+
# Check if sqlx is in PATH
773+
if ! command_exists sqlx; then
774+
log_warning "SQLx CLI not found in PATH, attempting to install..."
775+
cargo install sqlx-cli --no-default-features --features native-tls,postgres
776+
. "$HOME/.cargo/env"
777+
fi
778+
660779
export DATABASE_URL="$DATABASE_URL"
661-
sqlx migrate run
780+
# Try to run with full path if direct command fails
781+
sqlx migrate run || "$HOME/.cargo/bin/sqlx" migrate run
662782

663783
log_success "Database migrations completed"
664784
}

src/auth/middleware.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn extract_token_from_headers(headers: &HeaderMap) -> Result<String, AppError> {
4848
}
4949

5050
async fn verify_api_key(state: &AppState, api_key: &str) -> Result<Uuid, AppError> {
51+
print!("Verifying API key: {}", api_key);
5152
// Extract prefix to find the API key
5253
let prefix = &api_key[..state.config.api_key_prefix.len().min(api_key.len())];
5354

@@ -76,9 +77,9 @@ async fn verify_api_key(state: &AppState, api_key: &str) -> Result<Uuid, AppErro
7677

7778
Ok(record.user_id)
7879
} else {
79-
Err(AppError::auth("Invalid API key"))
80+
Err(AppError::auth("Invalid API key "))
8081
}
8182
}
82-
None => Err(AppError::auth("Invalid API key")),
83+
None => Err(AppError::auth("Invalid API key ")),
8384
}
8485
}

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# Integration test package for Open Container Engine
1+
# Integration test package for Open Container Engine

tests/conftest.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,18 @@ def authenticated_client(api_client):
5757
api_client.clear_auth()
5858

5959

60-
@pytest.fixture
60+
@pytest.fixture(scope="function")
6161
def api_key_client(authenticated_client):
6262
"""Fixture to provide API client with API key authentication"""
6363
from tests.integrate.conftest import create_test_api_key
64-
64+
import time
6565
client, user_info = authenticated_client
6666
api_key_info = create_test_api_key(client)
67-
67+
time.sleep(0.1)
6868
# Switch to API key authentication
6969
client.clear_auth()
70-
client.set_api_key(api_key_info["apiKey"])
70+
time.sleep(0.1)
71+
client.set_api_key(api_key_info["api_key"])
7172

7273
yield client, api_key_info, user_info
7374

0 commit comments

Comments
 (0)