Compare commits

...

2 Commits

Author SHA1 Message Date
DevelopmentCats ab2a61e8d4 chore: remove test from cloud-dev template 2026-01-05 15:35:26 -06:00
DevelopmentCats 9702f036a4 refactor(github-upload-public-key): enhance test structure and cleanup handling 2026-01-05 12:58:34 -06:00
2 changed files with 92 additions and 145 deletions
@@ -1,9 +1,17 @@
import { type Server, serve } from "bun";
import { describe, expect, it } from "bun:test";
import { serve } from "bun";
import {
afterEach,
beforeAll,
describe,
expect,
it,
setDefaultTimeout,
} from "bun:test";
import {
createJSONResponse,
execContainer,
findResourceInstance,
removeContainer,
runContainer,
runTerraformApply,
runTerraformInit,
@@ -11,77 +19,48 @@ import {
writeCoder,
} from "~test";
describe("github-upload-public-key", async () => {
await runTerraformInit(import.meta.dir);
testRequiredVariables(import.meta.dir, {
agent_id: "foo",
});
it("creates new key if one does not exist", async () => {
const { instance, id, server } = await setupContainer();
await writeCoder(id, "echo foo");
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env",
`CODER_ACCESS_URL=${url}`,
`GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github",
"bash",
"-c",
instance.script,
]);
expect(exec.stdout).toContain(
"Your Coder public key has been added to GitHub!",
);
expect(exec.exitCode).toBe(0);
// we need to increase timeout to pull the container
}, 15000);
it("does nothing if one already exists", async () => {
const { instance, id, server } = await setupContainer();
// use keyword to make server return a existing key
await writeCoder(id, "echo findkey");
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env",
`CODER_ACCESS_URL=${url}`,
`GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github",
"bash",
"-c",
instance.script,
]);
expect(exec.stdout).toContain(
"Your Coder public key is already on GitHub!",
);
expect(exec.exitCode).toBe(0);
});
let cleanupFunctions: (() => Promise<void>)[] = [];
const registerCleanup = (cleanup: () => Promise<void>) => {
cleanupFunctions.push(cleanup);
};
afterEach(async () => {
const cleanupFnsCopy = cleanupFunctions.slice().reverse();
cleanupFunctions = [];
for (const cleanup of cleanupFnsCopy) {
try {
await cleanup();
} catch (error) {
console.error("Error during cleanup:", error);
}
}
});
const setupContainer = async (
image = "lorello/alpine-bash",
vars: Record<string, string> = {},
) => {
const server = await setupServer();
const server = setupServer();
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
...vars,
});
const instance = findResourceInstance(state, "coder_script");
const id = await runContainer(image);
registerCleanup(async () => {
server.stop();
});
registerCleanup(async () => {
await removeContainer(id);
});
return { id, instance, server };
};
const setupServer = async (): Promise<Server> => {
let url: URL;
const fakeSlackHost = serve({
const setupServer = () => {
const fakeGithubHost = serve({
fetch: (req) => {
url = new URL(req.url);
const url = new URL(req.url);
if (url.pathname === "/api/v2/users/me/gitsshkey") {
return createJSONResponse({
public_key: "exists",
@@ -128,5 +107,60 @@ const setupServer = async (): Promise<Server> => {
port: 0,
});
return fakeSlackHost;
return fakeGithubHost;
};
setDefaultTimeout(30 * 1000);
describe("github-upload-public-key", () => {
beforeAll(async () => {
await runTerraformInit(import.meta.dir);
});
testRequiredVariables(import.meta.dir, {
agent_id: "foo",
});
it("creates new key if one does not exist", async () => {
const { instance, id, server } = await setupContainer();
await writeCoder(id, "echo foo");
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env",
`CODER_ACCESS_URL=${url}`,
`GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github",
"bash",
"-c",
instance.script,
]);
expect(exec.stdout).toContain(
"Your Coder public key has been added to GitHub!",
);
expect(exec.exitCode).toBe(0);
});
it("does nothing if one already exists", async () => {
const { instance, id, server } = await setupContainer();
// use keyword to make server return a existing key
await writeCoder(id, "echo findkey");
const url = server.url.toString().slice(0, -1);
const exec = await execContainer(id, [
"env",
`CODER_ACCESS_URL=${url}`,
`GITHUB_API_URL=${url}`,
"CODER_OWNER_SESSION_TOKEN=foo",
"CODER_EXTERNAL_AUTH_ID=github",
"bash",
"-c",
instance.script,
]);
expect(exec.stdout).toContain(
"Your Coder public key is already on GitHub!",
);
expect(exec.exitCode).toBe(0);
});
});
@@ -1,87 +0,0 @@
# Run 'terraform test' from this template directory (where main.tf lives)
# --- Mock cloud providers so no external calls happen ---
mock_provider "aws" {}
mock_provider "kubernetes" {}
# Provide fake values for data sources your template reads
override_data {
target = data.aws_eks_cluster.eks
values = {
name = "unit-test-eks"
endpoint = "https://example.eks.local"
certificate_authority = [{
data = base64encode("dummy-ca")
}]
}
}
override_data {
target = data.aws_eks_cluster_auth.eks
values = {
token = "dummy-token"
}
}
# ---------------------------
# 1) Validate configuration
# ---------------------------
run "validate" {
command = validate
}
# ---------------------------
# 2) Plan with representative inputs
# ---------------------------
run "plan_with_defaults" {
command = plan
variables {
host_cluster_name = "unit-test-eks"
# IaC/tooling toggles
iac_tool = "terraform"
enable_aws = true
enable_azure = false
enable_gcp = false
# Dev creds (empty OK for unit test)
aws_access_key_id = ""
aws_secret_access_key = ""
azure_client_id = ""
azure_tenant_id = ""
azure_client_secret = ""
gcp_service_account = ""
}
# Simple sanity assertions (adjust resource addresses to your template)
assert {
condition = can(resource.kubernetes_namespace.workspace)
error_message = "kubernetes_namespace.workspace was not created in plan."
}
assert {
condition = can(resource.coder_agent.main)
error_message = "coder_agent.main was not planned."
}
}
# ---------------------------
# 3) Plan with CDK selected
# ---------------------------
run "plan_with_cdk" {
command = plan
variables {
host_cluster_name = "unit-test-eks"
iac_tool = "cdk"
enable_aws = true
enable_azure = false
enable_gcp = false
}
# Ensure the env reflects choice (string map lookup)
assert {
condition = contains(keys(resource.coder_agent.main.env), "IAC_TOOL")
error_message = "IAC_TOOL env not present on coder_agent.main."
}
}