This directory contains comprehensive integration tests for the Container Engine API using pytest.
The integration test suite validates all API endpoints documented in APIs.md, covering:
- ✅ Authentication endpoints (register, login, logout, refresh)
- ✅ API key management (create, list, revoke)
- ✅ User profile management (get, update, change password)
- ✅ Deployment management (CRUD operations, scaling, lifecycle)
- ✅ Monitoring endpoints (logs, metrics, status)
- ✅ Custom domain management
- ✅ Health check endpoint
tests/
├── __init__.py # Test package initialization
├── conftest.py # Global pytest fixtures
├── requirements.txt # Python test dependencies
├── run_tests.sh # Test runner script
├── .env.test # Test environment configuration
└── integrate/
├── __init__.py # Integration test package
├── conftest.py # Test utilities and helpers
├── test_health.py # Health check tests
├── test_auth.py # Authentication tests
├── test_api_keys.py # API key management tests
├── test_user.py # User profile tests
├── test_deployments.py # Deployment management tests
├── test_monitoring.py # Logs, metrics, status tests
└── test_domains.py # Custom domain tests
- Python 3.8+
- Docker and Docker Compose
- Rust 1.70+ with Cargo
- PostgreSQL client (for migrations)
# Install Python dependencies
pip install -r tests/requirements.txt
# Install SQLx CLI for database migrations
cargo install sqlx-cli --no-default-features --features postgresUse the provided test runner script:
# Run all integration tests
./tests/run_tests.sh
# Run with verbose output
./tests/run_tests.sh --verbose
# Run specific test file
./tests/run_tests.sh --test test_auth
# Run specific test function
./tests/run_tests.sh --test "test_register_user_success"
# Skip building (if already built)
./tests/run_tests.sh --skip-build
# Cleanup only
./tests/run_tests.sh --cleanup-only-
Start test dependencies:
# Start PostgreSQL and Redis docker run -d --name test_postgres \ -e POSTGRES_DB=container_engine_test \ -e POSTGRES_USER=postgres \ -e POSTGRES_PASSWORD=password \ -p 5432:5432 postgres:16 docker run -d --name test_redis \ -p 6379:6379 redis:7-alpine -
Set environment variables:
export DATABASE_URL="postgresql://postgres:password@localhost:5432/container_engine_test" export REDIS_URL="redis://localhost:6379" export JWT_SECRET="test-jwt-secret-key" export API_KEY_PREFIX="ce_test_" export RUST_LOG="container_engine=info"
-
Run migrations:
sqlx migrate run --database-url $DATABASE_URL -
Start the server:
cargo run & -
Run tests:
python -m pytest tests/integrate/ -v
-
Cleanup:
docker stop test_postgres test_redis docker rm test_postgres test_redis pkill -f "cargo run"
Test behavior can be configured via environment variables:
| Variable | Description | Default |
|---|---|---|
TEST_BASE_URL |
Server base URL | http://localhost:3000 |
TEST_DB_HOST |
PostgreSQL host | localhost |
TEST_DB_PORT |
PostgreSQL port | 5432 |
TEST_DB_NAME |
Test database name | container_engine_test |
TEST_REDIS_HOST |
Redis host | localhost |
TEST_REDIS_PORT |
Redis port | 6379 |
The test suite uses the following pytest configuration (pytest.ini):
- Test discovery in
tests/integrate/ - Custom markers for test categorization
- Verbose output with duration reporting
- Timeout protection (5 minutes default)
Use markers to run specific test categories:
# Run only authentication tests
pytest -m auth
# Run all except slow tests
pytest -m "not slow"
# Run deployment-related tests
pytest -m deployment
# Run monitoring tests
pytest -m monitoringThe test suite automatically:
- Starts PostgreSQL and Redis containers
- Manages database migrations
- Starts and stops the Container Engine server
- Handles cleanup on test completion
Tests use pytest fixtures for user management:
clean_client: Unauthenticated API clientauthenticated_client: Client with JWT authenticationapi_key_client: Client with API key authentication
Each test file covers a specific API area:
-
Authentication (
test_auth.py):- User registration with validation
- Login/logout flow
- Token refresh mechanism
- Error handling for invalid credentials
-
API Keys (
test_api_keys.py):- API key creation and listing
- Key revocation and cleanup
- API key authentication validation
-
User Profile (
test_user.py):- Profile retrieval and updates
- Password change with validation
- Duplicate username/email handling
-
Deployments (
test_deployments.py):- Full CRUD operations
- Scaling and lifecycle management
- Validation of deployment configurations
-
Monitoring (
test_monitoring.py):- Log retrieval with filtering
- Metrics collection and formatting
- Status reporting and health checks
-
Domains (
test_domains.py):- Custom domain addition and removal
- DNS record generation
- Domain validation
All tests verify:
- Correct HTTP status codes
- Proper error response format
- Authentication and authorization
- Input validation and sanitization
The test suite runs automatically on:
- Push to
mainbranch - Pull requests to
mainbranch
The workflow (.github/workflows/integration-tests.yml):
- Sets up PostgreSQL and Redis services
- Installs Rust and Python dependencies
- Runs linting and formatting checks
- Executes the full integration test suite
- Uploads test results as artifacts
To simulate the CI environment locally:
# Run the same checks as CI
cargo fmt --all -- --check
cargo clippy --all-targets --all-features -- -D warnings
cargo build --verbose
./tests/run_tests.sh --verbose- Server won't start: Check that ports 3000, 5432, and 6379 are available
- Database connection errors: Ensure PostgreSQL is running and migrations are applied
- Redis connection errors: Verify Redis is running and accessible
- Test timeouts: Increase timeout values or check server performance
# Run single test with extra output
pytest tests/integrate/test_auth.py::test_register_user_success -v -s
# Run tests with Python debugger
pytest tests/integrate/test_auth.py --pdb
# Generate test coverage report
pytest tests/integrate/ --cov=tests --cov-report=html
# Run tests with timing information
pytest tests/integrate/ --durations=0Tests create isolated data for each test run. To inspect test data:
- Run tests with
--pdbto drop into debugger - Use
client.get("/v1/user/profile")to inspect created users - Check database directly:
psql postgresql://postgres:password@localhost:5432/container_engine_test
When adding new API endpoints:
- Create corresponding tests in the appropriate test file
- Follow naming conventions:
test_<operation>_<scenario> - Test both success and error cases
- Verify response structure and data types
- Include authentication/authorization tests
- Add appropriate pytest markers
- Use descriptive test names that explain the scenario
- Test one specific behavior per test function
- Use fixtures for common setup (authentication, test data)
- Verify both response status codes and response body structure
- Clean up test data when possible (though isolation handles this)
- Add comments for complex test logic
- Tests run in parallel where possible using pytest-xdist
- Database and Redis use Docker for isolation
- Server startup is optimized with health checks
- Test data is minimal to reduce execution time
For common issues and solutions, see the TROUBLESHOOTING.md file or check the GitHub Actions logs for CI-specific problems.