Skip to content

Commit

Permalink
test: cross stack dependencies can be removed from deployed stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Jan 10, 2022
1 parent 48f2972 commit f59e3f4
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 14 deletions.
4 changes: 4 additions & 0 deletions test/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export class TestDriver {
fse.writeFileSync(dest, content);
};

setEnv = (key, value) => {
this.env[key] = value;
};

stackDirectory = (stackName: string) => {
return path.join(this.workingDirectory, "cdktf.out", "stacks", stackName);
};
Expand Down
180 changes: 171 additions & 9 deletions test/typescript/cross-stack-references/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,160 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cross stack references deployed can remove references to deployed stacks: with dependency 1`] = `
"{
\\"version\\": \\"stubbed\\",
\\"stacks\\": {
\\"origin\\": {
\\"name\\": \\"origin\\",
\\"constructPath\\": \\"origin\\",
\\"workingDirectory\\": \\"stacks/origin\\",
\\"synthesizedStackPath\\": \\"stacks/origin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
\\"passthrough\\": {
\\"name\\": \\"passthrough\\",
\\"constructPath\\": \\"passthrough\\",
\\"workingDirectory\\": \\"stacks/passthrough\\",
\\"synthesizedStackPath\\": \\"stacks/passthrough/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"sink\\": {
\\"name\\": \\"sink\\",
\\"constructPath\\": \\"sink\\",
\\"workingDirectory\\": \\"stacks/sink\\",
\\"synthesizedStackPath\\": \\"stacks/sink/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"fns\\": {
\\"name\\": \\"fns\\",
\\"constructPath\\": \\"fns\\",
\\"workingDirectory\\": \\"stacks/fns\\",
\\"synthesizedStackPath\\": \\"stacks/fns/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"functionOutput\\": {
\\"name\\": \\"functionOutput\\",
\\"constructPath\\": \\"functionOutput\\",
\\"workingDirectory\\": \\"stacks/functionOutput\\",
\\"synthesizedStackPath\\": \\"stacks/functionOutput/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"secondOrigin\\": {
\\"name\\": \\"secondOrigin\\",
\\"constructPath\\": \\"secondOrigin\\",
\\"workingDirectory\\": \\"stacks/secondOrigin\\",
\\"synthesizedStackPath\\": \\"stacks/secondOrigin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
\\"switchedStack\\": {
\\"name\\": \\"switchedStack\\",
\\"constructPath\\": \\"switchedStack\\",
\\"workingDirectory\\": \\"stacks/switchedStack\\",
\\"synthesizedStackPath\\": \\"stacks/switchedStack/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"secondOrigin\\"
]
}
}
}"
`;

exports[`cross stack references deployed can remove references to deployed stacks: without dependency 1`] = `
"{
\\"version\\": \\"stubbed\\",
\\"stacks\\": {
\\"origin\\": {
\\"name\\": \\"origin\\",
\\"constructPath\\": \\"origin\\",
\\"workingDirectory\\": \\"stacks/origin\\",
\\"synthesizedStackPath\\": \\"stacks/origin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
\\"passthrough\\": {
\\"name\\": \\"passthrough\\",
\\"constructPath\\": \\"passthrough\\",
\\"workingDirectory\\": \\"stacks/passthrough\\",
\\"synthesizedStackPath\\": \\"stacks/passthrough/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"sink\\": {
\\"name\\": \\"sink\\",
\\"constructPath\\": \\"sink\\",
\\"workingDirectory\\": \\"stacks/sink\\",
\\"synthesizedStackPath\\": \\"stacks/sink/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"fns\\": {
\\"name\\": \\"fns\\",
\\"constructPath\\": \\"fns\\",
\\"workingDirectory\\": \\"stacks/fns\\",
\\"synthesizedStackPath\\": \\"stacks/fns/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"functionOutput\\": {
\\"name\\": \\"functionOutput\\",
\\"constructPath\\": \\"functionOutput\\",
\\"workingDirectory\\": \\"stacks/functionOutput\\",
\\"synthesizedStackPath\\": \\"stacks/functionOutput/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"origin\\"
]
},
\\"secondOrigin\\": {
\\"name\\": \\"secondOrigin\\",
\\"constructPath\\": \\"secondOrigin\\",
\\"workingDirectory\\": \\"stacks/secondOrigin\\",
\\"synthesizedStackPath\\": \\"stacks/secondOrigin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
\\"switchedStack\\": {
\\"name\\": \\"switchedStack\\",
\\"constructPath\\": \\"switchedStack\\",
\\"workingDirectory\\": \\"stacks/switchedStack\\",
\\"synthesizedStackPath\\": \\"stacks/switchedStack/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
}
}
}"
`;

exports[`cross stack references synth generates JSON 1`] = `
"{
\\"version\\": \\"stubbed\\",
\\"stacks\\": {
\\"source\\": {
\\"name\\": \\"source\\",
\\"constructPath\\": \\"source\\",
\\"workingDirectory\\": \\"stacks/source\\",
\\"synthesizedStackPath\\": \\"stacks/source/cdk.tf.json\\",
\\"origin\\": {
\\"name\\": \\"origin\\",
\\"constructPath\\": \\"origin\\",
\\"workingDirectory\\": \\"stacks/origin\\",
\\"synthesizedStackPath\\": \\"stacks/origin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
Expand All @@ -19,7 +165,7 @@ exports[`cross stack references synth generates JSON 1`] = `
\\"synthesizedStackPath\\": \\"stacks/passthrough/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"source\\"
\\"origin\\"
]
},
\\"sink\\": {
Expand All @@ -29,7 +175,7 @@ exports[`cross stack references synth generates JSON 1`] = `
\\"synthesizedStackPath\\": \\"stacks/sink/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"source\\"
\\"origin\\"
]
},
\\"fns\\": {
Expand All @@ -39,7 +185,7 @@ exports[`cross stack references synth generates JSON 1`] = `
\\"synthesizedStackPath\\": \\"stacks/fns/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"source\\"
\\"origin\\"
]
},
\\"functionOutput\\": {
Expand All @@ -49,8 +195,24 @@ exports[`cross stack references synth generates JSON 1`] = `
\\"synthesizedStackPath\\": \\"stacks/functionOutput/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": [
\\"source\\"
\\"origin\\"
]
},
\\"secondOrigin\\": {
\\"name\\": \\"secondOrigin\\",
\\"constructPath\\": \\"secondOrigin\\",
\\"workingDirectory\\": \\"stacks/secondOrigin\\",
\\"synthesizedStackPath\\": \\"stacks/secondOrigin/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
},
\\"switchedStack\\": {
\\"name\\": \\"switchedStack\\",
\\"constructPath\\": \\"switchedStack\\",
\\"workingDirectory\\": \\"stacks/switchedStack\\",
\\"synthesizedStackPath\\": \\"stacks/switchedStack/cdk.tf.json\\",
\\"annotations\\": [],
\\"dependencies\\": []
}
}
}"
Expand Down
17 changes: 13 additions & 4 deletions test/typescript/cross-stack-references/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export class SourceStack extends TerraformStack {
input: ["a", "b", "c", "d", "e", "f"],
});

