|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import { |
| 3 | + execContainer, |
| 4 | + findResourceInstance, |
| 5 | + removeContainer, |
| 6 | + runContainer, |
| 7 | + runTerraformApply, |
| 8 | + runTerraformInit, |
| 9 | + testRequiredVariables, |
| 10 | + type TerraformState, |
| 11 | +} from "~test"; |
| 12 | + |
| 13 | +interface TestFixture { |
| 14 | + state: TerraformState; |
| 15 | + server: ReturnType<typeof Bun.serve>; |
| 16 | + [Symbol.asyncDispose](): Promise<void>; |
| 17 | +} |
| 18 | + |
| 19 | +interface ContainerHandle { |
| 20 | + id: string; |
| 21 | + [Symbol.asyncDispose](): Promise<void>; |
| 22 | +} |
| 23 | + |
| 24 | +async function setupContainer(image: string): Promise<ContainerHandle> { |
| 25 | + const id = await runContainer(image); |
| 26 | + return { |
| 27 | + id, |
| 28 | + [Symbol.asyncDispose]: async () => { |
| 29 | + await removeContainer(id); |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +const ENV_PREFIX = |
| 35 | + 'export CODER_SCRIPT_DATA_DIR=/tmp/coder-script-data && export CODER_SCRIPT_BIN_DIR=/tmp/coder-script-data/bin && mkdir -p "$CODER_SCRIPT_DATA_DIR" "$CODER_SCRIPT_BIN_DIR" && '; |
| 36 | + |
| 37 | +async function setupFakeBinaryServer( |
| 38 | + dir: string, |
| 39 | + extraVars?: Record<string, string>, |
| 40 | +): Promise<TestFixture> { |
| 41 | + const fakeBinary = "#!/bin/sh\necho portabledesktop"; |
| 42 | + const server = Bun.serve({ |
| 43 | + port: 0, |
| 44 | + fetch() { |
| 45 | + return new Response(fakeBinary); |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + const state = await runTerraformApply(dir, { |
| 50 | + agent_id: "foo", |
| 51 | + url: `http://localhost:${server.port}/portabledesktop`, |
| 52 | + ...extraVars, |
| 53 | + }); |
| 54 | + |
| 55 | + return { |
| 56 | + state, |
| 57 | + server, |
| 58 | + [Symbol.asyncDispose]: async () => { |
| 59 | + server.stop(true); |
| 60 | + }, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("portabledesktop", async () => { |
| 65 | + await runTerraformInit(import.meta.dir); |
| 66 | + |
| 67 | + testRequiredVariables(import.meta.dir, { |
| 68 | + agent_id: "foo", |
| 69 | + }); |
| 70 | + |
| 71 | + it("installs portabledesktop successfully", async () => { |
| 72 | + await using fixture = await setupFakeBinaryServer(import.meta.dir); |
| 73 | + await using container = await setupContainer("alpine/curl"); |
| 74 | + |
| 75 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 76 | + const resp = await execContainer(container.id, [ |
| 77 | + "sh", |
| 78 | + "-c", |
| 79 | + ENV_PREFIX + script, |
| 80 | + ]); |
| 81 | + |
| 82 | + expect(resp.exitCode).toBe(0); |
| 83 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 84 | + |
| 85 | + // Check binary exists at CODER_SCRIPT_DATA_DIR. |
| 86 | + const checkBinary = await execContainer(container.id, [ |
| 87 | + "test", |
| 88 | + "-x", |
| 89 | + "/tmp/coder-script-data/portabledesktop", |
| 90 | + ]); |
| 91 | + expect(checkBinary.exitCode).toBe(0); |
| 92 | + |
| 93 | + // Check symlink exists at CODER_SCRIPT_BIN_DIR. |
| 94 | + const checkSymlink = await execContainer(container.id, [ |
| 95 | + "test", |
| 96 | + "-L", |
| 97 | + "/tmp/coder-script-data/bin/portabledesktop", |
| 98 | + ]); |
| 99 | + expect(checkSymlink.exitCode).toBe(0); |
| 100 | + }, 30000); |
| 101 | + |
| 102 | + it("verifies checksum when sha256 is provided", async () => { |
| 103 | + const fakeBinary = "#!/bin/sh\necho portabledesktop"; |
| 104 | + const hasher = new Bun.CryptoHasher("sha256"); |
| 105 | + hasher.update(fakeBinary); |
| 106 | + const sha256 = hasher.digest("hex"); |
| 107 | + |
| 108 | + await using fixture = await setupFakeBinaryServer(import.meta.dir, { |
| 109 | + sha256, |
| 110 | + }); |
| 111 | + await using container = await setupContainer("alpine/curl"); |
| 112 | + |
| 113 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 114 | + const resp = await execContainer(container.id, [ |
| 115 | + "sh", |
| 116 | + "-c", |
| 117 | + ENV_PREFIX + script, |
| 118 | + ]); |
| 119 | + |
| 120 | + expect(resp.exitCode).toBe(0); |
| 121 | + expect(resp.stdout).toContain("Checksum verified successfully"); |
| 122 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 123 | + }, 30000); |
| 124 | + |
| 125 | + it("fails when sha256 does not match", async () => { |
| 126 | + const wrongSha256 = |
| 127 | + "0000000000000000000000000000000000000000000000000000000000000000"; |
| 128 | + |
| 129 | + await using fixture = await setupFakeBinaryServer(import.meta.dir, { |
| 130 | + sha256: wrongSha256, |
| 131 | + }); |
| 132 | + await using container = await setupContainer("alpine/curl"); |
| 133 | + |
| 134 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 135 | + const resp = await execContainer(container.id, [ |
| 136 | + "sh", |
| 137 | + "-c", |
| 138 | + ENV_PREFIX + script, |
| 139 | + ]); |
| 140 | + |
| 141 | + expect(resp.exitCode).toBe(1); |
| 142 | + expect(resp.stdout).toContain("Checksum mismatch"); |
| 143 | + }, 30000); |
| 144 | + |
| 145 | + it("skips checksum verification when sha256 is not set", async () => { |
| 146 | + await using fixture = await setupFakeBinaryServer(import.meta.dir); |
| 147 | + await using container = await setupContainer("alpine/curl"); |
| 148 | + |
| 149 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 150 | + const resp = await execContainer(container.id, [ |
| 151 | + "sh", |
| 152 | + "-c", |
| 153 | + ENV_PREFIX + script, |
| 154 | + ]); |
| 155 | + |
| 156 | + expect(resp.exitCode).toBe(0); |
| 157 | + expect(resp.stdout).not.toContain("Checksum verified"); |
| 158 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 159 | + }, 30000); |
| 160 | + |
| 161 | + it("falls back to sudo when install_dir is not writable", async () => { |
| 162 | + await using fixture = await setupFakeBinaryServer(import.meta.dir, { |
| 163 | + install_dir: "/usr/local/bin", |
| 164 | + }); |
| 165 | + await using container = await setupContainer("alpine/curl"); |
| 166 | + |
| 167 | + await execContainer(container.id, [ |
| 168 | + "sh", |
| 169 | + "-c", |
| 170 | + "apk add sudo && " + |
| 171 | + "adduser -D testuser && " + |
| 172 | + "echo 'testuser ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && " + |
| 173 | + "mkdir -p /usr/local/bin", |
| 174 | + ]); |
| 175 | + |
| 176 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 177 | + const resp = await execContainer( |
| 178 | + container.id, |
| 179 | + ["sh", "-c", ENV_PREFIX + script], |
| 180 | + ["--user", "testuser"], |
| 181 | + ); |
| 182 | + |
| 183 | + expect(resp.exitCode).toBe(0); |
| 184 | + expect(resp.stdout).toContain("via sudo"); |
| 185 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 186 | + |
| 187 | + // Verify the binary was copied to the install_dir. |
| 188 | + const check = await execContainer(container.id, [ |
| 189 | + "test", |
| 190 | + "-x", |
| 191 | + "/usr/local/bin/portabledesktop", |
| 192 | + ]); |
| 193 | + expect(check.exitCode).toBe(0); |
| 194 | + }, 30000); |
| 195 | + |
| 196 | + it("creates install_dir if it does not exist", async () => { |
| 197 | + await using fixture = await setupFakeBinaryServer(import.meta.dir, { |
| 198 | + install_dir: "/opt/custom/bin", |
| 199 | + }); |
| 200 | + await using container = await setupContainer("alpine/curl"); |
| 201 | + |
| 202 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 203 | + const resp = await execContainer(container.id, [ |
| 204 | + "sh", |
| 205 | + "-c", |
| 206 | + ENV_PREFIX + script, |
| 207 | + ]); |
| 208 | + |
| 209 | + expect(resp.exitCode).toBe(0); |
| 210 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 211 | + |
| 212 | + const check = await execContainer(container.id, [ |
| 213 | + "test", |
| 214 | + "-x", |
| 215 | + "/opt/custom/bin/portabledesktop", |
| 216 | + ]); |
| 217 | + expect(check.exitCode).toBe(0); |
| 218 | + }, 30000); |
| 219 | + |
| 220 | + it("falls back to wget when curl is not available", async () => { |
| 221 | + await using fixture = await setupFakeBinaryServer(import.meta.dir); |
| 222 | + await using container = await setupContainer("alpine"); |
| 223 | + |
| 224 | + // Install wget but ensure curl is not present. |
| 225 | + await execContainer(container.id, [ |
| 226 | + "sh", |
| 227 | + "-c", |
| 228 | + "apk add wget && ! command -v curl", |
| 229 | + ]); |
| 230 | + |
| 231 | + const script = findResourceInstance(fixture.state, "coder_script").script; |
| 232 | + const resp = await execContainer(container.id, [ |
| 233 | + "sh", |
| 234 | + "-c", |
| 235 | + ENV_PREFIX + script, |
| 236 | + ]); |
| 237 | + |
| 238 | + expect(resp.exitCode).toBe(0); |
| 239 | + expect(resp.stdout).toContain("via wget"); |
| 240 | + expect(resp.stdout).toContain("portabledesktop installed successfully"); |
| 241 | + }, 30000); |
| 242 | +}); |
0 commit comments