fix(github-upload-public-key): resolve issues with flaky tests (#634)

## Description

Better test cleanup, and resolve flakiness.
<!-- Briefly describe what this PR does and why -->

## Type of Change

- [ ] New module
- [ ] New template
- [X] Bug fix
- [ ] Feature/enhancement
- [ ] Documentation
- [ ] Other

## Module Information

<!-- Delete this section if not applicable -->

**Path:** `registry/coder/modules/github-upload-public-key`  
**New version:** `v1.0.32`  
**Breaking change:** [ ] Yes [ ] No

## 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 -->
This commit is contained in:
DevCats 2026-01-05 14:59:06 -06:00 committed by GitHub
parent c819ca7f83
commit 99bd4a4139
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,17 @@
import { type Server, serve } from "bun"; import { serve } from "bun";
import { describe, expect, it } from "bun:test"; import {
afterEach,
beforeAll,
describe,
expect,
it,
setDefaultTimeout,
} from "bun:test";
import { import {
createJSONResponse, createJSONResponse,
execContainer, execContainer,
findResourceInstance, findResourceInstance,
removeContainer,
runContainer, runContainer,
runTerraformApply, runTerraformApply,
runTerraformInit, runTerraformInit,
@ -11,77 +19,48 @@ import {
writeCoder, writeCoder,
} from "~test"; } from "~test";
describe("github-upload-public-key", async () => { let cleanupFunctions: (() => Promise<void>)[] = [];
await runTerraformInit(import.meta.dir); const registerCleanup = (cleanup: () => Promise<void>) => {
cleanupFunctions.push(cleanup);
testRequiredVariables(import.meta.dir, { };
agent_id: "foo", afterEach(async () => {
}); const cleanupFnsCopy = cleanupFunctions.slice().reverse();
cleanupFunctions = [];
it("creates new key if one does not exist", async () => { for (const cleanup of cleanupFnsCopy) {
const { instance, id, server } = await setupContainer(); try {
await writeCoder(id, "echo foo"); await cleanup();
} catch (error) {
const url = server.url.toString().slice(0, -1); console.error("Error during cleanup:", error);
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);
});
}); });
const setupContainer = async ( const setupContainer = async (
image = "lorello/alpine-bash", image = "lorello/alpine-bash",
vars: Record<string, string> = {}, vars: Record<string, string> = {},
) => { ) => {
const server = await setupServer(); const server = setupServer();
const state = await runTerraformApply(import.meta.dir, { const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo", agent_id: "foo",
...vars, ...vars,
}); });
const instance = findResourceInstance(state, "coder_script"); const instance = findResourceInstance(state, "coder_script");
const id = await runContainer(image); const id = await runContainer(image);
registerCleanup(async () => {
server.stop();
});
registerCleanup(async () => {
await removeContainer(id);
});
return { id, instance, server }; return { id, instance, server };
}; };
const setupServer = async (): Promise<Server> => { const setupServer = () => {
let url: URL; const fakeGithubHost = serve({
const fakeSlackHost = serve({
fetch: (req) => { fetch: (req) => {
url = new URL(req.url); const url = new URL(req.url);
if (url.pathname === "/api/v2/users/me/gitsshkey") { if (url.pathname === "/api/v2/users/me/gitsshkey") {
return createJSONResponse({ return createJSONResponse({
public_key: "exists", public_key: "exists",
@ -128,5 +107,60 @@ const setupServer = async (): Promise<Server> => {
port: 0, 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);
});
});