|
| 1 | +import * as fs from 'fs' |
| 2 | +import { nanoid } from 'nanoid' |
| 3 | +import { GenericContainer, Network, StartedTestContainer, Wait } from 'testcontainers' |
| 4 | +import { ExecResult } from 'testcontainers/dist/docker/types' |
| 5 | +import { ContentType } from 'allure2-js-commons' |
| 6 | +import { attach, log } from '../utils/allure' |
| 7 | + |
| 8 | +/** |
| 9 | + * A Relay contains a running relay container that has a unique ID and two promises: |
| 10 | + * one for the `deno cache` and one for the `deno run` the required function |
| 11 | + */ |
| 12 | +export class Relay { |
| 13 | + container: StartedTestContainer |
| 14 | + id: string |
| 15 | + execCache: Promise<ExecResult> |
| 16 | + execRun: Promise<ExecResult> |
| 17 | + constructor( |
| 18 | + container: StartedTestContainer, |
| 19 | + id: string, |
| 20 | + execCache: Promise<ExecResult>, |
| 21 | + execRun: Promise<ExecResult> |
| 22 | + ) { |
| 23 | + this.container = container |
| 24 | + this.id = id |
| 25 | + this.execCache = execCache |
| 26 | + this.execRun = execRun |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * It starts a docker container with a deno relay, and waits for it to be ready |
| 32 | + * @param {string} slug - the name of the function to deploy |
| 33 | + * @param {string} jwtSecret - the JWT secret to access function |
| 34 | + * @param {string} [denoOrigin=http://localhost:8000] - the origin of the deno server |
| 35 | + * @param {Map<string, string>} env - list of environment variables for deno relay |
| 36 | + * @returns {Promise<Relay>} A Relay object. |
| 37 | + */ |
| 38 | +export async function runRelay( |
| 39 | + slug: string, |
| 40 | + jwtSecret: string, |
| 41 | + denoOrigin: string = 'http://localhost:8000', |
| 42 | + env?: Map<string, string> |
| 43 | +): Promise<Relay> { |
| 44 | + // read function to deploy |
| 45 | + log('read function body') |
| 46 | + const functionBytes = fs.readFileSync('test/functions/' + slug + '.ts', 'utf8') |
| 47 | + attach('function body', functionBytes, ContentType.TEXT) |
| 48 | + |
| 49 | + // random id for parallel execution |
| 50 | + const id = nanoid(5) |
| 51 | + |
| 52 | + //create network |
| 53 | + log('add network') |
| 54 | + const network = await new Network({ name: 'supabase_network_' + id }).start() |
| 55 | + |
| 56 | + // create relay container |
| 57 | + log(`create relay ${slug + '-' + id}`) |
| 58 | + const relay = await new GenericContainer('supabase/deno-relay') |
| 59 | + .withName(slug + '-' + id) |
| 60 | + .withCmd([ |
| 61 | + 'sh', |
| 62 | + '-c', |
| 63 | + `cat <<'EOF' > /home/deno/${slug}.ts && deno run --allow-all index.ts |
| 64 | +` + |
| 65 | + functionBytes + |
| 66 | + ` |
| 67 | +EOF |
| 68 | +`, |
| 69 | + ]) |
| 70 | + .withNetworkMode(network.getName()) |
| 71 | + .withExposedPorts(8081) |
| 72 | + .withWaitStrategy(Wait.forLogMessage('Listening on http://0.0.0.0:8081')) |
| 73 | + .withStartupTimeout(15000) |
| 74 | + .withReuse() |
| 75 | + |
| 76 | + // add envs |
| 77 | + env = parseEnv(env, jwtSecret, denoOrigin) |
| 78 | + env && env.forEach((value, key) => relay.withEnv(key, value)) |
| 79 | + |
| 80 | + // start relay and function |
| 81 | + log(`start relay ${slug + '-' + id}`) |
| 82 | + const startedRelay = await relay.start() |
| 83 | + const execCache = startedRelay.exec(['deno', 'cache', `/home/deno/${slug}.ts`]) |
| 84 | + const execRun = startedRelay.exec(['deno', 'run', '--allow-all', `/home/deno/${slug}.ts`]) |
| 85 | + |
| 86 | + // wait till function is running |
| 87 | + log(`check function is healthy: ${slug + '-' + id}`) |
| 88 | + for (let ctr = 0; ctr < 30; ctr++) { |
| 89 | + const healthCheck = await startedRelay.exec(['nc', '-z', 'localhost', '8000']) |
| 90 | + if (healthCheck.exitCode == 0) { |
| 91 | + log(`function started to serve: ${slug + '-' + id}`) |
| 92 | + return { container: startedRelay, id, execCache, execRun } |
| 93 | + } |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 500)) |
| 95 | + } |
| 96 | + |
| 97 | + // if function hasn't started, stop container and throw |
| 98 | + log(`function failed to start: ${slug + '-' + id}`) |
| 99 | + startedRelay.stop() |
| 100 | + throw new Error("function haven'start correctly") |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * If the JWT_SECRET and DENO_ORIGIN environment is not set, set it |
| 105 | + * @param env - The environment variables. |
| 106 | + * @param {string} jwtSecret - The JWT secret. |
| 107 | + * @param {string} denoOrigin - The origin of the Deno server. |
| 108 | + * @returns {Map<string, string>} - `env` variables map. |
| 109 | + */ |
| 110 | +function parseEnv( |
| 111 | + env: Map<string, string> | undefined | null, |
| 112 | + jwtSecret: string, |
| 113 | + denoOrigin: string |
| 114 | +): Map<string, string> { |
| 115 | + if (env) { |
| 116 | + !env.has('JWT_SECRET') && jwtSecret && env.set('JWT_SECRET', jwtSecret) |
| 117 | + !env.has('DENO_ORIGIN') && denoOrigin && env.set('DENO_ORIGIN', denoOrigin) |
| 118 | + } else { |
| 119 | + env = new Map([ |
| 120 | + ['JWT_SECRET', jwtSecret], |
| 121 | + ['DENO_ORIGIN', denoOrigin], |
| 122 | + ]) |
| 123 | + } |
| 124 | + return env |
| 125 | +} |
0 commit comments