@@ -25,35 +25,17 @@ export = nodeunit.testCase({
25
25
const filter = { foo : 'bar' } ;
26
26
const provider = new VpcNetworkContextProviderPlugin ( mockSDK ) ;
27
27
28
- AWS . mock ( 'EC2' , 'describeVpcs' , ( params : aws . EC2 . DescribeVpcsRequest , cb : AwsCallback < aws . EC2 . DescribeVpcsResult > ) => {
29
- test . deepEqual ( params . Filters , [ { Name : 'foo' , Values : [ 'bar' ] } ] ) ;
30
- return cb ( null , { Vpcs : [ { VpcId : 'vpc-1234567' } ] } ) ;
31
- } ) ;
32
- AWS . mock ( 'EC2' , 'describeSubnets' , ( params : aws . EC2 . DescribeSubnetsRequest , cb : AwsCallback < aws . EC2 . DescribeSubnetsResult > ) => {
33
- test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ 'vpc-1234567' ] } ] ) ;
34
- return cb ( null , {
35
- Subnets : [
36
- { SubnetId : 'sub-123456' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : true } ,
37
- { SubnetId : 'sub-789012' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : false }
38
- ]
39
- } ) ;
40
- } ) ;
41
- AWS . mock ( 'EC2' , 'describeRouteTables' , ( params : aws . EC2 . DescribeRouteTablesRequest , cb : AwsCallback < aws . EC2 . DescribeRouteTablesResult > ) => {
42
- test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ 'vpc-1234567' ] } ] ) ;
43
- return cb ( null , {
44
- RouteTables : [
45
- { Associations : [ { SubnetId : 'sub-123456' } ] , RouteTableId : 'rtb-123456' , } ,
46
- { Associations : [ { SubnetId : 'sub-789012' } ] , RouteTableId : 'rtb-789012' , }
47
- ]
48
- } ) ;
49
- } ) ;
50
- AWS . mock ( 'EC2' , 'describeVpnGateways' , ( params : aws . EC2 . DescribeVpnGatewaysRequest , cb : AwsCallback < aws . EC2 . DescribeVpnGatewaysResult > ) => {
51
- test . deepEqual ( params . Filters , [
52
- { Name : 'attachment.vpc-id' , Values : [ 'vpc-1234567' ] } ,
53
- { Name : 'attachment.state' , Values : [ 'attached' ] } ,
54
- { Name : 'state' , Values : [ 'available' ] }
55
- ] ) ;
56
- return cb ( null , { VpnGateways : [ { VpnGatewayId : 'gw-abcdef' } ] } ) ;
28
+ mockVpcLookup ( test , {
29
+ subnets : [
30
+ { SubnetId : 'sub-123456' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : true } ,
31
+ { SubnetId : 'sub-789012' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : false }
32
+ ] ,
33
+ routeTables : [
34
+ { Associations : [ { SubnetId : 'sub-123456' } ] , RouteTableId : 'rtb-123456' , } ,
35
+ { Associations : [ { SubnetId : 'sub-789012' } ] , RouteTableId : 'rtb-789012' , }
36
+ ] ,
37
+ vpnGateways : [ { VpnGatewayId : 'gw-abcdef' } ]
38
+
57
39
} ) ;
58
40
59
41
// WHEN
@@ -75,8 +57,8 @@ export = nodeunit.testCase({
75
57
vpnGatewayId : 'gw-abcdef'
76
58
} ) ;
77
59
78
- test . done ( ) ;
79
60
AWS . restore ( ) ;
61
+ test . done ( ) ;
80
62
} ,
81
63
82
64
async 'throws when no such VPC is found' ( test : nodeunit . Test ) {
@@ -97,8 +79,8 @@ export = nodeunit.testCase({
97
79
test . throws ( ( ) => { throw e ; } , / C o u l d n o t f i n d a n y V P C s m a t c h i n g / ) ;
98
80
}
99
81
100
- test . done ( ) ;
101
82
AWS . restore ( ) ;
83
+ test . done ( ) ;
102
84
} ,
103
85
104
86
async 'throws when multiple VPCs are found' ( test : nodeunit . Test ) {
@@ -119,44 +101,25 @@ export = nodeunit.testCase({
119
101
test . throws ( ( ) => { throw e ; } , / F o u n d 2 V P C s m a t c h i n g / ) ;
120
102
}
121
103
122
- test . done ( ) ;
123
104
AWS . restore ( ) ;
105
+ test . done ( ) ;
124
106
} ,
125
107
126
108
async 'uses the VPC main route table when a subnet has no specific association' ( test : nodeunit . Test ) {
127
109
// GIVEN
128
110
const filter = { foo : 'bar' } ;
129
111
const provider = new VpcNetworkContextProviderPlugin ( mockSDK ) ;
130
112
131
- AWS . mock ( 'EC2' , 'describeVpcs' , ( params : aws . EC2 . DescribeVpcsRequest , cb : AwsCallback < aws . EC2 . DescribeVpcsResult > ) => {
132
- test . deepEqual ( params . Filters , [ { Name : 'foo' , Values : [ 'bar' ] } ] ) ;
133
- return cb ( null , { Vpcs : [ { VpcId : 'vpc-1234567' } ] } ) ;
134
- } ) ;
135
- AWS . mock ( 'EC2' , 'describeSubnets' , ( params : aws . EC2 . DescribeSubnetsRequest , cb : AwsCallback < aws . EC2 . DescribeSubnetsResult > ) => {
136
- test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ 'vpc-1234567' ] } ] ) ;
137
- return cb ( null , {
138
- Subnets : [
139
- { SubnetId : 'sub-123456' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : true } ,
140
- { SubnetId : 'sub-789012' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : false }
141
- ]
142
- } ) ;
143
- } ) ;
144
- AWS . mock ( 'EC2' , 'describeRouteTables' , ( params : aws . EC2 . DescribeRouteTablesRequest , cb : AwsCallback < aws . EC2 . DescribeRouteTablesResult > ) => {
145
- test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ 'vpc-1234567' ] } ] ) ;
146
- return cb ( null , {
147
- RouteTables : [
148
- { Associations : [ { SubnetId : 'sub-123456' } ] , RouteTableId : 'rtb-123456' , } ,
149
- { Associations : [ { Main : true } ] , RouteTableId : 'rtb-789012' , }
150
- ]
151
- } ) ;
152
- } ) ;
153
- AWS . mock ( 'EC2' , 'describeVpnGateways' , ( params : aws . EC2 . DescribeVpnGatewaysRequest , cb : AwsCallback < aws . EC2 . DescribeVpnGatewaysResult > ) => {
154
- test . deepEqual ( params . Filters , [
155
- { Name : 'attachment.vpc-id' , Values : [ 'vpc-1234567' ] } ,
156
- { Name : 'attachment.state' , Values : [ 'attached' ] } ,
157
- { Name : 'state' , Values : [ 'available' ] }
158
- ] ) ;
159
- return cb ( null , { VpnGateways : [ { VpnGatewayId : 'gw-abcdef' } ] } ) ;
113
+ mockVpcLookup ( test , {
114
+ subnets : [
115
+ { SubnetId : 'sub-123456' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : true } ,
116
+ { SubnetId : 'sub-789012' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : false }
117
+ ] ,
118
+ routeTables : [
119
+ { Associations : [ { SubnetId : 'sub-123456' } ] , RouteTableId : 'rtb-123456' , } ,
120
+ { Associations : [ { Main : true } ] , RouteTableId : 'rtb-789012' , }
121
+ ] ,
122
+ vpnGateways : [ { VpnGatewayId : 'gw-abcdef' } ]
160
123
} ) ;
161
124
162
125
// WHEN
@@ -180,5 +143,99 @@ export = nodeunit.testCase({
180
143
181
144
test . done ( ) ;
182
145
AWS . restore ( ) ;
183
- }
146
+ } ,
147
+
148
+ async 'Recognize public subnet by route table' ( test : nodeunit . Test ) {
149
+ // GIVEN
150
+ const filter = { foo : 'bar' } ;
151
+ const provider = new VpcNetworkContextProviderPlugin ( mockSDK ) ;
152
+
153
+ mockVpcLookup ( test , {
154
+ subnets : [
155
+ { SubnetId : 'sub-123456' , AvailabilityZone : 'bermuda-triangle-1337' , MapPublicIpOnLaunch : false } ,
156
+ ] ,
157
+ routeTables : [
158
+ {
159
+ Associations : [ { SubnetId : 'sub-123456' } ] ,
160
+ RouteTableId : 'rtb-123456' ,
161
+ Routes : [
162
+ {
163
+ DestinationCidrBlock : "10.0.2.0/26" ,
164
+ Origin : "CreateRoute" ,
165
+ State : "active" ,
166
+ VpcPeeringConnectionId : "pcx-xxxxxx"
167
+ } ,
168
+ {
169
+ DestinationCidrBlock : "10.0.1.0/24" ,
170
+ GatewayId : "local" ,
171
+ Origin : "CreateRouteTable" ,
172
+ State : "active"
173
+ } ,
174
+ {
175
+ DestinationCidrBlock : "0.0.0.0/0" ,
176
+ GatewayId : "igw-xxxxxx" ,
177
+ Origin : "CreateRoute" ,
178
+ State : "active"
179
+ }
180
+ ] ,
181
+ } ,
182
+ ] ,
183
+ } ) ;
184
+
185
+ // WHEN
186
+ const result = await provider . getValue ( { filter } ) ;
187
+
188
+ // THEN
189
+ test . deepEqual ( result , {
190
+ vpcId : 'vpc-1234567' ,
191
+ availabilityZones : [ 'bermuda-triangle-1337' ] ,
192
+ isolatedSubnetIds : undefined ,
193
+ isolatedSubnetNames : undefined ,
194
+ isolatedSubnetRouteTableIds : undefined ,
195
+ privateSubnetIds : undefined ,
196
+ privateSubnetNames : undefined ,
197
+ privateSubnetRouteTableIds : undefined ,
198
+ publicSubnetIds : [ 'sub-123456' ] ,
199
+ publicSubnetNames : [ 'Public' ] ,
200
+ publicSubnetRouteTableIds : [ 'rtb-123456' ] ,
201
+ vpnGatewayId : undefined ,
202
+ } ) ;
203
+
204
+ AWS . restore ( ) ;
205
+ test . done ( ) ;
206
+ } ,
184
207
} ) ;
208
+
209
+ interface VpcLookupOptions {
210
+ subnets : aws . EC2 . Subnet [ ] ;
211
+ routeTables : aws . EC2 . RouteTable [ ] ;
212
+ vpnGateways ?: aws . EC2 . VpnGateway [ ] ;
213
+ }
214
+
215
+ function mockVpcLookup ( test : nodeunit . Test , options : VpcLookupOptions ) {
216
+ const VpcId = 'vpc-1234567' ;
217
+
218
+ AWS . mock ( 'EC2' , 'describeVpcs' , ( params : aws . EC2 . DescribeVpcsRequest , cb : AwsCallback < aws . EC2 . DescribeVpcsResult > ) => {
219
+ test . deepEqual ( params . Filters , [ { Name : 'foo' , Values : [ 'bar' ] } ] ) ;
220
+ return cb ( null , { Vpcs : [ { VpcId } ] } ) ;
221
+ } ) ;
222
+
223
+ AWS . mock ( 'EC2' , 'describeSubnets' , ( params : aws . EC2 . DescribeSubnetsRequest , cb : AwsCallback < aws . EC2 . DescribeSubnetsResult > ) => {
224
+ test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ VpcId ] } ] ) ;
225
+ return cb ( null , { Subnets : options . subnets } ) ;
226
+ } ) ;
227
+
228
+ AWS . mock ( 'EC2' , 'describeRouteTables' , ( params : aws . EC2 . DescribeRouteTablesRequest , cb : AwsCallback < aws . EC2 . DescribeRouteTablesResult > ) => {
229
+ test . deepEqual ( params . Filters , [ { Name : 'vpc-id' , Values : [ VpcId ] } ] ) ;
230
+ return cb ( null , { RouteTables : options . routeTables } ) ;
231
+ } ) ;
232
+
233
+ AWS . mock ( 'EC2' , 'describeVpnGateways' , ( params : aws . EC2 . DescribeVpnGatewaysRequest , cb : AwsCallback < aws . EC2 . DescribeVpnGatewaysResult > ) => {
234
+ test . deepEqual ( params . Filters , [
235
+ { Name : 'attachment.vpc-id' , Values : [ VpcId ] } ,
236
+ { Name : 'attachment.state' , Values : [ 'attached' ] } ,
237
+ { Name : 'state' , Values : [ 'available' ] }
238
+ ] ) ;
239
+ return cb ( null , { VpnGateways : options . vpnGateways } ) ;
240
+ } ) ;
241
+ }
0 commit comments