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>
This commit is contained in:
czlonkowski
2025-07-31 14:08:21 +02:00
parent 8047297abc
commit 9cd5e42cb7
6 changed files with 131 additions and 27 deletions

View File

@@ -1,11 +1,9 @@
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
import { exec as execCallback, execSync } from 'child_process';
import { promisify } from 'util';
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';
import os from 'os';
const exec = promisify(execCallback);
import { exec, waitForHealthy, isRunningInHttpMode, getProcessEnv } from './test-helpers';
// Skip tests if not in CI or if Docker is not available
const SKIP_DOCKER_TESTS = process.env.CI !== 'true' && !process.env.RUN_DOCKER_TESTS;
@@ -183,11 +181,14 @@ describeDocker('Docker Entrypoint Script', () => {
expect(psOutput).toContain('node');
expect(psOutput).toContain('/app/dist/mcp/index.js');
// Check environment variable to confirm HTTP mode
const { stdout: envOutput } = await exec(`docker exec ${containerName} sh -c "env | grep MCP_MODE || echo 'MCP_MODE not set'"`);
// Check that the server is actually running in HTTP mode
// We can verify this by checking if the HTTP server is listening
const { stdout: curlOutput } = await exec(
`docker exec ${containerName} sh -c "curl -s http://localhost:3000/health || echo 'Server not responding'"`
);
// Should have MCP_MODE=http
expect(envOutput.trim()).toBe('MCP_MODE=http');
// If running in HTTP mode, the health endpoint should respond
expect(curlOutput).toContain('ok');
} catch (error) {
console.error('Test error:', error);
throw error;
@@ -238,12 +239,19 @@ describeDocker('Docker Entrypoint Script', () => {
containers.push(containerName);
// Use a path that the nodejs user can create
const { stdout, stderr } = await exec(
`docker run --name ${containerName} -e NODE_DB_PATH=/tmp/custom/test.db ${imageName} sh -c "env | grep NODE_DB_PATH"`
// We need to check the environment inside the running process, not the initial shell
await exec(
`docker run -d --name ${containerName} -e NODE_DB_PATH=/tmp/custom/test.db -e AUTH_TOKEN=test ${imageName}`
);
// Give it time to start
await new Promise(resolve => setTimeout(resolve, 2000));
// Check the actual process environment
const { stdout } = await exec(
`docker exec ${containerName} sh -c "cat /proc/1/environ | tr '\0' '\n' | grep NODE_DB_PATH || echo 'NODE_DB_PATH not found'"`
);
// The script validates that NODE_DB_PATH ends with .db and should not error
expect(stderr).not.toContain('ERROR: NODE_DB_PATH must end with .db');
expect(stdout.trim()).toBe('NODE_DB_PATH=/tmp/custom/test.db');
});
@@ -272,13 +280,21 @@ describeDocker('Docker Entrypoint Script', () => {
const containerName = generateContainerName('root-permissions');
containers.push(containerName);
// Run as root and check that database directory permissions are fixed
// Run as root and let the container initialize
await exec(
`docker run -d --name ${containerName} --user root ${imageName}`
);
// Give entrypoint time to fix permissions
await new Promise(resolve => setTimeout(resolve, 2000));
// Check directory ownership
const { stdout } = await exec(
`docker run --name ${containerName} --user root ${imageName} sh -c "sleep 1 && ls -ld /app/data 2>/dev/null | awk '{print \\$3}' || echo 'ownership-check-failed'"`
`docker exec ${containerName} ls -ld /app/data | awk '{print $3}'`
);
// Directory should be owned by nodejs user after entrypoint runs
expect(stdout.trim()).toMatch(/nodejs|ownership-check-failed/);
expect(stdout.trim()).toBe('nodejs');
});
it('should switch to nodejs user when running as root', async () => {