Skip to content

Commit 43d05a9

Browse files
authored
feat(coder/agentapi/test-utils): add feature to optionally use coder_env (#595)
## Description `setup` now returns `coderEnvVariables` that can be used in `execModuleScript`. ## Type of Change - [ ] New module - [ ] New template - [ ] Bug fix - [x] Feature/enhancement - [ ] Documentation - [ ] Other ## Testing & Validation - [x] Tests pass (`bun test`) - [x] Code formatted (`bun fmt`) - [x] Changes tested locally ## Related Issues <!-- Link related issues or write "None" if not applicable --> --------- Signed-off-by: 35C4n0r <[email protected]>
1 parent e3f8b64 commit 43d05a9

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

registry/coder/modules/agentapi/test-util.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,35 @@ import {
44
removeContainer,
55
runContainer,
66
runTerraformApply,
7+
TerraformState,
78
writeFileContainer,
89
} from "~test";
910
import path from "path";
1011
import { expect } from "bun:test";
1112

13+
/**
14+
* Extracts all coder_env resources from Terraform state and returns them as
15+
* a Record of environment variable names to values.
16+
*/
17+
export const extractCoderEnvVars = (
18+
state: TerraformState,
19+
): Record<string, string> => {
20+
const envVars: Record<string, string> = {};
21+
22+
for (const resource of state.resources) {
23+
if (resource.type === "coder_env" && resource.instances.length > 0) {
24+
const instance = resource.instances[0].attributes;
25+
const name = instance.name as string;
26+
const value = instance.value as string;
27+
if (name && value) {
28+
envVars[name] = value;
29+
}
30+
}
31+
}
32+
33+
return envVars;
34+
};
35+
1236
export const setupContainer = async ({
1337
moduleDir,
1438
image,
@@ -23,10 +47,12 @@ export const setupContainer = async ({
2347
...vars,
2448
});
2549
const coderScript = findResourceInstance(state, "coder_script");
50+
const coderEnvVars = extractCoderEnvVars(state);
2651
const id = await runContainer(image ?? "codercom/enterprise-node:latest");
2752
return {
2853
id,
2954
coderScript,
55+
coderEnvVars,
3056
cleanup: async () => {
3157
if (
3258
process.env["DEBUG"] === "true" ||
@@ -79,9 +105,11 @@ interface SetupProps {
79105
agentapiMockScript?: string;
80106
}
81107

82-
export const setup = async (props: SetupProps): Promise<{ id: string }> => {
108+
export const setup = async (
109+
props: SetupProps,
110+
): Promise<{ id: string; coderEnvVars: Record<string, string> }> => {
83111
const projectDir = props.projectDir ?? "/home/coder/project";
84-
const { id, coderScript, cleanup } = await setupContainer({
112+
const { id, coderScript, coderEnvVars, cleanup } = await setupContainer({
85113
moduleDir: props.moduleDir,
86114
vars: props.moduleVariables,
87115
});
@@ -101,7 +129,7 @@ export const setup = async (props: SetupProps): Promise<{ id: string }> => {
101129
filePath: "/home/coder/script.sh",
102130
content: coderScript.script,
103131
});
104-
return { id };
132+
return { id, coderEnvVars };
105133
};
106134

107135
export const expectAgentAPIStarted = async (
@@ -125,18 +153,16 @@ export const execModuleScript = async (
125153
id: string,
126154
env?: Record<string, string>,
127155
) => {
128-
const envArgs = Object.entries(env ?? {})
129-
.map(([key, value]) => ["--env", `${key}=${value}`])
130-
.flat();
131-
const resp = await execContainer(
132-
id,
133-
[
134-
"bash",
135-
"-c",
136-
`set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`,
137-
],
138-
envArgs,
139-
);
156+
const envArgs = env
157+
? Object.entries(env)
158+
.map(([key, value]) => `export ${key}="${value.replace(/"/g, '\\"')}"`)
159+
.join(" && ") + " && "
160+
: "";
161+
const resp = await execContainer(id, [
162+
"bash",
163+
"-c",
164+
`${envArgs}set -o errexit; set -o pipefail; cd /home/coder && ./script.sh 2>&1 | tee /home/coder/script.log`,
165+
]);
140166
if (resp.exitCode !== 0) {
141167
console.log(resp.stdout);
142168
console.log(resp.stderr);

0 commit comments

Comments
 (0)