Skip to content

Commit 2959808

Browse files
authored
Merge pull request #6 from ngocbd/copilot/fix-9c30279f-0189-479e-8e4f-f086f4c76102
Fix GitHub Actions test failures by implementing environment-specific configuration files
2 parents 74722a8 + 939c2cf commit 2959808

7 files changed

Lines changed: 147 additions & 17 deletions

File tree

.env.development

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Development environment configuration for Container Engine
2+
# Database Configuration
3+
DATABASE_URL=postgresql://postgres:password@localhost:5432/container_engine
4+
5+
# Redis Configuration
6+
REDIS_URL=redis://localhost:6379
7+
8+
# Server Configuration
9+
PORT=3000
10+
11+
# JWT Configuration
12+
JWT_SECRET=development-jwt-secret-key-not-for-production
13+
JWT_EXPIRES_IN=3600
14+
15+
# API Key Configuration
16+
API_KEY_PREFIX=ce_dev_
17+
18+
# Kubernetes Configuration
19+
KUBERNETES_NAMESPACE=container-engine-dev
20+
21+
# Domain Configuration
22+
DOMAIN_SUFFIX=dev.container-engine.app
23+
24+
# Logging
25+
RUST_LOG=container_engine=debug,tower_http=debug

.env.integrate_test

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Integration test environment configuration for Container Engine
2+
# Database Configuration
3+
DATABASE_URL=postgresql://postgres:password@localhost:5432/container_engine_test
4+
5+
# Redis Configuration
6+
REDIS_URL=redis://localhost:6379
7+
8+
# Server Configuration
9+
PORT=3001
10+
11+
# JWT Configuration
12+
JWT_SECRET=test-jwt-secret-key-for-integration-tests
13+
JWT_EXPIRES_IN=3600
14+
15+
# API Key Configuration
16+
API_KEY_PREFIX=ce_test_
17+
18+
# Kubernetes Configuration
19+
KUBERNETES_NAMESPACE=container-engine-test
20+
21+
# Domain Configuration
22+
DOMAIN_SUFFIX=test.container-engine.app
23+
24+
# Logging
25+
RUST_LOG=container_engine=info,tower_http=info

.github/workflows/integration-tests.yml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,22 @@ jobs:
7878

