Skip to content

Commit

Permalink
additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kailey Chen committed Oct 3, 2024
1 parent 9f60103 commit 7222bf4
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions packages/@aws-cdk/aws-ec2-alpha/test/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,36 @@ describe('EC2 Routing', () => {
});
});

test('CIDR block overlap should throw error', () => {
test('Default region handling for same account peering', () => {
const vpc1 = new vpc.VpcV2(stack, 'VPC1', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.1.0.0/16'),
});

const vpc2 = new vpc.VpcV2(stack, 'VPC2', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.2.0.0/16'),
});

new route.VPCPeeringConnection(stack, 'TestPeering', {
isCrossAccount: false,
requestorVpc: vpc1,
acceptorVpc: vpc2,
});

const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::EC2::VPCPeeringConnection', {
VpcId: {
'Fn::GetAtt': ['VPC17DE2CF87', 'VpcId'],
},
PeerVpcId: {
'Fn::GetAtt': ['VPC2C1F0E711', 'VpcId'],
},
PeerRegion: {
Ref: 'AWS::Region',
},
});
});

test('Overlap with primary CIDR block should throw error', () => {
const vpc1 = new vpc.VpcV2(stack, 'VPC1', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.0.0.0/16'),
});
Expand All @@ -564,7 +593,7 @@ describe('EC2 Routing', () => {
}).toThrow(/CIDR block should not overlap with existing subnet blocks/);
});

test('CIDR block overlap with secondary CIDR block should throw error', () => {
test('Overlap with secondary CIDR block should throw error', () => {
const vpc1 = new vpc.VpcV2(stack, 'VPC1', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.0.0.0/16'),
secondaryAddressBlocks: [vpc.IpAddresses.ipv4('10.1.0.0/16', { cidrBlockName: 'Temp Block' })],
Expand All @@ -584,9 +613,9 @@ describe('EC2 Routing', () => {
}).toThrow(/CIDR block should not overlap with existing subnet blocks/);
});

test('Non overlapping CIDR blocks should succeed', () => {
test('Overlap with primary and secondary CIDR block should throw error', () => {
const vpc1 = new vpc.VpcV2(stack, 'VPC1', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.0.0.0/16'),
primaryAddressBlock: vpc.IpAddresses.ipv4('10.3.0.0/16'),
secondaryAddressBlocks: [vpc.IpAddresses.ipv4('10.1.0.0/16', { cidrBlockName: 'Temp Block' })],
});

Expand All @@ -601,36 +630,27 @@ describe('EC2 Routing', () => {
requestorVpc: vpc1,
acceptorVpc: vpc2,
});
}).not.toThrow();
}).toThrow(/CIDR block should not overlap with existing subnet blocks/);
});

test('Default region handling for same account peering', () => {
test('Non overlapping CIDR blocks should succeed', () => {
const vpc1 = new vpc.VpcV2(stack, 'VPC1', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.1.0.0/16'),
primaryAddressBlock: vpc.IpAddresses.ipv4('10.0.0.0/16'),
secondaryAddressBlocks: [vpc.IpAddresses.ipv4('10.1.0.0/16', { cidrBlockName: 'Temp Block' })],
});

const vpc2 = new vpc.VpcV2(stack, 'VPC2', {
primaryAddressBlock: vpc.IpAddresses.ipv4('10.2.0.0/16'),
secondaryAddressBlocks: [vpc.IpAddresses.ipv4('10.3.0.0/16', { cidrBlockName: 'Temp Block' })],
});

new route.VPCPeeringConnection(stack, 'TestPeering', {
isCrossAccount: false,
requestorVpc: vpc1,
acceptorVpc: vpc2,
});

const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::EC2::VPCPeeringConnection', {
VpcId: {
'Fn::GetAtt': ['VPC17DE2CF87', 'VpcId'],
},
PeerVpcId: {
'Fn::GetAtt': ['VPC2C1F0E711', 'VpcId'],
},
PeerRegion: {
Ref: 'AWS::Region', // CDK resolves this as the stack region
},
});
expect(() => {
new route.VPCPeeringConnection(stack, 'TestPeering', {
isCrossAccount: false,
requestorVpc: vpc1,
acceptorVpc: vpc2,
});
}).not.toThrow();
});
});
});

0 comments on commit 7222bf4

Please sign in to comment.