@aws-cdk-testing/cli-integ 2.173.4 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/.eslintrc.js +9 -0
  2. package/LICENSE +2 -1
  3. package/bin/query-github +1 -1
  4. package/bin/query-github.js +3 -3
  5. package/bin/query-github.ts +56 -0
  6. package/bin/run-suite +1 -1
  7. package/bin/run-suite.js +3 -3
  8. package/bin/run-suite.ts +140 -0
  9. package/bin/stage-distribution +1 -1
  10. package/bin/stage-distribution.js +3 -2
  11. package/bin/stage-distribution.ts +267 -0
  12. package/bin/test-root +1 -1
  13. package/bin/test-root.ts +3 -0
  14. package/lib/aws.js +9 -6
  15. package/lib/aws.ts +263 -0
  16. package/lib/cli/query-github.d.ts +1 -0
  17. package/lib/cli/query-github.js +54 -0
  18. package/lib/cli/query-github.ts +56 -0
  19. package/lib/cli/run-suite.d.ts +1 -0
  20. package/lib/cli/run-suite.js +131 -0
  21. package/lib/cli/run-suite.ts +140 -0
  22. package/lib/cli/stage-distribution.d.ts +1 -0
  23. package/lib/cli/stage-distribution.js +217 -0
  24. package/lib/cli/stage-distribution.ts +267 -0
  25. package/lib/cli/test-root.d.ts +1 -0
  26. package/lib/cli/test-root.js +6 -0
  27. package/lib/cli/test-root.ts +3 -0
  28. package/lib/corking.ts +33 -0
  29. package/lib/eventually.js +3 -3
  30. package/lib/eventually.ts +42 -0
  31. package/lib/files.js +3 -2
  32. package/lib/files.ts +80 -0
  33. package/lib/github.js +6 -5
  34. package/lib/github.ts +43 -0
  35. package/lib/index.ts +13 -0
  36. package/lib/integ-test.ts +81 -0
  37. package/lib/lists.ts +9 -0
  38. package/lib/memoize.ts +14 -0
  39. package/lib/npm.ts +41 -0
  40. package/lib/package-sources/release-source.js +3 -2
  41. package/lib/package-sources/release-source.ts +81 -0
  42. package/lib/package-sources/repo-source.ts +111 -0
  43. package/lib/package-sources/repo-tools/npm.js +5 -4
  44. package/lib/package-sources/repo-tools/npm.ts +48 -0
  45. package/lib/package-sources/source.ts +35 -0
  46. package/lib/package-sources/subprocess.ts +15 -0
  47. package/lib/resource-pool.js +2 -2
  48. package/lib/resource-pool.ts +140 -0
  49. package/lib/resources.ts +4 -0
  50. package/lib/shell.js +8 -5
  51. package/lib/shell.ts +168 -0
  52. package/lib/staging/codeartifact.js +11 -8
  53. package/lib/staging/codeartifact.ts +387 -0
  54. package/lib/staging/maven.js +5 -3
  55. package/lib/staging/maven.ts +95 -0
  56. package/lib/staging/npm.ts +62 -0
  57. package/lib/staging/nuget.ts +75 -0
  58. package/lib/staging/parallel-shell.js +2 -2
  59. package/lib/staging/parallel-shell.ts +51 -0
  60. package/lib/staging/pypi.ts +50 -0
  61. package/lib/staging/usage-dir.ts +99 -0
  62. package/lib/with-aws.js +3 -2
  63. package/lib/with-aws.ts +67 -0
  64. package/lib/with-cdk-app.js +23 -14
  65. package/lib/with-cdk-app.ts +742 -0
  66. package/lib/with-cli-lib.ts +134 -0
  67. package/lib/with-packages.ts +15 -0
  68. package/lib/with-sam.js +7 -4
  69. package/lib/with-sam.ts +288 -0
  70. package/lib/with-temporary-directory.ts +35 -0
  71. package/lib/with-timeout.ts +33 -0
  72. package/lib/xpmutex.js +2 -2
  73. package/lib/xpmutex.ts +218 -0
  74. package/package.json +86 -62
  75. package/resources/cloud-assemblies/0.36.0/cdk.out +1 -0
  76. package/resources/cloud-assemblies/1.10.0-lookup-default-vpc/cdk.out +1 -0
  77. package/resources/cloud-assemblies/1.10.0-request-azs/cdk.out +1 -0
  78. package/tests/cli-integ-tests/bootstrapping.integtest.js +22 -13
  79. package/tests/cli-integ-tests/bootstrapping.integtest.ts +493 -0
  80. package/tests/cli-integ-tests/cli-lib.integtest.js +3 -2
  81. package/tests/cli-integ-tests/cli-lib.integtest.ts +90 -0
  82. package/tests/cli-integ-tests/cli.integtest.js +76 -49
  83. package/tests/cli-integ-tests/cli.integtest.ts +2874 -0
  84. package/tests/cli-integ-tests/garbage-collection.integtest.js +2 -2
  85. package/tests/cli-integ-tests/garbage-collection.integtest.ts +392 -0
  86. package/tests/init-csharp/init-csharp.integtest.ts +15 -0
  87. package/tests/init-fsharp/init-fsharp.integtest.ts +15 -0
  88. package/tests/init-go/init-go.integtest.ts +23 -0
  89. package/tests/init-java/init-java.integtest.ts +14 -0
  90. package/tests/init-javascript/init-javascript.integtest.ts +59 -0
  91. package/tests/init-python/init-python.integtest.ts +20 -0
  92. package/tests/init-typescript-app/init-typescript-app.integtest.ts +66 -0
  93. package/tests/init-typescript-lib/init-typescript-lib.integtest.ts +13 -0
  94. package/tests/tool-integrations/amplify.integtest.ts +43 -0
  95. package/tests/tool-integrations/with-tool-context.ts +14 -0
  96. package/tests/uberpackage/uberpackage.integtest.ts +11 -0
  97. package/resources/cdk-apps/cfn-include-app/.gitignore +0 -1
