mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 09:22:49 -05:00
feat(ec2): support standalone network interface create/attach/detach (#2843)
* feat(ec2): support standalone network interface create/attach/detach
CreateNetworkInterface returned UnsupportedOperation unconditionally,
blocking every root that attaches a standalone ENI: the attach-eni and
override-default-eni examples in terraform-aws-server, the with-elb
example in terraform-aws-asg, and vpc-blackhole-routes in
terraform-aws-service-catalog (floci-kt9 in the floci-harness compat
survey).
Adds CreateNetworkInterface, DeleteNetworkInterface,
AttachNetworkInterface and DetachNetworkInterface, backed by a new
standalone ENI store separate from the implicit primary interface each
instance already carries. DescribeNetworkInterfaces now merges both
without double-counting. RunInstances also accepts a pre-existing
standalone ENI as an instance's primary interface via
NetworkInterface.1.NetworkInterfaceId, the shape Terraform's
network_interface { network_interface_id = ... } block sends for
override-default-eni.
* fix(ec2): keep a standalone ENI's own record when it is attached
Review found two ways the attachment state ended up recorded on only one side.
AttachNetworkInterface updated the standalone ENI and stopped there, so
DescribeNetworkInterfaces reported an attachment that DescribeInstances denied —
and the device-index conflict check, which reads the instance's own interface
list, could never see anything this operation had attached: two interfaces could
take device index 1 on the same instance. The instance now carries the interface
too, and DetachNetworkInterface removes it again.
RunInstances took the opposite approach and deleted the standalone record,
leaving the interface represented only inside the instance. Terminating that
instance then made a caller-created ENI vanish -- InvalidNetworkInterfaceID.
NotFound rather than returning to "available" -- so it could not be reused. AWS
attaches a pre-existing interface with deleteOnTermination false precisely so it
outlives the instance. The standalone record is now kept and marked attached,
and terminate releases it; describeNetworkInterfaces skips the instance-side
copy of any id the standalone store owns, so it is still reported exactly once,
from the side that knows its real attach time and its deleteOnTermination.
Also braces a single-line conditional, per AGENTS.md.
* fix(ec2): release an ENI whose instance died without TerminateInstances
TerminateInstances is not the only way an instance reaches "terminated": a
container-backed launch that fails or is cancelled ends there asynchronously,
inside Ec2ContainerManager, which has no access to the interface store. An ENI
the launch had already marked in-use therefore stayed pinned to an instance that
no longer runs, and every later attempt to reuse it failed with
InvalidNetworkInterface.InUse.
Rather than chase each terminal transition, the attachment is reconciled when
the interface is next read: an attachment naming an instance that is gone or
terminated is cleared, and the interface reports "available" again. That covers
attach, launch, delete and describe through the one path they share.
* fix(ec2): let a dead instance release its copy of a reclaimed ENI
Releasing the standalone record alone left the terminated instance still
reporting the interface, so once the ENI was reused two instance records claimed
it and nothing in DescribeInstances picks a winner. Reclaiming now clears both
sides, and the unit test asserts the instance no longer lists it.
Also drops the em-dashes from the comments and docs this branch adds, per
AGENTS.md.
* fix(ec2): drop the instance's copy on the TerminateInstances path too
The previous commit fixed only the reclaim-on-read path. TerminateInstances
released the standalone record while leaving the mirrored interface on the
terminated instance, so reusing the ENI still produced two instance records
claiming it. Both paths now clear the instance's copy, and the integration test
asserts DescribeInstances no longer mentions the interface before the same ENI
is handed to a second launch.
This commit is contained in:
1 parent
7828ab2430
commit
906af8c632
6 files changed
+1092
-66
No files matched your search
+547
@@ -0,0 +1,547 @@
|
||||
package io.github.hectorvent.floci.services.ec2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* Integration tests for standalone elastic network interfaces over the EC2 Query protocol,
|
||||
* floci-kt9: {@code CreateNetworkInterface} previously returned {@code UnsupportedOperation}
|
||||
* unconditionally, blocking every root that attaches a standalone ENI (the attach-eni and
|
||||
* override-default-eni patterns in terraform-aws-server/terraform-aws-asg/
|
||||
* terraform-aws-service-catalog).
|
||||
*
|
||||
* <p>Covers the full lifecycle the blocked roots exercise: create, describe (including the
|
||||
* pagination/filter surface DescribeNetworkInterfaces already had), attach to a running instance,
|
||||
* detach, and delete, plus RunInstances accepting a pre-existing standalone ENI as an instance's
|
||||
* primary interface (override-default-eni).
|
||||
*
|
||||
* <p>Ordered because the cases build on one ENI and one instance, mirroring how a client drives
|
||||
* the resources through their lifecycle. Runs in mock mode (floci.services.ec2.mock=true in test
|
||||
* application.yml) so no real Docker is required.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class Ec2NetworkInterfaceIntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request";
|
||||
|
||||
private static String subnetId;
|
||||
private static String securityGroupId;
|
||||
private static String eniId;
|
||||
private static String instanceId;
|
||||
private static String attachmentId;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void discoverDefaultSubnetAndSecurityGroup() {
|
||||
subnetId = given()
|
||||
.formParam("Action", "DescribeSubnets")
|
||||
.formParam("Filter.1.Name", "default-for-az")
|
||||
.formParam("Filter.1.Value.1", "true")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("DescribeSubnetsResponse.subnetSet.item[0].subnetId");
|
||||
|
||||
securityGroupId = given()
|
||||
.formParam("Action", "DescribeSecurityGroups")
|
||||
.formParam("Filter.1.Name", "group-name")
|
||||
.formParam("Filter.1.Value.1", "default")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("DescribeSecurityGroupsResponse.securityGroupInfo.item[0].groupId");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(subnetId.startsWith("subnet-"));
|
||||
org.junit.jupiter.api.Assertions.assertTrue(securityGroupId.startsWith("sg-"));
|
||||
}
|
||||
|
||||
// ─── Create ────────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createNetworkInterface() {
|
||||
eniId = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("Description", "attach-eni example ENI")
|
||||
.formParam("SecurityGroupId.1", securityGroupId)
|
||||
.formParam("TagSpecification.1.ResourceType", "network-interface")
|
||||
.formParam("TagSpecification.1.Tag.1.Key", "Name")
|
||||
.formParam("TagSpecification.1.Tag.1.Value", "example")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.subnetId", equalTo(subnetId))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.status", equalTo("available"))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.description", equalTo("attach-eni example ENI"))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.privateIpAddress", not(emptyOrNullString()))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.macAddress", not(emptyOrNullString()))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.groupSet.item.groupId", equalTo(securityGroupId))
|
||||
.body("CreateNetworkInterfaceResponse.networkInterface.tagSet.item.value", equalTo("example"))
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(eniId.startsWith("eni-"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void describeReturnsTheCreatedInterfaceAsAvailable() {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId", equalTo(eniId))
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status", equalTo("available"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void describeFiltersByNetworkInterfaceId() {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("Filter.1.Name", "network-interface-id")
|
||||
.formParam("Filter.1.Value.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId", equalTo(eniId));
|
||||
}
|
||||
|
||||
// ─── Attach / Detach (the attach-eni pattern) ────────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void launchAnInstanceToAttachTo() {
|
||||
instanceId = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(instanceId.startsWith("i-"));
|
||||
|
||||
// Mock mode settles a pending instance to "running" on the next describe (see
|
||||
// Ec2Service#describeInstances), AttachNetworkInterface requires running/stopped.
|
||||
given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", instanceId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.instanceState.name",
|
||||
equalTo("running"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void attachNetworkInterfaceToInstance() {
|
||||
attachmentId = given()
|
||||
.formParam("Action", "AttachNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", eniId)
|
||||
.formParam("InstanceId", instanceId)
|
||||
.formParam("DeviceIndex", "1")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AttachNetworkInterfaceResponse.attachmentId", not(emptyOrNullString()))
|
||||
.extract().path("AttachNetworkInterfaceResponse.attachmentId");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void describeShowsTheInterfaceAsInUseAndAttached() {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status", equalTo("in-use"))
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.attachment.attachmentId", equalTo(attachmentId))
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.attachment.instanceId", equalTo(instanceId))
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.attachment.deviceIndex", equalTo("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void attachingAnAlreadyAttachedInterfaceFails() {
|
||||
given()
|
||||
.formParam("Action", "AttachNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", eniId)
|
||||
.formParam("InstanceId", instanceId)
|
||||
.formParam("DeviceIndex", "2")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterface.InUse"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void deletingAnAttachedInterfaceFails() {
|
||||
given()
|
||||
.formParam("Action", "DeleteNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void detachNetworkInterface() {
|
||||
given()
|
||||
.formParam("Action", "DetachNetworkInterface")
|
||||
.formParam("AttachmentId", attachmentId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DetachNetworkInterfaceResponse.return", equalTo("true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void describeShowsTheInterfaceAvailableAgainAfterDetach() {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status", equalTo("available"))
|
||||
.body(not(containsString("<attachment>")));
|
||||
}
|
||||
|
||||
// ─── Delete ────────────────────────────────────────────────────────────────
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void deleteNetworkInterface() {
|
||||
given()
|
||||
.formParam("Action", "DeleteNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DeleteNetworkInterfaceResponse.return", equalTo("true"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void describeAfterDeleteReturnsNotFound() {
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.NotFound"));
|
||||
}
|
||||
|
||||
// ─── RunInstances with a pre-existing ENI (the override-default-eni pattern) ─
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void runInstancesAcceptsAPreExistingNetworkInterfaceAsThePrimary() {
|
||||
String overrideEniId = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("SecurityGroupId.1", securityGroupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
String overrideInstanceId = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("NetworkInterface.1.NetworkInterfaceId", overrideEniId)
|
||||
.formParam("NetworkInterface.1.DeviceIndex", "0")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("RunInstancesResponse.instancesSet.item.networkInterfaceSet.item.networkInterfaceId",
|
||||
equalTo(overrideEniId))
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(overrideInstanceId.startsWith("i-"));
|
||||
|
||||
// The interface keeps its own standalone record, that is the side that knows its real
|
||||
// attach time and its deleteOnTermination, while the instance carries a copy. Describe
|
||||
// reports it exactly once regardless, from the standalone record.
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", overrideEniId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()", equalTo(1))
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.attachment.instanceId",
|
||||
equalTo(overrideInstanceId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void runInstancesRejectsAPreExistingInterfaceWithMoreThanOneInstance() {
|
||||
String eni = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "2")
|
||||
.formParam("MaxCount", "2")
|
||||
.formParam("NetworkInterface.1.NetworkInterfaceId", eni)
|
||||
.formParam("NetworkInterface.1.DeviceIndex", "0")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An attachment has to be visible from both ends. Recording it only on the standalone ENI let
|
||||
* DescribeNetworkInterfaces report an attachment DescribeInstances denied, and left the
|
||||
* device-index conflict check, which reads the instance's own list, unable to see anything
|
||||
* this operation had attached.
|
||||
*/
|
||||
@Test
|
||||
@Order(16)
|
||||
void attachingAnInterfaceAlsoRecordsItOnTheInstance() {
|
||||
String eni = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("SecurityGroupId.1", securityGroupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
String host = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
// Mock mode settles a pending instance to "running" on the next describe; attach needs it.
|
||||
given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", host)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "AttachNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", eni)
|
||||
.formParam("InstanceId", host)
|
||||
.formParam("DeviceIndex", "1")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", host)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeInstancesResponse.reservationSet.item.instancesSet.item."
|
||||
+ "networkInterfaceSet.item.networkInterfaceId", hasItem(eni));
|
||||
|
||||
// ... and the device index it now occupies is refused to a second interface.
|
||||
String second = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "AttachNetworkInterface")
|
||||
.formParam("NetworkInterfaceId", second)
|
||||
.formParam("InstanceId", host)
|
||||
.formParam("DeviceIndex", "1")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
/**
|
||||
* An interface the caller created is not the instance's to destroy. AWS attaches it with
|
||||
* deleteOnTermination false, so terminating the instance returns it to "available" rather
|
||||
* than making it disappear, which is what a client that reuses one ENI across successive
|
||||
* instances depends on.
|
||||
*/
|
||||
@Test
|
||||
@Order(17)
|
||||
void terminatingTheInstanceReturnsAPreExistingInterfaceToAvailable() {
|
||||
String eni = given()
|
||||
.formParam("Action", "CreateNetworkInterface")
|
||||
.formParam("SubnetId", subnetId)
|
||||
.formParam("SecurityGroupId.1", securityGroupId)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("CreateNetworkInterfaceResponse.networkInterface.networkInterfaceId");
|
||||
|
||||
String host = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("NetworkInterface.1.NetworkInterfaceId", eni)
|
||||
.formParam("NetworkInterface.1.DeviceIndex", "0")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "TerminateInstances")
|
||||
.formParam("InstanceId.1", host)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// The terminated instance lets go of it too. Leaving it on that record would have two
|
||||
// instances claiming the interface once it is reused below.
|
||||
given()
|
||||
.formParam("Action", "DescribeInstances")
|
||||
.formParam("InstanceId.1", host)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString(eni)));
|
||||
|
||||
given()
|
||||
.formParam("Action", "DescribeNetworkInterfaces")
|
||||
.formParam("NetworkInterfaceId.1", eni)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.status",
|
||||
equalTo("available"));
|
||||
|
||||
// Free again, so it can be attached to a new instance, and this is the assertion that
|
||||
// proves the attachment is really gone, not just the status text: RunInstances resolves
|
||||
// the interface through takeNetworkInterfaceForLaunch, which refuses one that still
|
||||
// carries an attachment with InvalidNetworkInterface.InUse.
|
||||
String replacement = given()
|
||||
.formParam("Action", "RunInstances")
|
||||
.formParam("ImageId", "ami-amazonlinux2023")
|
||||
.formParam("InstanceType", "t2.micro")
|
||||
.formParam("MinCount", "1")
|
||||
.formParam("MaxCount", "1")
|
||||
.formParam("NetworkInterface.1.NetworkInterfaceId", eni)
|
||||
.formParam("NetworkInterface.1.DeviceIndex", "0")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertTrue(replacement.startsWith("i-"));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -57,7 +57,7 @@ class Ec2RunInstancesPublicIpParamTest {
|
||||
when(service.runInstances(anyString(), nullable(String.class), nullable(String.class),
|
||||
anyInt(), anyInt(), nullable(String.class), anyList(), nullable(String.class),
|
||||
nullable(String.class), anyList(), nullable(String.class), nullable(String.class),
|
||||
nullable(Boolean.class))).thenReturn(new Reservation());
|
||||
nullable(Boolean.class), nullable(String.class), anyInt())).thenReturn(new Reservation());
|
||||
|
||||
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
|
||||
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
|
||||
@@ -68,7 +68,7 @@ class Ec2RunInstancesPublicIpParamTest {
|
||||
verify(service).runInstances(anyString(), nullable(String.class), nullable(String.class),
|
||||
anyInt(), anyInt(), nullable(String.class), anyList(), nullable(String.class),
|
||||
nullable(String.class), anyList(), nullable(String.class), nullable(String.class),
|
||||
associatePublicIp.capture());
|
||||
associatePublicIp.capture(), nullable(String.class), anyInt());
|
||||
if (expected == null) {
|
||||
assertNull(associatePublicIp.getValue(),
|
||||
"an absent override must stay null so the subnet default decides");
|
||||
|
||||
@@ -117,6 +117,51 @@ class Ec2ServiceTest {
|
||||
verify(containerManager).cancelLaunch(instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* A container-backed launch that fails or is cancelled terminates the instance inside the
|
||||
* container manager, never passing through TerminateInstances, so an ENI the launch had
|
||||
* already marked in-use would stay pinned to an instance that no longer runs, and could
|
||||
* never be reused. The attachment is reconciled when the interface is next read instead.
|
||||
*/
|
||||
@Test
|
||||
void anInterfaceIsReleasedWhenItsInstanceDiedWithoutTerminateInstances() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
|
||||
new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
|
||||
String subnetId = service.describeSubnets("us-east-1", List.of(), Map.of())
|
||||
.getFirst().getSubnetId();
|
||||
NetworkInterface eni = service.createNetworkInterface("us-east-1", subnetId, null,
|
||||
null, List.of(), List.of(), List.of());
|
||||
|
||||
Reservation reservation = service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro",
|
||||
1, 1, null, List.of(), null, null, List.of(), null, null, null,
|
||||
eni.getNetworkInterfaceId(), 0);
|
||||
Instance instance = reservation.getInstances().getFirst();
|
||||
assertEquals("in-use", service.describeNetworkInterfaces("us-east-1",
|
||||
List.of(eni.getNetworkInterfaceId()), Map.of(), 0, null)
|
||||
.networkInterfaces().getFirst().getStatus());
|
||||
|
||||
// What a failed launch leaves behind: terminated, with no TerminateInstances call.
|
||||
instance.setState(InstanceState.terminated());
|
||||
|
||||
NetworkInterface afterFailure = service.describeNetworkInterfaces("us-east-1",
|
||||
List.of(eni.getNetworkInterfaceId()), Map.of(), 0, null)
|
||||
.networkInterfaces().getFirst();
|
||||
assertEquals("available", afterFailure.getStatus());
|
||||
assertNull(afterFailure.getAttachment());
|
||||
// The dead instance lets go of its copy too. Leaving it there would have the terminated
|
||||
// instance still reporting an interface that a later launch is free to take, so two
|
||||
// instance records would claim it.
|
||||
assertTrue(instance.getNetworkInterfaces().stream()
|
||||
.noneMatch(e -> eni.getNetworkInterfaceId().equals(e.getNetworkInterfaceId())));
|
||||
|
||||
// And it really is free: a second launch can take it, which the in-use check would refuse.
|
||||
Reservation retry = service.runInstances("us-east-1", "ami-1234567890abcdef0", "t3.micro",
|
||||
1, 1, null, List.of(), null, null, List.of(), null, null, null,
|
||||
eni.getNetworkInterfaceId(), 0);
|
||||
assertEquals(1, retry.getInstances().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void runInstancesRequiresImageIdInsteadOfDefaulting() {
|
||||
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
|
||||
|
||||
Reference in new issue
Block a user