mirror of
https://github.com/coder/registry.git
synced 2026-06-02 20:48:14 +00:00
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 <work.jaykumar@gmail.com>
This commit is contained in:
@@ -4,11 +4,35 @@ import {
|
|||||||
removeContainer,
|
removeContainer,
|
||||||
runContainer,
|
runContainer,
|
||||||
runTerraformApply,
|
runTerraformApply,
|
||||||
|
TerraformState,
|
||||||
writeFileContainer,
|
writeFileContainer,
|
||||||
} from "~test";
|
} from "~test";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { expect } from "bun:test";
|
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<string, string> => {
|
||||||
|
const envVars: Record<string, string> = {};
|
||||||
|
|
||||||
|
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 ({
|
export const setupContainer = async ({
|
||||||
moduleDir,
|
moduleDir,
|
||||||
image,
|
image,
|
||||||
@@ -23,10 +47,12 @@ export const setupContainer = async ({
|
|||||||
...vars,
|
...vars,
|
||||||
});
|
});
|
||||||
const coderScript = findResourceInstance(state, "coder_script");
|
const coderScript = findResourceInstance(state, "coder_script");
|
||||||
|
const coderEnvVars = extractCoderEnvVars(state);
|
||||||
const id = await runContainer(image ?? "codercom/enterprise-node:latest");
|
const id = await runContainer(image ?? "codercom/enterprise-node:latest");
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
coderScript,
|
coderScript,
|
||||||
|
coderEnvVars,
|
||||||
cleanup: async () => {
|
cleanup: async () => {
|
||||||
if (
|
if (
|
||||||
process.env["DEBUG"] === "true" ||
|
process.env["DEBUG"] === "true" ||
|
||||||
@@ -79,9 +105,11 @@ interface SetupProps {
|
|||||||
agentapiMockScript?: string;
|
agentapiMockScript?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const setup = async (props: SetupProps): Promise<{ id: string }> => {
|
export const setup = async (
|
||||||
|
props: SetupProps,
|
||||||
|
): Promise<{ id: string; coderEnvVars: Record<string, string> }> => {
|
||||||
const projectDir = props.projectDir ?? "/home/coder/project";
|
const projectDir = props.projectDir ?? "/home/coder/project";
|
||||||
const { id, coderScript, cleanup } = await setupContainer({
|
const { id, coderScript, coderEnvVars, cleanup } = await setupContainer({
|
||||||
moduleDir: props.moduleDir,
|
moduleDir: props.moduleDir,
|
||||||
vars: props.moduleVariables,
|
vars: props.moduleVariables,
|
||||||
});
|
});
|
||||||
@@ -101,7 +129,7 @@ export const setup = async (props: SetupProps): Promise<{ id: string }> => {
|
|||||||
filePath: "/home/coder/script.sh",
|
filePath: "/home/coder/script.sh",
|
||||||
content: coderScript.script,
|
content: coderScript.script,
|
||||||
});
|
});
|
||||||
return { id };
|
return { id, coderEnvVars };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const expectAgentAPIStarted = async (
|
export const expectAgentAPIStarted = async (
|
||||||
@@ -125,18 +153,16 @@ export const execModuleScript = async (
|
|||||||
id: string,
|
id: string,
|
||||||
env?: Record<string, string>,
|
env?: Record<string, string>,
|
||||||
) => {
|
) => {
|
||||||
const envArgs = Object.entries(env ?? {})
|
const envArgs = env
|
||||||
.map(([key, value]) => ["--env", `${key}=${value}`])
|
? Object.entries(env)
|
||||||
.flat();
|
.map(([key, value]) => `export ${key}="${value.replace(/"/g, '\\"')}"`)
|
||||||
const resp = await execContainer(
|
.join(" && ") + " && "
|
||||||
id,
|
: "";
|
||||||
[
|
const resp = await execContainer(id, [
|
||||||
"bash",
|
"bash",
|
||||||
"-c",
|
"-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) {
|
if (resp.exitCode !== 0) {
|
||||||
console.log(resp.stdout);
|
console.log(resp.stdout);
|
||||||
console.log(resp.stderr);
|
console.log(resp.stderr);
|
||||||
|
|||||||
Reference in New Issue
Block a user