-
Notifications
You must be signed in to change notification settings - Fork 146
Retry zrok token setup after stale-environment cleanup #838
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
Draft
qayshp
wants to merge
3
commits into
BlueBubblesApp:development
Choose a base branch
from
qayshp:agent/recover-zrok-environment
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,141 @@ | ||
| const assert = require("node:assert/strict"); | ||
| const path = require("node:path"); | ||
| const test = require("node:test"); | ||
| const babel = require("@babel/core"); | ||
|
|
||
| const loadTypeScriptModule = (modulePath, overrides = {}) => { | ||
| const transformed = babel.transformFileSync(modulePath, { | ||
| presets: [ | ||
| [require.resolve("@babel/preset-env"), { targets: { node: "20" }, modules: "commonjs" }], | ||
| require.resolve("@babel/preset-typescript") | ||
| ] | ||
| }); | ||
| const loadedModule = { exports: {} }; | ||
| const loadModule = new Function("module", "exports", "require", transformed.code); | ||
| const customRequire = request => | ||
| Object.prototype.hasOwnProperty.call(overrides, request) ? overrides[request] : require(request); | ||
| loadModule(loadedModule, loadedModule.exports, customRequire); | ||
| return loadedModule.exports; | ||
| }; | ||
|
|
||
| const zrokManagerPath = path.join(__dirname, "../src/server/managers/zrokManager/index.ts"); | ||
| const token = "test-token"; | ||
| const alreadyEnabledError = { | ||
| output: "[ERROR]: unable to enable environment (you already have an enabled environment)" | ||
| }; | ||
|
|
||
| const loadZrokManager = responses => { | ||
| const calls = []; | ||
| const logs = { | ||
| debug: [], | ||
| error: [], | ||
| info: [] | ||
| }; | ||
| const executeCommand = async (...args) => { | ||
| calls.push(args); | ||
| const response = responses.shift(); | ||
| if (!response) throw new Error("Unexpected ProcessSpawner call"); | ||
| if ("error" in response) throw response.error; | ||
| return response.output; | ||
| }; | ||
| const logger = { | ||
| debug: (...args) => logs.debug.push(args), | ||
| error: (...args) => logs.error.push(args), | ||
| info: (...args) => logs.info.push(args) | ||
| }; | ||
|
|
||
| const { ZrokManager } = loadTypeScriptModule(zrokManagerPath, { | ||
| axios: { post: async () => undefined }, | ||
| child_process: { spawn: () => undefined }, | ||
| electron: { app: { getVersion: () => "test" } }, | ||
| "@server": { Server: () => undefined }, | ||
| "@server/fileSystem": { FileSystem: { resources: "/resources" } }, | ||
| "@server/helpers/utils": { | ||
| isEmpty: value => value == null || value.length === 0, | ||
| isNotEmpty: value => value != null && value.length > 0 | ||
| }, | ||
| "@server/lib/logging/Loggable": { | ||
| Loggable: class {}, | ||
| getLogger: () => logger | ||
| }, | ||
| "@server/lib/ProcessSpawner": { | ||
| ProcessSpawner: { executeCommand } | ||
| } | ||
| }); | ||
|
|
||
| return { calls, logs, ZrokManager }; | ||
| }; | ||
|
|
||
| const success = output => ({ output }); | ||
| const failure = output => ({ error: { output } }); | ||
| const expectedCall = (ZrokManager, args) => [ZrokManager.daemonPath, args, {}, "ZrokManager"]; | ||
|
|
||
| test("returns the output when zrok enables normally", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([success("environment enabled")]); | ||
|
|
||
| assert.equal(await ZrokManager.setToken(token), "environment enabled"); | ||
| assert.deepEqual(calls, [expectedCall(ZrokManager, ["enable", token])]); | ||
| }); | ||
|
|
||
| test("preserves invalid-token errors without disabling or retrying", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([failure("[ERROR]: enableUnauthorized")]); | ||
|
|
||
| await assert.rejects(() => ZrokManager.setToken(token), { message: "Invalid Zrok token!" }); | ||
| assert.deepEqual(calls, [expectedCall(ZrokManager, ["enable", token])]); | ||
| }); | ||
|
|
||
| test("preserves generic enable errors without disabling or retrying", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([failure("[ERROR]: network unavailable")]); | ||
|
|
||
| await assert.rejects(() => ZrokManager.setToken(token), { | ||
| message: "Failed to set Zrok token! Please check your server logs for more information." | ||
| }); | ||
| assert.deepEqual(calls, [expectedCall(ZrokManager, ["enable", token])]); | ||
| }); | ||
|
|
||
| test("disables a stale environment and retries enable exactly once", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([ | ||
| { error: alreadyEnabledError }, | ||
| success("environment disabled"), | ||
| success("environment enabled") | ||
| ]); | ||
|
|
||
| assert.equal(await ZrokManager.setToken(token), "environment enabled"); | ||
| assert.deepEqual(calls, [ | ||
| expectedCall(ZrokManager, ["enable", token]), | ||
| expectedCall(ZrokManager, ["disable"]), | ||
| expectedCall(ZrokManager, ["enable", token]) | ||
| ]); | ||
| }); | ||
|
|
||
| test("does not retry again when the second enable fails", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([ | ||
| { error: alreadyEnabledError }, | ||
| success("environment disabled"), | ||
| { error: alreadyEnabledError } | ||
| ]); | ||
|
|
||
| await assert.rejects(() => ZrokManager.setToken(token), { | ||
| message: "Failed to set Zrok token! Please check your server logs for more information." | ||
| }); | ||
| assert.deepEqual(calls, [ | ||
| expectedCall(ZrokManager, ["enable", token]), | ||
| expectedCall(ZrokManager, ["disable"]), | ||
| expectedCall(ZrokManager, ["enable", token]) | ||
| ]); | ||
| }); | ||
|
|
||
| test("preserves invalid-token errors from the retry", async () => { | ||
| const { calls, ZrokManager } = loadZrokManager([ | ||
| { error: alreadyEnabledError }, | ||
| success("environment disabled"), | ||
| failure("[ERROR]: enableUnauthorized") | ||
| ]); | ||
|
|
||
| await assert.rejects(() => ZrokManager.setToken(token), { message: "Invalid Zrok token!" }); | ||
| assert.deepEqual(calls, [ | ||
| expectedCall(ZrokManager, ["enable", token]), | ||
| expectedCall(ZrokManager, ["disable"]), | ||
| expectedCall(ZrokManager, ["enable", token]) | ||
| ]); | ||
| }); | ||
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.