|
| 1 | +const assert = require("node:assert/strict"); |
| 2 | +const { spawnSync } = require("node:child_process"); |
| 3 | +const { mkdtempSync, readFileSync, rmSync, existsSync } = require("node:fs"); |
| 4 | +const { tmpdir } = require("node:os"); |
| 5 | +const { join } = require("node:path"); |
| 6 | +const { test } = require("node:test"); |
| 7 | + |
| 8 | +const workflow = readFileSync(join(__dirname, "../workflows/release.yml"), "utf8"); |
| 9 | +const step = workflow.match( |
| 10 | + /- name: Read production relay tracing config\n[\s\S]*? run: \|\n((?: .*\n|\n)+)/, |
| 11 | +); |
| 12 | +assert.ok(step, "Could not find the relay state workflow step"); |
| 13 | +const script = step[1].replace(/^ /gm, ""); |
| 14 | +const config = { |
| 15 | + clientTracingUrl: "https://example.invalid/traces", |
| 16 | + clientTracingDataset: "fixture-dataset", |
| 17 | + clientTracingToken: { __redacted__: "fixture-token" }, |
| 18 | +}; |
| 19 | +const json = JSON.stringify(config, null, 2); |
| 20 | + |
| 21 | +function runStep(stdout, exitCode = 0) { |
| 22 | + const runnerTemp = mkdtempSync(join(tmpdir(), "t3-relay-state-test-")); |
| 23 | + try { |
| 24 | + const result = spawnSync( |
| 25 | + "bash", |
| 26 | + ["-c", 'npx() { printf "%s\\n" "$FIXTURE_STDOUT"; return "$FIXTURE_EXIT"; }\n' + script], |
| 27 | + { |
| 28 | + encoding: "utf8", |
| 29 | + env: { |
| 30 | + PATH: process.env.PATH, |
| 31 | + RUNNER_TEMP: runnerTemp, |
| 32 | + FIXTURE_STDOUT: stdout, |
| 33 | + FIXTURE_EXIT: String(exitCode), |
| 34 | + }, |
| 35 | + }, |
| 36 | + ); |
| 37 | + assert.ifError(result.error); |
| 38 | + const envPath = join(runnerTemp, "relay-client-tracing.env"); |
| 39 | + return { |
| 40 | + ...result, |
| 41 | + envFile: existsSync(envPath) ? readFileSync(envPath, "utf8") : undefined, |
| 42 | + }; |
| 43 | + } finally { |
| 44 | + rmSync(runnerTemp, { recursive: true, force: true }); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +for (const prefix of [ |
| 49 | + "", |
| 50 | + "• Refreshing Cloudflare State Store credentials\n✓ Refreshing Cloudflare State Store credentials\n", |
| 51 | +]) { |
| 52 | + test(`extracts tracing config ${prefix ? "after progress output" : "from plain JSON"}`, () => { |
| 53 | + const result = runStep(prefix + json); |
| 54 | + assert.equal(result.status, 0, result.stderr); |
| 55 | + assert.equal(result.stdout, "::add-mask::fixture-token\n"); |
| 56 | + assert.equal( |
| 57 | + result.envFile, |
| 58 | + "T3CODE_RELAY_CLIENT_OTLP_TRACES_URL=https://example.invalid/traces\n" + |
| 59 | + "T3CODE_RELAY_CLIENT_OTLP_TRACES_DATASET=fixture-dataset\n" + |
| 60 | + "T3CODE_RELAY_CLIENT_OTLP_TRACES_TOKEN=fixture-token\n", |
| 61 | + ); |
| 62 | + }); |
| 63 | +} |
| 64 | + |
| 65 | +for (const [name, stdout, exitCode] of [ |
| 66 | + ["failed CLI even with valid JSON", json, 1], |
| 67 | + ["missing JSON", "Refreshing credentials...", 0], |
| 68 | + ["malformed JSON", "{not JSON", 0], |
| 69 | + ["missing token", JSON.stringify({ ...config, clientTracingToken: null }, null, 2), 0], |
| 70 | +]) { |
| 71 | + test(`rejects ${name} without writing config`, () => { |
| 72 | + const result = runStep(stdout, exitCode); |
| 73 | + assert.notEqual(result.status, 0); |
| 74 | + assert.equal(result.envFile, undefined); |
| 75 | + }); |
| 76 | +} |
0 commit comments