mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): apply Tags, DNS attributes and InstanceTenancy on AWS::EC2::VPC (#5224)
* fix(cloudformation): apply Tags, DNS attributes and InstanceTenancy on AWS::EC2::VPC Ec2VpcCfnProvisioner read only CidrBlock, so a VPC from a template had no tags and DescribeVpcs with a tag:Name filter (CDK Vpc.from_lookup) never found it. EnableDnsSupport, EnableDnsHostnames and InstanceTenancy were dropped as well. Tags are now reconciled through Ec2Tags on create and on update (keys removed from the template are deleted), the DNS attributes are applied with the AWS defaults when omitted, and InstanceTenancy is passed to CreateVpc, with the dedicated-to-default change applied in place through a new modifyVpcTenancy and any other change treated as a replacement. CreateVpc on the EC2 Query API now honours InstanceTenancy too so the two paths agree. Fn::GetAtt gains CidrBlockAssociations, DefaultNetworkAcl and Ipv6CidrBlocks. * fix(cloudformation): validate VPC tenancy and treat blank DNS properties as omitted EnableDnsSupport/EnableDnsHostnames resolving blank (Fn::If with AWS::NoValue) now take the AWS default instead of false. CreateVpc rejects an InstanceTenancy outside default/dedicated/host with InvalidParameterValue, and ModifyVpcTenancy requires the VPC to be dedicated, as on AWS; the provisioner replaces on any other tenancy change. The integration test reads outputs with XmlPath, and the query-handler test stub follows the new createVpc overload. * style(cloudformation): no final on the VPC local; compare tag sets unordered in the test * fix(ec2): CreateVpc accepts default or dedicated tenancy only * fix(ec2): share the enumerated-parameter check for VPC tenancy and cover it on the Query API requireMetadataOptionValue becomes requireParameterValue and requireVpcTenancy uses it, so the InvalidParameterValue wording lives in one place. A Query API test sends InstanceTenancy to CreateVpc and reads it back from DescribeVpcs, the provisioner test no longer turns DNS hostnames on with DNS support off, and the CreateVpc docs row names the accepted tenancies. * fix(ec2): only an absent InstanceTenancy defaults; order the test static imports
This commit is contained in:
1 parent
34c11b8981
commit
2cade5cd21
10 files changed
+501
-34
No files matched your search
+148
@@ -0,0 +1,148 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@code AWS::EC2::VPC} applies Tags, EnableDnsHostnames/EnableDnsSupport and InstanceTenancy
|
||||
* to the VPC it creates, so DescribeVpcs with a {@code tag:Name} filter (what CDK's
|
||||
* {@code Vpc.fromLookup} issues) finds it, and an UpdateStack replaces the tag set in place.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class CloudFormationVpcPropertiesIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/eu-west-3/cloudformation/aws4_request";
|
||||
private static final String EC2_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/eu-west-3/ec2/aws4_request";
|
||||
|
||||
private static String template(String name, String extraTags) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Vpc": {
|
||||
"Type": "AWS::EC2::VPC",
|
||||
"Properties": {
|
||||
"CidrBlock": "10.44.0.0/16",
|
||||
"EnableDnsHostnames": true,
|
||||
"EnableDnsSupport": true,
|
||||
"InstanceTenancy": "dedicated",
|
||||
"Tags": [{"Key": "Name", "Value": "%s"}%s]
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"VpcId": {"Value": {"Ref": "Vpc"}},
|
||||
"Acl": {"Value": {"Fn::GetAtt": ["Vpc", "DefaultNetworkAcl"]}},
|
||||
"Assoc": {"Value": {"Fn::GetAtt": ["Vpc", "CidrBlockAssociations"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(name, extraTags);
|
||||
}
|
||||
|
||||
@Test
|
||||
void vpcTagsAndAttributesAreAppliedOnCreateAndReplacedOnUpdate() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String stackName = "cfn-vpc-props-" + suffix;
|
||||
String name = "lookup-vpc-" + suffix;
|
||||
|
||||
cfn("CreateStack", stackName, template(name, ", {\"Key\": \"team\", \"Value\": \"blue\"}"))
|
||||
.statusCode(200);
|
||||
String described = describeStacks(stackName, "CREATE_COMPLETE");
|
||||
String vpcId = output(described, "VpcId");
|
||||
assertTrue(vpcId.startsWith("vpc-"), "stack output should carry the vpc id: " + vpcId);
|
||||
assertTrue(output(described, "Acl").startsWith("acl-"), "DefaultNetworkAcl should resolve: " + described);
|
||||
assertTrue(output(described, "Assoc").startsWith("vpc-cidr-assoc-"),
|
||||
"CidrBlockAssociations should resolve: " + described);
|
||||
|
||||
given()
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeVpcs")
|
||||
.formParam("Filter.1.Name", "tag:Name")
|
||||
.formParam("Filter.1.Value.1", name)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcsResponse.vpcSet.item.vpcId", equalTo(vpcId))
|
||||
.body("DescribeVpcsResponse.vpcSet.item.instanceTenancy", equalTo("dedicated"))
|
||||
.body("DescribeVpcsResponse.vpcSet.item.tagSet.item.find { it.key == 'team' }.value", equalTo("blue"));
|
||||
|
||||
given()
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeVpcAttribute")
|
||||
.formParam("VpcId", vpcId)
|
||||
.formParam("Attribute", "enableDnsHostnames")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcAttributeResponse.enableDnsHostnames.value", equalTo("true"));
|
||||
|
||||
// Update: Name changes, team is dropped. CloudFormation replaces the tag set.
|
||||
cfn("UpdateStack", stackName, template(name + "-v2", "")).statusCode(200);
|
||||
describeStacks(stackName, "UPDATE_COMPLETE");
|
||||
|
||||
given()
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeVpcs")
|
||||
.formParam("Filter.1.Name", "tag:Name")
|
||||
.formParam("Filter.1.Value.1", name + "-v2")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeVpcsResponse.vpcSet.item.vpcId", equalTo(vpcId))
|
||||
.body(not(containsString("<key>team</key>")));
|
||||
|
||||
given()
|
||||
.header("Authorization", EC2_AUTH)
|
||||
.formParam("Action", "DescribeVpcs")
|
||||
.formParam("Filter.1.Name", "tag:Name")
|
||||
.formParam("Filter.1.Value.1", name)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString(vpcId)));
|
||||
}
|
||||
|
||||
private static ValidatableResponse cfn(String action, String stackName, String template) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", stackName)
|
||||
.formParam("TemplateBody", template)
|
||||
.when()
|
||||
.post("/")
|
||||
.then();
|
||||
}
|
||||
|
||||
private static String describeStacks(String stackName, String expectedStatus) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", stackName)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
private static String output(String describeStacksXml, String key) {
|
||||
return XmlPath.from(describeStacksXml).getString("DescribeStacksResponse.DescribeStacksResult.Stacks.member"
|
||||
+ ".Outputs.member.find { it.OutputKey == '" + key + "' }.OutputValue");
|
||||
}
|
||||
}
|
||||
+108
-12
@@ -5,18 +5,26 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.ec2.Ec2Service;
|
||||
import io.github.hectorvent.floci.services.ec2.model.NetworkAcl;
|
||||
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Tag;
|
||||
import io.github.hectorvent.floci.services.ec2.model.Vpc;
|
||||
import io.github.hectorvent.floci.services.ec2.model.VpcCidrBlockAssociation;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatcher;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -60,6 +68,17 @@ class Ec2VpcCfnProvisionerTest {
|
||||
return v;
|
||||
}
|
||||
|
||||
/** Matches a tag list by its {@code key=value} pairs as a set; {@link Tag} has no equals. */
|
||||
private static ArgumentMatcher<List<Tag>> tags(String... expected) {
|
||||
return actual -> actual != null
|
||||
&& actual.stream().map(t -> t.getKey() + "=" + t.getValue()).collect(Collectors.toSet())
|
||||
.equals(Set.of(expected));
|
||||
}
|
||||
|
||||
private void stubCreate(Vpc created) {
|
||||
when(ec2.createVpc(anyString(), anyString(), anyBoolean(), anyBoolean(), anyString())).thenReturn(created);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provisions the way the engine does: an update arrives with the prior id on both the context
|
||||
* and the resource, so a provisioner reading the wrong one still looks correct here. The
|
||||
@@ -82,12 +101,20 @@ class Ec2VpcCfnProvisionerTest {
|
||||
|
||||
@Test
|
||||
void refIsTheVpcIdAndGetAttExposesTheDocumentedAttributes() {
|
||||
when(ec2.createVpc(anyString(), anyString(), anyBoolean())).thenReturn(vpc("vpc-new", "10.0.0.0/16"));
|
||||
Vpc created = vpc("vpc-new", "10.0.0.0/16");
|
||||
created.getCidrBlockAssociationSet().add(new VpcCidrBlockAssociation("vpc-cidr-assoc-1", "10.0.0.0/16"));
|
||||
stubCreate(created);
|
||||
SecurityGroup def = new SecurityGroup();
|
||||
def.setGroupId("sg-default");
|
||||
def.setVpcId("vpc-new");
|
||||
when(ec2.describeSecurityGroups(REGION, List.of(), List.of("default"), Map.of()))
|
||||
.thenReturn(List.of(def));
|
||||
NetworkAcl acl = new NetworkAcl();
|
||||
acl.setNetworkAclId("acl-default");
|
||||
acl.setVpcId("vpc-new");
|
||||
acl.setDefault(true);
|
||||
when(ec2.describeNetworkAcls(REGION, List.of(), Map.of("vpc-id", List.of("vpc-new"), "default", List.of("true"))))
|
||||
.thenReturn(List.of(acl));
|
||||
|
||||
StackResource r = provision(null, """
|
||||
{"CidrBlock": "10.0.0.0/16"}""");
|
||||
@@ -95,20 +122,69 @@ class Ec2VpcCfnProvisionerTest {
|
||||
assertEquals("vpc-new", r.getPhysicalId());
|
||||
assertEquals("vpc-new", r.getAttributes().get("VpcId"));
|
||||
assertEquals("10.0.0.0/16", r.getAttributes().get("CidrBlock"));
|
||||
assertEquals("vpc-cidr-assoc-1", r.getAttributes().get("CidrBlockAssociations"));
|
||||
assertEquals("sg-default", r.getAttributes().get("DefaultSecurityGroup"));
|
||||
assertEquals("acl-default", r.getAttributes().get("DefaultNetworkAcl"));
|
||||
assertEquals("", r.getAttributes().get("Ipv6CidrBlocks"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnchangedVpcIsReusedInsteadOfCreatedAgain() {
|
||||
when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
|
||||
.thenReturn(List.of(vpc("vpc-existing", "10.0.0.0/16")));
|
||||
void omittedDnsPropertiesApplyTheAwsDefaults() {
|
||||
stubCreate(vpc("vpc-new", "10.0.0.0/16"));
|
||||
|
||||
StackResource r = provision("vpc-existing", """
|
||||
provision(null, """
|
||||
{"CidrBlock": "10.0.0.0/16"}""");
|
||||
|
||||
verify(ec2, never()).createVpc(anyString(), anyString(), anyBoolean());
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false, false, "default");
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsSupport", "true");
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsHostnames", "false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void dnsPropertiesResolvingBlankApplyTheAwsDefaults() {
|
||||
// Fn::If with AWS::NoValue renders as "" through the engine; Boolean.parseBoolean("") would
|
||||
// otherwise silently switch DnsSupport off.
|
||||
stubCreate(vpc("vpc-new", "10.0.0.0/16"));
|
||||
|
||||
provision(null, """
|
||||
{"CidrBlock": "10.0.0.0/16", "EnableDnsSupport": "", "EnableDnsHostnames": ""}""");
|
||||
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsSupport", "true");
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsHostnames", "false");
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagsDnsSettingsAndTenancyFromTheTemplateAreApplied() {
|
||||
stubCreate(vpc("vpc-new", "10.0.0.0/16"));
|
||||
|
||||
provision(null, """
|
||||
{"CidrBlock": "10.0.0.0/16", "EnableDnsHostnames": true, "EnableDnsSupport": "true",
|
||||
"InstanceTenancy": "dedicated",
|
||||
"Tags": [{"Key": "Name", "Value": "my-vpc"}, {"Key": "env", "Value": "dev"}]}""");
|
||||
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false, false, "dedicated");
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsHostnames", "true");
|
||||
verify(ec2).modifyVpcAttribute(REGION, "vpc-new", "enableDnsSupport", "true");
|
||||
verify(ec2).createTags(eq(REGION), eq(List.of("vpc-new")), argThat(tags("Name=my-vpc", "env=dev")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnchangedVpcIsReusedAndItsTagSetReplaced() {
|
||||
when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
|
||||
.thenReturn(List.of(vpc("vpc-existing", "10.0.0.0/16")));
|
||||
when(ec2.describeTags(REGION, Map.of("resource-id", List.of("vpc-existing")))).thenReturn(List.of(
|
||||
Map.of("key", "Name", "value", "old"),
|
||||
Map.of("key", "stale", "value", "x")));
|
||||
|
||||
StackResource r = provision("vpc-existing", """
|
||||
{"CidrBlock": "10.0.0.0/16", "Tags": [{"Key": "Name", "Value": "new"}]}""");
|
||||
|
||||
verify(ec2, never()).createVpc(anyString(), anyString(), anyBoolean(), anyBoolean(), any());
|
||||
verify(ec2, never()).modifyVpcTenancy(anyString(), anyString(), anyString());
|
||||
assertEquals("vpc-existing", r.getPhysicalId(),
|
||||
"reusing must keep the id every subnet and route table already references");
|
||||
verify(ec2).deleteTags(eq(REGION), eq(List.of("vpc-existing")), argThat(tags("stale=x")));
|
||||
verify(ec2).createTags(eq(REGION), eq(List.of("vpc-existing")), argThat(tags("Name=new")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -142,18 +218,38 @@ class Ec2VpcCfnProvisionerTest {
|
||||
void aChangedCidrBlockCreatesAReplacement() {
|
||||
when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of()))
|
||||
.thenReturn(List.of(vpc("vpc-existing", "10.0.0.0/16")));
|
||||
when(ec2.createVpc(anyString(), anyString(), anyBoolean())).thenReturn(vpc("vpc-replaced", "10.1.0.0/16"));
|
||||
stubCreate(vpc("vpc-replaced", "10.1.0.0/16"));
|
||||
|
||||
StackResource r = provision("vpc-existing", """
|
||||
{"CidrBlock": "10.1.0.0/16"}""");
|
||||
|
||||
verify(ec2).createVpc(REGION, "10.1.0.0/16", false);
|
||||
verify(ec2).createVpc(REGION, "10.1.0.0/16", false, false, "default");
|
||||
assertEquals("vpc-replaced", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tenancyDedicatedToDefaultIsAppliedInPlaceAnyOtherChangeReplaces() {
|
||||
Vpc dedicated = vpc("vpc-existing", "10.0.0.0/16");
|
||||
dedicated.setInstanceTenancy("dedicated");
|
||||
when(ec2.describeVpcs(REGION, List.of("vpc-existing"), Map.of())).thenReturn(List.of(dedicated));
|
||||
|
||||
StackResource kept = provision("vpc-existing", """
|
||||
{"CidrBlock": "10.0.0.0/16", "InstanceTenancy": "default"}""");
|
||||
assertEquals("vpc-existing", kept.getPhysicalId());
|
||||
verify(ec2).modifyVpcTenancy(REGION, "vpc-existing", "default");
|
||||
|
||||
// Now a default VPC asked to become dedicated: the only in-place direction is dedicated->default.
|
||||
dedicated.setInstanceTenancy("default");
|
||||
stubCreate(vpc("vpc-replaced", "10.0.0.0/16"));
|
||||
StackResource replaced = provision("vpc-existing", """
|
||||
{"CidrBlock": "10.0.0.0/16", "InstanceTenancy": "dedicated"}""");
|
||||
assertEquals("vpc-replaced", replaced.getPhysicalId());
|
||||
verify(ec2).createVpc(eq(REGION), eq("10.0.0.0/16"), eq(false), eq(false), eq("dedicated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void anIdOnTheResourceAloneDoesNotCountAsAnUpdate() {
|
||||
when(ec2.createVpc(anyString(), anyString(), anyBoolean())).thenReturn(vpc("vpc-new", "10.0.0.0/16"));
|
||||
stubCreate(vpc("vpc-new", "10.0.0.0/16"));
|
||||
|
||||
// A resource carrying a physical id under a create context: what provision() itself
|
||||
// produces the moment it assigns the new id. Reading create-vs-update off the resource
|
||||
@@ -162,7 +258,7 @@ class Ec2VpcCfnProvisionerTest {
|
||||
{"CidrBlock": "10.0.0.0/16"}""");
|
||||
|
||||
verify(ec2, never()).describeVpcs(anyString(), any(), any());
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false);
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false, false, "default");
|
||||
assertEquals("vpc-new", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@@ -170,12 +266,12 @@ class Ec2VpcCfnProvisionerTest {
|
||||
void aVpcDeletedOutOfBandFallsBackToCreate() {
|
||||
when(ec2.describeVpcs(REGION, List.of("vpc-gone"), Map.of()))
|
||||
.thenReturn(List.of());
|
||||
when(ec2.createVpc(anyString(), anyString(), anyBoolean())).thenReturn(vpc("vpc-fresh", "10.0.0.0/16"));
|
||||
stubCreate(vpc("vpc-fresh", "10.0.0.0/16"));
|
||||
|
||||
StackResource r = provision("vpc-gone", """
|
||||
{"CidrBlock": "10.0.0.0/16"}""");
|
||||
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false);
|
||||
verify(ec2).createVpc(REGION, "10.0.0.0/16", false, false, "default");
|
||||
assertEquals("vpc-fresh", r.getPhysicalId());
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* CreateVpc's InstanceTenancy on the Query API: {@code default} or {@code dedicated} is stored and
|
||||
* read back by DescribeVpcs; omitting it means {@code default}; {@code host} is refused, as the
|
||||
* EC2 reference says ("The host value cannot be used with this parameter").
|
||||
*/
|
||||
@QuarkusTest
|
||||
class Ec2CreateVpcInstanceTenancyIntegrationTest {
|
||||
|
||||
private final List<String> createdVpcs = new ArrayList<>();
|
||||
|
||||
private static ValidatableResponse ec2(String action, String... formParams) {
|
||||
RequestSpecification request = given().header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=test/20261007/us-east-1/ec2/aws4_request")
|
||||
.formParam("Action", action);
|
||||
for (int i = 0; i < formParams.length; i += 2) {
|
||||
request.formParam(formParams[i], formParams[i + 1]);
|
||||
}
|
||||
return request.when().post("/").then();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteCreatedVpcs() {
|
||||
for (String vpcId : createdVpcs) {
|
||||
ec2("DeleteVpc", "VpcId", vpcId).statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void dedicatedTenancyIsStoredAndDescribed() {
|
||||
String vpcId = createVpc("10.97.0.0/16", "InstanceTenancy", "dedicated");
|
||||
ec2("DescribeVpcs", "VpcId.1", vpcId).statusCode(200)
|
||||
.body("DescribeVpcsResponse.vpcSet.item.instanceTenancy", equalTo("dedicated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void omittedTenancyIsDefault() {
|
||||
String vpcId = createVpc("10.98.0.0/16");
|
||||
ec2("DescribeVpcs", "VpcId.1", vpcId).statusCode(200)
|
||||
.body("DescribeVpcsResponse.vpcSet.item.instanceTenancy", equalTo("default"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void hostTenancyIsRejected() {
|
||||
ec2("CreateVpc", "CidrBlock", "10.99.0.0/16", "InstanceTenancy", "host").statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"))
|
||||
.body("Response.Errors.Error.Message",
|
||||
equalTo("Value (host) for parameter instanceTenancy is invalid. "
|
||||
+ "Valid values are: default, dedicated."));
|
||||
}
|
||||
|
||||
private String createVpc(String cidr, String... extraParams) {
|
||||
String[] params = new String[2 + extraParams.length];
|
||||
params[0] = "CidrBlock";
|
||||
params[1] = cidr;
|
||||
System.arraycopy(extraParams, 0, params, 2, extraParams.length);
|
||||
String vpcId = ec2("CreateVpc", params).statusCode(200)
|
||||
.extract().xmlPath().getString("CreateVpcResponse.vpc.vpcId");
|
||||
createdVpcs.add(vpcId);
|
||||
return vpcId;
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@ class Ec2QueryHandlerTest {
|
||||
Ec2Service service = mock(Ec2Service.class);
|
||||
Vpc vpc = new Vpc();
|
||||
vpc.setVpcId("vpc-test");
|
||||
when(service.createVpc("us-east-1", "10.38.0.0/16", false, false)).thenReturn(vpc);
|
||||
when(service.createVpc("us-east-1", "10.38.0.0/16", false, false, null)).thenReturn(vpc);
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("CidrBlock", "10.38.0.0/16");
|
||||
params.putSingle("TagSpecification.1.ResourceType", "vpc");
|
||||
|
||||
@@ -141,6 +141,52 @@ class Ec2ServiceTest {
|
||||
() -> service.requireVpc(region, unknown)).getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createVpcAcceptsDefaultOrDedicatedAndDefaultsWhenOmitted() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
String region = "us-east-1";
|
||||
|
||||
assertEquals("default", service.createVpc(region, "10.90.0.0/16", false, false, null).getInstanceTenancy());
|
||||
// A present blank is not an enum value; only an absent parameter defaults.
|
||||
assertEquals("InvalidParameterValue", assertThrows(AwsException.class,
|
||||
() -> service.createVpc(region, "10.91.0.0/16", false, false, " ")).getErrorCode());
|
||||
assertEquals("dedicated", service.createVpc(region, "10.92.0.0/16", false, false, "dedicated").getInstanceTenancy());
|
||||
|
||||
AwsException bogus = assertThrows(AwsException.class,
|
||||
() -> service.createVpc(region, "10.93.0.0/16", false, false, "bogus"));
|
||||
assertEquals("InvalidParameterValue", bogus.getErrorCode());
|
||||
assertEquals(400, bogus.getHttpStatus());
|
||||
assertTrue(bogus.getMessage().contains("Value (bogus) for parameter instanceTenancy is invalid"));
|
||||
|
||||
// The EC2 reference: "The host value cannot be used with this parameter."
|
||||
AwsException host = assertThrows(AwsException.class,
|
||||
() -> service.createVpc(region, "10.96.0.0/16", false, false, "host"));
|
||||
assertEquals("InvalidParameterValue", host.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyVpcTenancyOnlyMovesADedicatedVpcToDefault() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
String region = "us-east-1";
|
||||
String dedicated = service.createVpc(region, "10.94.0.0/16", false, false, "dedicated").getVpcId();
|
||||
String alreadyDefault = service.createVpc(region, "10.95.0.0/16", false, false, "default").getVpcId();
|
||||
|
||||
service.modifyVpcTenancy(region, dedicated, "default");
|
||||
assertEquals("default", service.requireVpc(region, dedicated).getInstanceTenancy());
|
||||
|
||||
AwsException fromDefault = assertThrows(AwsException.class,
|
||||
() -> service.modifyVpcTenancy(region, alreadyDefault, "default"));
|
||||
assertEquals("InvalidParameterValue", fromDefault.getErrorCode());
|
||||
|
||||
AwsException toDedicated = assertThrows(AwsException.class,
|
||||
() -> service.modifyVpcTenancy(region, dedicated, "dedicated"));
|
||||
assertEquals("InvalidParameterValue", toDedicated.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteVpcRemovesVpcOwnedDefaultResourcesAndRules() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
Reference in new issue
Block a user