Skip to content

Commit 3bca9cd

Browse files
fix(run-e2e): make local e2e runs work with root-owned files and unreachable bridge networks
- Hand files written by the root mxbuild/mxruntime containers in tests/ and results/ back to the host user, before setting up the test project and after each run, so reruns no longer fail with a missing .mpr file. - Add opt-in E2E_HOST_NETWORK=1 to run mxruntime on the host network, for machines where the host can't reach containers on Docker bridge networks. - Wait between attempts when looking up the runtime container id in the raw-Docker path instead of polling without a pause. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f386048 commit 3bca9cd

4 files changed

Lines changed: 124 additions & 9 deletions

File tree

‎automation/run-e2e/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ Please make sure to follow these steps:
7777
7. Once the project is running, proceed with the next step of the script in the terminal to verify if the desired test project port is listening. If it is, the Playwright GUI will be initiated.
7878
8. In the Playwright GUI interface, select the test spec and execute it. From there, you can debug or fix the test case in your preferred IDE.
7979

80+
### Running `pnpm run e2e` when the host can't reach Docker containers
81+
82+
On some setups (for example Docker-in-Docker sandboxes) the runtime starts and reports healthy, but every test times out loading the page because the host can't reach containers on Docker bridge networks. Set `E2E_HOST_NETWORK=1` to run the Mendix runtime on the host network instead. Ports `8080` and `8090` must be free:
83+
84+
```sh
85+
$ E2E_HOST_NETWORK=1 pnpm run e2e
86+
```
87+
88+
On Linux the runtime containers run as root. After each run, and before the next one, the runner hands the files they write in `tests/` and `results/` back to your user. You don't need `sudo` to clean up the test project.
89+
8090
### Focusing a test
8191
8292
To focus a test to only execute it instead of the whole suite, for that is possible to use `test.only` annotation in the test specification. You can see more details [here](https://playwright.dev/docs/test-annotations#focus-a-test).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Opt-in override (E2E_HOST_NETWORK=1): run mxruntime on the host network.
2+
#
3+
# For machines where the host can't reach containers on Docker bridge
4+
# networks (e.g. some Docker-in-Docker sandboxes). The runtime then listens
5+
# directly on host ports 8080 (app) and 8090 (admin), so both must be free.
6+
7+
services:
8+
mxruntime:
9+
network_mode: host
10+
ports: !reset []

‎automation/run-e2e/lib/ci.mjs‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@ import c from "ansi-colors";
22
import findFreePort from "find-free-port";
33
import nodeIp from "ip";
44
import { execSync } from "node:child_process";
5+
import { createServer } from "node:net";
56
import { fileURLToPath } from "node:url";
67
import p from "node:path";
78
import sh from "shelljs";
89
import parseArgs from "yargs-parser";
9-
import { createDeploymentBundle, prepareImage, startPlaywright, startRuntime } from "./docker-utils.mjs";
10+
import {
11+
createDeploymentBundle,
12+
getFullImageName,
13+
prepareImage,
14+
restoreWorkspaceOwnership,
15+
startPlaywright,
16+
startRuntime
17+
} from "./docker-utils.mjs";
1018
import { setupTestProject } from "./setup-test-project.mjs";
1119
import { updateTestProject } from "./update-test-project.mjs";
1220
import { fetchWithReport } from "./utils.mjs";
@@ -17,6 +25,14 @@ const { ls } = sh;
1725
// Path to the docker directory (contains docker-compose.yml + Dockerfiles)
1826
const DOCKER_DIR = fileURLToPath(new URL("../docker", import.meta.url));
1927
const COMPOSE_FILE = p.join(DOCKER_DIR, "docker-compose.yml");
28+
const COMPOSE_HOSTNET_FILE = p.join(DOCKER_DIR, "docker-compose.hostnet.yml");
29+
30+
// Opt-in: run mxruntime on the host network instead of publishing a port.
31+
// For machines where the host can't reach Docker bridge networks.
32+
const HOST_NETWORK = ["1", "true"].includes(String(process.env.E2E_HOST_NETWORK).toLowerCase());
33+
// Ports the runtime binds directly in host-network mode (see docker/m2ee.yml).
34+
const HOST_NETWORK_RUNTIME_PORT = 8080;
35+
const HOST_NETWORK_ADMIN_PORT = 8090;
2036

2137
export async function ci() {
2238
console.log(c.cyan("Run e2e tests in CI environment"));
@@ -39,8 +55,17 @@ export async function ci() {
3955
const options = parseArgs(process.argv.slice(2), parseArgsOptions);
4056
const mendixVersion = await getMendixVersion(options);
4157

42-
const freePort = await findFreePort(3000);
43-
const ip = nodeIp.address();
58+
let freePort;
59+
let ip;
60+
if (HOST_NETWORK) {
61+
console.log(c.yellow("E2E_HOST_NETWORK is set: running mxruntime on the host network."));
62+
await assertPortsFree([HOST_NETWORK_RUNTIME_PORT, HOST_NETWORK_ADMIN_PORT]);
63+
freePort = HOST_NETWORK_RUNTIME_PORT;
64+
ip = "127.0.0.1";
65+
} else {
66+
freePort = await findFreePort(3000);
67+
ip = nodeIp.address();
68+
}
4469

4570
if (!ip) {
4671
throw new Error("Could not determine local ip address!");
@@ -52,6 +77,11 @@ export async function ci() {
5277
throw new Error("To run e2e test locally, make sure docker is running. Exiting now...", { cause: e });
5378
}
5479

80+
// Files left by a previous (possibly interrupted) run may be root-owned;
81+
// reclaim them so the test project can be cleaned up and re-downloaded.
82+
const mxbuildImage = await prepareImage("mxbuild", mendixVersion);
83+
restoreWorkspaceOwnership(mxbuildImage);
84+
5585
if (options.setupProject) {
5686
await setupTestProject();
5787
}
@@ -82,6 +112,10 @@ async function runWithCompose({ mendixVersion, ip, freePort }) {
82112
throw new Error(`No .mpr file found matching ${config.mprFileGlob}`);
83113
}
84114

115+
const composeFiles = [COMPOSE_FILE, ...(HOST_NETWORK ? [COMPOSE_HOSTNET_FILE] : [])]
116+
.map(file => `-f "${file}"`)
117+
.join(" ");
118+
85119
const mprRelPath = projectFile; // relative to process.cwd() → /source mount
86120
const workspace = process.cwd();
87121

@@ -100,20 +134,24 @@ async function runWithCompose({ mendixVersion, ip, freePort }) {
100134
console.log(c.gray(` workspace : ${workspace}`));
101135
console.log(c.gray(` mpr : ${mprRelPath}`));
102136
console.log(c.gray(` runtime port : ${freePort}`));
137+
if (HOST_NETWORK) {
138+
console.log(c.gray(` network : host`));
139+
}
103140

104141
// Compose starts mxbuild first (via depends_on), then boots mxruntime.
105142
// Only wait on mxruntime – including mxbuild confuses Compose since its
106143
// clean exit is treated as a failure by --wait.
107-
execSync(`docker compose -f "${COMPOSE_FILE}" up --wait mxruntime`, { stdio: "inherit", env: composeEnv });
144+
execSync(`docker compose ${composeFiles} up --wait mxruntime`, { stdio: "inherit", env: composeEnv });
108145

109146
console.log(c.green("Runtime is healthy. Starting Playwright..."));
110147
startPlaywright(ip, freePort);
111148
} finally {
112149
console.log(c.cyan("Tearing down Docker Compose services..."));
113-
execSync(`docker compose -f "${COMPOSE_FILE}" down --volumes --remove-orphans`, {
150+
execSync(`docker compose ${composeFiles} down --volumes --remove-orphans`, {
114151
stdio: "inherit",
115152
env: composeEnv
116153
});
154+
restoreWorkspaceOwnership(getFullImageName("mxbuild", mendixVersion));
117155
}
118156
}
119157

@@ -129,12 +167,15 @@ async function runWithDockerRaw({ mendixVersion, ip, freePort }) {
129167
const projectFile = ls(config.mprFileGlob).toString();
130168
createDeploymentBundle(mxbuildImage, projectFile);
131169

132-
runtimeContainerId = await startRuntime(mxruntimeImage, mendixVersion, ip, freePort);
170+
runtimeContainerId = await startRuntime(mxruntimeImage, mendixVersion, ip, freePort, {
171+
hostNetwork: HOST_NETWORK
172+
});
133173
startPlaywright(ip, freePort);
134174
} finally {
135175
if (runtimeContainerId) {
136176
execSync(`docker rm -f ${runtimeContainerId}`);
137177
}
178+
restoreWorkspaceOwnership(getFullImageName("mxbuild", mendixVersion));
138179
}
139180
}
140181

@@ -157,3 +198,17 @@ async function getMendixVersion(options) {
157198
throw new Error(`Couldn't fetch mendix-versions.json: ${versionMapResponse.statusText}`);
158199
}
159200
}
201+
202+
async function assertPortsFree(ports) {
203+
for (const port of ports) {
204+
const free = await new Promise(resolve => {
205+
const server = createServer()
206+
.once("error", () => resolve(false))
207+
.once("listening", () => server.close(() => resolve(true)))
208+
.listen(port);
209+
});
210+
if (!free) {
211+
throw new Error(`E2E_HOST_NETWORK needs port ${port} free for the Mendix runtime, but it is in use.`);
212+
}
213+
}
214+
}

‎automation/run-e2e/lib/docker-utils.mjs‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { execSync, spawn } from "node:child_process";
1+
import { execSync, spawn, spawnSync } from "node:child_process";
2+
import { existsSync } from "node:fs";
23
import p from "node:path";
34
import { fileURLToPath } from "node:url";
45
import fetch from "node-fetch";
@@ -46,6 +47,42 @@ export async function prepareImage(name, mendixVersion) {
4647
return image;
4748
}
4849

50+
/**
51+
* mxbuild/mxruntime containers run as root and write into the bind-mounted
52+
* package dir (tests/testProject, results). On Linux those files end up
53+
* owned by root on the host, so the next run can't clean them up.
54+
* Hand them back to the current host user by running chown inside a container.
55+
*/
56+
export function restoreWorkspaceOwnership(image, paths = ["tests", "results"]) {
57+
if (typeof process.getuid !== "function" || process.getuid() === 0) {
58+
return;
59+
}
60+
61+
const existing = paths.filter(path => existsSync(p.join(process.cwd(), path)));
62+
if (existing.length === 0) {
63+
return;
64+
}
65+
66+
const owner = `${process.getuid()}:${process.getgid()}`;
67+
const args = [
68+
"run",
69+
"--rm",
70+
"--volume",
71+
`${process.cwd()}:/source`,
72+
"--entrypoint",
73+
"chown",
74+
image,
75+
"-R",
76+
owner,
77+
...existing.map(path => `/source/${path}`)
78+
];
79+
80+
const result = spawnSync("docker", args, { stdio: "inherit" });
81+
if (result.status !== 0) {
82+
console.warn(c.yellow(`Could not restore ownership of ${existing.join(", ")} to ${owner}.`));
83+
}
84+
}
85+
4986
export function createDeploymentBundle(mxbuildImage, projectFile) {
5087
console.log(`Start building deployment bundle.`);
5188

@@ -75,7 +112,7 @@ export function createDeploymentBundle(mxbuildImage, projectFile) {
75112
console.log("Success. Bundle created and all the widgets are updated.");
76113
}
77114

78-
export async function startRuntime(mxruntimeImage, mendixVersion, ip, freePort) {
115+
export async function startRuntime(mxruntimeImage, mendixVersion, ip, freePort, { hostNetwork = false } = {}) {
79116
console.log(`Start mxruntime image`);
80117

81118
const dockerDir = fileURLToPath(new URL("../docker", import.meta.url));
@@ -86,7 +123,8 @@ export async function startRuntime(mxruntimeImage, mendixVersion, ip, freePort)
86123
`run`,
87124
`--tty`,
88125
[`--workdir`, `/source`],
89-
[`--publish`, `${freePort}:8080`],
126+
// In host-network mode the runtime listens on the host's 8080 directly.
127+
hostNetwork ? [`--network`, `host`] : [`--publish`, `${freePort}:8080`],
90128
[`--env`, `MENDIX_VERSION=${mendixVersion}`],
91129
[`--entrypoint`, `/bin/bash`],
92130
[`--volume`, `${process.cwd()}:/source`],
@@ -107,6 +145,8 @@ export async function startRuntime(mxruntimeImage, mendixVersion, ip, freePort)
107145
if (runtimeContainerId) {
108146
break;
109147
}
148+
// Give `docker run` time to create the container (up to ~30s in total).
149+
await new Promise(resolve => setTimeout(resolve, 300));
110150
}
111151

112152
if (runtimeContainerId === "") {

0 commit comments

Comments
 (0)