pull/241/head
Garrett Delfosse 1 year ago
parent 36fa871e7b
commit aced7547bc

@ -10,7 +10,7 @@ To create a new module, clone this repository and run:
A suite of test-helpers exists to run `terraform apply` on modules with variables, and test script output against containers.
The testing suite must be able to run docker containers with the `--network=host` flag, which typically requires running the tests on linux as this flag does not apply to Docker Desktop for MacOS and Windows. MacOS users can work around this by using something like [Orbstack](https://orbstack.dev/) instead of Docker Desktop.
The testing suite must be able to run docker containers with the `--network=host` flag, which typically requires running the tests on linux as this flag does not apply to Docker Desktop for MacOS and Windows. MacOS users can work around this by using something like [Orbstack](https://orbstack.dev/) instead of Docker Desktop.
Reference existing `*.test.ts` files for implementation.

@ -1,5 +1,14 @@
import { describe, expect, it } from "bun:test";
import { createJSONResponse, execContainer, findResourceInstance, runContainer, runTerraformApply, runTerraformInit, testRequiredVariables, writeCoder } from "../test";
import {
createJSONResponse,
execContainer,
findResourceInstance,
runContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
writeCoder,
} from "../test";
import { Server, serve } from "bun";
describe("github-upload-public-key", async () => {
@ -13,7 +22,7 @@ describe("github-upload-public-key", async () => {
const { instance, id } = await setupContainer();
await writeCoder(id, "echo foo");
let exec = await execContainer(id, ["bash", "-c", instance.script]);
expect(exec.stdout).toContain("Coder public SSH key uploaded to GitHub!")
expect(exec.stdout).toContain("Coder public SSH key uploaded to GitHub!");
expect(exec.exitCode).toBe(0);
});
@ -21,69 +30,81 @@ describe("github-upload-public-key", async () => {
const { instance, id } = await setupContainer();
await writeCoder(id, "echo findkey");
let exec = await execContainer(id, ["bash", "-c", instance.script]);
expect(exec.stdout).toContain("Coder public SSH key is already uploaded to GitHub!")
expect(exec.stdout).toContain(
"Coder public SSH key is already uploaded to GitHub!",
);
expect(exec.exitCode).toBe(0);
});
});
const setupContainer = async (
image = "lorello/alpine-bash",
vars: Record<string, string> = {},
) => {
const server = await setupServer();
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
// trim the trailing slash on the URL
access_url: server.url.toString().slice(0, -1),
owner_session_token: "bar",
github_api_url: server.url.toString().slice(0, -1),
...vars,
});
const instance = findResourceInstance(state, "coder_script");
const id = await runContainer(image);
return { id, instance };
image = "lorello/alpine-bash",
vars: Record<string, string> = {},
) => {
const server = await setupServer();
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
// trim the trailing slash on the URL
access_url: server.url.toString().slice(0, -1),
owner_session_token: "bar",
github_api_url: server.url.toString().slice(0, -1),
...vars,
});
const instance = findResourceInstance(state, "coder_script");
const id = await runContainer(image);
return { id, instance };
};
const setupServer = async (): Promise<Server> => {
let url: URL;
const fakeSlackHost = serve({
fetch: (req) => {
url = new URL(req.url);
if (url.pathname === "/api/v2/users/me/gitsshkey") {
return createJSONResponse({
public_key: "exists",
});
}
if (url.pathname === "/user/keys") {
if (req.method === "POST") {
return createJSONResponse({
key: "created",
}, 201);
}
let url: URL;
const fakeSlackHost = serve({
fetch: (req) => {
url = new URL(req.url);
if (url.pathname === "/api/v2/users/me/gitsshkey") {
return createJSONResponse({
public_key: "exists",
});
}
// case: key already exists
if (req.headers.get("Authorization") == "Bearer findkey") {
return createJSONResponse([{
key: "foo",
}, {
key: "exists",
}]);
}
if (url.pathname === "/user/keys") {
if (req.method === "POST") {
return createJSONResponse(
{
key: "created",
},
201,
);
}
// case: key does not exist
return createJSONResponse([{
key: "foo",
}]);
// case: key already exists
if (req.headers.get("Authorization") == "Bearer findkey") {
return createJSONResponse([
{
key: "foo",
},
{
key: "exists",
},
]);
}
// case: key does not exist
return createJSONResponse([
{
key: "foo",
},
]);
}
return createJSONResponse({
error: "not_found"
}, 404);
},
port: 0,
});
return createJSONResponse(
{
error: "not_found",
},
404,
);
},
port: 0,
});
return fakeSlackHost;
}
return fakeSlackHost;
};

@ -23,7 +23,7 @@ variable "external_auth_id" {
variable "github_api_url" {
type = string
description = "The URL of the GitHub instance."
default = "https://api.github.com"
default = "https://api.github.com"
}
// Optional variables mostly for testing purposes, will normally come from data.coder_workspace.me

Loading…
Cancel
Save