-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathdeploy-assert.ts
80 lines (66 loc) · 2.32 KB
/
deploy-assert.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
import { Stack } from '@aws-cdk/core';
import { Construct, IConstruct, Node } from 'constructs';
import { IApiCall } from '../api-call-base';
import { EqualsAssertion } from '../assertions';
import { ActualResult, ExpectedResult } from '../common';
import { md5hash } from '../private/hash';
import { AwsApiCall, LambdaInvokeFunction, LambdaInvokeFunctionProps } from '../sdk';
import { IDeployAssert } from '../types';
const DEPLOY_ASSERT_SYMBOL = Symbol.for('@aws-cdk/integ-tests.DeployAssert');
/**
* Options for DeployAssert
*/
export interface DeployAssertProps {
/**
* A stack to use for assertions
*
* @default - a stack is created for you
*/
readonly stack?: Stack
}
/**
* Construct that allows for registering a list of assertions
* that should be performed on a construct
*/
export class DeployAssert extends Construct implements IDeployAssert {
/**
* Returns whether the construct is a DeployAssert construct
*/
public static isDeployAssert(x: any): x is DeployAssert {
return x !== null && typeof (x) === 'object' && DEPLOY_ASSERT_SYMBOL in x;
}
/**
* Finds a DeployAssert construct in the given scope
*/
public static of(construct: IConstruct): DeployAssert {
const scopes = Node.of(Node.of(construct).root).findAll();
const deployAssert = scopes.find(s => DeployAssert.isDeployAssert(s));
if (!deployAssert) {
throw new Error('No DeployAssert construct found in scopes');
}
return deployAssert as DeployAssert;
}
public scope: Stack;
constructor(scope: Construct, props?: DeployAssertProps) {
super(scope, 'Default');
this.scope = props?.stack ?? new Stack(scope, 'DeployAssert');
Object.defineProperty(this, DEPLOY_ASSERT_SYMBOL, { value: true });
}
public awsApiCall(service: string, api: string, parameters?: any): IApiCall {
return new AwsApiCall(this.scope, `AwsApiCall${service}${api}`, {
api,
service,
parameters,
});
}
public invokeFunction(props: LambdaInvokeFunctionProps): IApiCall {
const hash = md5hash(this.scope.resolve(props));
return new LambdaInvokeFunction(this.scope, `LambdaInvoke${hash}`, props);
}
public expect(id: string, expected: ExpectedResult, actual: ActualResult): void {
new EqualsAssertion(this.scope, `EqualsAssertion${id}`, {
expected,
actual,
});
}
}