-
Notifications
You must be signed in to change notification settings - Fork 85
feat: persist minimal deploy state to deployed-state.json #2105
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
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
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,152 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { existsSync } from "node:fs"; | ||
| import { mkdtemp, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { FsReadWriteJson } from "../../../../io"; | ||
| import { createSilentLogger } from "../../../../testing"; | ||
| import { | ||
| DEPLOYED_STATE_RELATIVE_PATH, | ||
| readDeployedState, | ||
| updateTargetState, | ||
| } from "./deployedState"; | ||
|
|
||
| const json = new FsReadWriteJson({ logger: createSilentLogger() }); | ||
|
|
||
| const tempDirectories: string[] = []; | ||
| afterEach(async () => { | ||
| await Promise.all( | ||
| tempDirectories.splice(0).map((directory) => rm(directory, { recursive: true, force: true })), | ||
| ); | ||
| }); | ||
|
|
||
| async function projectRoot(): Promise<string> { | ||
| const root = await mkdtemp(join(tmpdir(), "agentcore-deployed-state-")); | ||
| tempDirectories.push(root); | ||
| return root; | ||
| } | ||
|
|
||
| function statePath(root: string): string { | ||
| return join(root, DEPLOYED_STATE_RELATIVE_PATH); | ||
| } | ||
|
|
||
| async function readRaw(root: string): Promise<unknown> { | ||
| return JSON.parse(await Bun.file(statePath(root)).text()); | ||
| } | ||
|
|
||
| describe("readDeployedState", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a way to test this behavior through the handlers? The advantage being that implementation details here could change, but we still observe the same behavior e2e. If that isn't really possible, than maybe one level higher in cdk? |
||
| test("returns an empty state when the file does not exist", async () => { | ||
| const root = await projectRoot(); | ||
|
|
||
| expect(await readDeployedState(json, root)).toEqual({ targets: {} }); | ||
| expect(existsSync(statePath(root))).toBe(false); | ||
| }); | ||
|
|
||
| test("round-trips a previously written state", async () => { | ||
| const root = await projectRoot(); | ||
|
|
||
| await updateTargetState(json, root, "default", { stackArn: "arn:stack:default" }); | ||
|
|
||
| expect(await readDeployedState(json, root)).toEqual({ | ||
| targets: { default: { stackArn: "arn:stack:default" } }, | ||
| }); | ||
| }); | ||
|
|
||
| test("resolves the file under agentcore/.cli/", () => { | ||
| expect(DEPLOYED_STATE_RELATIVE_PATH).toBe(join("agentcore", ".cli", "deployed-state.json")); | ||
| }); | ||
| }); | ||
|
|
||
| describe("updateTargetState", () => { | ||
| test("creates the file with the target entry", async () => { | ||
| const root = await projectRoot(); | ||
|
|
||
| await updateTargetState(json, root, "default", { stackArn: "arn:stack:default" }); | ||
|
|
||
| expect(await readRaw(root)).toEqual({ | ||
| targets: { default: { stackArn: "arn:stack:default" } }, | ||
| }); | ||
| }); | ||
|
|
||
| test("preserves other targets", async () => { | ||
| const root = await projectRoot(); | ||
|
|
||
| await updateTargetState(json, root, "default", { stackArn: "arn:stack:default" }); | ||
| await updateTargetState(json, root, "prod", { stackArn: "arn:stack:prod" }); | ||
|
|
||
| expect(await readRaw(root)).toEqual({ | ||
| targets: { | ||
| default: { stackArn: "arn:stack:default" }, | ||
| prod: { stackArn: "arn:stack:prod" }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("merges resources without dropping the stack ARN", async () => { | ||
| const root = await projectRoot(); | ||
| const credentials = { "openai-key": { credentialProviderArn: "arn:apikey:openai-key" } }; | ||
|
|
||
| await updateTargetState(json, root, "default", { stackArn: "arn:stack:default" }); | ||
| await updateTargetState(json, root, "default", { resources: { credentials } }); | ||
|
|
||
| expect(await readRaw(root)).toEqual({ | ||
| targets: { default: { stackArn: "arn:stack:default", resources: { credentials } } }, | ||
| }); | ||
| }); | ||
|
|
||
| test("replaces a resource kind's map wholesale but keeps other kinds", async () => { | ||
| const root = await projectRoot(); | ||
|
|
||
| await updateTargetState(json, root, "default", { | ||
| resources: { | ||
| credentials: { old: { credentialProviderArn: "arn:apikey:old" } }, | ||
| // A resource kind the CLI does not own must survive the merge. | ||
| runtimes: { main: { runtimeArn: "arn:runtime:main" } }, | ||
| }, | ||
| }); | ||
| await updateTargetState(json, root, "default", { | ||
| resources: { credentials: { fresh: { credentialProviderArn: "arn:apikey:fresh" } } }, | ||
| }); | ||
|
|
||
| expect(await readRaw(root)).toEqual({ | ||
| targets: { | ||
| default: { | ||
| resources: { | ||
| credentials: { fresh: { credentialProviderArn: "arn:apikey:fresh" } }, | ||
| runtimes: { main: { runtimeArn: "arn:runtime:main" } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("preserves unknown fields inside a credential entry across an update", async () => { | ||
| const root = await projectRoot(); | ||
| // A field a newer CLI (or the CDK app) records that this code doesn't model. | ||
| await Bun.write( | ||
| statePath(root), | ||
| JSON.stringify({ | ||
| targets: { | ||
| default: { | ||
| resources: { | ||
| credentials: { k: { credentialProviderArn: "arn:a", futureField: "keep" } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| await updateTargetState(json, root, "default", { stackArn: "arn:stack:default" }); | ||
|
|
||
| expect(await readRaw(root)).toEqual({ | ||
| targets: { | ||
| default: { | ||
| stackArn: "arn:stack:default", | ||
| resources: { | ||
| credentials: { k: { credentialProviderArn: "arn:a", futureField: "keep" } }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.