|
| 1 | +import * as os from 'os'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { TestContext } from './integ-test'; |
| 4 | +import { RESOURCES_DIR } from './resources'; |
| 5 | +import { AwsContext, withAws } from './with-aws'; |
| 6 | +import { cloneDirectory, installNpmPackages, TestFixture, DEFAULT_TEST_TIMEOUT_S, CdkCliOptions } from './with-cdk-app'; |
| 7 | +import { withTimeout } from './with-timeout'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Higher order function to execute a block with a CliLib Integration CDK app fixture |
| 11 | + */ |
| 12 | +export function withCliLibIntegrationCdkApp<A extends TestContext & AwsContext>(block: (context: CliLibIntegrationTestFixture) => Promise<void>) { |
| 13 | + return async (context: A) => { |
| 14 | + const randy = context.randomString; |
| 15 | + const stackNamePrefix = `cdktest-${randy}`; |
| 16 | + const integTestDir = path.join(os.tmpdir(), `cdk-integ-${randy}`); |
| 17 | + |
| 18 | + context.log(` Stack prefix: ${stackNamePrefix}\n`); |
| 19 | + context.log(` Test directory: ${integTestDir}\n`); |
| 20 | + context.log(` Region: ${context.aws.region}\n`); |
| 21 | + |
| 22 | + await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', 'simple-app'), integTestDir, context.output); |
| 23 | + const fixture = new CliLibIntegrationTestFixture( |
| 24 | + integTestDir, |
| 25 | + stackNamePrefix, |
| 26 | + context.output, |
| 27 | + context.aws, |
| 28 | + context.randomString); |
| 29 | + |
| 30 | + let success = true; |
| 31 | + try { |
| 32 | + const installationVersion = fixture.packages.requestedFrameworkVersion(); |
| 33 | + |
| 34 | + if (fixture.packages.majorVersion() === '1') { |
| 35 | + throw new Error('This test suite is only compatible with AWS CDK v2'); |
| 36 | + } |
| 37 | + |
| 38 | + const alphaInstallationVersion = fixture.packages.requestedAlphaVersion(); |
| 39 | + await installNpmPackages(fixture, { |
| 40 | + 'aws-cdk-lib': installationVersion, |
| 41 | + '@aws-cdk/cli-lib-alpha': alphaInstallationVersion, |
| 42 | + '@aws-cdk/aws-lambda-go-alpha': alphaInstallationVersion, |
| 43 | + '@aws-cdk/aws-lambda-python-alpha': alphaInstallationVersion, |
| 44 | + 'constructs': '^10', |
| 45 | + }); |
| 46 | + |
| 47 | + await block(fixture); |
| 48 | + } catch (e: any) { |
| 49 | + // We survive certain cases involving gopkg.in |
| 50 | + if (errorCausedByGoPkg(e.message)) { |
| 51 | + return; |
| 52 | + } |
| 53 | + success = false; |
| 54 | + throw e; |
| 55 | + } finally { |
| 56 | + if (process.env.INTEG_NO_CLEAN) { |
| 57 | + context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)\n`); |
| 58 | + } else { |
| 59 | + await fixture.dispose(success); |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Return whether or not the error is being caused by gopkg.in being down |
| 67 | + * |
| 68 | + * Our Go build depends on https://gopkg.in/, which has errors pretty often |
| 69 | + * (every couple of days). It is run by a single volunteer. |
| 70 | + */ |
| 71 | +function errorCausedByGoPkg(error: string) { |
| 72 | + // The error is different depending on what request fails. Messages recognized: |
| 73 | + //////////////////////////////////////////////////////////////////// |
| 74 | + // go: github.com/aws/[email protected] requires |
| 75 | + // gopkg.in/yaml.v3@v3.0.0-20200615113413-eeeca48fe776: invalid version: git ls-remote -q origin in /go/pkg/mod/cache/vcs/0901dc1ef67fcce1c9b3ae51078740de4a0e2dc673e720584ac302973af82f36: exit status 128: |
| 76 | + // remote: Cannot obtain refs from GitHub: cannot talk to GitHub: Get https://github.com/go-yaml/yaml.git/info/refs?service=git-upload-pack: net/http: request canceled (Client.Timeout exceeded while awaiting headers) |
| 77 | + // fatal: unable to access 'https://gopkg.in/yaml.v3/': The requested URL returned error: 502 |
| 78 | + //////////////////////////////////////////////////////////////////// |
| 79 | + // go: downloading github.com/aws/aws-lambda-go v1.28.0 |
| 80 | + // go: github.com/aws/[email protected] requires |
| 81 | + // gopkg.in/yaml.v3@v3.0.0-20200615113413-eeeca48fe776: unrecognized import path "gopkg.in/yaml.v3": reading https://gopkg.in/yaml.v3?go-get=1: 502 Bad Gateway |
| 82 | + // server response: Cannot obtain refs from GitHub: cannot talk to GitHub: Get https://github.com/go-yaml/yaml.git/info/refs?service=git-upload-pack: net/http: request canceled (Client.Timeout exceeded while awaiting headers) |
| 83 | + //////////////////////////////////////////////////////////////////// |
| 84 | + // go: github.com/aws/[email protected] requires |
| 85 | + // gopkg.in/yaml.v3@v3.0.0-20200615113413-eeeca48fe776: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /go/pkg/mod/cache/vcs/0901dc1ef67fcce1c9b3ae51078740de4a0e2dc673e720584ac302973af82f36: exit status 128: |
| 86 | + // error: RPC failed; HTTP 502 curl 22 The requested URL returned error: 502 |
| 87 | + // fatal: the remote end hung up unexpectedly |
| 88 | + //////////////////////////////////////////////////////////////////// |
| 89 | + |
| 90 | + return (error.includes('gopkg\.in.*invalid version.*exit status 128') |
| 91 | + || error.match(/unrecognized import path[^\n]gopkg\.in/)); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * SAM Integration test fixture for CDK - SAM integration test cases |
| 96 | + */ |
| 97 | +export function withCliLibFixture(block: (context: CliLibIntegrationTestFixture) => Promise<void>) { |
| 98 | + return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCliLibIntegrationCdkApp(block))); |
| 99 | +} |
| 100 | + |
| 101 | +export class CliLibIntegrationTestFixture extends TestFixture { |
| 102 | + /** |
| 103 | + * |
| 104 | + */ |
| 105 | + public async cdk(args: string[], options: CdkCliOptions = {}) { |
| 106 | + const action = args[0]; |
| 107 | + const stackName = args[1]; |
| 108 | + |
| 109 | + const cliOpts: Record<string, any> = { |
| 110 | + stacks: stackName ? [stackName] : undefined, |
| 111 | + }; |
| 112 | + |
| 113 | + if (action === 'deploy') { |
| 114 | + cliOpts.requireApproval = options.neverRequireApproval ? 'never' : 'broadening'; |
| 115 | + } |
| 116 | + |
| 117 | + return this.shell(['node', '--input-type=module', `<<__EOS__ |
| 118 | + import { AwsCdkCli } from '@aws-cdk/cli-lib-alpha'; |
| 119 | + const cli = AwsCdkCli.fromCdkAppDirectory(); |
| 120 | +
|
| 121 | + await cli.${action}(${JSON.stringify(cliOpts)}); |
| 122 | +__EOS__`], { |
| 123 | + ...options, |
| 124 | + modEnv: { |
| 125 | + AWS_REGION: this.aws.region, |
| 126 | + AWS_DEFAULT_REGION: this.aws.region, |
| 127 | + STACK_NAME_PREFIX: this.stackNamePrefix, |
| 128 | + PACKAGE_LAYOUT_VERSION: this.packages.majorVersion(), |
| 129 | + ...options.modEnv, |
| 130 | + }, |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments