1
0
Fork 0
mirror of synced 2024-09-16 01:07:19 +12:00
budibase/packages/backend-core/tests/utilities/testContainerUtils.ts
2023-02-06 19:43:08 +00:00

50 lines
1.2 KiB
TypeScript

function getTestContainerSettings(
serverName: string,
key: string
): string | null {
const entry = Object.entries(global).find(
([k]) =>
k.includes(`_${serverName.toUpperCase()}`) &&
k.includes(`_${key.toUpperCase()}__`)
)
if (!entry) {
return null
}
return entry[1]
}
function getContainerInfo(containerName: string, port: number) {
const assignedPort = getTestContainerSettings(
containerName.toUpperCase(),
`PORT_${port}`
)
const host = getTestContainerSettings(containerName.toUpperCase(), "IP")
return {
port: assignedPort,
host,
url: host && assignedPort && `http://${host}:${assignedPort}`,
}
}
function getCouchConfig() {
return getContainerInfo("couchdb-service", 5984)
}
function getMinioConfig() {
return getContainerInfo("minio-service", 9000)
}
export function setupEnv(...envs: any[]) {
const configs = [
{ key: "COUCH_DB_PORT", value: getCouchConfig().port },
{ key: "COUCH_DB_URL", value: getCouchConfig().url },
{ key: "MINIO_PORT", value: getMinioConfig().port },
{ key: "MINIO_URL", value: getMinioConfig().url },
]
for (const config of configs.filter(x => !!x.value)) {
for (const env of envs) {
env._set(config.key, config.value)
}
}
}