|
| 1 | +describe("Check that GraalVM debugger can be enabled", () => { |
| 2 | + // these ports must be exposed in docker-compose.yml |
| 3 | + const ports = [9229, 10229]; |
| 4 | + |
| 5 | + ports.forEach((port) => { |
| 6 | + it(`Check that GraalVM debugger can be enabled on port ${port}`, function () { |
| 7 | + // extract the host from the JAHIA_URL env var |
| 8 | + const host = Cypress.env("JAHIA_URL").split("//")[1].split(":")[0]; |
| 9 | + |
| 10 | + // delete the config |
| 11 | + deleteGraalVMConfig(); |
| 12 | + waitForCurlExitCode( |
| 13 | + host, |
| 14 | + port, |
| 15 | + CONNECTION_ERROR_CODES, |
| 16 | + "debugger should be disabled by default (no config)", |
| 17 | + ); |
| 18 | + |
| 19 | + // Enable the debugger |
| 20 | + enableGraalVMDebugger(port); |
| 21 | + waitForCurlExitCode(host, port, CONNECTION_OK, "debugger should be enabled when configured"); |
| 22 | + verifyGraalVMInspector(host, port); |
| 23 | + }); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +// Constants for curl exit codes |
| 28 | +const CONNECTION_OK = 0; |
| 29 | +const CONNECTION_ERROR_CODES = [7, 56]; // 7=ECONNREFUSED, 56=ECONNRESET |
| 30 | + |
| 31 | +/** |
| 32 | + * Wait for curl to return the expected exit code when connecting to host:port Exit codes: |
| 33 | + * 0=success, 7=ECONNREFUSED, 56=ECONNRESET |
| 34 | + */ |
| 35 | +function waitForCurlExitCode( |
| 36 | + host: string, |
| 37 | + port: number, |
| 38 | + expectedExitCode: number | number[], |
| 39 | + errorMsg: string, |
| 40 | +) { |
| 41 | + const expectedCodes = Array.isArray(expectedExitCode) ? expectedExitCode : [expectedExitCode]; |
| 42 | + |
| 43 | + cy.waitUntil( |
| 44 | + () => |
| 45 | + cy |
| 46 | + .exec(`curl -s -o /dev/null -w "%{http_code}" http://${host}:${port}/json/version`, { |
| 47 | + failOnNonZeroExit: false, |
| 48 | + timeout: 4000, |
| 49 | + }) |
| 50 | + .then((result) => { |
| 51 | + const actualCode = result.code; |
| 52 | + |
| 53 | + if (expectedCodes.includes(actualCode)) { |
| 54 | + const msg = |
| 55 | + actualCode === 0 |
| 56 | + ? `Connection successful (HTTP ${result.stdout})` |
| 57 | + : `Got expected connection error (exit code: ${actualCode})`; |
| 58 | + Cypress.log({ message: msg }); |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + Cypress.log({ |
| 63 | + message: `Waiting... got exit code ${actualCode}, expected ${expectedCodes.join(" or ")}`, |
| 64 | + }); |
| 65 | + return false; |
| 66 | + }), |
| 67 | + { |
| 68 | + timeout: 5000, |
| 69 | + errorMsg: `${errorMsg} - Expected curl exit code ${expectedCodes.join(" or ")}`, |
| 70 | + }, |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +/** Delete the GraalVM Engine configuration */ |
| 75 | +function deleteGraalVMConfig() { |
| 76 | + cy.runProvisioningScript({ |
| 77 | + script: { |
| 78 | + fileContent: JSON.stringify([ |
| 79 | + { |
| 80 | + editConfiguration: "org.jahia.modules.javascript.modules.engine.jsengine.GraalVMEngine", |
| 81 | + content: "", |
| 82 | + }, |
| 83 | + ]), |
| 84 | + type: "application/yaml", |
| 85 | + }, |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +/** Enable the GraalVM debugger on a given port */ |
| 90 | +function enableGraalVMDebugger(port: number) { |
| 91 | + cy.apollo({ |
| 92 | + variables: { inspect: `0.0.0.0:${port}`, suspend: false, secure: false }, |
| 93 | + mutationFile: "graphql/enableGraalVMDebugger.graphql", |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +/** Verify the response is from the GraalVM Chrome DevTools inspector */ |
| 98 | +function verifyGraalVMInspector(host: string, port: number) { |
| 99 | + cy.request(`http://${host}:${port}/json/version`).then((response) => { |
| 100 | + expect(response.status).to.equal(200); |
| 101 | + expect(response.body).to.have.property("Browser", "GraalVM"); |
| 102 | + expect(response.body).to.have.property("Protocol-Version", "1.2"); |
| 103 | + }); |
| 104 | +} |
0 commit comments