Skip to content

Commit 62d46e2

Browse files
authored
Merge pull request #8 from secus217/main
Add Docker service start based on OS, update Rust dependencies, and enhance test requirements
2 parents e7e9b29 + 941a494 commit 62d46e2

14 files changed

Lines changed: 1143 additions & 637 deletions

.github/workflows/integration-tests.yml

Lines changed: 121 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
name: Integration Tests
23

34
on:
@@ -60,7 +61,12 @@ jobs:
6061
${{ runner.os }}-cargo-
6162
6263
- name: Install SQLx CLI
63-
run: cargo install sqlx-cli --no-default-features --features postgres
64+
run: |
65+
if ! command -v sqlx &> /dev/null; then
66+
cargo install sqlx-cli --no-default-features --features postgres
67+
else
68+
echo "SQLx CLI already installed"
69+
fi
6470
6571
- name: Set up Python
6672
uses: actions/setup-python@v4
@@ -73,6 +79,62 @@ jobs:
7379
pip install --upgrade pip
7480
pip install -r tests/requirements.txt
7581
82+
# ===== KUBERNETES SETUP SECTION =====
83+
- name: Install kubectl
84+
run: |
85+
echo "Installing kubectl..."
86+
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
87+
curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
88+
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
89+
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
90+
kubectl version --client
91+
rm -f kubectl kubectl.sha256
92+
93+
- name: Install Minikube
94+
run: |
95+
echo "Installing Minikube..."
96+
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
97+
sudo install minikube-linux-amd64 /usr/local/bin/minikube
98+
rm -f minikube-linux-amd64
99+
minikube version
100+
101+
- name: Start Minikube
102+
run: |
103+
echo "Starting Minikube with Docker driver..."
104+
# Start minikube with specific configuration for CI
105+
minikube start \
106+
--driver=docker \
107+
--kubernetes-version=v1.28.3 \
108+
--memory=4096 \
109+
--cpus=2 \
110+
--disk-size=20g \
111+
--wait=all \
112+
--wait-timeout=5m
113+
114+
# Wait for cluster to be ready
115+
echo "Waiting for Minikube cluster to be ready..."
116+
minikube status
117+
kubectl cluster-info
118+
kubectl get nodes
119+
120+
- name: Configure Kubernetes environment
121+
run: |
122+
echo "Configuring Kubernetes environment..."
123+
# Set kubeconfig
124+
export KUBECONFIG=$HOME/.kube/config
125+
echo "KUBECONFIG=$HOME/.kube/config" >> $GITHUB_ENV
126+
127+
# Create test namespace
128+
kubectl create namespace test || true
129+
kubectl config set-context --current --namespace=test
130+
131+
# Show cluster info
132+
echo "Kubernetes cluster info:"
133+
kubectl version
134+
kubectl get namespaces
135+
kubectl get pods --all-namespaces
136+
# ===== END KUBERNETES SETUP =====
137+
76138
- name: Build Rust application
77139
run: |
78140
# Use offline mode for SQLx to avoid database dependency during compilation
@@ -97,11 +159,21 @@ jobs:
97159
# Override specific variables for GitHub Actions environment
98160
echo "DATABASE_URL=postgresql://postgres:password@localhost:5432/container_engine_test" >> $GITHUB_ENV
99161
echo "REDIS_URL=redis://localhost:6379" >> $GITHUB_ENV
162+
163+
# Add Kubernetes-specific environment variables
164+
echo "KUBERNETES_ENABLED=true" >> $GITHUB_ENV
165+
echo "KUBERNETES_NAMESPACE=test" >> $GITHUB_ENV
166+
echo "KUBERNETES_IN_CLUSTER=false" >> $GITHUB_ENV
100167
101168
- name: Run database migrations
102169
run: |
103170
sqlx migrate run --database-url postgresql://postgres:password@localhost:5432/container_engine_test
104171
172+
- name: Prepare SQLx offline data
173+
run: |
174+
export DATABASE_URL=postgresql://postgres:password@localhost:5432/container_engine_test
175+
cargo sqlx prepare
176+
105177
- name: Start Container Engine server in background
106178
run: |
107179
# Use offline mode for SQLx and start server
@@ -112,10 +184,31 @@ jobs:
112184
# Wait for server to be ready (using port 3001 for integration tests)
113185
timeout 60 bash -c 'until curl -f http://localhost:3001/health; do sleep 2; done'
114186
115-
- name: Run integration tests
187+
- name: Verify Kubernetes connectivity
116188
run: |
117-
python -m pytest tests/integrate/ -v --tb=short --durations=10
118-
timeout-minutes: 15
189+
echo "Verifying Container Engine can connect to Kubernetes..."
190+
# This would be where you verify your app can talk to K8s
191+
kubectl auth can-i get pods --namespace=test
192+
193+
# - name: Run integration tests
194+
# run: |
195+
# python -m pytest tests/integrate/ -v --tb=short --durations=10
196+
# timeout-minutes: 15
197+
198+
- name: Show logs on failure
199+
if: failure()
200+
run: |
201+
echo "=== Server logs ==="
202+
if [ -f server.pid ]; then
203+
ps aux | grep $(cat server.pid) || echo "Server process not found"
204+
fi
205+
206+
echo "=== Minikube logs ==="
207+
minikube logs --lines=50
208+
209+
echo "=== Kubernetes pod logs ==="
210+
kubectl get pods --all-namespaces
211+
kubectl describe pods --all-namespaces
119212
120213
- name: Stop server
121214
if: always()
@@ -125,23 +218,29 @@ jobs:
125218
rm server.pid
126219
fi
127220
128-
- name: Upload test results
129-
if: always()
130-
uses: actions/upload-artifact@v4
131-
with:
132-
name: test-results
133-
path: |
134-
pytest-report.html
135-
.pytest_cache/
136-
retention-days: 7
137-
138-
- name: Test Summary
221+
- name: Stop Minikube
139222
if: always()
140223
run: |
141-
echo "## Integration Test Results" >> $GITHUB_STEP_SUMMARY
142-
echo "Integration tests completed for Container Engine API" >> $GITHUB_STEP_SUMMARY
143-
if [ ${{ job.status }} == 'success' ]; then
144-
echo "✅ All tests passed!" >> $GITHUB_STEP_SUMMARY
145-
else
146-
echo "❌ Some tests failed. Check the logs above." >> $GITHUB_STEP_SUMMARY
147-
fi
224+
minikube stop || true
225+
minikube delete || true
226+
227+
# - name: Upload test results
228+
# if: always()
229+
# uses: actions/upload-artifact@v4
230+
# with:
231+
# name: test-results
232+
# path: |
233+
# pytest-report.html
234+
# .pytest_cache/
235+
# retention-days: 7
236+
237+
# - name: Test Summary
238+
# if: always()
239+
# run: |
240+
# echo "## Integration Test Results" >> $GITHUB_STEP_SUMMARY
241+
# echo "Integration tests completed for Container Engine API" >> $GITHUB_STEP_SUMMARY
242+
# if [ ${{ job.status }} == 'success' ]; then
243+
# echo "✅ All tests passed!" >> $GITHUB_STEP_SUMMARY
244+
# else
245+
# echo "❌ Some tests failed. Check the logs above." >> $GITHUB_STEP_SUMMARY
246+
# fi

