Attempting to fix issues where wrong port is picked for test containers, occurred a lot locally.

This commit is contained in:
mike12345567 2023-02-27 21:19:07 +00:00
parent af2d1984f6
commit 20a8ec8600
2 changed files with 56 additions and 13 deletions

View File

@ -8,8 +8,8 @@ services:
# Last version that supports the "fs" backend # Last version that supports the "fs" backend
image: minio/minio:RELEASE.2022-10-24T18-35-07Z image: minio/minio:RELEASE.2022-10-24T18-35-07Z
ports: ports:
- 9000 - "9000"
- 9001 - "9001"
environment: environment:
MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY} MINIO_ACCESS_KEY: ${MINIO_ACCESS_KEY}
MINIO_SECRET_KEY: ${MINIO_SECRET_KEY} MINIO_SECRET_KEY: ${MINIO_SECRET_KEY}
@ -28,9 +28,9 @@ services:
- COUCHDB_PASSWORD=${COUCH_DB_PASSWORD} - COUCHDB_PASSWORD=${COUCH_DB_PASSWORD}
- COUCHDB_USER=${COUCH_DB_USER} - COUCHDB_USER=${COUCH_DB_USER}
ports: ports:
- 5984 - "5984"
- 4369 - "4369"
- 9100 - "9100"
healthcheck: healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5984/_up"] test: ["CMD", "curl", "-f", "http://localhost:5984/_up"]
interval: 30s interval: 30s
@ -42,6 +42,6 @@ services:
image: redis image: redis
command: redis-server --requirepass ${REDIS_PASSWORD} command: redis-server --requirepass ${REDIS_PASSWORD}
ports: ports:
- 6379 - "6379"
healthcheck: healthcheck:
test: ["CMD", "redis-cli", "ping"] test: ["CMD", "redis-cli", "ping"]

View File

@ -1,3 +1,31 @@
import { execSync } from "child_process"
let dockerPsResult: string | undefined
function formatDockerPsResult(serverName: string, port: number) {
const lines = dockerPsResult?.split("\n")
let first = true
if (!lines) {
return null
}
for (let line of lines) {
if (first) {
first = false
continue
}
let toLookFor = serverName.split("-service")[0]
if (!line.includes(toLookFor)) {
continue
}
const regex = new RegExp(`0.0.0.0:([0-9]*)->${port}`, "g")
const found = line.match(regex)
if (found) {
return found[0].split(":")[1].split("->")[0]
}
}
return null
}
function getTestContainerSettings( function getTestContainerSettings(
serverName: string, serverName: string,
key: string key: string
@ -14,10 +42,22 @@ function getTestContainerSettings(
} }
function getContainerInfo(containerName: string, port: number) { function getContainerInfo(containerName: string, port: number) {
const assignedPort = getTestContainerSettings( let assignedPort = getTestContainerSettings(
containerName.toUpperCase(), containerName.toUpperCase(),
`PORT_${port}` `PORT_${port}`
) )
if (!dockerPsResult) {
try {
const outputBuffer = execSync("docker ps")
dockerPsResult = outputBuffer.toString("utf8")
} catch (err) {
//no-op
}
}
const possiblePort = formatDockerPsResult(containerName, port)
if (possiblePort) {
assignedPort = possiblePort
}
const host = getTestContainerSettings(containerName.toUpperCase(), "IP") const host = getTestContainerSettings(containerName.toUpperCase(), "IP")
return { return {
port: assignedPort, port: assignedPort,
@ -39,12 +79,15 @@ function getRedisConfig() {
} }
export function setupEnv(...envs: any[]) { export function setupEnv(...envs: any[]) {
const couch = getCouchConfig(),
minio = getCouchConfig(),
redis = getRedisConfig()
const configs = [ const configs = [
{ key: "COUCH_DB_PORT", value: getCouchConfig().port }, { key: "COUCH_DB_PORT", value: couch.port },
{ key: "COUCH_DB_URL", value: getCouchConfig().url }, { key: "COUCH_DB_URL", value: couch.url },
{ key: "MINIO_PORT", value: getMinioConfig().port }, { key: "MINIO_PORT", value: minio.port },
{ key: "MINIO_URL", value: getMinioConfig().url }, { key: "MINIO_URL", value: minio.url },
{ key: "REDIS_URL", value: getRedisConfig().url }, { key: "REDIS_URL", value: redis.url },
] ]
for (const config of configs.filter(x => !!x.value)) { for (const config of configs.filter(x => !!x.value)) {