writeToFile(this, "originNum", this.numericResource.result);
writeToFile(this, "originStr", this.stringResource.result);
writeToFile(this, "originList", this.listResource.result);
writeToFile(this, `${id}Num`, this.numericResource.result);
writeToFile(this, `${id}Str`, this.stringResource.result);
writeToFile(this, `${id}List`, this.listResource.result);
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ export class ConsumerStack extends TerraformStack {
}

const app = Testing.stubVersion(new App({ stackTraces: false }));
const src = new SourceStack(app, "source");
const src = new SourceStack(app, "origin");
const passthrough = new ConsumerStack(app, "passthrough", {
numericResource: src.numericResource,
stringResource: src.stringResource,
Expand Down Expand Up @@ -137,4 +137,13 @@ new ConsumerStack(app, "functionOutput", {
stringValue: fns.stringValue,
});

// Check for Deadly embrace scenario: https://github.com/aws/aws-cdk/pull/12778
const secondOrigin = new SourceStack(app, "secondOrigin");
new ConsumerStack(app, "switchedStack", {
numericResource:
process.env.SWITCH_STACK === "on"
? secondOrigin.numericResource
: undefined,
});

app.synth();
16 changes: 15 additions & 1 deletion test/typescript/cross-stack-references/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("cross stack references", () => {

describe("deployed", () => {
beforeAll(async () => {
await driver.deploy("source");
await driver.deploy("origin");
await driver.deploy("passthrough");
await driver.deploy("sink");
});
Expand Down Expand Up @@ -76,5 +76,19 @@ describe("cross stack references", () => {
expect(originNum * 2).toBe(result);
});
});

// Check for Deadly embrace scenario: https://github.com/aws/aws-cdk/pull/12778
it("can remove references to deployed stacks", async () => {
driver.setEnv("SWITCH_STACK", "on");
console.log(driver.workingDirectory);
await driver.deploy("secondOrigin");
await driver.deploy("switchedStack");
expect(driver.manifest()).toMatchSnapshot("with dependency");

driver.setEnv("SWITCH_STACK", undefined);
await driver.deploy("secondOrigin");
await driver.deploy("switchedStack");
expect(driver.manifest()).toMatchSnapshot("without dependency");
});
});
});

0 comments on commit f59e3f4

Please sign in to comment.