@@ -0,0 +1,742 @@
1
+ /* eslint-disable no-console */
2
+ import * as assert from 'assert';
3
+ import * as fs from 'fs';
4
+ import * as os from 'os';
5
+ import * as path from 'path';
6
+ import { DescribeStacksCommand, Stack } from '@aws-sdk/client-cloudformation';
7
+ import { outputFromStack, AwsClients } from './aws';
8
+ import { TestContext } from './integ-test';
9
+ import { findYarnPackages } from './package-sources/repo-source';
10
+ import { IPackageSource } from './package-sources/source';
11
+ import { packageSourceInSubprocess } from './package-sources/subprocess';
12
+ import { RESOURCES_DIR } from './resources';
13
+ import { shell, ShellOptions, ShellHelper, rimraf } from './shell';
14
+ import { AwsContext, withAws } from './with-aws';
15
+ import { withTimeout } from './with-timeout';
16
+
17
+ export const DEFAULT_TEST_TIMEOUT_S = 20 * 60;
18
+ export const EXTENDED_TEST_TIMEOUT_S = 30 * 60;
19
+
20
+ /**
21
+ * Higher order function to execute a block with a CDK app fixture
22
+ *
23
+ * Requires an AWS client to be passed in.
24
+ *
25
+ * For backwards compatibility with existing tests (so we don't have to change
26
+ * too much) the inner block is expected to take a `TestFixture` object.
27
+ */
28
+ export function withSpecificCdkApp(
29
+ appName: string,
30
+ block: (context: TestFixture) => Promise<void>,
31
+ ): (context: TestContext & AwsContext & DisableBootstrapContext) => Promise<void> {
32
+ return async (context: TestContext & AwsContext & DisableBootstrapContext) => {
33
+ const randy = context.randomString;
34
+ const stackNamePrefix = `cdktest-${randy}`;
35
+ const integTestDir = path.join(os.tmpdir(), `cdk-integ-${randy}`);
36
+
37
+ context.output.write(` Stack prefix: ${stackNamePrefix}\n`);
38
+ context.output.write(` Test directory: ${integTestDir}\n`);
39
+ context.output.write(` Region: ${context.aws.region}\n`);
40
+
41
+ await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', appName), integTestDir, context.output);
42
+ const fixture = new TestFixture(
43
+ integTestDir,
44
+ stackNamePrefix,
45
+ context.output,
46
+ context.aws,
47
+ context.randomString);
48
+
49
+ let success = true;
50
+ try {
51
+ const installationVersion = fixture.packages.requestedFrameworkVersion();
52
+
53
+ if (fixture.packages.majorVersion() === '1') {
54
+ await installNpmPackages(fixture, {
55
+ '@aws-cdk/core': installationVersion,
56
+ '@aws-cdk/aws-sns': installationVersion,
57
+ '@aws-cdk/aws-sqs': installationVersion,
58
+ '@aws-cdk/aws-iam': installationVersion,
59
+ '@aws-cdk/aws-lambda': installationVersion,
60
+ '@aws-cdk/aws-ssm': installationVersion,
61
+ '@aws-cdk/aws-ecr-assets': installationVersion,
62
+ '@aws-cdk/aws-cloudformation': installationVersion,
63
+ '@aws-cdk/aws-ec2': installationVersion,
64
+ '@aws-cdk/aws-s3': installationVersion,
65
+ 'constructs': '^3',
66
+ });
67
+ } else {
68
+ await installNpmPackages(fixture, {
69
+ 'aws-cdk-lib': installationVersion,
70
+ 'constructs': '^10',
71
+ });
72
+ }
73
+
74
+ if (!context.disableBootstrap) {
75
+ await ensureBootstrapped(fixture);
76
+ }
77
+
78
+ await block(fixture);
79
+ } catch (e) {
80
+ success = false;
81
+ throw e;
82
+ } finally {
83
+ if (process.env.INTEG_NO_CLEAN) {
84
+ context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)\n`);
85
+ } else {
86
+ await fixture.dispose(success);
87
+ }
88
+ }
89
+ };
90
+ }
91
+
92
+ /**
93
+ * Like `withSpecificCdkApp`, but uses the default integration testing app with a million stacks in it
94
+ */
95
+ export function withCdkApp(
96
+ block: (context: TestFixture) => Promise<void>,
97
+ ): (context: TestContext & AwsContext & DisableBootstrapContext) => Promise<void> {
98
+ // 'app' is the name of the default integration app in the `cdk-apps` directory
99
+ return withSpecificCdkApp('app', block);
100
+ }
101
+
102
+ export function withCdkMigrateApp<A extends TestContext>(language: string, block: (context: TestFixture) => Promise<void>) {
103
+ return async (context: A) => {
104
+ const stackName = `cdk-migrate-${language}-integ-${context.randomString}`;
105
+ const integTestDir = path.join(os.tmpdir(), `cdk-migrate-${language}-integ-${context.randomString}`);
106
+
107
+ context.output.write(` Stack name: ${stackName}\n`);
108
+ context.output.write(` Test directory: ${integTestDir}\n`);
109
+
110
+ const awsClients = await AwsClients.default(context.output);
111
+ fs.mkdirSync(integTestDir);
112
+ const fixture = new TestFixture(
113
+ integTestDir,
114
+ stackName,
115
+ context.output,
116
+ awsClients,
117
+ context.randomString,
118
+ );
119
+
120
+ await fixture.cdkMigrate(language, stackName);
121
+
122
+ const testFixture = new TestFixture(
123
+ path.join(integTestDir, stackName),
124
+ stackName,
125
+ context.output,
126
+ awsClients,
127
+ context.randomString,
128
+ );
129
+
130
+ let success = true;
131
+ try {
132
+ await block(testFixture);
133
+ } catch (e) {
134
+ success = false;
135
+ throw e;
136
+ } finally {
137
+ if (process.env.INTEG_NO_CLEAN) {
138
+ context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)`);
139
+ } else {
140
+ await fixture.dispose(success);
141
+ }
142
+ }
143
+ };
144
+ }
145
+
146
+ export function withMonolithicCfnIncludeCdkApp<A extends TestContext>(block: (context: TestFixture) => Promise<void>) {
147
+ return async (context: A) => {
148
+ const uberPackage = process.env.UBERPACKAGE;
149
+ if (!uberPackage) {
150
+ throw new Error('The UBERPACKAGE environment variable is required for running this test!');
151
+ }
152
+
153
+ const randy = context.randomString;
154
+ const stackNamePrefix = `cdk-uber-cfn-include-${randy}`;
155
+ const integTestDir = path.join(os.tmpdir(), `cdk-uber-cfn-include-${randy}`);
156
+
157
+ context.output.write(` Stack prefix: ${stackNamePrefix}\n`);
158
+ context.output.write(` Test directory: ${integTestDir}\n`);
159
+
160
+ const awsClients = await AwsClients.default(context.output);
161
+ await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', 'cfn-include-app'), integTestDir, context.output);
162
+ const fixture = new TestFixture(
163
+ integTestDir,
164
+ stackNamePrefix,
165
+ context.output,
166
+ awsClients,
167
+ context.randomString,
168
+ );
169
+
170
+ let success = true;
171
+ try {
172
+ await installNpmPackages(fixture, {
173
+ [uberPackage]: fixture.packages.requestedFrameworkVersion(),
174
+ });
175
+
176
+ await block(fixture);
177
+ } catch (e) {
178
+ success = false;
179
+ throw e;
180
+ } finally {
181
+ if (process.env.INTEG_NO_CLEAN) {
182
+ context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)`);
183
+ } else {
184
+ await fixture.dispose(success);
185
+ }
186
+ }
187
+ };
188
+ }
189
+
190
+ /**
191
+ * Default test fixture for most (all?) integ tests
192
+ *
193
+ * It's a composition of withAws/withCdkApp, expecting the test block to take a `TestFixture`
194
+ * object.
195
+ *
196
+ * We could have put `withAws(withCdkApp(fixture => { /... actual test here.../ }))` in every
197
+ * test declaration but centralizing it is going to make it convenient to modify in the future.
198
+ */
199
+ export function withDefaultFixture(block: (context: TestFixture) => Promise<void>) {
200
+ return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCdkApp(block)));
201
+ }
202
+
203
+ export function withSpecificFixture(appName: string, block: (context: TestFixture) => Promise<void>) {
204
+ return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withSpecificCdkApp(appName, block)));
205
+ }
206
+
207
+ export function withExtendedTimeoutFixture(block: (context: TestFixture) => Promise<void>) {
208
+ return withAws(withTimeout(EXTENDED_TEST_TIMEOUT_S, withCdkApp(block)));
209
+ }
210
+
211
+ export function withCDKMigrateFixture(language: string, block: (content: TestFixture) => Promise<void>) {
212
+ return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withCdkMigrateApp(language, block)));
213
+ }
214
+
215
+ export interface DisableBootstrapContext {
216
+ /**
217
+ * Whether to disable creating the default bootstrap
218
+ * stack prior to running the test
219
+ *
220
+ * This should be set to true when running tests that
221
+ * explicitly create a bootstrap stack
222
+ *
223
+ * @default false
224
+ */
225
+ readonly disableBootstrap?: boolean;
226
+ }
227
+
228
+ /**
229
+ * To be used in place of `withDefaultFixture` when the test
230
+ * should not create the default bootstrap stack
231
+ */
232
+ export function withoutBootstrap(block: (context: TestFixture) => Promise<void>) {
233
+ return withAws(withCdkApp(block), true);
234
+ }
235
+
236
+ export interface CdkCliOptions extends ShellOptions {
237
+ options?: string[];
238
+ neverRequireApproval?: boolean;
239
+ verbose?: boolean;
240
+ }
241
+
242
+ /**
243
+ * Prepare a target dir byreplicating a source directory
244
+ */
245
+ export async function cloneDirectory(source: string, target: string, output?: NodeJS.WritableStream) {
246
+ await shell(['rm', '-rf', target], { outputs: output ? [output] : [] });
247
+ await shell(['mkdir', '-p', target], { outputs: output ? [output] : [] });
248
+ await shell(['cp', '-R', source + '/*', target], { outputs: output ? [output] : [] });
249
+ }
250
+
251
+ interface CommonCdkBootstrapCommandOptions {
252
+ /**
253
+ * Path to a custom bootstrap template.
254
+ *
255
+ * @default - the default CDK bootstrap template.
256
+ */
257
+ readonly bootstrapTemplate?: string;
258
+
259
+ readonly toolkitStackName: string;
260
+
261
+ /**
262
+ * @default false
263
+ */
264
+ readonly verbose?: boolean;
265
+
266
+ /**
267
+ * @default - auto-generated CloudFormation name
268
+ */
269
+ readonly bootstrapBucketName?: string;
270
+
271
+ readonly cliOptions?: CdkCliOptions;
272
+
273
+ /**
274
+ * @default - none
275
+ */
276
+ readonly tags?: string;
277
+
278
+ /**
279
+ * @default - the default CDK qualifier
280
+ */
281
+ readonly qualifier?: string;
282
+ }
283
+
284
+ export interface CdkLegacyBootstrapCommandOptions extends CommonCdkBootstrapCommandOptions {
285
+ /**
286
+ * @default false
287
+ */
288
+ readonly noExecute?: boolean;
289
+
290
+ /**
291
+ * @default true
292
+ */
293
+ readonly publicAccessBlockConfiguration?: boolean;
294
+ }
295
+
296
+ export interface CdkModernBootstrapCommandOptions extends CommonCdkBootstrapCommandOptions {
297
+ /**
298
+ * @default false
299
+ */
300
+ readonly force?: boolean;
301
+
302
+ /**
303
+ * @default - none
304
+ */
305
+ readonly cfnExecutionPolicy?: string;
306
+
307
+ /**
308
+ * @default false
309
+ */
310
+ readonly showTemplate?: boolean;
311
+
312
+ readonly template?: string;
313
+
314
+ /**
315
+ * @default false
316
+ */
317
+ readonly terminationProtection?: boolean;
318
+
319
+ /**
320
+ * @default undefined
321
+ */
322
+ readonly examplePermissionsBoundary?: boolean;
323
+
324
+ /**
325
+ * @default undefined
326
+ */
327
+ readonly customPermissionsBoundary?: string;
328
+
329
+ /**
330
+ * @default undefined
331
+ */
332
+ readonly usePreviousParameters?: boolean;
333
+ }
334
+
335
+ export interface CdkGarbageCollectionCommandOptions {
336
+ /**
337
+ * The amount of days an asset should stay isolated before deletion, to
338
+ * guard against some pipeline rollback scenarios
339
+ *
340
+ * @default 0
341
+ */
342
+ readonly rollbackBufferDays?: number;
343
+
344
+ /**
345
+ * The type of asset that is getting garbage collected.
346
+ *
347
+ * @default 'all'
348
+ */
349
+ readonly type?: 'ecr' | 's3' | 'all';
350
+
351
+ /**
352
+ * The name of the bootstrap stack
353
+ *
354
+ * @default 'CdkToolkit'
355
+ */
356
+ readonly bootstrapStackName?: string;
357
+ }
358
+
359
+ export class TestFixture extends ShellHelper {
360
+ public readonly qualifier = this.randomString.slice(0, 10);
361
+ private readonly bucketsToDelete = new Array<string>();
362
+ public readonly packages: IPackageSource;
363
+
364
+ constructor(
365
+ public readonly integTestDir: string,
366
+ public readonly stackNamePrefix: string,
367
+ public readonly output: NodeJS.WritableStream,
368
+ public readonly aws: AwsClients,
369
+ public readonly randomString: string) {
370
+
371
+ super(integTestDir, output);
372
+
373
+ this.packages = packageSourceInSubprocess();
374
+ }
375
+
376
+ public log(s: string) {
377
+ this.output.write(`${s}\n`);
378
+ }
379
+
380
+ public async cdkDeploy(stackNames: string | string[], options: CdkCliOptions = {}, skipStackRename?: boolean) {
381
+ stackNames = typeof stackNames === 'string' ? [stackNames] : stackNames;
382
+
383
+ const neverRequireApproval = options.neverRequireApproval ?? true;
384
+
385
+ return this.cdk(['deploy',
386
+ ...(neverRequireApproval ? ['--require-approval=never'] : []), // Default to no approval in an unattended test
387
+ ...(options.options ?? []),
388
+ // use events because bar renders bad in tests
389
+ '--progress', 'events',
390
+ ...(skipStackRename ? stackNames : this.fullStackName(stackNames))], options);
391
+ }
392
+
393
+ public async cdkSynth(options: CdkCliOptions = {}) {
394
+ return this.cdk([
395
+ 'synth',
396
+ ...(options.options ?? []),
397
+ ], options);
398
+ }
399
+
400
+ public async cdkDestroy(stackNames: string | string[], options: CdkCliOptions = {}) {
401
+ stackNames = typeof stackNames === 'string' ? [stackNames] : stackNames;
402
+
403
+ return this.cdk(['destroy',
404
+ '-f', // We never want a prompt in an unattended test
405
+ ...(options.options ?? []),
406
+ ...this.fullStackName(stackNames)], options);
407
+ }
408
+
409
+ public async cdkBootstrapLegacy(options: CdkLegacyBootstrapCommandOptions): Promise<string> {
410
+ const args = ['bootstrap'];
411
+
412
+ if (options.verbose) {
413
+ args.push('-v');
414
+ }
415
+ args.push('--toolkit-stack-name', options.toolkitStackName);
416
+ if (options.bootstrapBucketName) {
417
+ args.push('--bootstrap-bucket-name', options.bootstrapBucketName);
418
+ }
419
+ if (options.noExecute) {
420
+ args.push('--no-execute');
421
+ }
422
+ if (options.publicAccessBlockConfiguration !== undefined) {
423
+ args.push('--public-access-block-configuration', options.publicAccessBlockConfiguration.toString());
424
+ }
425
+ if (options.tags) {
426
+ args.push('--tags', options.tags);
427
+ }
428
+
429
+ return this.cdk(args, {
430
+ ...options.cliOptions,
431
+ modEnv: {
432
+ ...options.cliOptions?.modEnv,
433
+ // so that this works for V2,
434
+ // where the "new" bootstrap is the default
435
+ CDK_LEGACY_BOOTSTRAP: '1',
436
+ },
437
+ });
438
+ }
439
+
440
+ public async cdkBootstrapModern(options: CdkModernBootstrapCommandOptions): Promise<string> {
441
+ const args = ['bootstrap'];
442
+
443
+ if (options.verbose) {
444
+ args.push('-v');
445
+ }
446
+ if (options.showTemplate) {
447
+ args.push('--show-template');
448
+ }
449
+ if (options.template) {
450
+ args.push('--template', options.template);
451
+ }
452
+ args.push('--toolkit-stack-name', options.toolkitStackName);
453
+ if (options.bootstrapBucketName) {
454
+ args.push('--bootstrap-bucket-name', options.bootstrapBucketName);
455
+ }
456
+ args.push('--qualifier', options.qualifier ?? this.qualifier);
457
+ if (options.cfnExecutionPolicy) {
458
+ args.push('--cloudformation-execution-policies', options.cfnExecutionPolicy);
459
+ }
460
+ if (options.terminationProtection !== undefined) {
461
+ args.push('--termination-protection', options.terminationProtection.toString());
462
+ }
463
+ if (options.force) {
464
+ args.push('--force');
465
+ }
466
+ if (options.tags) {
467
+ args.push('--tags', options.tags);
468
+ }
469
+ if (options.customPermissionsBoundary !== undefined) {
470
+ args.push('--custom-permissions-boundary', options.customPermissionsBoundary);
471
+ } else if (options.examplePermissionsBoundary !== undefined) {
472
+ args.push('--example-permissions-boundary');
473
+ }
474
+ if (options.usePreviousParameters === false) {
475
+ args.push('--no-previous-parameters');
476
+ }
477
+ if (options.bootstrapTemplate) {
478
+ args.push('--template', options.bootstrapTemplate);
479
+ }
480
+
481
+ return this.cdk(args, {
482
+ ...options.cliOptions,
483
+ modEnv: {
484
+ ...options.cliOptions?.modEnv,
485
+ // so that this works for V1,
486
+ // where the "old" bootstrap is the default
487
+ CDK_NEW_BOOTSTRAP: '1',
488
+ },
489
+ });
490
+ }
491
+
492
+ public async cdkGarbageCollect(options: CdkGarbageCollectionCommandOptions): Promise<string> {
493
+ const args = [
494
+ 'gc',
495
+ '--unstable=gc', // TODO: remove when stabilizing
496
+ '--confirm=false',
497
+ '--created-buffer-days=0', // Otherwise all assets created during integ tests are too young
498
+ ];
499
+ if (options.rollbackBufferDays) {
500
+ args.push('--rollback-buffer-days', String(options.rollbackBufferDays));
501
+ }
502
+ if (options.type) {
503
+ args.push('--type', options.type);
504
+ }
505
+ if (options.bootstrapStackName) {
506
+ args.push('--bootstrapStackName', options.bootstrapStackName);
507
+ }
508
+
509
+ return this.cdk(args);
510
+ }
511
+
512
+ public async cdkMigrate(language: string, stackName: string, inputPath?: string, options?: CdkCliOptions) {
513
+ return this.cdk([
514
+ 'migrate',
515
+ '--language',
516
+ language,
517
+ '--stack-name',
518
+ stackName,
519
+ '--from-path',
520
+ inputPath ?? path.join(__dirname, '..', 'resources', 'templates', 'sqs-template.json').toString(),
521
+ ...(options?.options ?? []),
522
+ ], options);
523
+ }
524
+
525
+ public async cdk(args: string[], options: CdkCliOptions = {}) {
526
+ const verbose = options.verbose ?? true;
527
+
528
+ await this.packages.makeCliAvailable();
529
+
530
+ return this.shell(['cdk', ...(verbose ? ['-v'] : []), ...args], {
531
+ ...options,
532
+ modEnv: {
533
+ AWS_REGION: this.aws.region,
534
+ AWS_DEFAULT_REGION: this.aws.region,
535
+ STACK_NAME_PREFIX: this.stackNamePrefix,
536
+ PACKAGE_LAYOUT_VERSION: this.packages.majorVersion(),
537
+ ...options.modEnv,
538
+ },
539
+ });
540
+ }
541
+
542
+ public template(stackName: string): any {
543
+ const fullStackName = this.fullStackName(stackName);
544
+ const templatePath = path.join(this.integTestDir, 'cdk.out', `${fullStackName}.template.json`);
545
+ return JSON.parse(fs.readFileSync(templatePath, { encoding: 'utf-8' }).toString());
546
+ }
547
+
548
+ public async bootstrapRepoName(): Promise<string> {
549
+ await ensureBootstrapped(this);
550
+
551
+ const response = await this.aws.cloudFormation.send(new DescribeStacksCommand({}));
552
+
553
+ const stack = (response.Stacks ?? [])
554
+ .filter((s) => s.StackName && s.StackName == this.bootstrapStackName);
555
+ assert(stack.length == 1);
556
+ return outputFromStack('ImageRepositoryName', stack[0]) ?? '';
557
+ }
558
+
559
+ public get bootstrapStackName() {
560
+ return this.fullStackName('bootstrap-stack');
561
+ }
562
+
563
+ public fullStackName(stackName: string): string;
564
+ public fullStackName(stackNames: string[]): string[];
565
+ public fullStackName(stackNames: string | string[]): string | string[] {
566
+ if (typeof stackNames === 'string') {
567
+ return `${this.stackNamePrefix}-${stackNames}`;
568
+ } else {
569
+ return stackNames.map(s => `${this.stackNamePrefix}-${s}`);
570
+ }
571
+ }
572
+
573
+ /**
574
+ * Append this to the list of buckets to potentially delete
575
+ *
576
+ * At the end of a test, we clean up buckets that may not have gotten destroyed
577
+ * (for whatever reason).
578
+ */
579
+ public rememberToDeleteBucket(bucketName: string) {
580
+ this.bucketsToDelete.push(bucketName);
581
+ }
582
+
583
+ /**
584
+ * Cleanup leftover stacks and bootstrapped resources
585
+ */
586
+ public async dispose(success: boolean) {
587
+ const stacksToDelete = await this.deleteableStacks(this.stackNamePrefix);
588
+
589
+ this.sortBootstrapStacksToTheEnd(stacksToDelete);
590
+
591
+ // Bootstrap stacks have buckets that need to be cleaned
592
+ const bucketNames = stacksToDelete.map(stack => outputFromStack('BucketName', stack)).filter(defined);
593
+ // Parallelism will be reasonable
594
+ // eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism
595
+ await Promise.all(bucketNames.map(b => this.aws.emptyBucket(b)));
596
+ // The bootstrap bucket has a removal policy of RETAIN by default, so add it to the buckets to be cleaned up.
597
+ this.bucketsToDelete.push(...bucketNames);
598
+
599
+ // Bootstrap stacks have ECR repositories with images which should be deleted
600
+ const imageRepositoryNames = stacksToDelete.map(stack => outputFromStack('ImageRepositoryName', stack)).filter(defined);
601
+ // Parallelism will be reasonable
602
+ // eslint-disable-next-line @cdklabs/promiseall-no-unbounded-parallelism
603
+ await Promise.all(imageRepositoryNames.map(r => this.aws.deleteImageRepository(r)));
604
+
605
+ await this.aws.deleteStacks(
606
+ ...stacksToDelete.map((s) => {
607
+ if (!s.StackName) {
608
+ throw new Error('Stack name is required to delete a stack.');
609
+ }
610
+ return s.StackName;
611
+ }),
612
+ );
613
+
614
+ // We might have leaked some buckets by upgrading the bootstrap stack. Be
615
+ // sure to clean everything.
616
+ for (const bucket of this.bucketsToDelete) {
617
+ await this.aws.deleteBucket(bucket);
618
+ }
619
+
620
+ // If the tests completed successfully, happily delete the fixture
621
+ // (otherwise leave it for humans to inspect)
622
+ if (success) {
623
+ rimraf(this.integTestDir);
624
+ }
625
+ }
626
+
627
+ /**
628
+ * Return the stacks starting with our testing prefix that should be deleted
629
+ */
630
+ private async deleteableStacks(prefix: string): Promise<Stack[]> {
631
+ const statusFilter = [
632
+ 'CREATE_IN_PROGRESS', 'CREATE_FAILED', 'CREATE_COMPLETE',
633
+ 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_FAILED', 'ROLLBACK_COMPLETE',
634
+ 'DELETE_FAILED',
635
+ 'UPDATE_IN_PROGRESS', 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS',
636
+ 'UPDATE_COMPLETE', 'UPDATE_ROLLBACK_IN_PROGRESS',
637
+ 'UPDATE_ROLLBACK_FAILED',
638
+ 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS',
639
+ 'UPDATE_ROLLBACK_COMPLETE', 'REVIEW_IN_PROGRESS',
640
+ 'IMPORT_IN_PROGRESS', 'IMPORT_COMPLETE',
641
+ 'IMPORT_ROLLBACK_IN_PROGRESS', 'IMPORT_ROLLBACK_FAILED',
642
+ 'IMPORT_ROLLBACK_COMPLETE',
643
+ ];
644
+
645
+ const response = await this.aws.cloudFormation.send(new DescribeStacksCommand({}));
646
+
647
+ return (response.Stacks ?? [])
648
+ .filter((s) => s.StackName && s.StackName.startsWith(prefix))
649
+ .filter((s) => s.StackStatus && statusFilter.includes(s.StackStatus))
650
+ .filter((s) => s.RootId === undefined); // Only delete parent stacks. Nested stacks are deleted in the process
651
+ }
652
+
653
+ private sortBootstrapStacksToTheEnd(stacks: Stack[]) {
654
+ stacks.sort((a, b) => {
655
+
656
+ if (!a.StackName || !b.StackName) {
657
+ throw new Error('Stack names do not exists. These are required for sorting the bootstrap stacks.');
658
+ }
659
+
660
+ const aBs = a.StackName.startsWith(this.bootstrapStackName);
661
+ const bBs = b.StackName.startsWith(this.bootstrapStackName);
662
+
663
+ return aBs != bBs
664
+ // '+' converts a boolean to 0 or 1
665
+ ? (+aBs) - (+bBs)
666
+ : a.StackName.localeCompare(b.StackName);
667
+ });
668
+ }
669
+ }
670
+
671
+ /**
672
+ * Make sure that the given environment is bootstrapped
673
+ *
674
+ * Since we go striping across regions, it's going to suck doing this
675
+ * by hand so let's just mass-automate it.
676
+ */
677
+ async function ensureBootstrapped(fixture: TestFixture) {
678
+ // Always use the modern bootstrap stack, otherwise we may get the error
679
+ // "refusing to downgrade from version 7 to version 0" when bootstrapping with default
680
+ // settings using a v1 CLI.
681
+ //
682
+ // It doesn't matter for tests: when they want to test something about an actual legacy
683
+ // bootstrap stack, they'll create a bootstrap stack with a non-default name to test that exact property.
684
+ const envSpecifier = `aws://${await fixture.aws.account()}/${fixture.aws.region}`;
685
+ if (ALREADY_BOOTSTRAPPED_IN_THIS_RUN.has(envSpecifier)) { return; }
686
+
687
+ await fixture.cdk(['bootstrap', envSpecifier], {
688
+ modEnv: {
689
+ // Even for v1, use new bootstrap
690
+ CDK_NEW_BOOTSTRAP: '1',
691
+ },
692
+ });
693
+
694
+ ALREADY_BOOTSTRAPPED_IN_THIS_RUN.add(envSpecifier);
695
+ }
696
+
697
+ function defined<A>(x: A): x is NonNullable<A> {
698
+ return x !== undefined;
699
+ }
700
+
701
+ /**
702
+ * Install the given NPM packages, identified by their names and versions
703
+ *
704
+ * Works by writing the packages to a `package.json` file, and
705
+ * then running NPM7's "install" on it. The use of NPM7 will automatically
706
+ * install required peerDependencies.
707
+ *
708
+ * If we're running in REPO mode and we find the package in the set of local
709
+ * packages in the repository, we'll write the directory name to `package.json`
710
+ * so that NPM will create a symlink (this allows running tests against
711
+ * built-but-unpackaged modules, and saves dev cycle time).
712
+ *
713
+ * Be aware you MUST install all the packages you directly depend upon! In the case
714
+ * of a repo/symlinking install, transitive dependencies WILL NOT be installed in the
715
+ * current directory's `node_modules` directory, because they will already have been
716
+ * symlinked from the TARGET directory's `node_modules` directory (which is sufficient
717
+ * for Node's dependency lookup mechanism).
718
+ */
719
+ export async function installNpmPackages(fixture: TestFixture, packages: Record<string, string>) {
720
+ if (process.env.REPO_ROOT) {
721
+ const monoRepo = await findYarnPackages(process.env.REPO_ROOT);
722
+
723
+ // Replace the install target with the physical location of this package
724
+ for (const key of Object.keys(packages)) {
725
+ if (key in monoRepo) {
726
+ packages[key] = monoRepo[key];
727
+ }
728
+ }
729
+ }
730
+
731
+ fs.writeFileSync(path.join(fixture.integTestDir, 'package.json'), JSON.stringify({
732
+ name: 'cdk-integ-tests',
733
+ private: true,
734
+ version: '0.0.1',
735
+ devDependencies: packages,
736
+ }, undefined, 2), { encoding: 'utf-8' });
737
+
738
+ // Now install that `package.json` using NPM7
739
+ await fixture.shell(['node', require.resolve('npm'), 'install']);
740
+ }
741
+
742
+ const ALREADY_BOOTSTRAPPED_IN_THIS_RUN = new Set();