budibase/globalSetup.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

import { GenericContainer, Wait } from "testcontainers"
2024-04-03 13:11:28 +02:00
import path from "path"
import lockfile from "proper-lockfile"
export default async function setup() {
2024-04-03 13:11:28 +02:00
const lockPath = path.resolve(__dirname, "globalSetup.ts")
if (process.env.REUSE_CONTAINERS) {
// If you run multiple tests at the same time, it's possible for the CouchDB
// shared container to get started multiple times despite having an
// identical reuse hash. To avoid that, we do a filesystem-based lock so
// that only one globalSetup.ts is running at a time.
2024-04-03 13:11:28 +02:00
lockfile.lockSync(lockPath)
}
try {
let couchdb = new GenericContainer("budibase/couchdb")
.withExposedPorts(5984)
.withEnvironment({
COUCHDB_PASSWORD: "budibase",
COUCHDB_USER: "budibase",
})
.withCopyContentToContainer([
{
content: `
2024-03-26 10:58:40 +01:00
[log]
level = warn
`,
target: "/opt/couchdb/etc/local.d/test-couchdb.ini",
},
])
.withWaitStrategy(
Wait.forSuccessfulCommand(
"curl http://budibase:budibase@localhost:5984/_up"
).withStartupTimeout(20000)
)
if (process.env.REUSE_CONTAINERS) {
couchdb = couchdb.withReuse()
}
if (process.env.CONTAINER_NAMESPACE) {
couchdb = couchdb.withLabels({
"org.testcontainers.namespace": process.env.CONTAINER_NAMESPACE,
})
}
await couchdb.start()
} finally {
if (process.env.REUSE_CONTAINERS) {
2024-04-03 13:11:28 +02:00
lockfile.unlockSync(lockPath)
}
}
}