Skip to content

Commit

Permalink
fix(ec2): Vpc.fromLookup() does not work in unit tests
Browse files Browse the repository at this point in the history
The dummy AZ names used in `Vpc.fromLookup()` did not match the dummy
AZ names in `Stack.availabilityZones`, so using them together would
fail.

Fixes #6045.
  • Loading branch information
rix0rrr committed Apr 29, 2020
1 parent 07fe642 commit e869a0d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/lib/instance.ts
Expand Up @@ -291,7 +291,7 @@ export class Instance extends Resource implements IInstance {
if (selected.length === 1) {
subnet = selected[0];
} else {
throw new Error('When specifying AZ there has to be exactly one subnet of the given type in this az');
throw new Error(`Need exactly 1 subnet to match AZ '${props.availabilityZone}', found ${selected.length}. Use a different availabilityZone.`);
}
} else {
subnet = subnets[0];
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Expand Up @@ -1910,13 +1910,13 @@ const DUMMY_VPC_PROPS: cxapi.VpcContextResponse = {
type: cxapi.VpcSubnetGroupType.PUBLIC,
subnets: [
{
availabilityZone: 'dummy-1a',
availabilityZone: 'dummy1a',
subnetId: 's-12345',
routeTableId: 'rtb-12345s',
cidr: '1.2.3.4/5',
},
{
availabilityZone: 'dummy-1b',
availabilityZone: 'dummy1b',
subnetId: 's-67890',
routeTableId: 'rtb-67890s',
cidr: '1.2.3.4/5',
Expand All @@ -1928,13 +1928,13 @@ const DUMMY_VPC_PROPS: cxapi.VpcContextResponse = {
type: cxapi.VpcSubnetGroupType.PRIVATE,
subnets: [
{
availabilityZone: 'dummy-1a',
availabilityZone: 'dummy1a',
subnetId: 'p-12345',
routeTableId: 'rtb-12345p',
cidr: '1.2.3.4/5',
},
{
availabilityZone: 'dummy-1b',
availabilityZone: 'dummy1b',
subnetId: 'p-67890',
routeTableId: 'rtb-57890p',
cidr: '1.2.3.4/5',
Expand Down
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/test.vpc.from-lookup.ts
Expand Up @@ -172,6 +172,22 @@ export = {
restoreContextProvider(previous);
test.done();
},

'AZ in dummy lookup VPC matches AZ in Stack'(test: Test) {
// GIVEN
const stack = new Stack(undefined, 'MyTestStack', { env: { account: '1234567890', region: 'dummy' } });
const vpc = Vpc.fromLookup(stack, 'vpc', { isDefault: true });

// WHEN
const subnets = vpc.selectSubnets({
availabilityZones: stack.availabilityZones,
});

// THEN
test.equals(subnets.subnets.length, 2);

test.done();
},
},
};

Expand Down

0 comments on commit e869a0d

Please sign in to comment.