diff --git a/globalSetup.ts b/globalSetup.ts index 07a0cec5e2..9ebda5626d 100644 --- a/globalSetup.ts +++ b/globalSetup.ts @@ -6,6 +6,26 @@ import { import { ContainerInfo } from "dockerode" import path from "path" import lockfile from "proper-lockfile" +import { execSync } from "child_process" + +interface DockerContext { + Name: string + Description: string + DockerEndpoint: string + ContextType: string + Error: string +} + +function getCurrentDockerContext(): DockerContext { + const out = execSync("docker context ls --format json") + for (const line of out.toString().split("\n")) { + const parsed = JSON.parse(line) + if (parsed.Current) { + return parsed as DockerContext + } + } + throw new Error("No current Docker context") +} async function getBudibaseContainers() { const client = await getContainerRuntimeClient() @@ -27,6 +47,14 @@ async function killContainers(containers: ContainerInfo[]) { } export default async function setup() { + // For whatever reason, testcontainers doesn't always use the correct current + // docker context. This bit of code forces the issue by finding the current + // context and setting it as the DOCKER_HOST environment + if (!process.env.DOCKER_HOST) { + const dockerContext = getCurrentDockerContext() + process.env.DOCKER_HOST = dockerContext.DockerEndpoint + } + const lockPath = path.resolve(__dirname, "globalSetup.ts") // 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 diff --git a/packages/backend-core/tests/core/utilities/testContainerUtils.ts b/packages/backend-core/tests/core/utilities/testContainerUtils.ts index 727b290183..339656cf09 100644 --- a/packages/backend-core/tests/core/utilities/testContainerUtils.ts +++ b/packages/backend-core/tests/core/utilities/testContainerUtils.ts @@ -78,7 +78,34 @@ export function getExposedV4Port(container: ContainerInfo, port: number) { return getExposedV4Ports(container).find(x => x.container === port)?.host } +interface DockerContext { + Name: string + Description: string + DockerEndpoint: string + ContextType: string + Error: string +} + +function getCurrentDockerContext(): DockerContext { + const out = execSync("docker context ls --format json") + for (const line of out.toString().split("\n")) { + const parsed = JSON.parse(line) + if (parsed.Current) { + return parsed as DockerContext + } + } + throw new Error("No current Docker context") +} + export function setupEnv(...envs: any[]) { + // For whatever reason, testcontainers doesn't always use the correct current + // docker context. This bit of code forces the issue by finding the current + // context and setting it as the DOCKER_HOST environment + if (!process.env.DOCKER_HOST) { + const dockerContext = getCurrentDockerContext() + process.env.DOCKER_HOST = dockerContext.DockerEndpoint + } + // We start couchdb in globalSetup.ts, in the root of the monorepo, so it // should be relatively safe to look for it by its image name. const couch = getContainerByImage("budibase/couchdb")