Skip to content

Commit 6d3176d

Browse files
author
Leon Michalski
committed
Fix tests
1 parent a6d6422 commit 6d3176d

File tree

3 files changed

+38
-156
lines changed

3 files changed

+38
-156
lines changed

packages/aws-cdk-lib/aws-lambda/test/function.test.ts

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5154,76 +5154,6 @@ describe('L1 Relationships', () => {
51545154
});
51555155
});
51565156

5157-
it('nested union', () => {
5158-
const stack = new cdk.Stack();
5159-
const role = new iam.Role(stack, 'SomeRole', {
5160-
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
5161-
});
5162-
const bucket = new s3.Bucket(stack, 'MyBucket');
5163-
5164-
new lambda.CfnFunction(stack, 'MyLambda', {
5165-
code: {
5166-
s3Bucket: bucket, // Nested union
5167-
},
5168-
role: role,
5169-
});
5170-
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
5171-
Properties: {
5172-
Role: { 'Fn::GetAtt': ['SomeRole6DDC54DD', 'Arn'] },
5173-
Code: { S3Bucket: { Ref: 'MyBucketF68F3FF0' } },
5174-
},
5175-
});
5176-
});
5177-
5178-
it('deeply nested union', () => {
5179-
const stack = new cdk.Stack();
5180-
const topic = new sns.CfnTopic(stack, 'Topic');
5181-
5182-
new lambda.CfnEventInvokeConfig(stack, 'EventConfig', {
5183-
functionName: 'myFunction',
5184-
qualifier: '$LATEST',
5185-
destinationConfig: {
5186-
onFailure: {
5187-
destination: topic, // Deeply nested: destinationConfig -> onFailure -> destination (union)
5188-
},
5189-
},
5190-
});
5191-
Template.fromStack(stack).hasResource('AWS::Lambda::EventInvokeConfig', {
5192-
Properties: {
5193-
DestinationConfig: {
5194-
OnFailure: {
5195-
Destination: { Ref: 'Topic' },
5196-
},
5197-
},
5198-
},
5199-
});
5200-
});
5201-
5202-
it('nested array of unions', () => {
5203-
const stack = new cdk.Stack();
5204-
const role = new iam.Role(stack, 'SomeRole', {
5205-
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
5206-
});
5207-
const securityGroup = new ec2.SecurityGroup(stack, 'SG', {
5208-
vpc: new ec2.Vpc(stack, 'VPC'),
5209-
});
5210-
new lambda.CfnFunction(stack, 'MyLambda', {
5211-
code: { zipFile: 'foo' },
5212-
role: role,
5213-
vpcConfig: {
5214-
securityGroupIds: [securityGroup], // Nested array of union
5215-
},
5216-
});
5217-
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
5218-
Properties: {
5219-
Role: { 'Fn::GetAtt': ['SomeRole6DDC54DD', 'Arn'] },
5220-
VpcConfig: {
5221-
SecurityGroupIds: [{ 'Fn::GetAtt': ['SGADB53937', 'GroupId'] }],
5222-
},
5223-
},
5224-
});
5225-
});
5226-
52275157
it('array reference should be valid', () => {
52285158
const stack = new cdk.Stack();
52295159
const role = new iam.Role(stack, 'SomeRole', {
@@ -5250,35 +5180,6 @@ describe('L1 Relationships', () => {
52505180
});
52515181
});
52525182

5253-
it('nested array references should still be valid', () => {
5254-
const stack = new cdk.Stack();
5255-
const role = new iam.Role(stack, 'SomeRole', {
5256-
assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
5257-
});
5258-
const securityGroup = new ec2.SecurityGroup(stack, 'SG', {
5259-
vpc: new ec2.Vpc(stack, 'VPC'),
5260-
});
5261-
const vpcConfig = {
5262-
securityGroupIds: [securityGroup, 'securityGroupArn2'],
5263-
};
5264-
new lambda.CfnFunction(stack, 'MyLambda', {
5265-
code: { zipFile: 'foo' },
5266-
role: role,
5267-
vpcConfig,
5268-
});
5269-
5270-
vpcConfig.securityGroupIds.push('securityGroupArn3');
5271-
5272-
Template.fromStack(stack).hasResource('AWS::Lambda::Function', {
5273-
Properties: {
5274-
Role: { 'Fn::GetAtt': ['SomeRole6DDC54DD', 'Arn'] },
5275-
VpcConfig: {
5276-
SecurityGroupIds: [{ 'Fn::GetAtt': ['SGADB53937', 'GroupId'] }, 'securityGroupArn2', 'securityGroupArn3'],
5277-
},
5278-
},
5279-
});
5280-
});
5281-
52825183
it('tokens should be passed as is', () => {
52835184
const stack = new cdk.Stack();
52845185
const role = new iam.Role(stack, 'SomeRole', {

tools/@aws-cdk/spec2cdk/test/__snapshots__/relationships.test.ts.snap

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
4444
return ret;
4545
}
4646
47+
public static arnForRole(resource: IRoleRef): string {
48+
return resource.roleRef.roleArn;
49+
}
50+
4751
/**
4852
* @cloudformationAttribute RoleArn
4953
*/
@@ -189,7 +193,7 @@ export class CfnResource extends cdk.CfnResource implements cdk.IInspectable, IR
189193
properties: props
190194
});
191195
192-
this.permissions = (cdk.isResolvableObject(props.permissions) ? props.permissions : (!props.permissions ? undefined : props.permissions.forEach((item: any, i: number, arr: any[]) => { arr[i] = flattenCfnResourcePermissionProperty(item) }), props.permissions));
196+
this.permissions = props.permissions;
193197
}
194198
195199
public get resourceRef(): ResourceReference {
@@ -227,18 +231,10 @@ export namespace CfnResource {
227231
/**
228232
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-resource-permission.html#cfn-iam-resource-permission-rolearn
229233
*/
230-
readonly roleArn?: IRoleRef | string;
234+
readonly roleArn?: string;
231235
}
232236
}
233237
234-
// @ts-ignore TS6133
235-
function flattenCfnResourcePermissionProperty(props: cdk.IResolvable | CfnResource.PermissionProperty): cdk.IResolvable | CfnResource.PermissionProperty {
236-
if (cdk.isResolvableObject(props)) return props;
237-
return {
238-
roleArn: (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | string")
239-
};
240-
}
241-
242238
/**
243239
* Determine whether the given properties match those of a \`PermissionProperty\`
244240
*
@@ -385,6 +381,10 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
385381
return ret;
386382
}
387383
384+
public static arnForRole(resource: IRoleRef): string {
385+
return resource.roleRef.roleArn;
386+
}
387+
388388
/**
389389
* @cloudformationAttribute RoleArn
390390
*/
@@ -515,6 +515,10 @@ export class CfnUser extends cdk.CfnResource implements cdk.IInspectable, IUserR
515515
return ret;
516516
}
517517
518+
public static arnForUser(resource: IUserRef): string {
519+
return resource.userRef.userArn;
520+
}
521+
518522
/**
519523
* @cloudformationAttribute UserArn
520524
*/
@@ -792,6 +796,10 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
792796
return ret;
793797
}
794798
799+
public static arnForRole(resource: IRoleRef): string {
800+
return resource.roleRef.roleArn;
801+
}
802+
795803
/**
796804
* @cloudformationAttribute RoleArn
797805
*/
@@ -937,7 +945,7 @@ export class CfnTask extends cdk.CfnResource implements cdk.IInspectable, ITaskR
937945
properties: props
938946
});
939947
940-
this.executionConfig = (!props.executionConfig ? undefined : flattenCfnTaskExecutionConfigProperty(props.executionConfig));
948+
this.executionConfig = props.executionConfig;
941949
}
942950
943951
public get taskRef(): TaskReference {
@@ -975,18 +983,10 @@ export namespace CfnTask {
975983
/**
976984
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-task-executionconfig.html#cfn-iam-task-executionconfig-rolearn
977985
*/
978-
readonly roleArn?: IRoleRef | string;
986+
readonly roleArn?: string;
979987
}
980988
}
981989
982-
// @ts-ignore TS6133
983-
function flattenCfnTaskExecutionConfigProperty(props: CfnTask.ExecutionConfigProperty | cdk.IResolvable): CfnTask.ExecutionConfigProperty | cdk.IResolvable {
984-
if (cdk.isResolvableObject(props)) return props;
985-
return {
986-
roleArn: (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | string")
987-
};
988-
}
989-
990990
/**
991991
* Determine whether the given properties match those of a \`ExecutionConfigProperty\`
992992
*
@@ -1132,6 +1132,10 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
11321132
return ret;
11331133
}
11341134
1135+
public static arnForRole(resource: IRoleRef): string {
1136+
return resource.roleRef.roleArn;
1137+
}
1138+
11351139
/**
11361140
* @cloudformationAttribute RoleArn
11371141
*/
@@ -1277,7 +1281,7 @@ export class CfnJob extends cdk.CfnResource implements cdk.IInspectable, IJobRef
12771281
properties: props
12781282
});
12791283
1280-
this.config = (!props.config ? undefined : flattenCfnJobOldConfigProperty(props.config));
1284+
this.config = props.config;
12811285
}
12821286
12831287
public get jobRef(): JobReference {
@@ -1315,7 +1319,7 @@ export namespace CfnJob {
13151319
/**
13161320
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-job-config.html#cfn-iam-job-config-rolearn
13171321
*/
1318-
readonly roleArn?: IRoleRef | string;
1322+
readonly roleArn?: string;
13191323
13201324
/**
13211325
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-job-config.html#cfn-iam-job-config-timeout
@@ -1332,19 +1336,10 @@ export namespace CfnJob {
13321336
/**
13331337
* @see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iam-job-oldconfig.html#cfn-iam-job-oldconfig-rolearn
13341338
*/
1335-
readonly roleArn?: IRoleRef | string;
1339+
readonly roleArn?: string;
13361340
}
13371341
}
13381342
1339-
// @ts-ignore TS6133
1340-
function flattenCfnJobConfigProperty(props: CfnJob.ConfigProperty | cdk.IResolvable): CfnJob.ConfigProperty | cdk.IResolvable {
1341-
if (cdk.isResolvableObject(props)) return props;
1342-
return {
1343-
roleArn: (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | string"),
1344-
timeout: props.timeout
1345-
};
1346-
}
1347-
13481343
/**
13491344
* Determine whether the given properties match those of a \`ConfigProperty\`
13501345
*
@@ -1390,14 +1385,6 @@ function CfnJobConfigPropertyFromCloudFormation(properties: any): cfn_parse.From
13901385
return ret;
13911386
}
13921387
1393-
// @ts-ignore TS6133
1394-
function flattenCfnJobOldConfigProperty(props: cdk.IResolvable | CfnJob.OldConfigProperty): cdk.IResolvable | CfnJob.OldConfigProperty {
1395-
if (cdk.isResolvableObject(props)) return props;
1396-
return {
1397-
roleArn: (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | string")
1398-
};
1399-
}
1400-
14011388
/**
14021389
* Determine whether the given properties match those of a \`OldConfigProperty\`
14031390
*
@@ -1543,6 +1530,10 @@ export class CfnRole extends cdk.CfnResource implements cdk.IInspectable, IRoleR
15431530
return ret;
15441531
}
15451532
1533+
public static arnForRole(resource: IRoleRef): string {
1534+
return resource.roleRef.roleArn;
1535+
}
1536+
15461537
/**
15471538
* @cloudformationAttribute RoleArn
15481539
*/

tools/@aws-cdk/spec2cdk/test/relationships.test.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,10 @@ test('relationship have arns appear first in the constructor chain', () => {
293293
});
294294
db.link('hasResource', service, roleResource);
295295

296-
// Type definition with relationship
297-
const configType = db.allocate('typeDefinition', {
298-
name: 'ExecutionConfig',
296+
// Source resource with relationship
297+
const sourceResource = db.allocate('resource', {
298+
name: 'Function',
299+
attributes: {},
299300
properties: {
300301
RoleArn: {
301302
type: { type: 'string' },
@@ -311,26 +312,15 @@ test('relationship have arns appear first in the constructor chain', () => {
311312
}],
312313
},
313314
},
315+
cloudFormationType: 'AWS::IAM::Function',
314316
});
315317

316-
// Source resource with nested property
317-
const taskResource = db.allocate('resource', {
318-
name: 'Task',
319-
attributes: {},
320-
properties: {
321-
ExecutionConfig: {
322-
type: { type: 'ref', reference: { $ref: configType.$id } },
323-
},
324-
},
325-
cloudFormationType: 'AWS::IAM::Task',
326-
});
327-
db.link('hasResource', service, taskResource);
328-
db.link('usesType', taskResource, configType);
318+
db.link('hasResource', service, sourceResource);
329319

330320
const module = new AwsCdkLibBuilder({ db }).addService(service).resourcesMod.module;
331321

332322
const rendered = renderer.render(module);
333323

334-
const chain = 'roleArn: (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? (props.roleArn as IRoleRef)?.roleRef?.roleName ?? (props.roleArn as IRoleRef)?.roleRef?.otherPrimaryId ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | iam.IRoleRef | iam.IRoleRef | string")';
324+
const chain = 'this.roleArn = (props.roleArn as IRoleRef)?.roleRef?.roleArn ?? (props.roleArn as IRoleRef)?.roleRef?.roleName ?? (props.roleArn as IRoleRef)?.roleRef?.otherPrimaryId ?? cdk.ensureStringOrUndefined(props.roleArn, "roleArn", "iam.IRoleRef | iam.IRoleRef | iam.IRoleRef | string")';
335325
expect(rendered).toContain(chain);
336326
});

0 commit comments

Comments
 (0)