Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/core/src/cli/handlers/lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,63 @@ describe("runLifecycleSnapshot", () => {
expect(takeSnapshotMock).toHaveBeenCalledTimes(1);
});

// #1261 — each stack declares the region it deploys to. Dropping it here
// observed every stack against the ambient region, so a multi-region estate
// snapshotted only the stacks that shared it and reported the rest as
// "no valid resources or artifacts returned".
test("multi-stack: each stack's declared region reaches takeSnapshot", async () => {
buildMock.mockResolvedValue(makeBuildResult({ aws: ["bucket"] }));
takeSnapshotMock.mockResolvedValue({
snapshots: [{ lexicon: "aws", environment: "prod", resources: { bucket: meta() } }],
commit: "sha",
warnings: [],
errors: [],
});
loadChantConfigMock.mockResolvedValue({ config: { environments: ["prod"], stacks: [
{ name: "app-us-east-1", src: "src/us-east-1", region: "us-east-1" },
{ name: "app-us-west-2", src: "src/us-west-2", region: "us-west-2" },
] } });
const plugins: LexiconPlugin[] = [
createMockPlugin({ name: "aws", describeResources: staticDescribeResources({ bucket: meta() }) }),
];

const exit = await runLifecycleSnapshot({
args: makeArgs({ command: "state", path: "snapshot", extraPositional: "prod" }),
plugins,
serializers: plugins.map((p) => p.serializer),
});

expect(exit).toBe(0);
expect(takeSnapshotMock.mock.calls.map((c) => c[3])).toEqual([
{ stack: "app-us-east-1", region: "us-east-1" },
{ stack: "app-us-west-2", region: "us-west-2" },
]);
});

test("stack without a declared region: region stays undefined", async () => {
buildMock.mockResolvedValue(makeBuildResult({ aws: ["bucket"] }));
takeSnapshotMock.mockResolvedValue({
snapshots: [{ lexicon: "aws", environment: "prod", resources: { bucket: meta() } }],
commit: "sha",
warnings: [],
errors: [],
});
loadChantConfigMock.mockResolvedValue({ config: { environments: ["prod"], stacks: [
{ name: "app", src: "src/app" },
] } });
const plugins: LexiconPlugin[] = [
createMockPlugin({ name: "aws", describeResources: staticDescribeResources({ bucket: meta() }) }),
];

await runLifecycleSnapshot({
args: makeArgs({ command: "state", path: "snapshot", extraPositional: "prod" }),
plugins,
serializers: plugins.map((p) => p.serializer),
});

expect(takeSnapshotMock.mock.calls[0][3]).toEqual({ stack: "app", region: undefined });
});

// #1166 — a snapshot is always a live read, so a declared environment
// endpoint applies here too, unless the ambient shell already set it.
describe("declared endpoint (#1166)", () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/cli/handlers/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ interface StackTarget {
/** Build root to synthesize this stack from, scoped so its logical ids match
* what the stack actually deploys. */
root: string;
/** Region the stack is deployed in, from `stacks[].region` (#1261). Without
* it every stack is observed against the ambient region, so a multi-region
* estate snapshots only the stacks that happen to share it and reports the
* rest as "no valid resources or artifacts returned". */
region?: string;
}

/**
Expand All @@ -75,7 +80,7 @@ interface StackTarget {
function resolveStackTargets(args: ParsedArgs, config: ChantConfig): StackTarget[] {
if (args.src) return [{ root: resolve(args.src) }];
if (config.stacks && config.stacks.length > 0) {
return config.stacks.map((s) => ({ stack: s.name, root: resolve(s.src) }));
return config.stacks.map((s) => ({ stack: s.name, root: resolve(s.src), region: s.region }));
}
return [{ root: resolveBuildRoot(args, config) }];
}
Expand Down Expand Up @@ -145,7 +150,10 @@ export async function runLifecycleSnapshot(ctx: CommandContext): Promise<number>

let result;
try {
result = await takeSnapshot(environment, observingPlugins, buildResult, { stack: target.stack });
result = await takeSnapshot(environment, observingPlugins, buildResult, {
stack: target.stack,
region: target.region,
});
} catch (err) {
if (err instanceof StaleLifecycleBranchError) {
console.error(formatError({
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/lifecycle/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,37 @@ describe("takeSnapshot", () => {
expect(writeSnapshotMock.mock.calls[0][1]).toBe("loom-backend__aws");
});

test("region option: the stack's own region reaches describeResources (#1261)", async () => {
let observedRegion: string | undefined = "unset";
const plugin = createMockPlugin({
name: "aws",
describeResources: async (options: { region?: string }) => {
observedRegion = options.region;
return { bucket: { type: "AWS::S3::Bucket", status: "CREATE_COMPLETE", physicalId: "b" } };
},
});
await takeSnapshot("prod", [plugin], makeBuildResult({ aws: ["bucket"] }), {
stack: "loom-us-west-2",
region: "us-west-2",
});
// Without this the reader falls back to the ambient region, and every stack
// outside it snapshots as "no valid resources or artifacts returned".
expect(observedRegion).toBe("us-west-2");
});

test("no region declared: describeResources keeps its ambient-region default", async () => {
let observedRegion: string | undefined = "unset";
const plugin = createMockPlugin({
name: "aws",
describeResources: async (options: { region?: string }) => {
observedRegion = options.region;
return { bucket: { type: "AWS::S3::Bucket", status: "CREATE_COMPLETE", physicalId: "b" } };
},
});
await takeSnapshot("prod", [plugin], makeBuildResult({ aws: ["bucket"] }));
expect(observedRegion).toBeUndefined();
});

test("plugin without describeResources is skipped", async () => {
const plugin = createMockPlugin({ name: "aws" });
const result = await takeSnapshot("prod", [plugin], makeBuildResult({ aws: ["x"] }));
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/lifecycle/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ export async function takeSnapshot(
environment: string,
plugins: ObservationLexicon[],
buildResult: BuildResult,
opts?: { cwd?: string; stack?: string },
opts?: { cwd?: string; stack?: string; region?: string },
): Promise<TakeSnapshotResult> {
const stack = opts?.stack;
// A stack declares the region it deploys to (#1261). Passing it through is
// what lets a multi-region estate be observed at all: the reader targets the
// stack's own region rather than whichever one the shell happens to be set
// to, so out-of-region stacks stop coming back empty.
const region = opts?.region;
const warnings: string[] = [];
const errors: string[] = [];
const snapshots: LifecycleSnapshot[] = [];
Expand Down Expand Up @@ -131,6 +136,7 @@ export async function takeSnapshot(
entityNames,
entities,
stack,
region,
}),
);
const { valid, dropped, warnings: validationWarnings } = validateResources(observed.resources);
Expand All @@ -149,6 +155,8 @@ export async function takeSnapshot(
}

if (plugin.listArtifacts) {
// No region: artifacts are registry/chart objects (docker, helm), not
// regional cloud resources, and `listArtifacts` takes no region.
const raw = await plugin.listArtifacts({ environment, entities, stack });
const { valid, dropped, warnings: validationWarnings } = validateResources(raw);
warnings.push(...validationWarnings);
Expand Down