Files
n8n-mcp/tests/integration/docker/test-helpers.ts
czlonkowski 9cd5e42cb7 fix: resolve Docker integration test failures in CI
Root cause analysis and fixes:

1. **MCP_MODE environment variable tests**
   - Issue: Tests were checking env vars after exec process replacement
   - Fix: Test actual HTTP server behavior instead of env vars
   - Changed tests to verify health endpoint responds in HTTP mode

2. **NODE_DB_PATH configuration tests**
   - Issue: Tests expected env var output but got initialization logs
   - Fix: Check process environment via /proc/1/environ
   - Added proper async handling for container startup

3. **Permission handling tests**
   - Issue: BusyBox sleep syntax and timing race conditions
   - Fix: Use detached containers with proper wait times
   - Check permissions after entrypoint completes

4. **Implementation improvements**
   - Export NODE_DB_PATH in entrypoint for visibility
   - Preserve env vars when switching to nodejs user
   - Add debug output option in n8n-mcp wrapper
   - Handle NODE_DB_PATH case preservation in parse-config.js

5. **Test infrastructure**
   - Created test-helpers.ts with proper async utilities
   - Use health checks instead of arbitrary sleep times
   - Test actual functionality rather than implementation details

These changes ensure tests verify the actual behavior (server running,
health endpoint responding) rather than checking internal implementation
details that aren't accessible after process replacement.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-31 14:08:21 +02:00

59 lines
1.6 KiB
TypeScript

import { promisify } from 'util';
import { exec as execCallback } from 'child_process';
export const exec = promisify(execCallback);
/**
* Wait for a container to be healthy by checking the health endpoint
*/
export async function waitForHealthy(containerName: string, timeout = 10000): Promise<boolean> {
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
const { stdout } = await exec(
`docker exec ${containerName} curl -s http://localhost:3000/health`
);
if (stdout.includes('ok')) {
return true;
}
} catch (error) {
// Container might not be ready yet
}
await new Promise(resolve => setTimeout(resolve, 500));
}
return false;
}
/**
* Check if a container is running in HTTP mode by verifying the server is listening
*/
export async function isRunningInHttpMode(containerName: string): Promise<boolean> {
try {
const { stdout } = await exec(
`docker exec ${containerName} sh -c "netstat -tln 2>/dev/null | grep :3000 || echo 'Not listening'"`
);
return stdout.includes(':3000');
} catch {
return false;
}
}
/**
* Get process environment variables from inside a running container
*/
export async function getProcessEnv(containerName: string, varName: string): Promise<string | null> {
try {
const { stdout } = await exec(
`docker exec ${containerName} sh -c "cat /proc/1/environ | tr '\\0' '\\n' | grep '^${varName}=' | cut -d= -f2-"`
);
return stdout.trim() || null;
} catch {
return null;
}
}