Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 68ce447

Browse files
phorcys420matifali
authored andcommitted
fix: use the proper logs for filebrowser and add generic testBaseLine function
1 parent e175b1a commit 68ce447

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

filebrowser/main.test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { describe, expect, it } from "bun:test";
2+
import {
3+
executeScriptInContainer,
4+
runTerraformApply,
5+
runTerraformInit,
6+
type scriptOutput,
7+
testRequiredVariables,
8+
} from "../test";
9+
10+
function testBaseLine(output: scriptOutput) {
11+
expect(output.exitCode).toBe(0);
12+
13+
const expectedLines = [
14+
"\u001b[[0;1mInstalling filebrowser ",
15+
"🥳 Installation complete! ",
16+
"👷 Starting filebrowser in background... ",
17+
"📂 Serving /root at http://localhost:13339 ",
18+
"📝 Logs at /tmp/filebrowser.log",
19+
];
20+
21+
// we could use expect(output.stdout).toEqual(expect.arrayContaining(expectedLines)), but when it errors, it doesn't say which line is wrong
22+
for (const line of expectedLines) {
23+
expect(output.stdout).toContain(line);
24+
}
25+
}
26+
27+
describe("filebrowser", async () => {
28+
await runTerraformInit(import.meta.dir);
29+
30+
testRequiredVariables(import.meta.dir, {
31+
agent_id: "foo",
32+
});
33+
34+
it("fails with wrong database_path", async () => {
35+
const state = await runTerraformApply(import.meta.dir, {
36+
agent_id: "foo",
37+
database_path: "nofb",
38+
}).catch((e) => {
39+
if (!e.message.startsWith("\nError: Invalid value for variable")) {
40+
throw e;
41+
}
42+
});
43+
});
44+
45+
it("runs with default", async () => {
46+
const state = await runTerraformApply(import.meta.dir, {
47+
agent_id: "foo",
48+
});
49+
50+
const output = await executeScriptInContainer(
51+
state,
52+
"alpine/curl",
53+
"sh",
54+
"apk add bash",
55+
);
56+
57+
testBaseLine(output);
58+
});
59+
60+
it("runs with database_path var", async () => {
61+
const state = await runTerraformApply(import.meta.dir, {
62+
agent_id: "foo",
63+
database_path: ".config/filebrowser.db",
64+
});
65+
66+
const output = await await executeScriptInContainer(
67+
state,
68+
"alpine/curl",
69+
"sh",
70+
"apk add bash",
71+
);
72+
73+
testBaseLine(output);
74+
});
75+
76+
it("runs with folder var", async () => {
77+
const state = await runTerraformApply(import.meta.dir, {
78+
agent_id: "foo",
79+
folder: "/home/coder/project",
80+
});
81+
const output = await await executeScriptInContainer(
82+
state,
83+
"alpine/curl",
84+
"sh",
85+
"apk add bash",
86+
);
87+
});
88+
89+
it("runs with subdomain=false", async () => {
90+
const state = await runTerraformApply(import.meta.dir, {
91+
agent_id: "foo",
92+
agent_name: "main",
93+
subdomain: false,
94+
});
95+
96+
const output = await await executeScriptInContainer(
97+
state,
98+
"alpine/curl",
99+
"sh",
100+
"apk add bash",
101+
);
102+
103+
testBaseLine(output);
104+
});
105+
});

test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ export const runContainer = async (
3030
return containerID.trim();
3131
};
3232

33+
export interface scriptOutput {
34+
exitCode: number;
35+
stdout: string[];
36+
stderr: string[];
37+
}
38+
3339
/**
3440
* Finds the only "coder_script" resource in the given state and runs it in a
3541
* container.
@@ -38,13 +44,15 @@ export const executeScriptInContainer = async (
3844
state: TerraformState,
3945
image: string,
4046
shell = "sh",
41-
): Promise<{
42-
exitCode: number;
43-
stdout: string[];
44-
stderr: string[];
45-
}> => {
47+
before?: string,
48+
): Promise<scriptOutput> => {
4649
const instance = findResourceInstance(state, "coder_script");
4750
const id = await runContainer(image);
51+
52+
if (before) {
53+
const respBefore = await execContainer(id, [shell, "-c", before]);
54+
}
55+
4856
const resp = await execContainer(id, [shell, "-c", instance.script]);
4957
const stdout = resp.stdout.trim().split("\n");
5058
const stderr = resp.stderr.trim().split("\n");

0 commit comments

Comments
 (0)