Skip to content

Commit 9139ca9

Browse files
authored
fix(integ-runner): Fix call to spawnSync for hooks commands (#22429)
Fixes the issue #22344 I reworked the approach of calling `exec` by splitting each command in hook to the command itself and it's arguments. All hooks were affected: preDeploy, postDeploy, preDestroy, postDestroy. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 98386f5 commit 9139ca9

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

packages/@aws-cdk/integ-runner/lib/runner/integ-test-runner.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RequireApproval } from '@aws-cdk/cloud-assembly-schema';
33
import { DeployOptions, DestroyOptions } from 'cdk-cli-wrapper';
44
import * as fs from 'fs-extra';
55
import * as logger from '../logger';
6-
import { chain, exec } from '../utils';
6+
import { chunks, exec } from '../utils';
77
import { DestructiveChange, AssertionResults, AssertionResult } from '../workers/common';
88
import { IntegRunnerOptions, IntegRunner, DEFAULT_SYNTH_OPTIONS } from './runner-base';
99

@@ -222,17 +222,21 @@ export class IntegTestRunner extends IntegRunner {
222222
const actualTestCase = this.actualTestSuite.testSuite[testCaseName];
223223
try {
224224
if (actualTestCase.hooks?.preDestroy) {
225-
exec([chain(actualTestCase.hooks.preDestroy)], {
226-
cwd: path.dirname(this.snapshotDir),
225+
actualTestCase.hooks.preDestroy.forEach(cmd => {
226+
exec(chunks(cmd), {
227+
cwd: path.dirname(this.snapshotDir),
228+
});
227229
});
228230
}
229231
this.cdk.destroy({
230232
...destroyArgs,
231233
});
232234

233235
if (actualTestCase.hooks?.postDestroy) {
234-
exec([chain(actualTestCase.hooks.postDestroy)], {
235-
cwd: path.dirname(this.snapshotDir),
236+
actualTestCase.hooks.postDestroy.forEach(cmd => {
237+
exec(chunks(cmd), {
238+
cwd: path.dirname(this.snapshotDir),
239+
});
236240
});
237241
}
238242
} catch (e) {
@@ -255,8 +259,10 @@ export class IntegTestRunner extends IntegRunner {
255259
const actualTestCase = this.actualTestSuite.testSuite[testCaseName];
256260
try {
257261
if (actualTestCase.hooks?.preDeploy) {
258-
exec([chain(actualTestCase.hooks?.preDeploy)], {
259-
cwd: path.dirname(this.snapshotDir),
262+
actualTestCase.hooks.preDeploy.forEach(cmd => {
263+
exec(chunks(cmd), {
264+
cwd: path.dirname(this.snapshotDir),
265+
});
260266
});
261267
}
262268
// if the update workflow is not disabled, first
@@ -297,8 +303,10 @@ export class IntegTestRunner extends IntegRunner {
297303
});
298304

299305
if (actualTestCase.hooks?.postDeploy) {
300-
exec([chain(actualTestCase.hooks?.postDeploy)], {
301-
cwd: path.dirname(this.snapshotDir),
306+
actualTestCase.hooks.postDeploy.forEach(cmd => {
307+
exec(chunks(cmd), {
308+
cwd: path.dirname(this.snapshotDir),
309+
});
302310
});
303311
}
304312

packages/@aws-cdk/integ-runner/lib/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ export function chain(commands: string[]): string {
4141
return commands.filter(c => !!c).join(' && ');
4242
}
4343

44+
/**
45+
* Split command to chunks by space
46+
*/
47+
export function chunks(command: string): string[] {
48+
const result = command.match(/(?:[^\s"]+|"[^"]*")+/g);
49+
return result ?? [];
50+
}
51+
4452

4553
/**
4654
* A class holding a set of items which are being crossed off in time

packages/@aws-cdk/integ-runner/test/runner/integ-test-runner.test.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,22 @@ describe('IntegTest runIntegTests', () => {
347347
// THEN
348348
expect(spawnSyncMock.mock.calls).toEqual(expect.arrayContaining([
349349
expect.arrayContaining([
350-
'echo "preDeploy"',
350+
'echo', ['"preDeploy hook"'],
351351
]),
352352
expect.arrayContaining([
353-
'echo "postDeploy"',
353+
'echo', ['"postDeploy hook"'],
354354
]),
355355
expect.arrayContaining([
356-
'echo "preDestroy"',
356+
'echo', ['"preDestroy hook"'],
357357
]),
358358
expect.arrayContaining([
359-
'echo "postDestroy"',
359+
'echo', ['"postDestroy hook"'],
360+
]),
361+
expect.arrayContaining([
362+
'ls', [],
363+
]),
364+
expect.arrayContaining([
365+
'echo', ['-n', '"No new line"'],
360366
]),
361367
]));
362368
});

packages/@aws-cdk/integ-runner/test/test-data/cdk-integ.out.test-with-snapshot-assets/integ.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"stackUpdateWorkflow": false,
77
"diffAssets": false,
88
"hooks": {
9-
"preDeploy": ["echo \"preDeploy\""],
10-
"postDeploy": ["echo \"postDeploy\""],
11-
"preDestroy": ["echo \"preDestroy\""],
12-
"postDestroy": ["echo \"postDestroy\""]
9+
"preDeploy": ["echo \"preDeploy hook\"", "ls", "echo -n \"No new line\""],
10+
"postDeploy": ["echo \"postDeploy hook\""],
11+
"preDestroy": ["echo \"preDestroy hook\""],
12+
"postDestroy": ["echo \"postDestroy hook\""]
1313
},
1414
"allowDestroy": [
1515
"AWS::IAM::Role"

0 commit comments

Comments
 (0)