-
Notifications
You must be signed in to change notification settings - Fork 1
Support fuzz configuration via env variables #952
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import assert from "node:assert"; | ||
| import { describe, it } from "node:test"; | ||
| import { KnownChainSpec, NODE_DEFAULTS } from "@typeberry/config-node"; | ||
| import { Level } from "@typeberry/logger"; | ||
| import { Command } from "./args.js"; | ||
| import { | ||
| JAM_FUZZ, | ||
| JAM_FUZZ_DATA_PATH, | ||
| JAM_FUZZ_LOG_LEVEL, | ||
| JAM_FUZZ_SOCK_PATH, | ||
| JAM_FUZZ_SPEC, | ||
| readFuzzEnv, | ||
| synthesizeFuzzArgs, | ||
| } from "./fuzz-env.js"; | ||
|
|
||
| describe("readFuzzEnv", () => { | ||
| it("returns null when JAM_FUZZ is unset", () => { | ||
| assert.strictEqual(readFuzzEnv({}), null); | ||
| }); | ||
|
|
||
| it("returns null when JAM_FUZZ is empty string", () => { | ||
| assert.strictEqual(readFuzzEnv({ [JAM_FUZZ]: "" }), null); | ||
| }); | ||
|
|
||
| it("parses tiny spec happy path", () => { | ||
| const result = readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/jam.sock", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/jam-data", | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(result, { | ||
| spec: KnownChainSpec.Tiny, | ||
| socketPath: "/tmp/jam.sock", | ||
| dataPath: "/tmp/jam-data", | ||
| logLevel: null, | ||
| }); | ||
| }); | ||
|
|
||
| it("parses full spec happy path", () => { | ||
| const result = readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "full", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/jam.sock", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/jam-data", | ||
| }); | ||
|
|
||
| assert.strictEqual(result?.spec, KnownChainSpec.Full); | ||
| }); | ||
|
|
||
| it("rejects missing JAM_FUZZ_SPEC", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_SPEC} is required`), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects missing JAM_FUZZ_SOCK_PATH", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_SOCK_PATH} is required`), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects missing JAM_FUZZ_DATA_PATH", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_DATA_PATH} is required`), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects empty JAM_FUZZ_SOCK_PATH", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_SOCK_PATH} is required`), | ||
| ); | ||
| }); | ||
|
|
||
| it("rejects bogus JAM_FUZZ_SPEC", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "huge", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_SPEC} must be one of: tiny, full`), | ||
| ); | ||
| }); | ||
|
|
||
| it("parses JAM_FUZZ_LOG_LEVEL=debug as Level.LOG", () => { | ||
| const result = readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| [JAM_FUZZ_LOG_LEVEL]: "debug", | ||
| }); | ||
| assert.strictEqual(result?.logLevel, Level.LOG); | ||
| }); | ||
|
|
||
| it("parses JAM_FUZZ_LOG_LEVEL=TRACE case-insensitively", () => { | ||
| const result = readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| [JAM_FUZZ_LOG_LEVEL]: "TRACE", | ||
| }); | ||
| assert.strictEqual(result?.logLevel, Level.TRACE); | ||
| }); | ||
|
|
||
| it("parses each documented log level", () => { | ||
| const cases: [string, Level][] = [ | ||
| ["error", Level.ERROR], | ||
| ["warn", Level.WARN], | ||
| ["info", Level.INFO], | ||
| ["debug", Level.LOG], | ||
| ["trace", Level.TRACE], | ||
| ]; | ||
| for (const [raw, expected] of cases) { | ||
| const result = readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| [JAM_FUZZ_LOG_LEVEL]: raw, | ||
| }); | ||
| assert.strictEqual(result?.logLevel, expected, `level for '${raw}'`); | ||
| } | ||
| }); | ||
|
|
||
| it("rejects bogus JAM_FUZZ_LOG_LEVEL", () => { | ||
| assert.throws( | ||
| () => | ||
| readFuzzEnv({ | ||
| [JAM_FUZZ]: "1", | ||
| [JAM_FUZZ_SPEC]: "tiny", | ||
| [JAM_FUZZ_SOCK_PATH]: "/tmp/s", | ||
| [JAM_FUZZ_DATA_PATH]: "/tmp/d", | ||
| [JAM_FUZZ_LOG_LEVEL]: "BOGUS", | ||
| }), | ||
| new RegExp(`${JAM_FUZZ_LOG_LEVEL} must be one of: error, warn, info, debug, trace`), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("synthesizeFuzzArgs", () => { | ||
| it("builds a FuzzTarget Arguments value with tiny flavor override", () => { | ||
| const args = synthesizeFuzzArgs({ | ||
| spec: KnownChainSpec.Tiny, | ||
| socketPath: "/tmp/jam.sock", | ||
| dataPath: "/tmp/jam-data", | ||
| logLevel: null, | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(args, { | ||
| command: Command.FuzzTarget, | ||
| args: { | ||
| nodeName: NODE_DEFAULTS.name, | ||
| config: [...NODE_DEFAULTS.config, '.flavor="tiny"'], | ||
| pvm: NODE_DEFAULTS.pvm, | ||
| socket: "/tmp/jam.sock", | ||
| version: 1, | ||
| initGenesisFromAncestry: false, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("uses 'full' flavor when spec is full", () => { | ||
| const args = synthesizeFuzzArgs({ | ||
| spec: KnownChainSpec.Full, | ||
| socketPath: "/tmp/jam.sock", | ||
| dataPath: "/tmp/jam-data", | ||
| logLevel: null, | ||
| }); | ||
| if (args.command !== Command.FuzzTarget) { | ||
| throw new Error("expected FuzzTarget command"); | ||
| } | ||
| assert.deepStrictEqual(args.args.config, [...NODE_DEFAULTS.config, '.flavor="full"']); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { KnownChainSpec, NODE_DEFAULTS } from "@typeberry/config-node"; | ||
| import { Level } from "@typeberry/logger"; | ||
| import { type Arguments, Command } from "./args.js"; | ||
|
|
||
| export type FuzzEnv = { | ||
| spec: KnownChainSpec; | ||
| socketPath: string; | ||
| dataPath: string; | ||
| logLevel: Level | null; | ||
| }; | ||
|
|
||
| export const JAM_FUZZ = "JAM_FUZZ"; | ||
| export const JAM_FUZZ_SPEC = "JAM_FUZZ_SPEC"; | ||
| export const JAM_FUZZ_SOCK_PATH = "JAM_FUZZ_SOCK_PATH"; | ||
| export const JAM_FUZZ_DATA_PATH = "JAM_FUZZ_DATA_PATH"; | ||
| export const JAM_FUZZ_LOG_LEVEL = "JAM_FUZZ_LOG_LEVEL"; | ||
|
|
||
| const REQUIRED_VARS = [JAM_FUZZ_SPEC, JAM_FUZZ_SOCK_PATH, JAM_FUZZ_DATA_PATH] as const; | ||
|
|
||
| // Note the JAM-conformance vocabulary uses "debug" but the typeberry Level | ||
| // enum names the same level "LOG" (see packages/core/logger/options.ts). | ||
| const LOG_LEVELS: Record<string, Level> = { | ||
| error: Level.ERROR, | ||
| warn: Level.WARN, | ||
| info: Level.INFO, | ||
| debug: Level.LOG, | ||
| trace: Level.TRACE, | ||
| }; | ||
|
|
||
| export function readFuzzEnv(env: NodeJS.ProcessEnv | Record<string, string | undefined>): FuzzEnv | null { | ||
| const flag = env[JAM_FUZZ] ?? ""; | ||
| if (flag.trim().length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| for (const name of REQUIRED_VARS) { | ||
| const value = env[name] ?? ""; | ||
| if (value.trim().length === 0) { | ||
| throw new Error(`${JAM_FUZZ} is set but ${name} is required.`); | ||
| } | ||
| } | ||
|
|
||
| const specRaw = env[JAM_FUZZ_SPEC] ?? ""; | ||
| let spec: KnownChainSpec; | ||
| if (specRaw === KnownChainSpec.Tiny) { | ||
| spec = KnownChainSpec.Tiny; | ||
| } else if (specRaw === KnownChainSpec.Full) { | ||
| spec = KnownChainSpec.Full; | ||
| } else { | ||
| throw new Error( | ||
| `${JAM_FUZZ_SPEC} must be one of: ${KnownChainSpec.Tiny}, ${KnownChainSpec.Full}. Got: '${specRaw}'.`, | ||
| ); | ||
| } | ||
|
|
||
| let logLevel: Level | null = null; | ||
| const rawLogLevel = env[JAM_FUZZ_LOG_LEVEL] ?? ""; | ||
| if (rawLogLevel !== "") { | ||
| const parsed = LOG_LEVELS[rawLogLevel.toLowerCase()]; | ||
| if (parsed === undefined) { | ||
| throw new Error( | ||
| `${JAM_FUZZ_LOG_LEVEL} must be one of: ${Object.keys(LOG_LEVELS).join(", ")}. Got: '${rawLogLevel}'.`, | ||
| ); | ||
| } | ||
| logLevel = parsed; | ||
| } | ||
|
|
||
| return { | ||
| spec, | ||
| socketPath: env[JAM_FUZZ_SOCK_PATH] ?? "", | ||
| dataPath: env[JAM_FUZZ_DATA_PATH] ?? "", | ||
| logLevel, | ||
| }; | ||
| } | ||
|
|
||
| export function synthesizeFuzzArgs(env: FuzzEnv): Arguments { | ||
| return { | ||
| command: Command.FuzzTarget, | ||
| args: { | ||
| nodeName: NODE_DEFAULTS.name, | ||
| config: [...NODE_DEFAULTS.config, `.flavor="${env.spec}"`], | ||
| pvm: NODE_DEFAULTS.pvm, | ||
| socket: env.socketPath, | ||
| version: 1, | ||
| initGenesisFromAncestry: false, | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.