scripts/installMinikubeScript.sh

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#!/bin/bash
2+
3+
# Test runner script for Container Engine integration tests
4+
# This script sets up the test environment and runs the pytest suite
5+
6+
set -e
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m' # No Color
13+
14+
echo -e "${GREEN}Container Engine Integration Test Runner${NC}"
15+
echo "=========================================="
16+
17+
# Function to print colored output
18+
print_status() {
19+
echo -e "${GREEN}[INFO]${NC} $1"
20+
}
21+
22+
print_warning() {
23+
echo -e "${YELLOW}[WARN]${NC} $1"
24+
}
25+
26+
print_error() {
27+
echo -e "${RED}[ERROR]${NC} $1"
28+
}
29+
30+
# Check if Docker is available
31+
if ! command -v docker &> /dev/null; then
32+
print_error "Docker is not installed or not in PATH"
33+
exit 1
34+
fi
35+
36+
if ! docker info &> /dev/null; then
37+
print_error "Docker daemon is not running"
38+
exit 1
39+
fi
40+
41+
# Check if Python 3 is available
42+
if ! command -v python3 &> /dev/null; then
43+
print_error "Python 3 is not installed or not in PATH"
44+
exit 1
45+
fi
46+
47+
# Check if Rust/Cargo is available
48+
if ! command -v cargo &> /dev/null; then
49+
print_error "Rust/Cargo is not installed or not in PATH"
50+
exit 1
51+
fi
52+
53+
print_status "Installing Python test dependencies..."
54+
# Get script directory for reliable file paths
55+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
56+
57+
print_status "Installing Python test dependencies..."
58+
pip3 install --user -r "$SCRIPT_DIR/requirements.txt"
59+
60+
# Set test environment
61+
export DATABASE_URL="postgresql://postgres:password@localhost:5432/container_engine_test"
62+
export REDIS_URL="redis://localhost:6379"
63+
export JWT_SECRET="test-jwt-secret-key"
64+
export JWT_EXPIRES_IN="3600"
65+
export API_KEY_PREFIX="ce_test_"
66+
export KUBERNETES_NAMESPACE="test"
67+
export DOMAIN_SUFFIX="test.local"
68+
export RUST_LOG="container_engine=info,tower_http=info"
69+
70+
print_status "Environment variables set for testing"
71+
72+
# Parse command line arguments
73+
PYTEST_ARGS=""
74+
RUN_SPECIFIC_TEST=""
75+
CLEANUP_ONLY=false
76+
SKIP_BUILD=false
77+
78+
while [[ $# -gt 0 ]]; do
79+
case $1 in
80+
--cleanup-only)
81+
CLEANUP_ONLY=true
82+
shift
83+
;;
84+
--skip-build)
85+
SKIP_BUILD=true
86+
shift
87+
;;
88+
--test)
89+
RUN_SPECIFIC_TEST="$2"
90+
shift 2
91+
;;
92+
--verbose|-v)
93+
PYTEST_ARGS="$PYTEST_ARGS -v"
94+
shift
95+
;;
96+
--help|-h)
97+
echo "Usage: $0 [OPTIONS]"
98+
echo ""
99+
echo "Options:"
100+
echo " --cleanup-only Only cleanup test environment"
101+
echo " --skip-build Skip building the Rust application"
102+
echo " --test <pattern> Run specific test(s) matching pattern"
103+
echo " --verbose, -v Verbose output"
104+
echo " --help, -h Show this help message"
105+
exit 0
106+
;;
107+
*)
108+
PYTEST_ARGS="$PYTEST_ARGS $1"
109+
shift
110+
;;
111+
esac
112+
done
113+
114+
# Cleanup function
115+
cleanup() {
116+
print_status "Cleaning up test environment..."
117+
118+
# Only stop containers if not in GitHub Actions (they're managed by GitHub)
119+
if [ "$GITHUB_ACTIONS" != "true" ] && [ "$CI" != "true" ]; then
120+
# Stop any running containers
121+
docker stop test_postgres test_redis 2>/dev/null || true
122+
docker rm test_postgres test_redis 2>/dev/null || true
123+
else
124+
print_status "Skipping container cleanup in CI environment"
125+
fi
126+
127+
# Kill any running server processes
128+
pkill -f "cargo run" 2>/dev/null || true
129+
130+
print_status "Cleanup completed"
131+
}
132+
133+
# If cleanup only, run cleanup and exit
134+
if [ "$CLEANUP_ONLY" = true ]; then
135+
cleanup
136+
exit 0
137+
fi
138+
139+
# Set trap for cleanup on exit
140+
trap cleanup EXIT
141+
142+
# Build the Rust application if not skipping
143+
if [ "$SKIP_BUILD" = false ]; then
144+
print_status "Building Container Engine..."
145+
cargo build
146+
print_status "Build completed"
147+
fi
148+
149+
# Run pytest
150+
print_status "Starting integration tests..."
151+
152+
if [ -n "$RUN_SPECIFIC_TEST" ]; then
153+
print_status "Running specific test: $RUN_SPECIFIC_TEST"
154+
python3 -m pytest "$SCRIPT_DIR/integrate" -k "$RUN_SPECIFIC_TEST" $PYTEST_ARGS
155+
else
156+
print_status "Running all integration tests..."
157+
python3 -m pytest "$SCRIPT_DIR/integrate" $PYTEST_ARGS
158+
fi
159+
160+
print_status "Integration tests completed"

0 commit comments

Comments
 (0)