@@ -952,13 +952,13 @@ func TestCreateInstance(t *testing.T) {
952
952
Filters : []* ec2.Filter {
953
953
filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
954
954
filter .EC2 .VPC ("vpc-id" ),
955
- filter .EC2 .AvailabilityZone ("us-east-1b" ),
956
955
{Name : aws .String ("tag:some-tag" ), Values : aws .StringSlice ([]string {"some-value" })},
957
956
},
958
957
}).
959
958
Return (& ec2.DescribeSubnetsOutput {
960
959
Subnets : []* ec2.Subnet {{
961
- SubnetId : aws .String ("filtered-subnet-1" ),
960
+ SubnetId : aws .String ("filtered-subnet-1" ),
961
+ AvailabilityZone : aws .String ("us-east-1b" ),
962
962
}},
963
963
}, nil )
964
964
m .
@@ -1053,6 +1053,20 @@ func TestCreateInstance(t *testing.T) {
1053
1053
},
1054
1054
},
1055
1055
expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1056
+ m .
1057
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1058
+ Filters : []* ec2.Filter {
1059
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1060
+ filter .EC2 .VPC ("vpc-id" ),
1061
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"matching-subnet" })},
1062
+ },
1063
+ }).
1064
+ Return (& ec2.DescribeSubnetsOutput {
1065
+ Subnets : []* ec2.Subnet {{
1066
+ SubnetId : aws .String ("matching-subnet" ),
1067
+ AvailabilityZone : aws .String ("us-east-1b" ),
1068
+ }},
1069
+ }, nil )
1056
1070
m .
1057
1071
RunInstances (gomock .Any ()).
1058
1072
Return (& ec2.Reservation {
@@ -1093,7 +1107,7 @@ func TestCreateInstance(t *testing.T) {
1093
1107
},
1094
1108
},
1095
1109
{
1096
- name : "with subnet ID that does not belong to Cluster " ,
1110
+ name : "with subnet ID that does not exist " ,
1097
1111
machine : clusterv1.Machine {
1098
1112
ObjectMeta : metav1.ObjectMeta {
1099
1113
Labels : map [string ]string {"set" : "node" },
@@ -1145,9 +1159,20 @@ func TestCreateInstance(t *testing.T) {
1145
1159
},
1146
1160
},
1147
1161
expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1162
+ m .
1163
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1164
+ Filters : []* ec2.Filter {
1165
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1166
+ filter .EC2 .VPC ("vpc-id" ),
1167
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"non-matching-subnet" })},
1168
+ },
1169
+ }).
1170
+ Return (& ec2.DescribeSubnetsOutput {
1171
+ Subnets : []* ec2.Subnet {},
1172
+ }, nil )
1148
1173
},
1149
1174
check : func (instance * infrav1.Instance , err error ) {
1150
- expectedErrMsg := "failed to run machine \" aws-test1\" , subnet with id \" non- matching-subnet \" not found "
1175
+ expectedErrMsg := "failed to run machine \" aws-test1\" , no subnets available matching criteria "
1151
1176
if err == nil {
1152
1177
t .Fatalf ("Expected error, but got nil" )
1153
1178
}
@@ -1157,6 +1182,111 @@ func TestCreateInstance(t *testing.T) {
1157
1182
}
1158
1183
},
1159
1184
},
1185
+ {
1186
+ name : "with subnet ID that does not belong to Cluster" ,
1187
+ machine : clusterv1.Machine {
1188
+ ObjectMeta : metav1.ObjectMeta {
1189
+ Labels : map [string ]string {"set" : "node" },
1190
+ },
1191
+ Spec : clusterv1.MachineSpec {
1192
+ Bootstrap : clusterv1.Bootstrap {
1193
+ DataSecretName : pointer .StringPtr ("bootstrap-data" ),
1194
+ },
1195
+ },
1196
+ },
1197
+ machineConfig : & infrav1.AWSMachineSpec {
1198
+ AMI : infrav1.AMIReference {
1199
+ ID : aws .String ("abc" ),
1200
+ },
1201
+ InstanceType : "m5.large" ,
1202
+ Subnet : & infrav1.AWSResourceReference {
1203
+ ID : aws .String ("matching-subnet" ),
1204
+ },
1205
+ },
1206
+ awsCluster : & infrav1.AWSCluster {
1207
+ ObjectMeta : metav1.ObjectMeta {Name : "test" },
1208
+ Spec : infrav1.AWSClusterSpec {
1209
+ NetworkSpec : infrav1.NetworkSpec {
1210
+ VPC : infrav1.VPCSpec {
1211
+ ID : "vpc-id" ,
1212
+ },
1213
+ Subnets : infrav1.Subnets {{
1214
+ ID : "subnet-1" ,
1215
+ }},
1216
+ },
1217
+ },
1218
+ Status : infrav1.AWSClusterStatus {
1219
+ Network : infrav1.NetworkStatus {
1220
+ SecurityGroups : map [infrav1.SecurityGroupRole ]infrav1.SecurityGroup {
1221
+ infrav1 .SecurityGroupControlPlane : {
1222
+ ID : "1" ,
1223
+ },
1224
+ infrav1 .SecurityGroupNode : {
1225
+ ID : "2" ,
1226
+ },
1227
+ infrav1 .SecurityGroupLB : {
1228
+ ID : "3" ,
1229
+ },
1230
+ },
1231
+ APIServerELB : infrav1.ClassicELB {
1232
+ DNSName : "test-apiserver.us-east-1.aws" ,
1233
+ },
1234
+ },
1235
+ },
1236
+ },
1237
+ expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1238
+ m .
1239
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1240
+ Filters : []* ec2.Filter {
1241
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1242
+ filter .EC2 .VPC ("vpc-id" ),
1243
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"matching-subnet" })},
1244
+ },
1245
+ }).
1246
+ Return (& ec2.DescribeSubnetsOutput {
1247
+ Subnets : []* ec2.Subnet {{
1248
+ SubnetId : aws .String ("matching-subnet" ),
1249
+ }},
1250
+ }, nil )
1251
+ m .
1252
+ RunInstances (gomock .Any ()).
1253
+ Return (& ec2.Reservation {
1254
+ Instances : []* ec2.Instance {
1255
+ {
1256
+ State : & ec2.InstanceState {
1257
+ Name : aws .String (ec2 .InstanceStateNamePending ),
1258
+ },
1259
+ IamInstanceProfile : & ec2.IamInstanceProfile {
1260
+ Arn : aws .String ("arn:aws:iam::123456789012:instance-profile/foo" ),
1261
+ },
1262
+ InstanceId : aws .String ("two" ),
1263
+ InstanceType : aws .String ("m5.large" ),
1264
+ SubnetId : aws .String ("matching-subnet" ),
1265
+ ImageId : aws .String ("ami-1" ),
1266
+ RootDeviceName : aws .String ("device-1" ),
1267
+ BlockDeviceMappings : []* ec2.InstanceBlockDeviceMapping {
1268
+ {
1269
+ DeviceName : aws .String ("device-1" ),
1270
+ Ebs : & ec2.EbsInstanceBlockDevice {
1271
+ VolumeId : aws .String ("volume-1" ),
1272
+ },
1273
+ },
1274
+ },
1275
+ Placement : & ec2.Placement {
1276
+ AvailabilityZone : & az ,
1277
+ },
1278
+ },
1279
+ },
1280
+ }, nil )
1281
+ m .WaitUntilInstanceRunningWithContext (gomock .Any (), gomock .Any (), gomock .Any ()).
1282
+ Return (nil )
1283
+ },
1284
+ check : func (instance * infrav1.Instance , err error ) {
1285
+ if err != nil {
1286
+ t .Fatalf ("did not expect error: %v" , err )
1287
+ }
1288
+ },
1289
+ },
1160
1290
{
1161
1291
name : "subnet id and failureDomain don't match" ,
1162
1292
machine : clusterv1.Machine {
@@ -1212,9 +1342,23 @@ func TestCreateInstance(t *testing.T) {
1212
1342
},
1213
1343
},
1214
1344
expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1345
+ m .
1346
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1347
+ Filters : []* ec2.Filter {
1348
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1349
+ filter .EC2 .VPC ("vpc-id" ),
1350
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"subnet-1" })},
1351
+ },
1352
+ }).
1353
+ Return (& ec2.DescribeSubnetsOutput {
1354
+ Subnets : []* ec2.Subnet {{
1355
+ SubnetId : aws .String ("subnet-1" ),
1356
+ AvailabilityZone : aws .String ("us-west-1b" ),
1357
+ }},
1358
+ }, nil )
1215
1359
},
1216
1360
check : func (instance * infrav1.Instance , err error ) {
1217
- expectedErrMsg := "subnet's availability zone \" us-west-1b\" does not match with the failure domain \" us-east-1b\" "
1361
+ expectedErrMsg := "failed to run machine \" aws-test1 \" , found 1 subnets matching criteria but post-filtering failed. subnet \" subnet-1 \" availability zone \" us-west-1b\" does not match failure domain \" us-east-1b\" "
1218
1362
if err == nil {
1219
1363
t .Fatalf ("Expected error, but got nil" )
1220
1364
}
@@ -1345,6 +1489,21 @@ func TestCreateInstance(t *testing.T) {
1345
1489
},
1346
1490
},
1347
1491
expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1492
+ m .
1493
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1494
+ Filters : []* ec2.Filter {
1495
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1496
+ filter .EC2 .VPC ("vpc-id" ),
1497
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"public-subnet-1" })},
1498
+ },
1499
+ }).
1500
+ Return (& ec2.DescribeSubnetsOutput {
1501
+ Subnets : []* ec2.Subnet {{
1502
+ SubnetId : aws .String ("public-subnet-1" ),
1503
+ AvailabilityZone : aws .String ("us-east-1b" ),
1504
+ MapPublicIpOnLaunch : aws .Bool (true ),
1505
+ }},
1506
+ }, nil )
1348
1507
m .
1349
1508
RunInstances (gomock .Any ()).
1350
1509
Return (& ec2.Reservation {
@@ -1439,9 +1598,24 @@ func TestCreateInstance(t *testing.T) {
1439
1598
},
1440
1599
},
1441
1600
expect : func (m * mock_ec2iface.MockEC2APIMockRecorder ) {
1601
+ m .
1602
+ DescribeSubnets (& ec2.DescribeSubnetsInput {
1603
+ Filters : []* ec2.Filter {
1604
+ filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1605
+ filter .EC2 .VPC ("vpc-id" ),
1606
+ {Name : aws .String ("subnet-id" ), Values : aws .StringSlice ([]string {"private-subnet-1" })},
1607
+ },
1608
+ }).
1609
+ Return (& ec2.DescribeSubnetsOutput {
1610
+ Subnets : []* ec2.Subnet {{
1611
+ SubnetId : aws .String ("private-subnet-1" ),
1612
+ AvailabilityZone : aws .String ("us-east-1b" ),
1613
+ MapPublicIpOnLaunch : aws .Bool (false ),
1614
+ }},
1615
+ }, nil )
1442
1616
},
1443
1617
check : func (instance * infrav1.Instance , err error ) {
1444
- expectedErrMsg := "failed to run machine \" aws-test1\" with public IP, a specified subnet \" private-subnet-1\" is a private subnet"
1618
+ expectedErrMsg := "failed to run machine \" aws-test1\" , found 1 subnets matching criteria but post-filtering failed. subnet \" private-subnet-1\" is a private subnet. "
1445
1619
if err == nil {
1446
1620
t .Fatalf ("Expected error, but got nil" )
1447
1621
}
@@ -1520,13 +1694,13 @@ func TestCreateInstance(t *testing.T) {
1520
1694
Filters : []* ec2.Filter {
1521
1695
filter .EC2 .SubnetStates (ec2 .SubnetStatePending , ec2 .SubnetStateAvailable ),
1522
1696
filter .EC2 .VPC ("vpc-id" ),
1523
- {Name : aws .String ("map-public-ip-on-launch" ), Values : aws .StringSlice ([]string {"true" })},
1524
1697
{Name : aws .String ("tag:some-tag" ), Values : aws .StringSlice ([]string {"some-value" })},
1525
1698
},
1526
1699
}).
1527
1700
Return (& ec2.DescribeSubnetsOutput {
1528
1701
Subnets : []* ec2.Subnet {{
1529
- SubnetId : aws .String ("filtered-subnet-1" ),
1702
+ SubnetId : aws .String ("filtered-subnet-1" ),
1703
+ MapPublicIpOnLaunch : aws .Bool (true ),
1530
1704
}},
1531
1705
}, nil )
1532
1706
m .
0 commit comments