-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
with-sam.ts
284 lines (254 loc) · 11.2 KB
/
with-sam.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import axios from 'axios';
import { TestContext } from './integ-test';
import { RESOURCES_DIR } from './resources';
import { ShellOptions, rimraf } from './shell';
import { AwsContext, withAws } from './with-aws';
import { cloneDirectory, installNpmPackages, TestFixture, DEFAULT_TEST_TIMEOUT_S } from './with-cdk-app';
import { withTimeout } from './with-timeout';
export interface ActionOutput {
actionSucceeded?: boolean;
actionOutput?: any;
shellOutput?: string;
}
/**
* Higher order function to execute a block with a SAM Integration CDK app fixture
*/
export function withSamIntegrationCdkApp<A extends TestContext & AwsContext>(block: (context: SamIntegrationTestFixture) => Promise<void>) {
return async (context: A) => {
const randy = context.randomString;
const stackNamePrefix = `cdktest-${randy}`;
const integTestDir = path.join(os.tmpdir(), `cdk-integ-${randy}`);
context.log(` Stack prefix: ${stackNamePrefix}\n`);
context.log(` Test directory: ${integTestDir}\n`);
context.log(` Region: ${context.aws.region}\n`);
await cloneDirectory(path.join(RESOURCES_DIR, 'cdk-apps', 'sam_cdk_integ_app'), integTestDir, context.output);
const fixture = new SamIntegrationTestFixture(
integTestDir,
stackNamePrefix,
context.output,
context.aws,
context.randomString);
let success = true;
try {
const installationVersion = fixture.packages.requestedFrameworkVersion();
if (fixture.packages.majorVersion() === '1') {
await installNpmPackages(fixture, {
'@aws-cdk/aws-iam': installationVersion,
'@aws-cdk/aws-apigateway': installationVersion,
'@aws-cdk/aws-lambda': installationVersion,
'@aws-cdk/aws-lambda-go': installationVersion,
'@aws-cdk/aws-lambda-nodejs': installationVersion,
'@aws-cdk/aws-lambda-python': installationVersion,
'@aws-cdk/aws-logs': installationVersion,
'@aws-cdk/core': installationVersion,
'constructs': '^3',
});
} else {
const alphaInstallationVersion = fixture.packages.requestedAlphaVersion();
await installNpmPackages(fixture, {
'aws-cdk-lib': installationVersion,
'@aws-cdk/aws-lambda-go-alpha': alphaInstallationVersion,
'@aws-cdk/aws-lambda-python-alpha': alphaInstallationVersion,
'constructs': '^10',
});
}
await block(fixture);
} catch (e) {
// We survive certain cases involving gopkg.in
if (errorCausedByGoPkg(e.message)) {
return;
}
success = false;
throw e;
} finally {
if (process.env.INTEG_NO_CLEAN) {
context.log(`Left test directory in '${integTestDir}' ($INTEG_NO_CLEAN)\n`);
} else {
await fixture.dispose(success);
}
}
};
}
/**
* Return whether or not the error is being caused by gopkg.in being down
*
* Our Go build depends on https://gopkg.in/, which has errors pretty often
* (every couple of days). It is run by a single volunteer.
*/
function errorCausedByGoPkg(error: string) {
// The error is different depending on what request fails. Messages recognized:
////////////////////////////////////////////////////////////////////
// go: github.com/aws/aws-lambda-go@v1.28.0 requires
// 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:
// 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)
// fatal: unable to access 'https://gopkg.in/yaml.v3/': The requested URL returned error: 502
////////////////////////////////////////////////////////////////////
// go: downloading github.com/aws/aws-lambda-go v1.28.0
// go: github.com/aws/aws-lambda-go@v1.28.0 requires
// 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
// 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)
////////////////////////////////////////////////////////////////////
// go: github.com/aws/aws-lambda-go@v1.28.0 requires
// 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:
// error: RPC failed; HTTP 502 curl 22 The requested URL returned error: 502
// fatal: the remote end hung up unexpectedly
////////////////////////////////////////////////////////////////////
return (error.includes('gopkg\.in.*invalid version.*exit status 128')
|| error.match(/unrecognized import path[^\n]gopkg\.in/));
}
/**
* SAM Integration test fixture for CDK - SAM integration test cases
*/
export function withSamIntegrationFixture(block: (context: SamIntegrationTestFixture) => Promise<void>) {
return withAws(withTimeout(DEFAULT_TEST_TIMEOUT_S, withSamIntegrationCdkApp(block)));
}
export class SamIntegrationTestFixture extends TestFixture {
public async samShell(command: string[], filter?: string, action?: () => any, options: Omit<ShellOptions, 'cwd' | 'output'> = {}): Promise<ActionOutput> {
return shellWithAction(command, filter, action, {
output: this.output,
cwd: path.join(this.integTestDir, 'cdk.out').toString(),
...options,
});
}
public async samBuild(stackName: string) {
const fullStackName = this.fullStackName(stackName);
const templatePath = path.join(this.integTestDir, 'cdk.out', `${fullStackName}.template.json`);
const args = ['--template', templatePath.toString()];
return this.samShell(['sam', 'build', ...args]);
}
public async samLocalStartApi(stackName: string, isBuilt: boolean, port: number, apiPath: string): Promise<ActionOutput> {
const fullStackName = this.fullStackName(stackName);
const templatePath = path.join(this.integTestDir, 'cdk.out', `${fullStackName}.template.json`);
const args = isBuilt? [] : ['--template', templatePath.toString()];
args.push('--port');
args.push(port.toString());
return this.samShell(['sam', 'local', 'start-api', ...args], 'Press CTRL+C to quit', ()=>{
return new Promise<ActionOutput>((resolve, reject) => {
axios.get(`http://127.0.0.1:${port}${apiPath}`).then( resp => {
resolve(resp.data);
}).catch( error => {
reject(new Error(`Failed to invoke api path ${apiPath} on port ${port} with error ${error}`));
});
});
});
}
/**
* Cleanup leftover stacks and buckets
*/
public async dispose(success: boolean) {
// If the tests completed successfully, happily delete the fixture
// (otherwise leave it for humans to inspect)
if (success) {
rimraf(this.integTestDir);
}
}
}
export function randomInteger(min: number, max: number) {
return Math.floor(Math.random() * (max - min) + min);
}
/**
* A shell command that does what you want
*
* Is platform-aware, handles errors nicely.
*/
export async function shellWithAction(
command: string[],
filter?: string,
action?: () => Promise<any>,
options: ShellOptions = {},
actionTimeoutSeconds: number = 600,
): Promise<ActionOutput> {
if (options.modEnv && options.env) {
throw new Error('Use either env or modEnv but not both');
}
options.output?.write(`💻 ${command.join(' ')}\n`);
const env = options.env ?? (options.modEnv ? { ...process.env, ...options.modEnv } : undefined);
const child = child_process.spawn(command[0], command.slice(1), {
...options,
env,
// Need this for Windows where we want .cmd and .bat to be found as well.
shell: true,
stdio: ['ignore', 'pipe', 'pipe'],
});
return new Promise<ActionOutput>((resolve, reject) => {
const out = new Array<Buffer>();
const stdout = new Array<Buffer>();
const stderr = new Array<Buffer>();
let actionSucceeded = false;
let actionOutput: any;
let actionExecuted = false;
function executeAction(chunk: any) {
out.push(Buffer.from(chunk));
if (!actionExecuted && typeof filter === 'string' && Buffer.concat(out).toString('utf-8').includes(filter) && typeof action === 'function') {
actionExecuted = true;
options.output?.write('before executing action');
action().then((output) => {
options.output?.write(`action output is ${output}`);
actionOutput = output;
actionSucceeded = true;
}).catch((error) => {
options.output?.write(`action error is ${error}`);
actionSucceeded = false;
actionOutput = error;
}).finally(() => {
options.output?.write('terminate sam sub process');
killSubProcess(child, command.join(' '));
});
}
}
if (typeof filter === 'string' && typeof action === 'function') {
// Reject with an error if an action is configured, but the filter failed
// to show up in the output before the timeout occurred.
setTimeout(
() => {
if (!actionExecuted) {
reject(new Error(`Timed out waiting for filter ${JSON.stringify(filter)} to appear in command output after ${actionTimeoutSeconds} seconds\nOutput so far:\n${Buffer.concat(out).toString('utf-8')}`));
}
}, actionTimeoutSeconds * 1_000,
).unref();
}
child.stdout!.on('data', chunk => {
options.output?.write(chunk);
stdout.push(chunk);
executeAction(chunk);
});
child.stderr!.on('data', chunk => {
options.output?.write(chunk);
if (options.captureStderr ?? true) {
stderr.push(chunk);
}
executeAction(chunk);
});
child.once('error', reject);
child.once('close', code => {
const output = (Buffer.concat(stdout).toString('utf-8') + Buffer.concat(stderr).toString('utf-8')).trim();
if (code == null || code === 0 || options.allowErrExit) {
let result = new Array<string>();
result.push(actionOutput);
result.push(output);
resolve({
actionSucceeded: actionSucceeded,
actionOutput: actionOutput,
shellOutput: output,
});
} else {
reject(new Error(`'${command.join(' ')}' exited with error code ${code}. Output: \n${output}`));
}
});
});
}
function killSubProcess(child: child_process.ChildProcess, command: string) {
/**
* Check if the sub process is running in container, so child_process.spawn will
* create multiple processes, and to kill all of them we need to run different logic
*/
if (fs.existsSync('/.dockerenv')) {
child_process.exec(`for pid in $(ps -ef | grep "${command}" | awk '{print $2}'); do kill -2 $pid; done`);
} else {
child.kill('SIGINT');
}
}