fix(ec2) Fix/1031 ec2 describe network interfaces (#1035)

* feat(ec2): add partial DescribeNetworkInterfaces (phases 1-3)

Wire DescribeNetworkInterfaces into the EC2 query handler with ENI
data derived from running instances. Covers:
- Empty response skeleton
- Basic ENI mapping (networkInterfaceId, subnetId, vpcId, groupSet, attachment)
- Full field coverage (privateIpAddressesSet, EIP association, tagSet,
  availabilityZone, interfaceType, attachTime, deleteOnTermination)

Remaining: filter support, pagination, error handling, Terraform compat
validation (phases 4-6 for #1031).

* feat(ec2): add filter support for DescribeNetworkInterfaces (phase 4)

Support these filter keys: network-interface-id, subnet-id, vpc-id,
group-id, status, private-ip-address, description, owner-id, mac-address,
private-dns-name, tag:<key>, tag-key. Filters use AND semantics.
No-match filters return HTTP 200 with empty networkInterfaceSet.

* feat(ec2): add pagination and error handling for DescribeNetworkInterfaces (phases 5-6)

- Add NetworkInterfaceListResult record for paginated responses
- Implement offset-based MaxResults/NextToken pagination
- Add Base64 token encode/decode for pagination cursor
- Validate MaxResults range (5-1000) and MaxResults+NetworkInterfaceId conflict
- Conditionally emit nextToken in XML response (only when truncated)
- Add InvalidNetworkInterfaceID.NotFound for non-existent IDs
- Add InvalidNetworkInterfaceID.Malformed for IDs not starting with 'eni-'
- Track found IDs via HashSet for accurate NotFound detection

* test(ec2): add DescribeNetworkInterfaces pagination, error, and multipage tests

- 5 pagination validation tests (MaxResults range, param conflict, invalid token)
- 1 pagination success test (MaxResults > available ENIs, no nextToken emitted)
- 2 error handling tests (InvalidNetworkInterfaceID.NotFound and .Malformed)
- 1 self-contained multipage pagination test (launches 5 extra instances,
  verifies MaxResults=5 truncation, NextToken continuation, final page cleanup)
- All 87 Ec2IntegrationTest tests pass

* test(ec2): add SDK and Terraform compat tests for DescribeNetworkInterfaces

- Add Ec2NetworkInterfacePaginationTest with 4 scenarios:
  all 6 ENIs without pagination, MaxResults=5 truncation,
  NextToken continuation (no overlap between pages),
  and token re-use stability
- Add eni_destroy.bats Terraform compat test verifying destroy succeeds
  on VPC+subnet+SG now that DescribeNetworkInterfaces is implemented
- Add minimal Terraform config (VPC 10.0.0.0/16, subnet, SG) for ENI test

* fix(ec2): align DescribeNetworkInterfaces MaxResults errors

* test(terraform): make ENI destroy compat test runnable
This commit is contained in:
nblomquist authored and GitHub committed 2026-05-26 21:54:58 -05:00
1 parent 8be5d03bbc
commit 55a90e8299
13 files changed
+1266 -3

No files matched your search

@@ -3,8 +3,12 @@ package io.github.hectorvent.floci.services.ec2;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.containsString;
import java.util.List;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@@ -34,6 +38,7 @@ class Ec2IntegrationTest {
private static String allocationId;
private static String associationId;
private static String volumeId;
private static String networkInterfaceId;
// =========================================================================
// Default resources
@@ -732,6 +737,71 @@ class Ec2IntegrationTest {
.body("RebootInstancesResponse.return", equalTo("true"));
}
// =========================================================================
// Network Interfaces
// =========================================================================
@Test
@Order(79)
void describeNetworkInterfacesBeforeRun() {
// Before any instances exist, the set should be empty
given()
.formParam("Action", "DescribeNetworkInterfaces")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()", equalTo(0));
}
@Test
@Order(88)
void describeNetworkInterfacesAfterRun() {
networkInterfaceId = given()
.formParam("Action", "DescribeNetworkInterfaces")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()", greaterThanOrEqualTo(1))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].networkInterfaceId",
startsWith("eni-"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].vpcId", notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].subnetId", notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].status", equalTo("in-use"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].privateIpAddress",
notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].attachment.attachmentId",
startsWith("eni-attach-"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].attachment.deviceIndex",
equalTo("0"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].attachment.instanceId",
notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].groupSet.item.size()",
greaterThanOrEqualTo(1))
.extract().path("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].networkInterfaceId");
}
@Test
@Order(89)
void describeNetworkInterfacesByFilter() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NetworkInterfaceId.1", networkInterfaceId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()", equalTo(1))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].networkInterfaceId",
equalTo(networkInterfaceId));
}
// =========================================================================
// Tags
// =========================================================================
@@ -812,6 +882,353 @@ class Ec2IntegrationTest {
.body("DescribeTagsResponse.tagSet.item.size()", equalTo(0));
}
@Test
@Order(92)
void describeNetworkInterfacesFullFields() {
// Phase 3: Full field coverage — privateIpAddressesSet, association, tagSet, enriched attachment
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NetworkInterfaceId.1", networkInterfaceId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
// availabilityZone from instance placement
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].availabilityZone",
startsWith("us-east-1"))
// tagSet propagated from instance tags (created at Order 90)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].tagSet.item[0].key",
equalTo("Name"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].tagSet.item[0].value",
equalTo("test-instance"))
// attachment: attachTime (from instance launchTime) and deleteOnTermination
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].attachment.attachTime",
notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].attachment.deleteOnTermination",
equalTo("true"))
// privateIpAddressesSet with primary IP and EIP association (from Order 84)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0]." +
"privateIpAddressesSet.item[0].privateIpAddress", notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0]." +
"privateIpAddressesSet.item[0].primary", equalTo("true"))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0]." +
"privateIpAddressesSet.item[0].association.publicIp", notNullValue())
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0]." +
"privateIpAddressesSet.item[0].association.allocationId",
startsWith("eipalloc-"));
}
// =========================================================================
// Network Interfaces — Filter tests (Phase 4)
// =========================================================================
@Test
@Order(92)
void describeNetworkInterfacesFilterBySubnetId() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "subnet-id")
.formParam("Filter.1.Value.1", subnetId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
greaterThanOrEqualTo(1))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].subnetId",
equalTo(subnetId));
}
@Test
@Order(92)
void describeNetworkInterfacesFilterByVpcId() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "vpc-id")
.formParam("Filter.1.Value.1", vpcId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
greaterThanOrEqualTo(1))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].vpcId",
equalTo(vpcId));
}
@Test
@Order(92)
void describeNetworkInterfacesFilterByGroupId() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "group-id")
.formParam("Filter.1.Value.1", securityGroupId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
greaterThanOrEqualTo(1));
}
@Test
@Order(92)
void describeNetworkInterfacesFilterByStatus() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "status")
.formParam("Filter.1.Value.1", "in-use")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
greaterThanOrEqualTo(1))
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item[0].status",
equalTo("in-use"));
}
@Test
@Order(92)
void describeNetworkInterfacesFilterByTag() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "tag:Name")
.formParam("Filter.1.Value.1", "test-instance")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
greaterThanOrEqualTo(1));
}
@Test
@Order(92)
void describeNetworkInterfacesFilterNoMatch() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("Filter.1.Name", "status")
.formParam("Filter.1.Value.1", "available")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
equalTo(0));
}
// =========================================================================
// Network Interfaces — Pagination (Phase 5)
// =========================================================================
@Test
@Order(92)
void describeNetworkInterfacesMaxResultsTooLow() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "4")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidMaxResults"));
}
@Test
@Order(92)
void describeNetworkInterfacesMaxResultsTooHigh() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "1001")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidMaxResults"));
}
@Test
@Order(92)
void describeNetworkInterfacesMaxResultsNonNumeric() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "abc")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidMaxResults"));
}
@Test
@Order(92)
void describeNetworkInterfacesMaxResultsWithNetworkInterfaceId() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NetworkInterfaceId.1", networkInterfaceId)
.formParam("MaxResults", "5")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterCombination"));
}
@Test
@Order(92)
void describeNetworkInterfacesInvalidNextToken() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NextToken", "invalid-token")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
}
@Test
@Order(92)
void describeNetworkInterfacesWithMaxResultsNoNextToken() {
// When MaxResults exceeds the number of available ENIs,
// all results are returned and nextToken is omitted.
// This test works regardless of how many instances exist
// (including zero, e.g. when run in isolation).
String body = given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "5")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().body().asString();
org.hamcrest.MatcherAssert.assertThat(body, not(containsString("<nextToken>")));
}
// =========================================================================
// Network Interfaces — Error Handling (Phase 6)
// =========================================================================
@Test
@Order(92)
void describeNetworkInterfacesNotFound() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NetworkInterfaceId.1", "eni-0000000000000dead")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.NotFound"));
}
@Test
@Order(92)
void describeNetworkInterfacesMalformed() {
given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("NetworkInterfaceId.1", "not-an-eni-id")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidNetworkInterfaceID.Malformed"));
}
// =========================================================================
// Network Interfaces — Multipage Pagination (Phase 5 completion)
// =========================================================================
//
// Self-contained test: launches additional instances, tests full
// pagination cycle (MaxResults truncation + NextToken continuation),
// then terminates the extra instances. Does not affect other tests.
@Test
@Order(92)
void describeNetworkInterfacesMultipagePagination() {
// ── Launch 5 additional instances to have 6 total ENIs ──
List<String> batchIds = given()
.formParam("Action", "RunInstances")
.formParam("ImageId", "ami-0abcdef1234567890")
.formParam("InstanceType", "t2.micro")
.formParam("MinCount", "5")
.formParam("MaxCount", "5")
.formParam("KeyName", "test-key")
.formParam("SubnetId", subnetId)
.formParam("SecurityGroupId.1", securityGroupId)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.extract().xmlPath().getList("RunInstancesResponse.instancesSet.item.instanceId", String.class);
assert batchIds.size() == 5 : "Expected 5 new instances, got " + batchIds.size();
// ── Page 1: MaxResults=5, expect 5 ENIs + nextToken ──
String nextToken = given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "5")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()", equalTo(5))
.body("DescribeNetworkInterfacesResponse.nextToken", notNullValue())
.extract().path("DescribeNetworkInterfacesResponse.nextToken");
assert nextToken != null && !nextToken.isEmpty() : "Expected non-empty nextToken on truncated page";
// ── Page 2: use NextToken, expect remaining ENIs, no nextToken ──
String body = given()
.formParam("Action", "DescribeNetworkInterfaces")
.formParam("MaxResults", "5")
.formParam("NextToken", nextToken)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.size()",
org.hamcrest.Matchers.greaterThanOrEqualTo(1))
.extract().body().asString();
// Final page must NOT contain a nextToken element
org.hamcrest.MatcherAssert.assertThat(body,
not(containsString("<nextToken>")));
// ── Cleanup: terminate the 5 extra instances ──
for (String id : batchIds) {
given()
.formParam("Action", "TerminateInstances")
.formParam("InstanceId.1", id)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200);
}
}
// =========================================================================
// Volumes
// =========================================================================
@@ -1243,4 +1660,4 @@ class Ec2IntegrationTest {
.when()
.post("/");
}
}
}