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 Mar 3, 2024
1 parent 416da4f commit 8ed03b8
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-efs/lib/efs-file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export class FileSystem extends FileSystemBase {
fileSystemId: props.replicationConfiguration.destinationFileSystem?.fileSystemId,
kmsKeyId: props.replicationConfiguration.kmsKey?.keyArn,
region: props.replicationConfiguration.region ??
props.replicationConfiguration.destinationFileSystem ? undefined : Stack.of(this).region,
(props.replicationConfiguration.destinationFileSystem ? undefined : Stack.of(this).region),
availabilityZoneName: props.replicationConfiguration.availabilityZone,
},
],
Expand Down
188 changes: 188 additions & 0 deletions packages/aws-cdk-lib/aws-efs/test/efs-file-system.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,191 @@ test.each([
},
});
});

describe('replication configuration', () => {
test('default settings', () => {
// WHEN
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EFS::FileSystem', {
ReplicationConfiguration: {
Destinations: [
{
Region: {
Ref: 'AWS::Region',
},
},
],
},
});
});

test('with destination file system', () => {
// WHEN
const destination = new FileSystem(stack, 'DestinationFileSystem', {
vpc,
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EFS::FileSystem', {
ReplicationConfiguration: {
Destinations: [
{
FileSystemId: {
Ref: 'DestinationFileSystem12545967',
},
},
],
},
});
});

test('with full settings', () => {
// WHEN
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
kmsKey: new kms.Key(stack, 'customKey'),
region: 'us-east-1',
availabilityZone: 'us-east-1a',
},
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EFS::FileSystem', {
ReplicationConfiguration: {
Destinations: [
{
Region: 'us-east-1',
AvailabilityZoneName: 'us-east-1a',
KmsKeyId: {
'Fn::GetAtt': [
'customKeyFEB2B57F',
'Arn',
],
},
},
],
},
});
});

test('throw error for read-only file system', () => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: true,
},
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});
}).toThrow('Cannot configure `replicationConfiguration` when `replicationOverwriteProtection` is set to `DISABLED`');
});

test.each([
{ region: 'us-east-1' },
{ availabilityZone: 'us-east-1a' },
])('throw error for specifing both destinationFileSystem and other parameters', (config) => {
// WHEN
const destination = new FileSystem(stack, 'DestinationFileSystem', {
vpc,
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});

// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
...config,
},
});
}).toThrow('Cannot configure `replicationConfiguration.region`, `replicationConfiguration.az` or `replicationConfiguration.kmsKey` when `replicationConfiguration.destinationFileSystem` is set');
});

test('throw error for specifing both destinationFileSystem and kmsKey', () => {
// WHEN
const destination = new FileSystem(stack, 'DestinationFileSystem', {
vpc,
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});

// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
destinationFileSystem: destination,
enable: true,
kmsKey: new kms.Key(stack, 'customKey'),
},
});
}).toThrow('Cannot configure `replicationConfiguration.region`, `replicationConfiguration.az` or `replicationConfiguration.kmsKey` when `replicationConfiguration.destinationFileSystem` is set');
});

test.each([
{ region: 'us-east-1' },
{ availabilityZone: 'us-east-1a' },
])('throw error when configure replication settings for replication disabled file system', (config) => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
...config,
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
});

test('throw error when configure kmsKey for replication disabled file system', () => {
// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
kmsKey: new kms.Key(stack, 'customKey'),
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
});

test('throw error when configure destinationFileSystem for replication disabled file system', () => {
// WHEN
const destination = new FileSystem(stack, 'DestinationFileSystem', {
vpc,
replicationOverwriteProtection: ReplicationOverwriteProtection.DISABLED,
});

// THEN
expect(() => {
new FileSystem(stack, 'EfsFileSystem', {
vpc,
replicationConfiguration: {
enable: false,
destinationFileSystem: destination,
},
});
}).toThrow('Cannot configure replication when `replicationConfiguration.enableReplication` is set to `false`');
});
});

0 comments on commit 8ed03b8

Please sign in to comment.