mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
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:
1 parent
8be5d03bbc
commit
55a90e8299
13 files changed
+1266
-3
No files matched your search
+201
@@ -0,0 +1,201 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import software.amazon.awssdk.services.ec2.Ec2Client;
|
||||
import software.amazon.awssdk.services.ec2.model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* Verifies pagination for DescribeNetworkInterfaces (MaxResults / NextToken).
|
||||
*
|
||||
* <p>Launches 6 instances to produce 6 ENIs, then exercises full pagination:
|
||||
* first page truncated with nextToken, second page continued from token,
|
||||
* and final page omitting nextToken.
|
||||
*/
|
||||
@DisplayName("EC2 DescribeNetworkInterfaces Pagination")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class Ec2NetworkInterfacePaginationTest {
|
||||
|
||||
private static Ec2Client ec2;
|
||||
private static String vpcId;
|
||||
private static String subnetId;
|
||||
private static String sgId;
|
||||
private static String keyName;
|
||||
private static final List<String> instanceIds = new ArrayList<>();
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
ec2 = TestFixtures.ec2Client();
|
||||
|
||||
// --- Create VPC ---
|
||||
CreateVpcResponse vpcResp = ec2.createVpc(CreateVpcRequest.builder()
|
||||
.cidrBlock("10.1.0.0/16").build());
|
||||
vpcId = vpcResp.vpc().vpcId();
|
||||
assertThat(vpcId).isNotNull().startsWith("vpc-");
|
||||
|
||||
// --- Create Subnet ---
|
||||
CreateSubnetResponse subnetResp = ec2.createSubnet(CreateSubnetRequest.builder()
|
||||
.vpcId(vpcId).cidrBlock("10.1.1.0/24").build());
|
||||
subnetId = subnetResp.subnet().subnetId();
|
||||
assertThat(subnetId).isNotNull().startsWith("subnet-");
|
||||
|
||||
// --- Create Security Group ---
|
||||
String sgName = TestFixtures.uniqueName("pagination-sg");
|
||||
CreateSecurityGroupResponse sgResp = ec2.createSecurityGroup(
|
||||
CreateSecurityGroupRequest.builder()
|
||||
.groupName(sgName).description("Pagination test SG").vpcId(vpcId).build());
|
||||
sgId = sgResp.groupId();
|
||||
assertThat(sgId).isNotNull().startsWith("sg-");
|
||||
|
||||
// --- Create Key Pair ---
|
||||
keyName = TestFixtures.uniqueName("pagination-key");
|
||||
ec2.createKeyPair(CreateKeyPairRequest.builder().keyName(keyName).build());
|
||||
|
||||
// --- Launch 6 instances to get 6 ENIs ---
|
||||
RunInstancesResponse runResp = ec2.runInstances(RunInstancesRequest.builder()
|
||||
.imageId("ami-0abcdef1234567890")
|
||||
.instanceType(InstanceType.T2_MICRO)
|
||||
.minCount(6)
|
||||
.maxCount(6)
|
||||
.keyName(keyName)
|
||||
.subnetId(subnetId)
|
||||
.securityGroupIds(List.of(sgId))
|
||||
.build());
|
||||
|
||||
for (Instance inst : runResp.instances()) {
|
||||
instanceIds.add(inst.instanceId());
|
||||
}
|
||||
assertThat(instanceIds).hasSize(6);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (ec2 != null) {
|
||||
// Terminate instances
|
||||
if (!instanceIds.isEmpty()) {
|
||||
try {
|
||||
ec2.terminateInstances(TerminateInstancesRequest.builder()
|
||||
.instanceIds(instanceIds).build());
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
// Delete subnet
|
||||
try {
|
||||
if (subnetId != null) {
|
||||
ec2.deleteSubnet(DeleteSubnetRequest.builder().subnetId(subnetId).build());
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// Delete SG
|
||||
try {
|
||||
if (sgId != null) {
|
||||
ec2.deleteSecurityGroup(DeleteSecurityGroupRequest.builder().groupId(sgId).build());
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// Delete key pair
|
||||
try {
|
||||
if (keyName != null) {
|
||||
ec2.deleteKeyPair(DeleteKeyPairRequest.builder().keyName(keyName).build());
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
// Delete VPC
|
||||
try {
|
||||
if (vpcId != null) {
|
||||
ec2.deleteVpc(DeleteVpcRequest.builder().vpcId(vpcId).build());
|
||||
}
|
||||
} catch (Exception ignored) {}
|
||||
ec2.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
@DisplayName("DescribeNetworkInterfaces without pagination returns all 6 ENIs")
|
||||
void describeNetworkInterfacesAll() {
|
||||
DescribeNetworkInterfacesResponse resp = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder().build());
|
||||
|
||||
assertThat(resp.networkInterfaces()).hasSize(6);
|
||||
assertThat(resp.nextToken()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
@DisplayName("MaxResults=5 truncates first page and returns nextToken")
|
||||
void describeNetworkInterfacesFirstPage() {
|
||||
DescribeNetworkInterfacesResponse resp = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.build());
|
||||
|
||||
assertThat(resp.networkInterfaces()).hasSize(5);
|
||||
assertThat(resp.nextToken()).isNotNull().isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
@DisplayName("NextToken continuation returns remaining ENIs, no nextToken on final page")
|
||||
void describeNetworkInterfacesContinuation() {
|
||||
// Page 1: get nextToken
|
||||
DescribeNetworkInterfacesResponse page1 = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.build());
|
||||
assertThat(page1.nextToken()).isNotNull();
|
||||
|
||||
// Page 2: use nextToken
|
||||
DescribeNetworkInterfacesResponse page2 = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.nextToken(page1.nextToken())
|
||||
.build());
|
||||
|
||||
assertThat(page2.networkInterfaces()).hasSize(1);
|
||||
assertThat(page2.nextToken()).isNull();
|
||||
|
||||
// Verify all 6 ENIs accounted for across both pages
|
||||
List<String> page1Ids = page1.networkInterfaces().stream()
|
||||
.map(NetworkInterface::networkInterfaceId).toList();
|
||||
List<String> page2Ids = page2.networkInterfaces().stream()
|
||||
.map(NetworkInterface::networkInterfaceId).toList();
|
||||
|
||||
// No overlap between pages
|
||||
assertThat(page1Ids).doesNotContainAnyElementsOf(page2Ids);
|
||||
// Combined = 6 unique
|
||||
List<String> all = new ArrayList<>(page1Ids);
|
||||
all.addAll(page2Ids);
|
||||
assertThat(all).hasSize(6).doesNotHaveDuplicates();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@DisplayName("NextToken past end returns empty result set")
|
||||
void describeNetworkInterfacesTokenPastEnd() {
|
||||
// Get a valid token from first page
|
||||
DescribeNetworkInterfacesResponse page1 = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.build());
|
||||
String token = page1.nextToken();
|
||||
|
||||
// Use it to get the last page
|
||||
DescribeNetworkInterfacesResponse page2 = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.nextToken(token)
|
||||
.build());
|
||||
|
||||
// Using the token from page2 (which is null since it's the last page)
|
||||
// would be invalid, but using a valid token again should still work
|
||||
DescribeNetworkInterfacesResponse repeat = ec2.describeNetworkInterfaces(
|
||||
DescribeNetworkInterfacesRequest.builder()
|
||||
.maxResults(5)
|
||||
.nextToken(token)
|
||||
.build());
|
||||
|
||||
assertThat(repeat.networkInterfaces()).hasSize(1);
|
||||
assertThat(repeat.nextToken()).isNull();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user