From a073f59f8eef7635e891e58616a14f7d76e6eb04 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Thu, 11 Dec 2025 04:55:30 +0000 Subject: [PATCH 1/2] feat(coder/agentapi/test-utils): add feature to optionally consume coder_env Signed-off-by: 35C4n0r --- registry/coder/modules/agentapi/test-util.ts | 41 ++++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/registry/coder/modules/agentapi/test-util.ts b/registry/coder/modules/agentapi/test-util.ts index 368af5093..41346d8d2 100644 --- a/registry/coder/modules/agentapi/test-util.ts +++ b/registry/coder/modules/agentapi/test-util.ts @@ -4,11 +4,33 @@ import { removeContainer, runContainer, runTerraformApply, + TerraformState, writeFileContainer, } from "~test"; import path from "path"; import { expect } from "bun:test"; +/** + * Extracts all coder_env resources from Terraform state and returns them as + * a Record of environment variable names to values. + */ +export const extractCoderEnvVars = (state: TerraformState): Record => { + const envVars: Record = {}; + + for (const resource of state.resources) { + if (resource.type === "coder_env" && resource.instances.length > 0) { + const instance = resource.instances[0].attributes; + const name = instance.name as string; + const value = instance.value as string; + if (name && value) { + envVars[name] = value; + } + } + } + + return envVars; +}; + export const setupContainer = async ({ moduleDir, image, @@ -23,10 +45,12 @@ export const setupContainer = async ({ ...vars, }); const coderScript = findResourceInstance(state, "coder_script"); + const coderEnvVars = extractCoderEnvVars(state); const id = await runContainer(image ?? "codercom/enterprise-node:latest"); return { id, coderScript, + coderEnvVars, cleanup: async () => { if ( process.env["DEBUG"] === "true" || @@ -79,9 +103,9 @@ interface SetupProps { agentapiMockScript?: string; } -export const setup = async (props: SetupProps): Promise<{ id: string }> => { +export const setup = async (props: SetupProps): Promise<{ id: string; coderEnvVars: Record }> => { const projectDir = props.projectDir ?? "/home/coder/project"; - const { id, coderScript, cleanup } = await setupContainer({ + const { id, coderScript, coderEnvVars, cleanup } = await setupContainer({ moduleDir: props.moduleDir, vars: props.moduleVariables, }); @@ -101,7 +125,7 @@ export const setup = async (props: SetupProps): Promise<{ id: string }> => { filePath: "/home/coder/script.sh", content: coderScript.script, }); - return { id }; + return { id, coderEnvVars }; }; export const expectAgentAPIStarted = async ( @@ -125,17 +149,18 @@ export const execModuleScript = async ( id: string, env?: Record, ) => { - const envArgs = Object.entries(env ?? {}) - .map(([key, value]) => ["--env", `${key}=${value}`]) - .flat(); + const envArgs = env + ? Object.entries(env) + .map(([key, value]) => `export ${key}="${value.replace(/"/g, '\\"')}"`) + .join(" && ") + " && " + : ""; const resp = await execContainer( id, [ "bash", "-c", - `set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`, + `${envArgs}set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`, ], - envArgs, ); if (resp.exitCode !== 0) { console.log(resp.stdout); From d64146b70d34d3f82d18db94929b8774abbdb122 Mon Sep 17 00:00:00 2001 From: 35C4n0r Date: Thu, 11 Dec 2025 05:00:27 +0000 Subject: [PATCH 2/2] fmt: bun fmt Signed-off-by: 35C4n0r --- registry/coder/modules/agentapi/test-util.ts | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/registry/coder/modules/agentapi/test-util.ts b/registry/coder/modules/agentapi/test-util.ts index 41346d8d2..85d1bddd8 100644 --- a/registry/coder/modules/agentapi/test-util.ts +++ b/registry/coder/modules/agentapi/test-util.ts @@ -14,7 +14,9 @@ import { expect } from "bun:test"; * Extracts all coder_env resources from Terraform state and returns them as * a Record of environment variable names to values. */ -export const extractCoderEnvVars = (state: TerraformState): Record => { +export const extractCoderEnvVars = ( + state: TerraformState, +): Record => { const envVars: Record = {}; for (const resource of state.resources) { @@ -103,7 +105,9 @@ interface SetupProps { agentapiMockScript?: string; } -export const setup = async (props: SetupProps): Promise<{ id: string; coderEnvVars: Record }> => { +export const setup = async ( + props: SetupProps, +): Promise<{ id: string; coderEnvVars: Record }> => { const projectDir = props.projectDir ?? "/home/coder/project"; const { id, coderScript, coderEnvVars, cleanup } = await setupContainer({ moduleDir: props.moduleDir, @@ -154,14 +158,11 @@ export const execModuleScript = async ( .map(([key, value]) => `export ${key}="${value.replace(/"/g, '\\"')}"`) .join(" && ") + " && " : ""; - const resp = await execContainer( - id, - [ - "bash", - "-c", - `${envArgs}set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`, - ], - ); + const resp = await execContainer(id, [ + "bash", + "-c", + `${envArgs}set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`, + ]); if (resp.exitCode !== 0) { console.log(resp.stdout); console.log(resp.stderr);