-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
201 additions
and
195 deletions.
There are no files selected for viewing
This file contains 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 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
35 changes: 16 additions & 19 deletions
35
...glang/src/commands/test/harness/awscdk.ts → .../@winglang/platform-awscdk/src/harness.ts
This file contains 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 |
---|---|---|
@@ -1,48 +1,45 @@ | ||
import { readFile, rm } from "fs/promises"; | ||
import { ITestRunnerClient } from "@winglang/sdk/lib/std"; | ||
import { Util } from "@winglang/sdk/lib/util"; | ||
import { ITestHarness } from "./api"; | ||
import { withSpinner } from "../../../util"; | ||
import { execCapture } from "../util"; | ||
import { ITestHarness } from "@winglang/sdk/lib/platform"; | ||
import * as path from "path"; | ||
|
||
const ENV_WING_TEST_RUNNER_FUNCTION_IDENTIFIERS_AWSCDK = "WingTestRunnerFunctionArns"; | ||
export const WING_TEST_RUNNER_FUNCTION_IDENTIFIERS_AWSCDK = "WingTestRunnerFunctionArns"; | ||
|
||
const OUTPUT_FILE = "output.json"; | ||
|
||
export class AwsCdkTestHarness implements ITestHarness { | ||
public async deploy(synthDir: string): Promise<ITestRunnerClient> { | ||
const opts = { | ||
cwd: synthDir, | ||
inheritEnv: true, | ||
}; | ||
try { | ||
await execCapture("cdk version --ci true", { cwd: synthDir }); | ||
Util.exec("cdk", ["version", "--ci", "true"], opts); | ||
} catch (err) { | ||
throw new Error( | ||
"AWS-CDK is not installed. Please install AWS-CDK to run tests in the cloud (npm i -g aws-cdk).", | ||
); | ||
} | ||
|
||
await withSpinner("cdk deploy", () => | ||
execCapture("cdk deploy --require-approval never --ci true -O ./output.json --app . ", { | ||
cwd: synthDir, | ||
}), | ||
); | ||
Util.exec("cdk", ["deploy", "--require-approval", "never", "--ci", "true", "-O", OUTPUT_FILE, "--app", "."], opts); | ||
|
||
const stackName = process.env.CDK_STACK_NAME! + Util.sha256(synthDir).slice(-8); | ||
const testArns = await this.getFunctionArnsOutput(synthDir, stackName); | ||
|
||
const { TestRunnerClient } = await import("@winglang/sdk/lib/shared-aws/test-runner.inflight"); | ||
const runner = new TestRunnerClient({ $tests: testArns }); | ||
return runner; | ||
return new TestRunnerClient({ $tests: testArns }); | ||
} | ||
|
||
public async cleanup(synthDir: string): Promise<void> { | ||
await withSpinner("aws-cdk destroy", async () => { | ||
await rm(synthDir.concat("/output.json")); | ||
await execCapture("cdk destroy -f --ci true --app ./", { cwd: synthDir }); | ||
}); | ||
|
||
await rm(path.join(synthDir, OUTPUT_FILE)); | ||
Util.exec("cdk", ["destroy", "-f", "--ci", "true", "--app", "."], { cwd: synthDir, inheritEnv: true }); | ||
await rm(synthDir, { recursive: true, force: true }); | ||
} | ||
|
||
private async getFunctionArnsOutput(synthDir: string, stackName: string) { | ||
const file = await readFile(synthDir.concat("/output.json")); | ||
const file = await readFile(path.join(synthDir, OUTPUT_FILE)); | ||
const parsed = JSON.parse(Buffer.from(file).toString()); | ||
return parsed[stackName][ENV_WING_TEST_RUNNER_FUNCTION_IDENTIFIERS_AWSCDK]; | ||
return parsed[stackName][WING_TEST_RUNNER_FUNCTION_IDENTIFIERS_AWSCDK]; | ||
} | ||
} |
This file contains 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 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 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 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 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,72 @@ | ||
import { rm } from "fs/promises"; | ||
import { ITestHarness } from "../platform"; | ||
import { ITestRunnerClient } from "../std"; | ||
import { Util } from "../util"; | ||
|
||
export const WING_TEST_RUNNER_FUNCTION_IDENTIFIERS = | ||
"WING_TEST_RUNNER_FUNCTION_IDENTIFIERS"; | ||
|
||
export interface TerraformTestHarnessOptions { | ||
readonly parallelism?: number; | ||
readonly clientModule: string; | ||
} | ||
|
||
export class TerraformTestHarness implements ITestHarness { | ||
private readonly options: TerraformTestHarnessOptions; | ||
private readonly parallelism: string; | ||
|
||
constructor(options: TerraformTestHarnessOptions) { | ||
this.options = options; | ||
this.parallelism = options.parallelism | ||
? `-parallelism=${options.parallelism}` | ||
: ""; | ||
} | ||
|
||
public async deploy(synthDir: string): Promise<ITestRunnerClient> { | ||
const opts = { | ||
cwd: synthDir, | ||
inheritEnv: true, | ||
}; | ||
|
||
// Check if Terraform is installed | ||
const tfVersion = Util.exec("terraform", ["version"], opts); | ||
const installed = tfVersion.stdout.startsWith("Terraform v"); | ||
if (!installed) { | ||
throw new Error( | ||
"Terraform is not installed. Please install Terraform to run tests in the cloud.", | ||
); | ||
} | ||
|
||
Util.exec("terraform", ["init"], opts); | ||
Util.exec("terraform", ["apply", "-auto-approve", this.parallelism], opts); | ||
|
||
// Get the test runner function ARNs | ||
const output = Util.exec("terraform", ["output", "-json"], opts); | ||
|
||
const parsed = JSON.parse(output.stdout); | ||
const testArns = parsed[WING_TEST_RUNNER_FUNCTION_IDENTIFIERS]?.value; | ||
if (!testArns) { | ||
throw new Error( | ||
`terraform output ${WING_TEST_RUNNER_FUNCTION_IDENTIFIERS} not found`, | ||
); | ||
} | ||
|
||
// Create the test runner client | ||
const { TestRunnerClient } = await import(this.options.clientModule); | ||
const runner = new TestRunnerClient({ $tests: testArns }); | ||
return runner; | ||
} | ||
|
||
public async cleanup(synthDir: string): Promise<void> { | ||
try { | ||
Util.exec("terraform", ["destroy", "-auto-approve", this.parallelism], { | ||
cwd: synthDir, | ||
inheritEnv: true, | ||
}); | ||
|
||
await rm(synthDir, { recursive: true, force: true }); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.