7979
- name: Set up test environment
8080
run: |
81-
# Set environment variables for the application
81+
# Set the environment variable to use the integration test configuration
82+
echo "ENVIRONMENT=integrate_test" >> $GITHUB_ENV
83+
84+
# Load configuration from .env.integrate_test file
85+
if [ -f .env.integrate_test ]; then
86+
while IFS= read -r line; do
87+
# Skip comments and empty lines
88+
if [[ $line =~ ^[^#]*= ]]; then
89+
echo "$line" >> $GITHUB_ENV
90+
fi
91+
done < .env.integrate_test
92+
fi
93+
94+
# Override specific variables for GitHub Actions environment
8295
echo "DATABASE_URL=postgresql://postgres:password@localhost:5432/container_engine_test" >> $GITHUB_ENV
8396
echo "REDIS_URL=redis://localhost:6379" >> $GITHUB_ENV
84-
echo "PORT=3000" >> $GITHUB_ENV
85-
echo "JWT_SECRET=test-jwt-secret-key-for-github-actions" >> $GITHUB_ENV
86-
echo "JWT_EXPIRES_IN=3600" >> $GITHUB_ENV
87-
echo "API_KEY_PREFIX=ce_test_" >> $GITHUB_ENV
88-
echo "KUBERNETES_NAMESPACE=test" >> $GITHUB_ENV
89-
echo "DOMAIN_SUFFIX=test.local" >> $GITHUB_ENV
90-
echo "RUST_LOG=container_engine=info,tower_http=info" >> $GITHUB_ENV
9197
9298
- name: Run database migrations
9399
run: |
@@ -98,8 +104,8 @@ jobs:
98104
cargo run &
99105
echo $! > server.pid
100106
101-
# Wait for server to be ready
102-
timeout 60 bash -c 'until curl -f http://localhost:3000/health; do sleep 2; done'
107+
# Wait for server to be ready (using port 3001 for integration tests)
108+
timeout 60 bash -c 'until curl -f http://localhost:3001/health; do sleep 2; done'
103109
104110
- name: Run integration tests
105111
run: |

ENVIRONMENT_CONFIG.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Environment Configuration
2+
3+
This project uses environment-specific configuration files to manage different deployment and testing scenarios.
4+
5+
## Environment Files
6+
7+
- `.env.development` - Configuration for local development
8+
- `.env.integrate_test` - Configuration for integration testing
9+
- `.env.example` - Example configuration template
10+
11+
## Usage
12+
13+
### Development
14+
```bash
15+
# Uses .env.development by default or when ENVIRONMENT=development
16+
cargo run
17+
18+
# Explicitly set development environment
19+
ENVIRONMENT=development cargo run
20+
```
21+
22+
### Integration Testing
23+
```bash
24+
# Run integration tests (automatically uses .env.integrate_test)
25+
ENVIRONMENT=integrate_test cargo run
26+
27+
# Run Python integration tests
28+
python -m pytest tests/integrate/ -v
29+
```
30+
31+
## Key Differences
32+
33+
### Development (.env.development)
34+
- Port: 3000
35+
- Database: `container_engine`
36+
- Namespace: `container-engine-dev`
37+
- Domain: `dev.container-engine.app`
38+
- Log level: debug
39+
40+
### Integration Test (.env.integrate_test)
41+
- Port: 3001 (avoids conflicts)
42+
- Database: `container_engine_test`
43+
- Namespace: `container-engine-test`
44+
- Domain: `test.container-engine.app`
45+
- Log level: info
46+
47+
## Port Separation
48+
49+
Different ports are used to prevent conflicts when running development server and tests simultaneously:
50+
- Development: port 3000
51+
- Integration tests: port 3001
52+
53+
This ensures GitHub Actions and local testing won't encounter "port already in use" errors.

src/config.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ pub struct Config {
1515

1616
impl Config {
1717
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
18-
// Load .env file if it exists
19-
dotenv::dotenv().ok();
18+
// Determine environment and load appropriate .env file
19+
let environment = env::var("ENVIRONMENT").unwrap_or_else(|_| "development".to_string());
20+
21+
let env_file = match environment.as_str() {
22+
"test" | "integrate_test" => ".env.integrate_test",
23+
"development" | "dev" => ".env.development",
24+
_ => ".env.development", // default to development
25+
};
26+
27+
// Try to load the environment-specific file, fall back to default .env
28+
if let Err(_) = dotenv::from_filename(env_file) {
29+
dotenv::dotenv().ok();
30+
}
2031

2132
let config = Config {
2233
database_url: env::var("DATABASE_URL")

tests/.env.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Test environment configuration for Container Engine integration tests
22

33
# Server settings
4-
TEST_BASE_URL=http://localhost:3000
4+
TEST_BASE_URL=http://localhost:3001
55

66
# Database settings (for test isolation)
77
TEST_DB_HOST=localhost

tests/integrate/conftest.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@
1111
import redis
1212
from dotenv import load_dotenv
1313

14-
# Load environment variables
15-
load_dotenv()
14+
# Load environment-specific .env file for tests
15+
environment = os.getenv("ENVIRONMENT", "integrate_test")
16+
env_file = f".env.{environment}"
17+
18+
# Try to load environment-specific file, fallback to .env.test, then .env
19+
if os.path.exists(env_file):
20+
load_dotenv(env_file)
21+
elif os.path.exists("tests/.env.test"):
22+
load_dotenv("tests/.env.test")
23+
else:
24+
load_dotenv()
1625

1726
class TestConfig:
1827
"""Test configuration settings"""
1928

2029
# Server settings
21-
BASE_URL = os.getenv("TEST_BASE_URL", "http://localhost:3000")
30+
BASE_URL = os.getenv("TEST_BASE_URL", "http://localhost:3001") # Use port 3001 for tests
2231
HEALTH_ENDPOINT = "/health"
2332

2433
# Database settings
@@ -138,9 +147,10 @@ def start_server(self):
138147
# Set environment variables for the server
139148
env = os.environ.copy()
140149
env.update({
150+
"ENVIRONMENT": "integrate_test", # This will load .env.integrate_test
141151
"DATABASE_URL": TestConfig.DATABASE_URL,
142152
"REDIS_URL": TestConfig.REDIS_URL,
143-
"PORT": "3000",
153+
"PORT": "3001", # Use port 3001 to avoid conflicts
144154
"JWT_SECRET": "test-jwt-secret-key",
145155
"JWT_EXPIRES_IN": "3600",
146156
"API_KEY_PREFIX": "ce_test_",

0 commit comments

Comments
 (0)