Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

test(harness): isolate the Docker context too, and restore test env

VNATdev-ops0f1510bd6e50
diff --git a/server/testing/container-env.test.ts b/server/testing/container-env.test.ts
index 71f872de09..0f232a498c 100644
--- a/server/testing/container-env.test.ts
+++ b/server/testing/container-env.test.ts
@@ -3,21 +3,35 @@ import { describe, expect, it } from "vitest";
import { noHostContainerEnv } from "./container-env.ts";
+const original = process.env.OMB_TEST_ALLOW_CONTAINERS;
+
+function withAllowContainers(value: string | undefined, check: () => void): void {
+ if (value === undefined) delete process.env.OMB_TEST_ALLOW_CONTAINERS;
+ else process.env.OMB_TEST_ALLOW_CONTAINERS = value;
+ try {
+ check();
+ } finally {
+ if (original === undefined) delete process.env.OMB_TEST_ALLOW_CONTAINERS;
+ else process.env.OMB_TEST_ALLOW_CONTAINERS = original;
+ }
+}
+
describe("noHostContainerEnv", () => {
it("points both container CLIs at sockets inside the throwaway home", () => {
- const home = join("tmp", "omb-test-home");
- expect(noHostContainerEnv(home)).toEqual({
- DOCKER_HOST: `unix://${join(home, "no-docker.sock")}`,
- CONTAINER_HOST: `unix://${join(home, "no-podman.sock")}`,
+ withAllowContainers(undefined, () => {
+ const home = join("tmp", "omb-test-home");
+ expect(noHostContainerEnv(home)).toEqual({
+ DOCKER_HOST: `unix://${join(home, "no-docker.sock")}`,
+ CONTAINER_HOST: `unix://${join(home, "no-podman.sock")}`,
+ DOCKER_CONFIG: join(home, ".docker"),
+ DOCKER_CONTEXT: "default",
+ });
});
});
it("leaves the runtime alone when a test opts in", () => {
- process.env.OMB_TEST_ALLOW_CONTAINERS = "1";
- try {
+ withAllowContainers("1", () => {
expect(noHostContainerEnv("any-home")).toEqual({});
- } finally {
- delete process.env.OMB_TEST_ALLOW_CONTAINERS;
- }
+ });
});
});
diff --git a/server/testing/container-env.ts b/server/testing/container-env.ts
index 6b2f91719e..488ec8bc0c 100644
--- a/server/testing/container-env.ts
+++ b/server/testing/container-env.ts
@@ -1,10 +1,15 @@
import { join } from "node:path";
+type ContainerEnv = Partial<Pick<NodeJS.ProcessEnv, "DOCKER_HOST" | "CONTAINER_HOST" | "DOCKER_CONFIG" | "DOCKER_CONTEXT">>;
+
/** Test servers never see the developer's container daemon; a test that needs one opts in with OMB_TEST_ALLOW_CONTAINERS=1. */
-export function noHostContainerEnv(home: string): Partial<Pick<NodeJS.ProcessEnv, "DOCKER_HOST" | "CONTAINER_HOST">> {
+export function noHostContainerEnv(home: string): ContainerEnv {
if (process.env.OMB_TEST_ALLOW_CONTAINERS === "1") return {};
return {
DOCKER_HOST: `unix://${join(home, "no-docker.sock")}`,
CONTAINER_HOST: `unix://${join(home, "no-podman.sock")}`,
+ // A selected Docker context wins over DOCKER_HOST, so isolate its config and select the default context.
+ DOCKER_CONFIG: join(home, ".docker"),
+ DOCKER_CONTEXT: "default",
};
}