Skip to content

Commit

Permalink
Merge pull request #1235 from hashicorp/improve-provider-generation-t…
Browse files Browse the repository at this point in the history
…ests

test(cli): test generated providers
  • Loading branch information
DanielMSchmidt authored Nov 4, 2021
2 parents 3058eb7 + 5fab300 commit 258d373
Show file tree
Hide file tree
Showing 21 changed files with 489 additions and 84 deletions.
4 changes: 3 additions & 1 deletion test/csharp/synth-app/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("csharp full integration test synth", () => {

test("synth generates JSON", async () => {
await driver.synth();
expect(driver.synthesizedStack("csharp-simple")).toMatchSnapshot();
expect(
driver.synthesizedStack("csharp-simple").toString()
).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion test/go/synth-app/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ describe("Go full integration test synth", () => {

test("synth generates JSON", async () => {
await driver.synth();
expect(driver.synthesizedStack("go-simple")).toMatchSnapshot();
expect(driver.synthesizedStack("go-simple").toString()).toMatchSnapshot();
});
});
2 changes: 1 addition & 1 deletion test/java/synth-app/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ describe("java full integration", () => {

test("synth generates JSON", async () => {
await driver.synth();
expect(driver.synthesizedStack("java-simple")).toMatchSnapshot();
expect(driver.synthesizedStack("java-simple").toString()).toMatchSnapshot();
});
});
11 changes: 7 additions & 4 deletions test/python/asset/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ describe("python full integration test assets", () => {

test("synth generates JSON and copies files", async () => {
await driver.synth();
expect(driver.synthesizedStack("python-assets")).toMatchSnapshot();
const stack = JSON.parse(driver.synthesizedStack("python-assets"));
expect(
driver.synthesizedStack("python-assets").toString()
).toMatchSnapshot();
const stack = driver.synthesizedStack("python-assets");

expect(
fs.readFileSync(
path.resolve(
driver.stackDirectory("python-assets"),
stack.output.fixtureoutput.value
stack.output("fixtureoutput")
),
"utf-8"
)
).toMatchSnapshot();

const stat = fs.statSync(
path.resolve(
driver.stackDirectory("python-assets"),
stack.output.fixturesoutput.value
stack.output("fixtureoutput")
)
);
expect(stat.isFile()).toBe(true);
Expand Down
8 changes: 6 additions & 2 deletions test/python/multiple-stacks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ describe("python full integration test synth", () => {

test("synth generates JSON for both stacks", async () => {
await driver.synth();
expect(driver.synthesizedStack("python-simple-one")).toMatchSnapshot();
expect(driver.synthesizedStack("python-simple-two")).toMatchSnapshot();
expect(
driver.synthesizedStack("python-simple-one").toString()
).toMatchSnapshot();
expect(
driver.synthesizedStack("python-simple-two").toString()
).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ exports[`python full integration 3rd party synth generates JSON 1`] = `
\\"//\\": {
\\"metadata\\": {
\\"version\\": \\"stubbed\\",
\\"stackName\\": \\"python-third-party-provider\\",
\\"stackName\\": \\"references\\",
\\"backend\\": \\"local\\"
}
},
\\"terraform\\": {
\\"required_providers\\": {
\\"docker\\": {
\\"version\\": \\"~> 2.0\\",
\\"source\\": \\"terraform-providers/docker\\"
\\"version\\": \\"~> 2.15\\",
\\"source\\": \\"kreuzwerker/docker\\"
}
}
},
Expand All @@ -24,22 +24,22 @@ exports[`python full integration 3rd party synth generates JSON 1`] = `
},
\\"resource\\": {
\\"docker_image\\": {
\\"nginxlatest\\": {
\\"keep_locally\\": false,
\\"nginxImage\\": {
\\"keep_locally\\": true,
\\"name\\": \\"nginx:latest\\",
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"python-third-party-provider/nginx-latest\\",
\\"uniqueId\\": \\"nginxlatest\\"
\\"path\\": \\"references/nginxImage\\",
\\"uniqueId\\": \\"nginxImage\\"
}
}
}
},
\\"docker_container\\": {
\\"nginxcdktf\\": {
\\"image\\": \\"\${docker_image.nginxlatest.name}\\",
\\"nginxContainer\\": {
\\"image\\": \\"\${docker_image.nginxImage.repo_digest}\\",
\\"name\\": \\"nginx-python-cdktf\\",
\\"privileged\\": false,
\\"privileged\\": \\"\${docker_image.nginxImage.keep_locally}\\",
\\"ports\\": [
{
\\"external\\": 8000,
Expand All @@ -48,12 +48,17 @@ exports[`python full integration 3rd party synth generates JSON 1`] = `
],
\\"//\\": {
\\"metadata\\": {
\\"path\\": \\"python-third-party-provider/nginx-cdktf\\",
\\"uniqueId\\": \\"nginxcdktf\\"
\\"path\\": \\"references/nginxContainer\\",
\\"uniqueId\\": \\"nginxContainer\\"
}
}
}
}
},
\\"output\\": {
\\"containerCapAdd\\": {
\\"value\\": \\"\${docker_container.nginxContainer.capabilities[0].add}\\"
}
}
}"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"language": "python",
"app": "pipenv run python main.py",
"terraformProviders": [
"terraform-providers/docker@~> 2.0"
"kreuzwerker/docker@~> 2.15"
],
"codeMakerOutput": "imports",
"context": {
Expand Down
35 changes: 35 additions & 0 deletions test/python/providers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python3 -tt
from constructs import Construct
from cdktf import App, TerraformStack, Testing, TerraformOutput
from imports.docker import Image, Container, DockerProvider

class References(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)

DockerProvider(self, "provider")

docker_image = Image(self, 'nginxImage', name='nginx:latest', keep_locally=False)

# Simple References
container = Container(self, 'nginxContainer', name='nginx-python-cdktf',
image=docker_image.repo_digest, ports=[
{
'internal': 80,
'external': 8000
}], privileged=False)
# Single-item References
TerraformOutput(self, "containerCapAdd", value=container.capabilities.add)

# Direct Mutation
docker_image.keep_locally = True

# Reference Mutation
container.privileged = docker_image.keep_locally



app = Testing.stub_version(App(stack_traces=False))
References(app, "references")

app.synth()
43 changes: 43 additions & 0 deletions test/python/providers/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { TestDriver } from "../../test-helper";

describe("python full integration 3rd party", () => {
let driver: TestDriver;

beforeAll(async () => {
driver = new TestDriver(__dirname);
await driver.setupPythonProject();
await driver.synth();
});

test("synth generates JSON", async () => {
expect(driver.synthesizedStack("references").toString()).toMatchSnapshot();
});

describe("references", () => {
test("simple references", () => {
expect(
driver.synthesizedStack("references").byId("nginxContainer").image
).toContain("nginxImage.repo_digest");
});

test("single-item references", () => {
expect(
driver.synthesizedStack("references").output("containerCapAdd")
).toContain("nginxContainer.capabilities[0].add");
});
});

describe("mutation", () => {
test("direct mutation", () => {
expect(
driver.synthesizedStack("references").byId("nginxImage").keep_locally
).toBeTruthy();
});

test("reference mutation", () => {
expect(
driver.synthesizedStack("references").byId("nginxContainer").privileged
).toContain("nginxImage.keep_locally");
});
});
});
4 changes: 3 additions & 1 deletion test/python/synth-app/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("python full integration test synth", () => {

test("synth generates JSON", async () => {
await driver.synth();
expect(driver.synthesizedStack("python-simple")).toMatchSnapshot();
expect(
driver.synthesizedStack("python-simple").toString()
).toMatchSnapshot();
});
});
4 changes: 3 additions & 1 deletion test/python/terraform-functions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("python terraform functions test synth", () => {

test("synth generates JSON", async () => {
await driver.synth();
expect(driver.synthesizedStack("python-simple")).toMatchSnapshot();
expect(
driver.synthesizedStack("python-simple").toString()
).toMatchSnapshot();
});
});
25 changes: 0 additions & 25 deletions test/python/third-party-provider/main.py

This file was deleted.

17 changes: 0 additions & 17 deletions test/python/third-party-provider/test.ts

This file was deleted.

41 changes: 38 additions & 3 deletions test/test-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ const path = require("path");
const fs = require("fs");
const fse = require("fs-extra");

class QueryableStack {
private readonly stack: Record<string, any>;
constructor(stackInput: string) {
this.stack = JSON.parse(stackInput);
}

/**
* Returns the construct with the given ID in the stack, no matter if
* it's a data source or resource and which type it has
*/
public byId(id: string): Record<string, any> {
const constructs = (
[
...Object.values(this.stack.resource || {}),
...Object.values(this.stack.data || {}),
] as Record<string, any>[]
).reduce(
(carry, item) => ({ ...carry, ...item }),
{} as Record<string, any>
);

return constructs[id];
}

public output(id: string): string {
return this.stack.output[id].value;
}

public toString(): string {
return JSON.stringify(this.stack, null, 2);
}
}

export class TestDriver {
public env: Record<string, string>;
public workingDirectory: string;
Expand Down Expand Up @@ -91,9 +124,11 @@ export class TestDriver {
};

synthesizedStack = (stackName: string) => {
return fs.readFileSync(
path.join(this.stackDirectory(stackName), "cdk.tf.json"),
"utf-8"
return new QueryableStack(
fs.readFileSync(
path.join(this.stackDirectory(stackName), "cdk.tf.json"),
"utf-8"
)
);
};

Expand Down
2 changes: 1 addition & 1 deletion test/typescript/asset/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("full integration test", () => {

test("synth", async () => {
await driver.synth("fixed");
expect(driver.synthesizedStack("fixed")).toMatchSnapshot();
expect(driver.synthesizedStack("fixed").toString()).toMatchSnapshot();
});

test("file asset copied", async () => {
Expand Down
8 changes: 6 additions & 2 deletions test/typescript/modules/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ describe("full integration test", () => {

onPosix("build modules posix", async () => {
await driver.synth();
expect(driver.synthesizedStack("hello-modules")).toMatchSnapshot();
expect(
driver.synthesizedStack("hello-modules").toString()
).toMatchSnapshot();
});

onWindows("build modules windows", async () => {
await driver.synth();
expect(driver.synthesizedStack("hello-modules")).toMatchSnapshot();
expect(
driver.synthesizedStack("hello-modules").toString()
).toMatchSnapshot();
});
});
4 changes: 2 additions & 2 deletions test/typescript/multiple-stacks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe("full integration test", () => {

test("synth", async () => {
await driver.synth();
expect(driver.synthesizedStack("first")).toMatchSnapshot();
expect(driver.synthesizedStack("second")).toMatchSnapshot();
expect(driver.synthesizedStack("first").toString()).toMatchSnapshot();
expect(driver.synthesizedStack("second").toString()).toMatchSnapshot();
});

test("synth with json output", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/providers/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`full integration test build providers 1`] = `
"Deploying Stack: hello-deploy
"Deploying Stack: using-all-providers
Resources
✔ NULL_RESOURCE test null_resource.test
Expand Down
Loading

0 comments on commit 258d373

Please sign in to comment.