Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ansgarm committed Mar 30, 2022
1 parent 9773853 commit 6f2b8c2
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 133 deletions.
10 changes: 7 additions & 3 deletions packages/cdktf-cli/lib/models/terraform-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,14 @@ export class TerraformCloud implements Terraform {
this.removeRun("cancel");
});

const httpsProxy = process.env.https_proxy || process.env.HTTPS_PROXY;
if (httpsProxy) {
const httpProxy = process.env.http_proxy || process.env.HTTP_PROXY;
if (httpProxy) {
const url = new URL(httpProxy);
logger.debug(
`setting tunnel agent via hostname=${url.hostname} port=${url.port}`
);
this.client.client.defaults.httpsAgent = agent.httpsOverHttp({
proxy: new URL(httpsProxy),
proxy: { host: url.hostname, port: url.port },
});
}
}
Expand Down
7 changes: 3 additions & 4 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,19 @@
"license": "MPL-2.0",
"devDependencies": {
"@skorfmann/terraform-cloud": "^1.9.1",
"@types/express": "^4.17.13",
"@types/fs-extra": "^8.1.0",
"@types/jest": "^27.0.1",
"@types/express": "^4.17.13",
"@types/express-http-proxy": "^1.6.3",
"archiver": "^5.3.0",
"execa": "^5.1.1",
"express": "^4.17.2",
"express-http-proxy": "^1.6.3",
"fs-extra": "^8.1.0",
"jest": "^27.2.1",
"jsii-rosetta": "^1.53.0",
"node-pty": "0.10.0",
"strip-ansi": "^6.0.0",
"ts-jest": "^27.0.7",
"typescript": "^3.9.7"
}
},
"dependencies": {}
}
64 changes: 43 additions & 21 deletions test/test-helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { TemplateServer } from "./template-server";
import { spawn } from "child_process";
import { spawn, execSync } from "child_process";
import * as execa from "execa";
import { spawn as ptySpawn } from "node-pty";

const { execSync } = require("child_process");
const os = require("os");
const path = require("path");
const fs = require("fs");
const fse = require("fs-extra");
const stripAnsi = require("strip-ansi");

function execSyncLogErrors(...args: Parameters<typeof execSync>) {
try {
return execSync(...args);
} catch (e) {
console.log(e.stdout?.toString());
console.error(e.stderr?.toString());
throw e;
}
}

