Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
badmintoncryer committed Dec 15, 2023
1 parent bc719de commit d1d7629
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ export class Cluster extends Resource implements ICluster {
* @param keyPattern Task id pattern
*/
public arnForTasks(keyPattern: string): string {
return this.clusterArn.replace(/cluster\/(.*)$/, `task/$1/${keyPattern}`);
return `arn:${Stack.of(this).partition}:ecs:${Stack.of(this).region}:${Stack.of(this).account}:task/${this.clusterName}/${keyPattern}`;
}

private configureWindowsAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) {
Expand Down
29 changes: 27 additions & 2 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { testDeprecated } from '@aws-cdk/cdk-build-tools';
import { Match, Template } from '../../assertions';
import * as autoscaling from '../../aws-autoscaling';
import * as ec2 from '../../aws-ec2';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import * as logs from '../../aws-logs';
import * as s3 from '../../aws-s3';
Expand Down Expand Up @@ -1092,10 +1093,34 @@ describe('cluster', () => {
const taskIdPattern = '*';

// WHEN
const taskArn = cluster.arnForTasks(taskIdPattern);
const policyStatement = new iam.PolicyStatement({
resources: [cluster.arnForTasks(taskIdPattern)],
actions: ['ecs:RunTask'],
principals: [new iam.ServicePrincipal('ecs.amazonaws.com')],
});

// THEN
expect(taskArn).toEqual(`arn:${stack.partition}:ecs:${stack.region}:${stack.account}:task/${cluster.clusterName}/${taskIdPattern}`);
expect(stack.resolve(policyStatement.toStatementJson())).toEqual({
Action: 'ecs:RunTask',
Effect: 'Allow',
Principal: { Service: 'ecs.amazonaws.com' },
Resource: {
'Fn::Join': [
'',
[
'arn:',
{ Ref: 'AWS::Partition' },
':ecs:',
{ Ref: 'AWS::Region' },
':',
{ Ref: 'AWS::AccountId' },
':task/',
{ Ref: 'EcsCluster97242B84' },
`/${taskIdPattern}`,
],
],
},
});
});

/*
Expand Down

0 comments on commit d1d7629

Please sign in to comment.