Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the proper logs for filebrowser and add generic testBaseLine function #418

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 51 additions & 60 deletions filebrowser/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@ import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
type scriptOutput,
testRequiredVariables,
} from "../test";

function testBaseLine(output: scriptOutput) {
expect(output.exitCode).toBe(0);

const expectedLines = [
"\u001b[[0;1mInstalling filebrowser ",
"🥳 Installation complete! ",
"👷 Starting filebrowser in background... ",
"📂 Serving /root at http://localhost:13339 ",
"📝 Logs at /tmp/filebrowser.log",
];

// we could use expect(output.stdout).toEqual(expect.arrayContaining(expectedLines)), but when it errors, it doesn't say which line is wrong
for (const line of expectedLines) {
expect(output.stdout).toContain(line);
}
}

describe("filebrowser", async () => {
await runTerraformInit(import.meta.dir);

Expand All @@ -28,65 +46,44 @@ describe("filebrowser", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
});
const output = await executeScriptInContainer(state, "alpine");
expect(output.exitCode).toBe(0);
expect(output.stdout).toEqual([
"\u001b[0;1mInstalling filebrowser ",
"",
"🥳 Installation complete! ",
"",
"👷 Starting filebrowser in background... ",
"",
"📂 Serving /root at http://localhost:13339 ",
"",
"Running 'filebrowser --noauth --root /root --port 13339 --baseurl ' ",
"",
"📝 Logs at /tmp/filebrowser.log",
]);

const output = await executeScriptInContainer(
state,
"alpine/curl",
"sh",
"apk add bash",
);

testBaseLine(output);
});

it("runs with database_path var", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
database_path: ".config/filebrowser.db",
});
const output = await executeScriptInContainer(state, "alpine");
expect(output.exitCode).toBe(0);
expect(output.stdout).toEqual([
"\u001b[0;1mInstalling filebrowser ",
"",
"🥳 Installation complete! ",
"",
"👷 Starting filebrowser in background... ",
"",
"📂 Serving /root at http://localhost:13339 ",
"",
"Running 'filebrowser --noauth --root /root --port 13339 -d .config/filebrowser.db --baseurl ' ",
"",
"📝 Logs at /tmp/filebrowser.log",
]);

const output = await await executeScriptInContainer(
state,
"alpine/curl",
"sh",
"apk add bash",
);

testBaseLine(output);
});

it("runs with folder var", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
folder: "/home/coder/project",
});
const output = await executeScriptInContainer(state, "alpine");
expect(output.exitCode).toBe(0);
expect(output.stdout).toEqual([
"\u001b[0;1mInstalling filebrowser ",
"",
"🥳 Installation complete! ",
"",
"👷 Starting filebrowser in background... ",
"",
"📂 Serving /home/coder/project at http://localhost:13339 ",
"",
"Running 'filebrowser --noauth --root /home/coder/project --port 13339 --baseurl ' ",
"",
"📝 Logs at /tmp/filebrowser.log",
]);
const output = await await executeScriptInContainer(
state,
"alpine/curl",
"sh",
"apk add bash",
);
});

it("runs with subdomain=false", async () => {
Expand All @@ -95,20 +92,14 @@ describe("filebrowser", async () => {
agent_name: "main",
subdomain: false,
});
const output = await executeScriptInContainer(state, "alpine");
expect(output.exitCode).toBe(0);
expect(output.stdout).toEqual([
"\u001B[0;1mInstalling filebrowser ",
"",
"🥳 Installation complete! ",
"",
"👷 Starting filebrowser in background... ",
"",
"📂 Serving /root at http://localhost:13339 ",
"",
"Running 'filebrowser --noauth --root /root --port 13339 --baseurl /@default/default.main/apps/filebrowser' ",
"",
"📝 Logs at /tmp/filebrowser.log",
]);

const output = await await executeScriptInContainer(
state,
"alpine/curl",
"sh",
"apk add bash",
);

testBaseLine(output);
});
});
8 changes: 5 additions & 3 deletions filebrowser/run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -euo pipefail

BOLD='\033[[0;1m'

printf "$${BOLD}Installing filebrowser \n\n"
Expand All @@ -22,11 +24,11 @@ export FB_DATABASE="${DB_PATH}"

# Check if filebrowser db exists
if [[ ! -f "${DB_PATH}" ]]; then
filebrowser config init 2>&1 | tee -a ${LOG_PATH}
filebrowser users add admin "" --perm.admin=true --viewMode=mosaic 2>&1 | tee -a ${LOG_PATH}
filebrowser config init 2>&1 | tee -a ${LOG_PATH}
filebrowser users add admin "" --perm.admin=true --viewMode=mosaic 2>&1 | tee -a ${LOG_PATH}
fi

filebrowser config set --baseurl=${SERVER_BASE_PATH} --port=${PORT} --auth.method=noauth --root=$ROOT_DIR 2>&1 | tee -a ${LOG_PATH}
filebrowser config set --baseurl=${SERVER_BASE_PATH} --port=${PORT} --auth.method=noauth --root=$ROOT_DIR 2>&1 | tee -a ${LOG_PATH}

printf "👷 Starting filebrowser in background... \n\n"

Expand Down
18 changes: 13 additions & 5 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export const runContainer = async (
return containerID.trim();
};

export interface scriptOutput {
exitCode: number;
stdout: string[];
stderr: string[];
}

/**
* Finds the only "coder_script" resource in the given state and runs it in a
* container.
Expand All @@ -38,13 +44,15 @@ export const executeScriptInContainer = async (
state: TerraformState,
image: string,
shell = "sh",
): Promise<{
exitCode: number;
stdout: string[];
stderr: string[];
}> => {
before?: string,
): Promise<scriptOutput> => {
const instance = findResourceInstance(state, "coder_script");
const id = await runContainer(image);

if (before) {
const respBefore = await execContainer(id, [shell, "-c", before]);
}

const resp = await execContainer(id, [shell, "-c", instance.script]);
const stdout = resp.stdout.trim().split("\n");
const stderr = resp.stderr.trim().split("\n");
Expand Down
Loading