feat(ec2): support registered images and snapshots (#1601)

* feat(ec2): support registered images and snapshots

* fix(ec2): scope self-owned image matches
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-05 09:31:08 -05:00
1 parent c233eb4891
commit 2a6cf1c147
10 files changed
+704 -3

No files matched your search

@@ -53,6 +53,7 @@ class Ec2IntegrationTest {
private static String launchTemplateId;
private static String vpcEndpointId;
private static String natGatewayId;
private static String registeredImageId;
// =========================================================================
// Default resources
@@ -242,6 +243,139 @@ class Ec2IntegrationTest {
.body("DescribeImagesResponse.imagesSet.item.architecture", everyItem(equalTo("arm64")));
}
@Test
@Order(10)
void registerImageCreatesDescribableImageWithSnapshotMapping() {
registeredImageId = given()
.formParam("Action", "RegisterImage")
.formParam("Name", "test-image")
.formParam("Description", "test image")
.formParam("Architecture", "x86_64")
.formParam("RootDeviceName", "/dev/sda1")
.formParam("BlockDeviceMapping.1.DeviceName", "/dev/sda1")
.formParam("BlockDeviceMapping.1.Ebs.SnapshotId", "snap-1234567890abcdef0")
.formParam("BlockDeviceMapping.1.Ebs.VolumeSize", "8")
.formParam("BlockDeviceMapping.1.Ebs.VolumeType", "gp3")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("RegisterImageResponse.imageId", startsWith("ami-"))
.extract().path("RegisterImageResponse.imageId");
given()
.formParam("Action", "DescribeImages")
.formParam("Filter.1.Name", "name")
.formParam("Filter.1.Value.1", "test-image")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeImagesResponse.imagesSet.item.imageId", equalTo(registeredImageId))
.body("DescribeImagesResponse.imagesSet.item.blockDeviceMapping.item.ebs.snapshotId",
equalTo("snap-1234567890abcdef0"));
}
@Test
@Order(11)
void describeSnapshotsReturnsRegisteredImageSnapshot() {
given()
.formParam("Action", "DescribeSnapshots")
.formParam("SnapshotId.1", "snap-1234567890abcdef0")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeSnapshotsResponse.snapshotSet.item.snapshotId", equalTo("snap-1234567890abcdef0"))
.body("DescribeSnapshotsResponse.snapshotSet.item.status", equalTo("completed"))
.body("DescribeSnapshotsResponse.snapshotSet.item.volumeSize", equalTo("8"));
}
@Test
@Order(12)
void describeSnapshotsAcceptsOwnerIdParameter() {
given()
.formParam("Action", "DescribeSnapshots")
.formParam("SnapshotId.1", "snap-1234567890abcdef0")
.formParam("OwnerId.1", "self")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeSnapshotsResponse.snapshotSet.item.snapshotId", equalTo("snap-1234567890abcdef0"));
}
@Test
@Order(13)
void describeSnapshotsAcceptsOwnerIdsParameter() {
given()
.formParam("Action", "DescribeSnapshots")
.formParam("SnapshotId.1", "snap-1234567890abcdef0")
.formParam("OwnerIds.1", "000000000000")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("DescribeSnapshotsResponse.snapshotSet.item.snapshotId", equalTo("snap-1234567890abcdef0"));
}
@Test
@Order(14)
void registerImageRejectsInvalidBlockDeviceVolumeSize() {
given()
.formParam("Action", "RegisterImage")
.formParam("Name", "bad-volume-size-image")
.formParam("BlockDeviceMapping.1.DeviceName", "/dev/sda1")
.formParam("BlockDeviceMapping.1.Ebs.VolumeSize", "large")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
}
@Test
@Order(15)
void registerImageRejectsInvalidBlockDeviceBoolean() {
given()
.formParam("Action", "RegisterImage")
.formParam("Name", "bad-boolean-image")
.formParam("BlockDeviceMapping.1.DeviceName", "/dev/sda1")
.formParam("BlockDeviceMapping.1.Ebs.Encrypted", "sometimes")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
}
@Test
@Order(16)
void registerImageRejectsBlockDeviceMappingWithoutDeviceName() {
given()
.formParam("Action", "RegisterImage")
.formParam("Name", "missing-device-image")
.formParam("BlockDeviceMapping.1.Ebs.SnapshotId", "snap-missing-device")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400)
.body("Response.Errors.Error.Code", equalTo("InvalidParameterValue"));
}
@Test
@Order(17)
void describeInstanceTypes() {
@@ -5,8 +5,11 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.services.ec2.model.Address;
import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping;
import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.InternetGateway;
import io.github.hectorvent.floci.services.ec2.model.Image;
import io.github.hectorvent.floci.services.ec2.model.NetworkAcl;
import io.github.hectorvent.floci.services.ec2.model.KeyPair;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
@@ -14,12 +17,14 @@ import io.github.hectorvent.floci.services.ec2.model.NatGateway;
import io.github.hectorvent.floci.services.ec2.model.RouteTable;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroupRule;
import io.github.hectorvent.floci.services.ec2.model.Snapshot;
import io.github.hectorvent.floci.services.ec2.model.Subnet;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.Volume;
import io.github.hectorvent.floci.services.ec2.model.Vpc;
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
import io.github.hectorvent.floci.services.ec2.model.SpotInstanceRequest;
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
@@ -65,10 +70,42 @@ class Ec2ServicePersistenceTest {
assertEquals("10.0.1.0/24", subnets.get(0).getCidrBlock());
}
@Test
void registeredImageAndSnapshotSurviveRestart(@TempDir Path dir) {
Ec2Service first = newService(dir);
Image image = first.registerImage(REGION, "persisted-image", "persisted image", "x86_64",
"/dev/sda1", List.of(blockDeviceMapping("snap-persisted", 12)));
Ec2Service restarted = newService(dir);
List<Image> images = restarted.describeImages(REGION, List.of(image.getImageId()), List.of(), Map.of());
assertEquals(1, images.size(), "registered image must survive restart");
assertEquals("persisted-image", images.getFirst().getName());
assertEquals("snap-persisted",
images.getFirst().getBlockDeviceMappings().getFirst().getEbs().getSnapshotId());
List<Snapshot> snapshots = restarted.describeSnapshots(REGION, List.of("snap-persisted"), List.of(), Map.of());
assertEquals(1, snapshots.size(), "linked snapshot must survive restart");
assertEquals(12, snapshots.getFirst().getVolumeSize());
}
private BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
EbsBlockDevice ebs = new EbsBlockDevice();
ebs.setSnapshotId(snapshotId);
ebs.setVolumeSize(volumeSize);
BlockDeviceMapping mapping = new BlockDeviceMapping();
mapping.setDeviceName("/dev/sda1");
mapping.setEbs(ebs);
return mapping;
}
private Ec2Service newService(Path dir) {
EmulatorConfig config = mock(EmulatorConfig.class);
when(config.defaultAccountId()).thenReturn("000000000000");
return new Ec2Service(config, null, null, null, null, new Ec2InstanceTypeCatalog(),
Ec2ImageCatalog imageCatalog = new Ec2ImageCatalog();
return new Ec2Service(config, null, mock(Ec2PortForwardManager.class),
new AmiImageResolver(imageCatalog), imageCatalog,
new Ec2InstanceTypeCatalog(),
load(dir, "ec2-vpcs.json", new TypeReference<Map<String, Vpc>>() {}),
load(dir, "ec2-subnets.json", new TypeReference<Map<String, Subnet>>() {}),
load(dir, "ec2-security-groups.json", new TypeReference<Map<String, SecurityGroup>>() {}),
@@ -79,6 +116,8 @@ class Ec2ServicePersistenceTest {
load(dir, "ec2-addresses.json", new TypeReference<Map<String, Address>>() {}),
load(dir, "ec2-instances.json", new TypeReference<Map<String, Instance>>() {}),
load(dir, "ec2-volumes.json", new TypeReference<Map<String, Volume>>() {}),
load(dir, "ec2-registered-images.json", new TypeReference<Map<String, Image>>() {}),
load(dir, "ec2-snapshots.json", new TypeReference<Map<String, Snapshot>>() {}),
load(dir, "ec2-launch-templates.json", new TypeReference<Map<String, LaunchTemplate>>() {}),
load(dir, "ec2-vpc-endpoints.json", new TypeReference<Map<String, VpcEndpoint>>() {}),
load(dir, "ec2-nat-gateways.json", new TypeReference<Map<String, NatGateway>>() {}),
@@ -7,12 +7,15 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
import io.github.hectorvent.floci.services.ec2.model.BlockDeviceMapping;
import io.github.hectorvent.floci.services.ec2.model.EbsBlockDevice;
import io.github.hectorvent.floci.services.ec2.model.GroupIdentifier;
import io.github.hectorvent.floci.services.ec2.model.Instance;
import io.github.hectorvent.floci.services.ec2.model.LaunchTemplate;
import io.github.hectorvent.floci.services.ec2.model.NetworkInterface;
import io.github.hectorvent.floci.services.ec2.model.Reservation;
import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;
import io.github.hectorvent.floci.services.ec2.model.Snapshot;
import io.github.hectorvent.floci.services.ec2.model.Tag;
import io.github.hectorvent.floci.services.ec2.model.VpcEndpoint;
import org.junit.jupiter.api.Test;
@@ -245,6 +248,71 @@ class Ec2ServiceTest {
assertEquals("InvalidGroup.NotFound", error.getErrorCode());
}
@Test
void registerImageNamesAreScopedToRegion() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
service.registerImage("us-east-1", "shared-name", null, null, null, List.of());
service.registerImage("us-west-2", "shared-name", null, null, null, List.of());
AwsException error = assertThrows(AwsException.class,
() -> service.registerImage("us-east-1", "shared-name", null, null, null, List.of()));
assertEquals("InvalidAMIName.Duplicate", error.getErrorCode());
}
@Test
void registerImageReusingSnapshotDoesNotOverwriteSnapshotMetadata() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory());
service.registerImage("us-east-1", "first-image", null, null, null,
List.of(blockDeviceMapping("snap-reused", 8)));
service.registerImage("us-east-1", "second-image", null, null, null,
List.of(blockDeviceMapping("snap-reused", 64)));
List<Snapshot> snapshots = service.describeSnapshots("us-east-1", List.of("snap-reused"), List.of(), Map.of());
assertEquals(1, snapshots.size());
assertEquals(8, snapshots.getFirst().getVolumeSize());
assertEquals("Created by RegisterImage for first-image", snapshots.getFirst().getDescription());
}
@Test
void describeSnapshotsDefaultsToOwnedSnapshots() {
InMemoryStorage<String, Snapshot> snapshotStore = new InMemoryStorage<>();
Snapshot foreign = new Snapshot();
foreign.setSnapshotId("snap-foreign");
foreign.setOwnerId("111111111111");
foreign.setRegion("us-east-1");
snapshotStore.put("us-east-1::snap-foreign", foreign);
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
mock(Ec2PortForwardManager.class),
mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), new Ec2InstanceTypeCatalog(),
new InMemoryStorageFactory(Map.of("ec2-snapshots.json", snapshotStore)));
service.registerImage("us-east-1", "owned-image", null, null, null,
List.of(blockDeviceMapping("snap-owned", 16)));
List<Snapshot> snapshots = service.describeSnapshots("us-east-1", List.of(), List.of(), Map.of());
assertEquals(1, snapshots.size());
assertEquals("snap-owned", snapshots.getFirst().getSnapshotId());
}
private static BlockDeviceMapping blockDeviceMapping(String snapshotId, int volumeSize) {
EbsBlockDevice ebs = new EbsBlockDevice();
ebs.setSnapshotId(snapshotId);
ebs.setVolumeSize(volumeSize);
BlockDeviceMapping mapping = new BlockDeviceMapping();
mapping.setDeviceName("/dev/sda1");
mapping.setEbs(ebs);
return mapping;
}
private static EmulatorConfig mockConfig(boolean ec2Mock) {
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
@@ -257,13 +325,25 @@ class Ec2ServiceTest {
}
private static final class InMemoryStorageFactory extends StorageFactory {
private final Map<String, StorageBackend<String, ?>> overrides;
private InMemoryStorageFactory() {
this(Map.of());
}
private InMemoryStorageFactory(Map<String, StorageBackend<String, ?>> overrides) {
super(null, null);
this.overrides = overrides;
}
@Override
@SuppressWarnings("unchecked")
public <V> StorageBackend<String, V> create(String serviceName, String fileName,
TypeReference<Map<String, V>> typeReference) {
StorageBackend<String, ?> override = overrides.get(fileName);
if (override != null) {
return (StorageBackend<String, V>) override;
}
return new InMemoryStorage<>();
}
}