2024-04-26 15:01:52 +02:00
|
|
|
import {
|
|
|
|
GenericContainer,
|
|
|
|
Wait,
|
|
|
|
getContainerRuntimeClient,
|
|
|
|
} from "testcontainers"
|
|
|
|
import { ContainerInfo } from "dockerode"
|
2024-04-03 13:11:28 +02:00
|
|
|
import path from "path"
|
2024-04-03 13:06:49 +02:00
|
|
|
import lockfile from "proper-lockfile"
|
2024-03-22 18:34:50 +01:00
|
|
|
|
2024-04-26 15:01:52 +02:00
|
|
|
async function getBudibaseContainers() {
|
|
|
|
const client = await getContainerRuntimeClient()
|
|
|
|
const conatiners = await client.container.list()
|
|
|
|
return conatiners.filter(
|
|
|
|
container =>
|
|
|
|
container.Labels["com.budibase"] === "true" &&
|
|
|
|
container.Labels["org.testcontainers"] === "true"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function killContainers(containers: ContainerInfo[]) {
|
|
|
|
const client = await getContainerRuntimeClient()
|
|
|
|
for (const container of containers) {
|
|
|
|
const c = client.container.getById(container.Id)
|
|
|
|
await c.kill()
|
|
|
|
await c.remove()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 18:34:50 +01:00
|
|
|
export default async function setup() {
|
2024-04-03 13:11:28 +02:00
|
|
|
const lockPath = path.resolve(__dirname, "globalSetup.ts")
|
2024-04-26 12:35:23 +02:00
|
|
|
// 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.
|
|
|
|
lockfile.lockSync(lockPath)
|
2024-04-03 13:06:49 +02:00
|
|
|
|
2024-04-26 15:01:52 +02:00
|
|
|
// Remove any containers that are older than 24 hours. This is to prevent
|
|
|
|
// containers getting full volumes or accruing any other problems from being
|
|
|
|
// left up for very long periods of time.
|
|
|
|
const threshold = new Date(Date.now() - 1000 * 60 * 60 * 24)
|
|
|
|
const containers = (await getBudibaseContainers()).filter(container => {
|
|
|
|
const created = new Date(container.Created * 1000)
|
|
|
|
return created < threshold
|
|
|
|
})
|
|
|
|
|
|
|
|
await killContainers(containers)
|
|
|
|
|
2024-04-03 13:06:49 +02:00
|
|
|
try {
|
2024-09-20 15:52:28 +02:00
|
|
|
const couchdb = new GenericContainer("budibase/couchdb:v3.3.3-sqs-v2.1.1")
|
2024-04-09 16:31:32 +02:00
|
|
|
.withExposedPorts(5984, 4984)
|
2024-04-03 13:06:49 +02:00
|
|
|
.withEnvironment({
|
|
|
|
COUCHDB_PASSWORD: "budibase",
|
|
|
|
COUCHDB_USER: "budibase",
|
|
|
|
})
|
|
|
|
.withCopyContentToContainer([
|
|
|
|
{
|
|
|
|
content: `
|
2024-03-26 10:58:40 +01:00
|
|
|
[log]
|
|
|
|
level = warn
|
|
|
|
`,
|
2024-04-03 13:06:49 +02:00
|
|
|
target: "/opt/couchdb/etc/local.d/test-couchdb.ini",
|
|
|
|
},
|
|
|
|
])
|
2024-04-26 15:01:52 +02:00
|
|
|
.withLabels({ "com.budibase": "true" })
|
2024-11-07 13:51:30 +01:00
|
|
|
.withTmpFs({ "/data": "rw" })
|
2024-04-26 12:35:23 +02:00
|
|
|
.withReuse()
|
2024-04-03 13:06:49 +02:00
|
|
|
.withWaitStrategy(
|
|
|
|
Wait.forSuccessfulCommand(
|
|
|
|
"curl http://budibase:budibase@localhost:5984/_up"
|
|
|
|
).withStartupTimeout(20000)
|
|
|
|
)
|
2024-03-27 17:40:41 +01:00
|
|
|
|
2024-05-08 15:08:34 +02:00
|
|
|
const minio = new GenericContainer("minio/minio")
|
|
|
|
.withExposedPorts(9000)
|
|
|
|
.withCommand(["server", "/data"])
|
2024-11-07 13:51:30 +01:00
|
|
|
.withTmpFs({ "/data": "rw" })
|
2024-05-08 15:08:34 +02:00
|
|
|
.withEnvironment({
|
|
|
|
MINIO_ACCESS_KEY: "budibase",
|
|
|
|
MINIO_SECRET_KEY: "budibase",
|
|
|
|
})
|
|
|
|
.withLabels({ "com.budibase": "true" })
|
|
|
|
.withReuse()
|
|
|
|
.withWaitStrategy(
|
|
|
|
Wait.forHttp("/minio/health/ready", 9000).withStartupTimeout(10000)
|
|
|
|
)
|
|
|
|
|
|
|
|
await Promise.all([couchdb.start(), minio.start()])
|
2024-04-03 13:06:49 +02:00
|
|
|
} finally {
|
2024-04-26 12:35:23 +02:00
|
|
|
lockfile.unlockSync(lockPath)
|
2024-04-03 13:06:49 +02:00
|
|
|
}
|
2024-03-22 18:34:50 +01:00
|
|
|
}
|