|
| 1 | +import axios from "axios"; |
| 2 | +import { GenericContainer, StartedTestContainer } from "testcontainers"; |
| 3 | +import { findAPortNotInUse } from "portscanner"; |
| 4 | +import { |
| 5 | + WILDFLY_MANAGEMENT_PORT, |
| 6 | + WILDFLY_PORT_RANGE, |
| 7 | + DEFAULT_WILDFLY_CONFIG, |
| 8 | + WILDFLY_READY_TIMEOUT_MS, |
| 9 | + WILDFLY_POLL_INTERVAL_MS, |
| 10 | + JBOSS_CLI_PATH, |
| 11 | + MANAGEMENT_INTERFACE_ADDRESS, |
| 12 | +} from "../../cypress.config"; |
| 13 | +import { WildflyManagementResponse } from "../interfaces"; |
| 14 | +import { buildLocalhostUrl } from "../helpers"; |
| 15 | + |
| 16 | +export function pollWildflyState(managementApi: string, container: StartedTestContainer): Promise<string> { |
| 17 | + const startTime = new Date().getTime(); |
| 18 | + return new Promise<string>((resolve, reject) => { |
| 19 | + const interval = setInterval(() => { |
| 20 | + if (new Date().getTime() - startTime > WILDFLY_READY_TIMEOUT_MS) { |
| 21 | + clearInterval(interval); |
| 22 | + reject(new Error("Timeout waiting for WildFly to start")); |
| 23 | + } |
| 24 | + axios |
| 25 | + .post(managementApi, { |
| 26 | + operation: "read-attribute", |
| 27 | + name: "server-state", |
| 28 | + }) |
| 29 | + .then((response: WildflyManagementResponse) => { |
| 30 | + if (response.data.result === "running") { |
| 31 | + clearInterval(interval); |
| 32 | + const wildflyServer = buildLocalhostUrl(container.getMappedPort(WILDFLY_MANAGEMENT_PORT)); |
| 33 | + resolve(wildflyServer); |
| 34 | + } |
| 35 | + }) |
| 36 | + .catch(() => { |
| 37 | + console.log("WildFly server is not ready yet"); |
| 38 | + }); |
| 39 | + }, WILDFLY_POLL_INTERVAL_MS); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +export function executeJBossCLI( |
| 44 | + container: StartedTestContainer, |
| 45 | + managementPort: number, |
| 46 | + command: string, |
| 47 | +): Promise<string> { |
| 48 | + return container |
| 49 | + .exec([ |
| 50 | + `/bin/sh`, |
| 51 | + `-c`, |
| 52 | + `${JBOSS_CLI_PATH} --connect --controller=localhost:${managementPort} --command="${command}"`, |
| 53 | + ]) |
| 54 | + .then((result) => result.output); |
| 55 | +} |
| 56 | + |
| 57 | +export function configureWildflyNetworkMode( |
| 58 | + wildfly: GenericContainer, |
| 59 | + configuration: string, |
| 60 | + useHostMode: boolean, |
| 61 | + networkName?: string, |
| 62 | +): Promise<{ portOffset: number }> { |
| 63 | + if (useHostMode) { |
| 64 | + console.log("host mode"); |
| 65 | + return findAPortNotInUse(WILDFLY_PORT_RANGE.min, WILDFLY_PORT_RANGE.max).then((freePort) => { |
| 66 | + const portOffset = freePort - WILDFLY_PORT_RANGE.min; |
| 67 | + wildfly |
| 68 | + .withNetworkMode("host") |
| 69 | + .withCommand([ |
| 70 | + "-c", |
| 71 | + configuration || DEFAULT_WILDFLY_CONFIG, |
| 72 | + `-Djboss.socket.binding.port-offset=${portOffset.toString()}`, |
| 73 | + "-Djboss.node.name=localhost", |
| 74 | + ] as string[]); |
| 75 | + return { portOffset }; |
| 76 | + }); |
| 77 | + } else { |
| 78 | + console.log(`default network mode, network name: ${networkName}`); |
| 79 | + wildfly |
| 80 | + .withNetworkMode(networkName!) |
| 81 | + .withNetworkAliases("wildfly") |
| 82 | + .withExposedPorts(WILDFLY_MANAGEMENT_PORT) |
| 83 | + .withCommand(["-c", configuration || DEFAULT_WILDFLY_CONFIG] as string[]); |
| 84 | + return Promise.resolve({ portOffset: 0 }); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +export function configureWildflyPostStart( |
| 89 | + container: StartedTestContainer, |
| 90 | + halPort: string, |
| 91 | + useHostMode: boolean, |
| 92 | + managementPort?: number, |
| 93 | +): Promise<string> { |
| 94 | + if (useHostMode) { |
| 95 | + const effectiveManagementPort = managementPort!; |
| 96 | + return executeJBossCLI( |
| 97 | + container, |
| 98 | + effectiveManagementPort, |
| 99 | + `/core-service=management/management-interface=http-interface:list-add(name=allowed-origins,value=${buildLocalhostUrl(Number(halPort))}`, |
| 100 | + ) |
| 101 | + .then(() => executeJBossCLI(container, effectiveManagementPort, "reload")) |
| 102 | + .then(() => executeJBossCLI(container, effectiveManagementPort, "read-attribute server-state")) |
| 103 | + .then((output) => { |
| 104 | + if (output.includes("running")) { |
| 105 | + return buildLocalhostUrl(effectiveManagementPort); |
| 106 | + } |
| 107 | + throw new Error("WildFly did not reach running state"); |
| 108 | + }); |
| 109 | + } else { |
| 110 | + const managementApi = buildLocalhostUrl(container.getMappedPort(WILDFLY_MANAGEMENT_PORT), "/management"); |
| 111 | + |
| 112 | + return axios |
| 113 | + .post(managementApi, { |
| 114 | + operation: "list-add", |
| 115 | + address: MANAGEMENT_INTERFACE_ADDRESS, |
| 116 | + name: "allowed-origins", |
| 117 | + value: buildLocalhostUrl(Number(halPort)), |
| 118 | + }) |
| 119 | + .then(() => |
| 120 | + axios.post(managementApi, { |
| 121 | + operation: "reload", |
| 122 | + }), |
| 123 | + ) |
| 124 | + .then(() => pollWildflyState(managementApi, container)); |
| 125 | + } |
| 126 | +} |
0 commit comments