export class QueryableStack {
private readonly stack: Record<string, any>;
constructor(stackInput: string) {
Expand Down Expand Up @@ -169,38 +179,47 @@ export class TestDriver {

list = (flags?: string) => {
return stripAnsi(
execSync(`cdktf list ${flags ? flags : ""}`, {
execSyncLogErrors(`cdktf list ${flags ? flags : ""}`, {
env: this.env,
}).toString()
);
};

diff = (stackName?: string) => {
return stripAnsi(
execSync(`cdktf diff ${stackName ? stackName : ""}`, {
execSyncLogErrors(`cdktf diff ${stackName ? stackName : ""}`, {
env: this.env,
}).toString()
);
};

deploy = (stackNames?: string[], outputsFilePath?: string) => {
return stripAnsi(
execSync(
`cdktf deploy ${
stackNames ? stackNames.join(" ") : ""
} --auto-approve ${
outputsFilePath ? `--outputs-file=${outputsFilePath}` : ""
}`,
{ env: this.env }
).toString()
deploy = async (stackNames?: string[], outputsFilePath?: string) => {
const result = await execa(
"cdktf",
[
"deploy",
...stackNames,
"--auto-approve",
...(outputsFilePath ? [`--outputs-file=${outputsFilePath}`] : []),
],
{ env: { ...process.env, ...this.env } } // make sure env is up to date
);
return stripAnsi(result.stdout);
};

output = (stackName?: string, outputsFilePath?: string) => {
output = (
stackName?: string,
outputsFilePath?: string,
includeSensitiveOutputs?: boolean
) => {
return stripAnsi(
execSync(
execSyncLogErrors(
`cdktf output ${stackName ? stackName : ""} ${
outputsFilePath ? `--outputs-file=${outputsFilePath}` : ""
} ${
includeSensitiveOutputs
? `--outputs-file-include-sensitive-outputs=true`
: ""
}`,
{ env: this.env }
).toString()
Expand All @@ -209,7 +228,7 @@ export class TestDriver {

destroy = (stackNames?: string[]) => {
return stripAnsi(
execSync(
execSyncLogErrors(
`cdktf destroy ${
stackNames ? stackNames.join(" ") : ""
} --auto-approve`,
Expand Down Expand Up @@ -250,10 +269,13 @@ export class TestDriver {
await this.init("csharp");
this.copyFiles("Main.cs", "cdktf.json");
await this.get();
execSync("dotnet add reference .gen/Providers.Null/Providers.Null.csproj", {
stdio: "inherit",
env: this.env,
});
execSyncLogErrors(
"dotnet add reference .gen/Providers.Null/Providers.Null.csproj",
{
stdio: "inherit",
env: this.env,
}
);
};

setupJavaProject = async () => {
Expand Down
10 changes: 5 additions & 5 deletions test/typescript/diff-deploy-output-destroy/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe("full integration test", () => {
expect(driver.diff()).toContain(`1 to add, 0 to change, 0 to destroy.`);
});

test("deploy", () => {
const output = driver.deploy();
test("deploy", async () => {
const output = await driver.deploy();
expect(output).toContain(`null_resource.test (test) will be created`);
expect(output).not.toContain(`"world"`);
expect(output).toContain(`output = "hello"`);
Expand All @@ -29,7 +29,7 @@ describe("full integration test", () => {
expect(output).toContain(`output2 = <sensitive>`);
});

it("deploy and output write the same outputs file", () => {
it("deploy and output write the same outputs file", async () => {
const deployOutputsPath = path.resolve(
driver.workingDirectory,
"deploy.outputs.json"
Expand All @@ -39,9 +39,9 @@ describe("full integration test", () => {
"output.outputs.json"
);

driver.deploy(undefined, deployOutputsPath);
await driver.deploy(undefined, deployOutputsPath);
const deployOutput = JSON.parse(fs.readFileSync(deployOutputsPath, "utf8"));
driver.output(undefined, outputOutputsPath);
await driver.output(undefined, outputOutputsPath);
const outputOutput = JSON.parse(fs.readFileSync(outputOutputsPath, "utf8"));

expect(deployOutput).toMatchInlineSnapshot(`
Expand Down
10 changes: 6 additions & 4 deletions test/typescript/multiple-stacks/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ describe("multiple stacks", () => {
expect(stderr).toEqual("");
});

test("deploy", () => {
expect(driver.deploy(["first"])).toContain(`Apply complete!`);
expect(driver.deploy(["first", "second"])).toContain(`Apply complete!`);
test("deploy", async () => {
expect(await driver.deploy(["first"])).toContain(`Apply complete!`);
expect(await driver.deploy(["first", "second"])).toContain(
`Apply complete!`
);

expect(() => driver.deploy()).toThrowError(
expect(driver.deploy()).toThrowError(
"Found more than one stack, please specify a target stack. Run cdktf deploy <stack> with one of these stacks: first, second"
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/providers/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe("full integration test", () => {
// This is a workaround for that, by having a new driver every run we don't have this bug.
const deployDriver = new TestDriver(__dirname);
await deployDriver.setupTypescriptProject();
const deployLog = deployDriver.deploy(["using-all-providers"]);
const deployLog = await deployDriver.deploy(["using-all-providers"]);
expect(deployLog).toContain("null_resource.test (test) will be created");
expect(deployLog).toContain("1 to add");
expect(deployLog).toContain("Apply complete!");
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/terraform-cloud/cdktf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"language": "typescript",
"app": "npm run --silent compile && node main.js",
"app": "npx ts-node main.ts",
"terraformProviders": [
"random@= 3.1.0",
"local@= 2.1.0",
Expand Down
25 changes: 12 additions & 13 deletions test/typescript/terraform-cloud/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Testing,
TerraformAsset,
TerraformOutput,
RemoteBackend,
NamedRemoteWorkspace,
} from "cdktf";
import * as NullProvider from "./.gen/providers/null";
import * as local from "./.gen/providers/local";
Expand All @@ -29,11 +31,6 @@ export class SourceStack extends TerraformStack {
length: 32,
});

new local.File(this, "file", {
filename: "../../../origin-file.txt",
content: this.password.result,
});

const nullResouce = new NullProvider.Resource(this, "test", {});

nullResouce.addOverride("provisioner", [
Expand All @@ -45,21 +42,23 @@ export class SourceStack extends TerraformStack {
]);

if (!localExecution) {
this.addOverride("terraform.backend", {
remote: {
organization,
workspaces: {
name,
},
token,
},
new RemoteBackend(this, {
organization: organization!,
workspaces: new NamedRemoteWorkspace(name!),
token,
});
}

new TerraformOutput(this, "output", {
value: "constant value",
});

new TerraformOutput(this, "password_output", {
value: this.password.result,
staticId: true,
sensitive: true,
});

const asset = new TerraformAsset(this, "asset-a", {
path: path.resolve(__dirname, "fixtures/a.txt"),
});
Expand Down
Loading

0 comments on commit 6f2b8c2

Please sign in to comment.