Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(ec2): create the EBS volumes a launch's block device mappings describe (#6734)

Jackson Price65df465fc37a
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2LaunchBlockDeviceMappingsTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2LaunchBlockDeviceMappingsTest.java
new file mode 100644
index 0000000000..5f29f1810d
--- /dev/null
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/Ec2LaunchBlockDeviceMappingsTest.java
@@ -0,0 +1,132 @@
+package com.floci.test;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import software.amazon.awssdk.services.ec2.Ec2Client;
+import software.amazon.awssdk.services.ec2.model.ArchitectureValues;
+import software.amazon.awssdk.services.ec2.model.BlockDeviceMapping;
+import software.amazon.awssdk.services.ec2.model.Ec2Exception;
+import software.amazon.awssdk.services.ec2.model.Filter;
+import software.amazon.awssdk.services.ec2.model.Instance;
+import software.amazon.awssdk.services.ec2.model.InstanceBlockDeviceMapping;
+import software.amazon.awssdk.services.ec2.model.InstanceType;
+import software.amazon.awssdk.services.ec2.model.Volume;
+import software.amazon.awssdk.services.ec2.model.VolumeType;
+
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * RunInstances creates one EBS volume per device of the AMI's block device mapping with the
+ * launch's own entries over it, and TerminateInstances keeps or deletes each as its
+ * DeleteOnTermination says.
+ */
+class Ec2LaunchBlockDeviceMappingsTest {
+
+ /** An account of its own, so the volumes listed are only this test's. */
+ private static final String ACCOUNT = "724800000001";
+ private static final String SNAPSHOT = "snap-0a1b2c3d4e5f60719";
+
+ @Test
+ @DisplayName("RunInstances creates the AMI's and the request's volumes and termination keeps those it should")
+ void launchCreatesTheMappedVolumes() {
+ try (Ec2Client ec2 = TestFixtures.ec2Client(ACCOUNT)) {
+ String imageId = registerImage(ec2);
+ String instanceId = null;
+ try {
+ Instance launched = ec2.runInstances(r -> r.imageId(imageId).instanceType(InstanceType.T3_MICRO)
+ .minCount(1).maxCount(1)
+ .blockDeviceMappings(
+ BlockDeviceMapping.builder().deviceName("/dev/sda1")
+ .ebs(e -> e.volumeSize(20).deleteOnTermination(false)).build(),
+ BlockDeviceMapping.builder().deviceName("/dev/sdg")
+ .ebs(e -> e.volumeSize(5).volumeType(VolumeType.GP3)).build()))
+ .instances().get(0);
+ instanceId = launched.instanceId();
+
+ Instance described = ec2.describeInstances(r -> r.instanceIds(launched.instanceId()))
+ .reservations().get(0).instances().get(0);
+ assertThat(described.rootDeviceName()).isEqualTo("/dev/sda1");
+ assertThat(described.blockDeviceMappings()).extracting(InstanceBlockDeviceMapping::deviceName)
+ .containsExactly("/dev/sda1", "/dev/sdf", "/dev/sdg");
+ assertThat(described.blockDeviceMappings()).extracting(m -> m.ebs().deleteOnTermination())
+ .containsExactly(false, false, true);
+
+ Map<String, Volume> volumes = volumesByDevice(ec2, launched.instanceId());
+ assertThat(volumes.get("/dev/sda1").size()).isEqualTo(20);
+ assertThat(volumes.get("/dev/sda1").snapshotId()).isEqualTo(SNAPSHOT);
+ assertThat(volumes.get("/dev/sda1").volumeType()).isEqualTo(VolumeType.GP2);
+ assertThat(volumes.get("/dev/sdf").size()).isEqualTo(125);
+ assertThat(volumes.get("/dev/sdf").volumeType()).isEqualTo(VolumeType.ST1);
+ assertThat(volumes.get("/dev/sdg").size()).isEqualTo(5);
+ assertThat(volumes.get("/dev/sdg").volumeType()).isEqualTo(VolumeType.GP3);
+
+ ec2.terminateInstances(r -> r.instanceIds(launched.instanceId()));
+ instanceId = null;
+ List<Volume> left = ec2.describeVolumes(r -> r.volumeIds(volumes.get("/dev/sda1").volumeId(),
+ volumes.get("/dev/sdf").volumeId())).volumes();
+ assertThat(left).allSatisfy(volume -> {
+ assertThat(volume.stateAsString()).isEqualTo("available");
+ assertThat(volume.attachments()).isEmpty();
+ });
+ assertThat(ec2.describeVolumes().volumes()).extracting(Volume::volumeId)
+ .doesNotContain(volumes.get("/dev/sdg").volumeId());
+ for (Volume volume : left) {
+ ec2.deleteVolume(r -> r.volumeId(volume.volumeId()));
+ }
+ } finally {
+ if (instanceId != null) {
+ String cleanup = instanceId;
+ ec2.terminateInstances(r -> r.instanceIds(cleanup));
+ }
+ ec2.deregisterImage(r -> r.imageId(imageId));
+ }
+ }
+ }
+
+ @Test
+ @DisplayName("RunInstances refuses a volume smaller than its snapshot and launches nothing")
+ void refusesAVolumeSmallerThanItsSnapshot() {
+ try (Ec2Client ec2 = TestFixtures.ec2Client(ACCOUNT)) {
+ String imageId = registerImage(ec2);
+ try {
+ Ec2Exception refused = assertThrows(Ec2Exception.class, () -> ec2.runInstances(r -> r
+ .imageId(imageId).instanceType(InstanceType.T3_MICRO).minCount(1).maxCount(1)
+ .blockDeviceMappings(BlockDeviceMapping.builder().deviceName("/dev/sda1")
+ .ebs(e -> e.volumeSize(8)).build())));
+ assertThat(refused.statusCode()).isEqualTo(400);
+ assertThat(refused.awsErrorDetails().errorCode()).isEqualTo("InvalidBlockDeviceMapping");
+ assertThat(refused.awsErrorDetails().errorMessage())
+ .isEqualTo("Volume of size 8GB is smaller than snapshot '" + SNAPSHOT + "', expect size>= 16GB");
+ assertThat(ec2.describeInstances(r -> r.filters(Filter.builder().name("image-id").values(imageId)
+ .build())).reservations()).isEmpty();
+ } finally {
+ ec2.deregisterImage(r -> r.imageId(imageId));
+ }
+ }
+ }
+
+ /** A /dev/sda1-rooted gp2 AMI on a 16 GiB snapshot, with a 125 GiB st1 /dev/sdf that is kept on termination. */
+ private static String registerImage(Ec2Client ec2) {
+ return ec2.registerImage(r -> r.name("launch-volumes-" + UUID.randomUUID())
+ .rootDeviceName("/dev/sda1").architecture(ArchitectureValues.X86_64)
+ .blockDeviceMappings(
+ BlockDeviceMapping.builder().deviceName("/dev/sda1")
+ .ebs(e -> e.snapshotId(SNAPSHOT).volumeSize(16).volumeType(VolumeType.GP2)).build(),
+ BlockDeviceMapping.builder().deviceName("/dev/sdf")
+ .ebs(e -> e.volumeSize(125).volumeType(VolumeType.ST1).deleteOnTermination(false))
+ .build()))
+ .imageId();
+ }
+
+ private static Map<String, Volume> volumesByDevice(Ec2Client ec2, String instanceId) {
+ return ec2.describeVolumes(r -> r.filters(Filter.builder().name("attachment.instance-id")
+ .values(instanceId).build())).volumes().stream()
+ .collect(Collectors.toMap(volume -> volume.attachments().get(0).device(), volume -> volume));
+ }
+}
diff --git a/docs/services/ec2.md b/docs/services/ec2.md
index cc4a089ae1..193f0a9c07 100644
--- a/docs/services/ec2.md
+++ b/docs/services/ec2.md
@@ -926,16 +926,65 @@ Within `NetworkInterfaces`, `ConnectionTrackingSpecification` is stored and read
three of its timeout members.
At launch, `RunInstances` and `CreateFleet` apply the template's `BlockDeviceMappings` under the
-request's, field by field: a request entry for a `DeviceName` the template maps changes only the
-members it sets, and the rest keep the template's values; devices only one side maps apply as
-given. An Auto Scaling group launching from a template gets the template's mappings the same
-way. The root volume's type comes from the mapping for the AMI's root device, else from the
-AMI's own root mapping, else `gp3`, and `CreateImage` records that type on the new AMI's root
-mapping. A mapping with `NoDevice` set removes its device whatever else it names, so it sets no
-type and its `VolumeType` is not checked. A mapping naming a `VolumeType` outside the service model's enum fails with
-`InvalidParameterValue`, as `ModifyVolume` does. Template versions are not checked when they are
-stored, so a template naming an unknown `VolumeType` is accepted by `CreateLaunchTemplate` and
-fails at launch.
+request's, field by field: a request entry for a `DeviceName` the template maps, spelled `xvdf` or
+`/dev/xvdf`, changes only the members it sets, and the rest keep the template's values; devices
+only one side maps apply as given. A request entry that makes the device something else, an EBS
+volume (`Ebs`), an instance store volume (`VirtualName`) or nothing (`NoDevice`), replaces the
+template's choice for it, an `Ebs` block still merging member by member into the template's. An
+Auto Scaling group launching from a template gets its mappings the same way; one launching from a
+launch configuration does not pass the configuration's mappings yet. CloudFormation does not pass
+them yet: an `AWS::EC2::Instance` launches without the `BlockDeviceMappings` it or its launch
+template declares, and an `AWS::EC2::LaunchTemplate` or
+`AWS::AutoScaling::LaunchConfiguration` stores none, so instances launched from what a stack
+creates get the AMI's devices alone. A mapping naming a `VolumeType` outside the service model's
+enum fails with `InvalidParameterValue`, as `ModifyVolume` does; one with `NoDevice` set creates no
+device, so its `VolumeType` is not checked. Template versions are not checked when they are stored,
+so a template naming an unknown `VolumeType` is accepted by `CreateLaunchTemplate` and fails at
+launch.
+
+The launch's mappings then apply over the AMI's own block device mapping the same way, and the
+launch creates one EBS volume for each device left, attached at that device:
+
+- The instance's root device is the AMI's `RootDeviceName`, which the AMI's entry for it may spell
+ `xvda` or `/dev/xvda`. An AMI that maps no root device gets an 8 GiB root volume. A launch entry
+ for a device the AMI maps applies to it whether it is spelled `xvdf` or `/dev/xvdf`. `NoDevice`
+ keeps any other device the AMI maps from being created, and an entry naming a `VirtualName`, an
+ instance store volume, takes the device over, so neither leaves an EBS volume there. The root
+ is always created: an entry setting either on it leaves the AMI's root as the AMI maps it. An
+ AMI entry with no `Ebs` member, such as an instance store volume, creates nothing.
+- The catalog's images are rooted at `/dev/xvda`, including some AWS publishes with a
+ `/dev/sda1` root, such as Ubuntu and Windows, so on them an entry for `/dev/sda1` adds a data
+ volume rather than resizing the root.
+- A volume from a snapshot takes the snapshot's size unless its entry names a larger one. A smaller
+ size fails with `InvalidBlockDeviceMapping` ("Volume of size 8GB is smaller than snapshot
+ 'snap-...', expect size>= 16GB"), and a snapshot that does not exist fails with
+ `InvalidSnapshot.NotFound`. An entry naming neither a size nor a snapshot gets 8 GiB. Snapshots
+ hold no data in Floci, so a volume made from one starts empty.
+- The root takes the AMI root's `VolumeType`, else `gp3`; other devices default to `gp2`, as
+ `CreateVolume` does. `Iops` applies only to `gp3`, `io1` and `io2` volumes and `Throughput` only
+ to `gp3`, so a request entry that changes a template device's type does not carry the old type's
+ settings over. A volume is encrypted when its entry or its snapshot says so, or when encryption
+ by default is on.
+- `DeleteOnTermination` defaults to `true` for every device a launch creates. When the instance
+ terminates, the volumes whose
+ attachment says `true` are deleted, the root included, unless another instance still has them
+ attached; the rest are detached. That holds as well for an instance that ends `terminated`
+ because its container never started: its volumes are settled the next time an EBS volume call
+ reads them, `DescribeVolumes` or a call naming one of them. A root kept this way holds no data
+ when the instance had a container, whose own file system was the root.
+- An entry without a `DeviceName` fails with `InvalidBlockDeviceMapping` ("Missing device name"),
+ as do a device name `AttachVolume` would refuse ("Invalid device name ...") and a device the
+ request or the template names twice ("/dev/sdh is being assigned to multiple times."), whether or
+ not a launch template is used. `CreateFleet` checks the device names before answering `DryRun`.
+- Nothing of a refused launch is kept: no instance, volume or network interface. A launch undone
+ after its instances exist, an instant `CreateFleet` whose later launch fails or a `RunInstances`
+ whose capacity reservation another launch took in the meantime, deletes every volume it created,
+ those with `DeleteOnTermination` false included. Auto Scaling and CloudFormation undo instances
+ they launched by terminating them, so there the volumes termination keeps stay.
+
+`DescribeInstances` lists every EBS volume attached to the instance in `BlockDeviceMappings`, root
+device first: those its launch created and any attached since, each with its own
+`DeleteOnTermination`; a terminated instance lists none.
Both blocks are validated by `CreateLaunchTemplate` and `CreateLaunchTemplateVersion` before
anything is stored. An `InstanceRequirements` block must carry `VCpuCount` and `MemoryMiB`, each
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
index 4dad37aa93..13c995b28e 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java
@@ -863,8 +863,13 @@ public class Ec2QueryHandler {
if (marketType == null && launchTemplateData.getInstanceMarketOptions() != null) {
marketType = launchTemplateData.getInstanceMarketOptions().getMarketType();
}
+ // Checked on each side before the merge, which would fold a device named twice into one;
+ // a request entry spelling a template device differently ("xvdf") then merges into it.
+ Ec2Service.requireValidLaunchDevices(launchTemplateData.getBlockDeviceMappings());
+ Ec2Service.requireValidLaunchDevices(blockDeviceMappings);
blockDeviceMappings = LaunchTemplateData.BlockDeviceMapping.merge(
- launchTemplateData.getBlockDeviceMappings(), blockDeviceMappings);
+ launchTemplateData.getBlockDeviceMappings(),
+ Ec2Service.spelledAs(launchTemplateData.getBlockDeviceMappings(), blockDeviceMappings));
if (!suppliedInterfaces) {
// With neither request interfaces nor, after the checks above, a request SubnetId
// or PrivateIpAddress, the template's primary interface places the launch.
@@ -953,8 +958,9 @@ public class Ec2QueryHandler {
.start("groupSet").end("groupSet")
.start("instancesSet");
Map<String, String> capacityAssociations = service.capacityReservationAssociations(region);
+ Map<String, List<VolumeAttachment>> volumeAttachments = service.instanceVolumeAttachments(res.getInstances());
for (Instance inst : res.getInstances()) {
- xml.start("item").raw(instanceXml(inst, capacityAssociations)).end("item");
+ xml.start("item").raw(instanceXml(inst, capacityAssociations, volumeAttachments)).end("item");
}
xml.end("instancesSet")
.end("RunInstancesResponse");
@@ -1053,11 +1059,12 @@ public class Ec2QueryHandler {
}
// The template stored them unchecked; the launch would refuse them, so the dry run does.
Ec2Service.validateMetadataOptions(launch.metadataOptions());
+ Ec2Service.requireValidLaunchDevices(launch.blockDeviceMappings());
}
checkDryRun(p);
List<FleetLaunchResult> results = new ArrayList<>(targetCapacity);
- List<String> launchedInstanceIds = new ArrayList<>(targetCapacity);
+ List<Reservation> launched = new ArrayList<>(targetCapacity);
try {
for (int i = 0; i < targetCapacity; i++) {
FleetLaunch launch = launches.get(i % launches.size());
@@ -1081,18 +1088,19 @@ public class Ec2QueryHandler {
null, null, launch.blockDeviceMappings(), "spot".equals(capacityType), null,
launch.capacityReservationSpecification());
Instance instance = reservation.getInstances().get(0);
- launchedInstanceIds.add(instance.getInstanceId());
+ launched.add(reservation);
results.add(new FleetLaunchResult(instance, launch));
}
} catch (RuntimeException e) {
- if (!launchedInstanceIds.isEmpty()) {
+ if (!launched.isEmpty()) {
LOG.warnv("CreateFleet failed after launching instances {0}; rolling them back: {1}",
- launchedInstanceIds, e.getMessage());
+ launched.stream().map(r -> r.getInstances().get(0).getInstanceId()).toList(), e.getMessage());
List<String> rollbackFailureInstanceIds = new ArrayList<>();
List<RuntimeException> rollbackFailures = new ArrayList<>();
- for (String instanceId : launchedInstanceIds) {
+ for (Reservation reservation : launched) {
+ String instanceId = reservation.getInstances().get(0).getInstanceId();
try {
- service.terminateInstances(region, List.of(instanceId));
+ service.rollBackLaunch(region, reservation);
} catch (RuntimeException cleanupFailure) {
rollbackFailureInstanceIds.add(instanceId);
rollbackFailures.add(cleanupFailure);
@@ -1415,6 +1423,8 @@ public class Ec2QueryHandler {
.elem("requestId", UUID.randomUUID().toString())
.start("reservationSet");
Map<String, String> capacityAssociations = service.capacityReservationAssociations(region);
+ Map<String, List<VolumeAttachment>> volumeAttachments = service.instanceVolumeAttachments(
+ reservations.stream().flatMap(res -> res.getInstances().stream()).toList());
for (Reservation res : reservations) {
xml.start("item")
.elem("reservationId", res.getReservationId())
@@ -1422,7 +1432,7 @@ public class Ec2QueryHandler {
.start("groupSet").end("groupSet")
.start("instancesSet");
for (Instance inst : res.getInstances()) {
- xml.start("item").raw(instanceXml(inst, capacityAssociations)).end("item");
+ xml.start("item").raw(instanceXml(inst, capacityAssociations, volumeAttachments)).end("item");
}
xml.end("instancesSet").end("item");
}
@@ -5134,7 +5144,8 @@ public class Ec2QueryHandler {
// ─── XML fragment builders ────────────────────────────────────────────────
- private String instanceXml(Instance inst, Map<String, String> capacityAssociations) {
+ private String instanceXml(Instance inst, Map<String, String> capacityAssociations,
+ Map<String, List<VolumeAttachment>> volumeAttachments) {
XmlBuilder xml = new XmlBuilder()
.elem("instanceId", inst.getInstanceId())
.elem("imageId", inst.getImageId())
@@ -5264,19 +5275,7 @@ public class Ec2QueryHandler {
if (capacityReservationId != null) {
xml.elem("capacityReservationId", capacityReservationId);
}
- if (inst.getRootVolumeId() != null) {
- xml.start("blockDeviceMapping")
- .start("item")
- .elem("deviceName", inst.getRootDeviceName())
- .start("ebs")
- .elem("volumeId", inst.getRootVolumeId())
- .elem("status", "attached")
- .elem("deleteOnTermination", "true")
- .elem("attachTime", inst.getLaunchTime() != null ? ISO_FMT.format(inst.getLaunchTime()) : "")
- .end("ebs")
- .end("item")
- .end("blockDeviceMapping");
- }
+ instanceBlockDeviceMappingXml(xml, volumeAttachments.getOrDefault(inst.getInstanceId(), List.of()));
if (inst.getIamInstanceProfileArn() != null) {
xml.start("iamInstanceProfile")
.elem("arn", inst.getIamInstanceProfileArn())
@@ -5287,6 +5286,24 @@ public class Ec2QueryHandler {
return xml.build();
}
+ /** An instance's {@code blockDeviceMapping}: the EBS volumes attached to it, root device first. */
+ private void instanceBlockDeviceMappingXml(XmlBuilder xml, List<VolumeAttachment> attachments) {
+ xml.start("blockDeviceMapping");
+ for (VolumeAttachment attachment : attachments) {
+ xml.start("item")
+ .elem("deviceName", attachment.getDevice())
+ .start("ebs")
+ .elem("volumeId", attachment.getVolumeId())
+ .elem("status", attachment.getState())
+ .elem("deleteOnTermination", String.valueOf(attachment.isDeleteOnTermination()));
+ if (attachment.getAttachTime() != null) {
+ xml.elem("attachTime", ISO_FMT.format(attachment.getAttachTime()));
+ }
+ xml.end("ebs").end("item");
+ }
+ xml.end("blockDeviceMapping");
+ }
+
private String resolveIamInstanceProfileArn(MultivaluedMap<String, String> p) {
return resolveIamInstanceProfileArn(p, "IamInstanceProfile");
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
index 3e4b80566c..1003899d1b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
@@ -194,6 +194,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
private static final String DOCUMENTATION_PREFIX = "203.0.113.";
private static final Set<String> VALID_VOLUME_TYPES =
Set.of("standard", "io1", "io2", "gp2", "sc1", "st1", "gp3");
+ /** The volume types whose IOPS are provisioned, and so the only ones a launch keeps Iops for. */
+ private static final Set<String> PROVISIONED_IOPS_VOLUME_TYPES = Set.of("gp3", "io1", "io2");
/** The accounts behind the two non-self owner aliases DescribeImages accepts. */
private static final String AMAZON_OWNER_ID = "137112412989";
private static final String AWS_MARKETPLACE_OWNER_ID = "679593333241";
@@ -3826,8 +3828,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
* released; with no subnet or zone named, the launch lands in the host's zone.
* @param tenancy Placement.Tenancy; defaults to {@code host} when a host is named.
* @param blockDeviceMappings the launch's BlockDeviceMappings (request merged over launch
- * template), or null. An entry naming the AMI's root device
- * overrides the root volume type, as on AWS.
+ * template), or null. They apply over the AMI's own mappings, and
+ * each EBS device left creates a volume; see {@link #planLaunchVolumes}.
*/
public Reservation runInstances(String region, String imageId, String instanceType,
int minCount, int maxCount, String keyName,
@@ -4063,13 +4065,13 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
String architecture = architectureFor(region, imageId, effectiveInstanceType);
- // An entry with NoDevice set removes its device whatever else it names, as on AWS.
- List<LaunchTemplateData.BlockDeviceMapping> mappedDevices = blockDeviceMappings == null ? List.of()
- : blockDeviceMappings.stream().filter(m -> m.getNoDevice() == null).toList();
- mappedDevices.stream()
+ List<LaunchTemplateData.BlockDeviceMapping> launchMappings =
+ blockDeviceMappings == null ? List.of() : blockDeviceMappings;
+ // An entry with NoDevice set names no device to create, so its type is not checked.
+ launchMappings.stream()
+ .filter(m -> m.getNoDevice() == null)
.filter(m -> m.getEbs() != null && m.getEbs().getVolumeType() != null)
.forEach(m -> requireValidVolumeType(m.getEbs().getVolumeType()));
- String rootVolumeType = rootVolumeTypeFor(findImageForCapture(region, imageId), mappedDevices);
List<Instance> launched = new ArrayList<>();
// Resolving the AMI, building the instances that depend on it and publishing them is one
// step. A DeregisterImage releases the captured layer only when no live instance resolves
@@ -4088,6 +4090,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
ResolvedAmiImage dockerImage = null;
synchronized (imageRegistryLock) {
requireNotDeregistered(region, imageId);
+ // Planned under the lock DeregisterImage deletes snapshots under, so a snapshot a
+ // mapping names is still there when its volume is stored.
+ Image image = findImageForCapture(region, imageId);
+ String rootDeviceName = image != null && image.getRootDeviceName() != null
+ ? image.getRootDeviceName() : new Instance().getRootDeviceName();
+ List<LaunchVolume> launchVolumes = planLaunchVolumes(region, image, rootDeviceName, launchMappings);
if (!config.services().ec2().mock()) {
// A CreateImage AMI is not in the catalog, so resolve through its source. The
// ancestor supplies the guest runtime (systemd vs minimal, cloud-init), which a
@@ -4214,27 +4222,25 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
- // Root EBS volume
- String rootVolId = "vol-" + randomHex(17);
- inst.setRootVolumeId(rootVolId);
- Volume rootVol = new Volume();
- rootVol.setVolumeId(rootVolId);
- rootVol.setAvailabilityZone(az);
- rootVol.setVolumeType(rootVolumeType);
- rootVol.setSize(DEFAULT_ROOT_VOLUME_SIZE_GIB);
- rootVol.setState("in-use");
- rootVol.setRegion(region);
- rootVol.setEncrypted(ebsEncryptionByDefault(region));
- rootVol.setCreateTime(Instant.now());
- VolumeAttachment att = new VolumeAttachment();
- att.setVolumeId(rootVolId);
- att.setInstanceId(instanceId);
- att.setDevice(inst.getRootDeviceName());
- att.setState("attached");
- att.setDeleteOnTermination(true);
- att.setAttachTime(Instant.now());
- rootVol.getAttachments().add(att);
- volumes.put(key(region, rootVolId), rootVol);
+ inst.setRootDeviceName(rootDeviceName);
+ for (LaunchVolume planned : launchVolumes) {
+ Volume volume = newVolume(region, az, planned.volumeType(), planned.size(),
+ planned.encrypted(), planned.iops(), planned.throughput(), planned.snapshotId());
+ volume.setState("in-use");
+ VolumeAttachment attachment = new VolumeAttachment();
+ attachment.setVolumeId(volume.getVolumeId());
+ attachment.setInstanceId(instanceId);
+ attachment.setDevice(planned.deviceName());
+ attachment.setState("attached");
+ attachment.setDeleteOnTermination(planned.deleteOnTermination());
+ attachment.setAttachTime(inst.getLaunchTime());
+ volume.getAttachments().add(attachment);
+ volumes.put(key(region, volume.getVolumeId()), volume);
+ reservation.getLaunchVolumeIds().add(volume.getVolumeId());
+ if (rootDeviceName.equals(planned.deviceName())) {
+ inst.setRootVolumeId(volume.getVolumeId());
+ }
+ }
instances.put(key(region, instanceId), inst);
launched.add(inst);
@@ -4248,7 +4254,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
acquireCapacity(region, launched);
} catch (RuntimeException failure) {
try {
- terminateInstances(region, launched.stream().map(Instance::getInstanceId).toList());
+ rollBackLaunch(region, reservation);
} catch (RuntimeException cleanupFailure) {
failure.addSuppressed(cleanupFailure);
LOG.errorv(cleanupFailure, "Failed to clean up rejected capacity launch in {0}", region);
@@ -5066,21 +5072,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
writePublicAddress(inst, () -> clearPublicAddress(inst));
}
}
- // Delete root volume if deleteOnTermination (matches real AWS behavior)
- if (inst.getRootVolumeId() != null) {
- Volume rootVol = volumes.get(key(region, inst.getRootVolumeId())).orElse(null);
- if (rootVol != null) {
- boolean attachedElsewhere = rootVol.getAttachments().stream()
- .anyMatch(a -> !inst.getInstanceId().equals(a.getInstanceId()));
- if (!attachedElsewhere) {
- if (volumeBlockDeviceManager != null) {
- volumeBlockDeviceManager.deleteVolume(inst.getRootVolumeId());
- }
- volumes.delete(key(region, inst.getRootVolumeId()));
- removeTags(region, inst.getRootVolumeId());
- }
- }
- }
detachAttachedVolumesOnTermination(region, inst);
releaseStandaloneInterfacesOnTermination(region, inst);
instances.put(key(region, id), inst);
@@ -8684,31 +8675,188 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return mapping;
}
+ /** One EBS volume a launch creates, settled before anything of the launch is stored. */
+ private record LaunchVolume(String deviceName, String snapshotId, int size, String volumeType,
+ int iops, Integer throughput, boolean encrypted, boolean deleteOnTermination) {}
+
/**
- * The root volume takes the type a request mapping for the AMI's root device names, else the
- * type the AMI's root mapping declares, as on AWS.
- */
- private static String rootVolumeTypeFor(Image image,
- List<LaunchTemplateData.BlockDeviceMapping> requestMappings) {
- String rootDeviceName = image != null && image.getRootDeviceName() != null
- ? image.getRootDeviceName() : new Instance().getRootDeviceName();
- if (requestMappings != null) {
- for (LaunchTemplateData.BlockDeviceMapping m : requestMappings) {
- if (m.getEbs() != null && m.getEbs().getVolumeType() != null
- && Objects.equals(m.getDeviceName(), rootDeviceName)) {
- return m.getEbs().getVolumeType();
+ * The EBS volumes a launch creates, one per device: the AMI's block device mappings with the
+ * launch's own over them. A launch's entries "overwrite or merge with the block device mapping
+ * of the AMI" field by field, so an entry resizing a device the AMI maps keeps its snapshot,
+ * and the AMI itself is not changed. {@code NoDevice} keeps a device the AMI maps from being
+ * created, and an entry naming an instance store volume ({@code VirtualName}) takes its device
+ * over, but neither applies to the root device, which every launch has: such an entry for the
+ * root leaves the AMI's root as it maps it. An entry without an {@code Ebs} block creates
+ * nothing.
+ *
+ * <p>A volume takes its snapshot's size unless its entry names one, which may not be smaller.
+ * The root takes the AMI root's type, else {@code gp3}, and DeleteOnTermination defaults to
+ * true for every device the launch creates. IOPS are kept only for the types that take them
+ * (gp3, io1, io2) and throughput only for gp3, so an entry that changes a template device's
+ * type does not carry the old type's settings onto the new one.
+ *
+ * @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-block-device-mapping.html">Add block device mappings to Amazon EC2 instance</a>
+ * @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/preserving-volumes-on-termination.html">Preserve data when an instance is terminated</a>
+ */
+ private List<LaunchVolume> planLaunchVolumes(String region, Image image, String rootDeviceName,
+ List<LaunchTemplateData.BlockDeviceMapping> launchMappings) {
+ requireValidLaunchDevices(launchMappings);
+ List<LaunchTemplateData.BlockDeviceMapping> amiMappings = new ArrayList<>();
+ if (image != null && image.getBlockDeviceMappings() != null) {
+ for (BlockDeviceMapping declared : image.getBlockDeviceMappings()) {
+ LaunchTemplateData.BlockDeviceMapping mapping = launchMapping(declared);
+ if (isSameDevice(mapping.getDeviceName(), rootDeviceName)) {
+ // An AMI may map its root as "xvda" while its RootDeviceName says "/dev/xvda".
+ mapping.setDeviceName(rootDeviceName);
}
+ amiMappings.add(mapping);
+ }
+ }
+ if (amiMappings.stream().noneMatch(m -> rootDeviceName.equals(m.getDeviceName()))) {
+ // A catalog AMI need not map its root device; it then gets the default root volume.
+ // Its size is left unset until the launch's entries are merged, so a root entry
+ // naming only a snapshot takes the snapshot's size.
+ LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs();
+ LaunchTemplateData.BlockDeviceMapping root = new LaunchTemplateData.BlockDeviceMapping();
+ root.setDeviceName(rootDeviceName);
+ root.setEbs(ebs);
+ amiMappings.addFirst(root);
+ }
+ // An entry naming a device the AMI maps, "xvdf" for its "/dev/xvdf" included, is merged
+ // into it. NoDevice removes its device and a VirtualName makes it an instance store
+ // volume, so neither leaves an EBS volume there, whatever else the entry names; the root
+ // can be neither, so such an entry for it leaves the AMI's own root in place.
+ List<LaunchTemplateData.BlockDeviceMapping> overrides = new ArrayList<>();
+ for (LaunchTemplateData.BlockDeviceMapping mapping : spelledAs(amiMappings, launchMappings)) {
+ if (rootDeviceName.equals(mapping.getDeviceName())
+ && (mapping.getNoDevice() != null || mapping.namesInstanceStore())) {
+ continue;
}
+ overrides.add(mapping);
}
- if (image == null || image.getBlockDeviceMappings() == null) {
- return DEFAULT_ROOT_VOLUME_TYPE;
+ List<LaunchVolume> planned = new ArrayList<>();
+ for (LaunchTemplateData.BlockDeviceMapping mapping
+ : LaunchTemplateData.BlockDeviceMapping.merge(amiMappings, overrides)) {
+ boolean root = rootDeviceName.equals(mapping.getDeviceName());
+ LaunchTemplateData.Ebs ebs = mapping.getEbs();
+ if (!root && (mapping.getNoDevice() != null || mapping.namesInstanceStore() || ebs == null)) {
+ continue;
+ }
+ if (ebs == null) {
+ ebs = new LaunchTemplateData.Ebs();
+ }
+ String snapshotId = ebs.getSnapshotId() == null || ebs.getSnapshotId().isBlank()
+ ? null : ebs.getSnapshotId();
+ Snapshot snapshot = snapshotId == null ? null : snapshots.get(key(region, snapshotId))
+ .orElseThrow(() -> new AwsException("InvalidSnapshot.NotFound",
+ "The snapshot '" + snapshotId + "' does not exist.", 400));
+ Integer snapshotSize = snapshot == null ? null : snapshot.getVolumeSize();
+ // With neither a size nor a sized snapshot, the root gets the default root size and
+ // any other device 0, which leaves the size to CreateVolume's default.
+ int size = ebs.getVolumeSize() != null ? ebs.getVolumeSize()
+ : snapshotSize != null ? snapshotSize : root ? DEFAULT_ROOT_VOLUME_SIZE_GIB : 0;
+ if (snapshotSize != null && size < snapshotSize) {
+ throw new AwsException("InvalidBlockDeviceMapping", "Volume of size " + size
+ + "GB is smaller than snapshot '" + snapshotId + "', expect size>= " + snapshotSize + "GB", 400);
+ }
+ String volumeType = ebs.getVolumeType() != null ? ebs.getVolumeType()
+ : root ? DEFAULT_ROOT_VOLUME_TYPE : null;
+ boolean takesIops = volumeType != null && PROVISIONED_IOPS_VOLUME_TYPES.contains(volumeType);
+ int iops = takesIops && ebs.getIops() != null ? ebs.getIops() : 0;
+ Integer throughput = "gp3".equals(volumeType) ? ebs.getThroughput() : null;
+ planned.add(new LaunchVolume(mapping.getDeviceName(), snapshotId, size, volumeType, iops, throughput,
+ Boolean.TRUE.equals(ebs.getEncrypted()) || snapshot != null && snapshot.isEncrypted(),
+ !Boolean.FALSE.equals(ebs.getDeleteOnTermination())));
+ }
+ return planned;
+ }
+
+ /**
+ * Refuses the launch block device mappings AWS refuses before anything else: an entry without
+ * a device name, a device name AttachVolume would refuse, and a device named twice, "xvdh" and
+ * "/dev/xvdh" being one device as they are to AttachVolume.
+ */
+ static void requireValidLaunchDevices(List<LaunchTemplateData.BlockDeviceMapping> mappings) {
+ if (mappings == null) {
+ return;
}
- return image.getBlockDeviceMappings().stream()
- .filter(m -> m.getEbs() != null && m.getEbs().getVolumeType() != null
- && Objects.equals(m.getDeviceName(), image.getRootDeviceName()))
- .map(m -> m.getEbs().getVolumeType())
- .findFirst()
- .orElse(DEFAULT_ROOT_VOLUME_TYPE);
+ Set<String> named = new HashSet<>();
+ for (LaunchTemplateData.BlockDeviceMapping mapping : mappings) {
+ String device = mapping.getDeviceName();
+ if (device == null || device.isBlank()) {
+ throw new AwsException("InvalidBlockDeviceMapping", "Missing device name", 400);
+ }
+ if (!DEVICE_NAME_PATTERN.matcher(device).matches() || device.contains("..")) {
+ throw new AwsException("InvalidBlockDeviceMapping", "Invalid device name " + device, 400);
+ }
+ if (!named.add(normalizedDevice(device))) {
+ throw new AwsException("InvalidBlockDeviceMapping",
+ device + " is being assigned to multiple times.", 400);
+ }
+ }
+ }
+
+ /**
+ * Launch entries with each device the base maps spelled as the base spells it, "xvdf" becoming
+ * a base's "/dev/xvdf", so that merging them over the base folds each entry into the device it
+ * names. A renamed entry is a copy; the entries given are never changed.
+ */
+ static List<LaunchTemplateData.BlockDeviceMapping> spelledAs(List<LaunchTemplateData.BlockDeviceMapping> base,
+ List<LaunchTemplateData.BlockDeviceMapping> entries) {
+ Map<String, String> spellings = new HashMap<>();
+ for (LaunchTemplateData.BlockDeviceMapping mapping : base) {
+ if (mapping.getDeviceName() != null) {
+ spellings.putIfAbsent(normalizedDevice(mapping.getDeviceName()), mapping.getDeviceName());
+ }
+ }
+ List<LaunchTemplateData.BlockDeviceMapping> spelled = new ArrayList<>(entries.size());
+ for (LaunchTemplateData.BlockDeviceMapping mapping : entries) {
+ String device = mapping.getDeviceName() == null ? null
+ : spellings.get(normalizedDevice(mapping.getDeviceName()));
+ spelled.add(device == null || device.equals(mapping.getDeviceName()) ? mapping : renamed(mapping, device));
+ }
+ return spelled;
+ }
+
+ /** A copy of a launch entry under another spelling of its device name; the original is untouched. */
+ private static LaunchTemplateData.BlockDeviceMapping renamed(LaunchTemplateData.BlockDeviceMapping mapping,
+ String deviceName) {
+ LaunchTemplateData.BlockDeviceMapping copy = new LaunchTemplateData.BlockDeviceMapping();
+ copy.setDeviceName(deviceName);
+ copy.setVirtualName(mapping.getVirtualName());
+ copy.setNoDevice(mapping.getNoDevice());
+ copy.setEbs(mapping.getEbs());
+ return copy;
+ }
+
+ /**
+ * An AMI's block device mapping in the launch-time shape the launch's own entries merge over.
+ * RegisterImage keeps an entry naming no Ebs member, such as an instance store or a NoDevice
+ * entry, with an empty Ebs block; that entry describes no EBS volume, so it gets none here.
+ */
+ private static LaunchTemplateData.BlockDeviceMapping launchMapping(BlockDeviceMapping declared) {
+ LaunchTemplateData.BlockDeviceMapping mapping = new LaunchTemplateData.BlockDeviceMapping();
+ mapping.setDeviceName(declared.getDeviceName());
+ EbsBlockDevice declaredEbs = declared.getEbs();
+ if (describesVolume(declaredEbs)) {
+ LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs();
+ ebs.setSnapshotId(declaredEbs.getSnapshotId());
+ ebs.setVolumeSize(declaredEbs.getVolumeSize());
+ ebs.setVolumeType(declaredEbs.getVolumeType());
+ ebs.setDeleteOnTermination(declaredEbs.getDeleteOnTermination());
+ ebs.setEncrypted(declaredEbs.getEncrypted());
+ mapping.setEbs(ebs);
+ }
+ return mapping;
+ }
+
+ /**
+ * Whether an AMI entry's Ebs block describes a volume. RegisterImage keeps an empty one for an
+ * entry naming no Ebs member, such as an instance store or a NoDevice entry.
+ */
+ private static boolean describesVolume(EbsBlockDevice ebs) {
+ return ebs != null && (ebs.getSnapshotId() != null || ebs.getVolumeSize() != null
+ || ebs.getVolumeType() != null || ebs.getDeleteOnTermination() != null || ebs.getEncrypted() != null);
}
/** The image a CreateImage source was launched from, whether catalog-backed or registered. */
@@ -13232,10 +13380,29 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
int size, boolean encrypted, int iops, Integer throughput,
String snapshotId, List<Tag> volumeTags) {
ensureDefaultResources(region);
- String volumeId = "vol-" + randomHex(17);
+ Volume vol = newVolume(region, availabilityZone, volumeType, size, encrypted, iops, throughput, snapshotId);
+ String volumeId = vol.getVolumeId();
+ if (volumeTags != null) {
+ vol.setTags(new ArrayList<>(volumeTags));
+ // Tags on create are tags like any other: DescribeTags lists them in this region.
+ putTags(region, vol.getVolumeId(), new ArrayList<>(volumeTags));
+ }
+ volumes.put(key(region, volumeId), vol);
+ if (volumeBlockDeviceManager != null) {
+ volumeBlockDeviceManager.createVolume(volumeId, vol.getSize());
+ }
+ return vol;
+ }
+
+ /**
+ * A new, unattached volume record, not yet stored, with the defaults CreateVolume and a
+ * launch's block device mappings share.
+ */
+ private Volume newVolume(String region, String availabilityZone, String volumeType, int size,
+ boolean encrypted, int iops, Integer throughput, String snapshotId) {
String effectiveType = volumeType != null ? volumeType : "gp2";
Volume vol = new Volume();
- vol.setVolumeId(volumeId);
+ vol.setVolumeId("vol-" + randomHex(17));
vol.setAvailabilityZone(availabilityZone != null ? availabilityZone : region + "a");
vol.setVolumeType(effectiveType);
vol.setSize(size > 0 ? size : 8);
@@ -13253,15 +13420,6 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
vol.setCreateTime(Instant.now());
vol.setState("available");
vol.setRegion(region);
- if (volumeTags != null) {
- vol.setTags(new ArrayList<>(volumeTags));
- // Tags on create are tags like any other: DescribeTags lists them in this region.
- putTags(region, vol.getVolumeId(), new ArrayList<>(volumeTags));
- }
- volumes.put(key(region, volumeId), vol);
- if (volumeBlockDeviceManager != null) {
- volumeBlockDeviceManager.createVolume(volumeId, vol.getSize());
- }
return vol;
}
@@ -13274,7 +13432,7 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
List<Map.Entry<String, List<String>>> filters) {
if (volumeIds != null && !volumeIds.isEmpty()) {
for (String id : volumeIds) {
- if (volumes.get(key(region, id)).orElse(null) == null) {
+ if (volumes.get(key(region, id)).flatMap(v -> settleEndedHolders(region, v)).isEmpty()) {
throw new AwsException("InvalidVolume.NotFound",
"The volume '" + id + "' does not exist.", 400);
}
@@ -13283,6 +13441,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
return volumes.scan(k -> true).stream()
.filter(v -> v.getRegion().equals(region))
.filter(v -> volumeIds == null || volumeIds.isEmpty() || volumeIds.contains(v.getVolumeId()))
+ .map(v -> settleEndedHolders(region, v))
+ .flatMap(Optional::stream)
.filter(v -> matchesFilters(v, filters, region))
.collect(Collectors.toList());
}
@@ -13293,8 +13453,8 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
withVolumeLock(volumeId, () -> {
ensureDefaultResources(region);
- Volume volume = volumes.get(key(region, volumeId)).orElseThrow(() ->
- new AwsException("InvalidVolume.NotFound",
+ Volume volume = volumes.get(key(region, volumeId)).flatMap(v -> settleEndedHolders(region, v))
+ .orElseThrow(() -> new AwsException("InvalidVolume.NotFound",
"The volume '" + volumeId + "' does not exist.", 400));
if ("in-use".equals(volume.getState()) || !volume.getAttachments().isEmpty()) {
throw new AwsException("VolumeInUse",
@@ -13357,9 +13517,12 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
if (d1 == null || d2 == null) {
return false;
}
- String n1 = d1.startsWith("/") ? d1 : "/dev/" + d1;
- String n2 = d2.startsWith("/") ? d2 : "/dev/" + d2;
- return n1.equals(n2);
+ return normalizedDevice(d1).equals(normalizedDevice(d2));
+ }
+
+ /** A device name with the "/dev/" a bare name such as "xvdf" leaves implicit. */
+ private static String normalizedDevice(String device) {
+ return device.startsWith("/") ? device : "/dev/" + device;
}
private boolean isRootVolume(String region, Volume volume) {
@@ -13535,6 +13698,45 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
.collect(Collectors.toList());
}
+ /**
+ * The EBS volumes attached to each of the instances, as their block device mappings report
+ * them: the volumes a launch created and any attached since, root device first, then by device
+ * name. The volume store is read once for all of them, so a response listing many instances
+ * does not scan it once per instance. A terminated instance has none, whichever way it ended.
+ */
+ public Map<String, List<VolumeAttachment>> instanceVolumeAttachments(Collection<Instance> instances) {
+ Map<String, List<VolumeAttachment>> byInstance = new HashMap<>();
+ Map<String, Instance> live = new HashMap<>();
+ Set<String> regions = new HashSet<>();
+ for (Instance instance : instances) {
+ byInstance.put(instance.getInstanceId(), new ArrayList<>());
+ if (instance.getRegion() != null
+ && (instance.getState() == null || !"terminated".equals(instance.getState().getName()))) {
+ live.put(instance.getInstanceId(), instance);
+ regions.add(instance.getRegion());
+ }
+ }
+ for (String region : regions) {
+ for (Volume volume : volumes.scan(k -> k.startsWith(region + "::"))) {
+ if (!region.equals(volume.getRegion())) {
+ continue;
+ }
+ // A copy: termination, a settle, AttachVolume and DetachVolume change the list
+ // under the volume's lock, which a read does not take.
+ for (VolumeAttachment attachment : new ArrayList<>(volume.getAttachments())) {
+ Instance instance = live.get(attachment.getInstanceId());
+ if (instance != null && region.equals(instance.getRegion())) {
+ byInstance.get(instance.getInstanceId()).add(attachment);
+ }
+ }
+ }
+ }
+ live.values().forEach(instance -> byInstance.get(instance.getInstanceId()).sort(
+ Comparator.comparing((VolumeAttachment a) -> !isSameDevice(a.getDevice(), instance.getRootDeviceName()))
+ .thenComparing(VolumeAttachment::getDevice, Comparator.nullsLast(Comparator.naturalOrder()))));
+ return byInstance;
+ }
+
public VolumeAttachment attachVolume(String region, String volumeId, String instanceId, String device) {
ensureDefaultResources(region);
if (volumeId == null || volumeId.isEmpty()) {
@@ -13651,9 +13853,9 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
private Volume getRequiredVolume(String region, String volumeId) {
- return volumes.get(key(region, volumeId)).orElseThrow(() ->
- new AwsException("InvalidVolume.NotFound", "The volume '" + volumeId + "' does not exist.", 400)
- );
+ return volumes.get(key(region, volumeId)).flatMap(volume -> settleEndedHolders(region, volume))
+ .orElseThrow(() -> new AwsException("InvalidVolume.NotFound",
+ "The volume '" + volumeId + "' does not exist.", 400));
}
// ─── Network Interfaces ──────────────────────────────────────────────────────────
@@ -14191,37 +14393,113 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider, Resettab
}
}
+ /**
+ * Undoes a launch refused after its instances were stored: terminates them and deletes every
+ * volume the launch created, those it would keep on termination included, so nothing of the
+ * refused launch is left. The volumes are the ones the launch recorded as it created them, so
+ * one whose attachment already ended, as a failed container's are settled by whichever call
+ * reads them first, is still found. Only a volume termination left unattached is deleted: an
+ * instance whose termination fails keeps its volumes as they were, and a volume attached to
+ * another instance since stays.
+ */
+ public List<Map<String, String>> rollBackLaunch(String region, Reservation launch) {
+ try {
+ return terminateInstances(region, launch.getInstances().stream().map(Instance::getInstanceId).toList());
+ } finally {
+ for (String volumeId : launch.getLaunchVolumeIds()) {
+ String k = key(region, volumeId);
+ withVolumeLock(volumeId, () -> volumes.get(k)
+ .filter(current -> current.getAttachments().isEmpty())
+ .ifPresent(current -> deleteVolumeRecord(region, k, current)));
+ }
+ }
+ }
+
+ /**
+ * Each volume attached to a terminating instance, the root included, goes as its attachment's
+ * DeleteOnTermination says: deleted when it is set, unless another instance still has the
+ * volume attached, and otherwise detached, left available or, still held by another
+ * instance, in-use.
+ */
private void detachAttachedVolumesOnTermination(String region, Instance inst) {
for (String k : volumes.keys()) {
Volume vol = volumes.get(k).orElse(null);
- if (vol == null || !region.equals(vol.getRegion()) || vol.getAttachments().isEmpty()) {
+ if (vol == null || !region.equals(vol.getRegion())
+ || vol.getAttachments().stream().noneMatch(a -> inst.getInstanceId().equals(a.getInstanceId()))) {
continue;
}
- boolean modified = false;
- for (VolumeAttachment att : new ArrayList<>(vol.getAttachments())) {
- if (inst.getInstanceId().equals(att.getInstanceId())) {
- if (volumeBlockDeviceManager != null) {
- volumeBlockDeviceManager.detachVolume(vol, inst, att.getDevice());
- }
- if (att.isDeleteOnTermination()) {
- if (volumeBlockDeviceManager != null) {
- volumeBlockDeviceManager.deleteVolume(vol.getVolumeId());
- }
- volumes.delete(k);
- removeTags(region, vol.getVolumeId());
- modified = false;
- break;
- } else {
- vol.getAttachments().remove(att);
- modified = true;
- }
+ withVolumeLock(vol.getVolumeId(), () -> endAttachments(region, k, inst));
+ }
+ }
+
+ /**
+ * Ends one volume's attachments to an instance that has terminated. Called under the volume's
+ * lock, and on the volume as the store holds it now.
+ */
+ private void endAttachments(String region, String storeKey, Instance inst) {
+ Volume vol = volumes.get(storeKey).orElse(null);
+ if (vol == null) {
+ return;
+ }
+ boolean modified = false;
+ for (VolumeAttachment att : new ArrayList<>(vol.getAttachments())) {
+ if (inst.getInstanceId().equals(att.getInstanceId())) {
+ // The root has no block device to remove: it is the container's own file system.
+ boolean root = vol.getVolumeId().equals(inst.getRootVolumeId())
+ || isSameDevice(att.getDevice(), inst.getRootDeviceName());
+ if (volumeBlockDeviceManager != null && !root) {
+ volumeBlockDeviceManager.detachVolume(vol, inst, att.getDevice());
}
+ boolean attachedElsewhere = vol.getAttachments().stream()
+ .anyMatch(other -> !inst.getInstanceId().equals(other.getInstanceId()));
+ if (att.isDeleteOnTermination() && !attachedElsewhere) {
+ deleteVolumeRecord(region, storeKey, vol);
+ return;
+ }
+ vol.getAttachments().remove(att);
+ modified = true;
}
- if (modified) {
- vol.setState(vol.getAttachments().isEmpty() ? "available" : "in-use");
- volumes.put(k, vol);
+ }
+ if (modified) {
+ vol.setState(vol.getAttachments().isEmpty() ? "available" : "in-use");
+ volumes.put(storeKey, vol);
+ }
+ }
+
+ /** Deletes a volume, its block device and its tags. Called under the volume's lock. */
+ private void deleteVolumeRecord(String region, String storeKey, Volume vol) {
+ if (volumeBlockDeviceManager != null) {
+ volumeBlockDeviceManager.deleteVolume(vol.getVolumeId());
+ }
+ volumes.delete(storeKey);
+ removeTags(region, vol.getVolumeId());
+ }
+
+ /**
+ * A volume as it stands once the attachments it still holds to a terminated instance are
+ * ended. TerminateInstances ends them itself, but a container-backed launch that fails or is
+ * cancelled ends "terminated" inside the container manager, which has no access to this
+ * store, and would leave the volumes its launch created in-use on an instance that is gone.
+ * Settling on read covers every such path at once, as {@link #releaseIfHostIsGone} does for
+ * interfaces, and applies what termination would have: DeleteOnTermination deletes.
+ *
+ * @return the volume, or empty when it was deleted
+ */
+ private Optional<Volume> settleEndedHolders(String region, Volume volume) {
+ List<Instance> ended = new ArrayList<>();
+ for (VolumeAttachment attachment : new ArrayList<>(volume.getAttachments())) {
+ Instance holder = attachment.getInstanceId() == null ? null
+ : instances.get(key(region, attachment.getInstanceId())).orElse(null);
+ if (holder != null && holder.getState() != null && "terminated".equals(holder.getState().getName())) {
+ ended.add(holder);
}
}
+ if (ended.isEmpty()) {
+ return Optional.of(volume);
+ }
+ String storeKey = key(region, volume.getVolumeId());
+ withVolumeLock(volume.getVolumeId(), () -> ended.forEach(holder -> endAttachments(region, storeKey, holder)));
+ return volumes.get(storeKey);
}
private NetworkInterface requireStandaloneNetworkInterface(String region, String networkInterfaceId) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java
index 77167cde88..4d179b8a1f 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/LaunchTemplateData.java
@@ -405,10 +405,18 @@ public class LaunchTemplateData {
public Ebs getEbs() { return ebs; }
public void setEbs(Ebs ebs) { this.ebs = ebs; }
+ /** Whether this entry names an instance store volume: a non-blank {@code VirtualName}. */
+ public boolean namesInstanceStore() {
+ return virtualName != null && !virtualName.isBlank();
+ }
+
/**
* A launch's mappings, as EC2 documents template overrides: a request entry for a device the
* template maps changes only the members it sets, and every other device keeps its own
- * entry. The template's stored entries are never modified.
+ * entry. A device is an EBS volume, an instance store volume or suppressed, so a request
+ * entry naming any of the three replaces the template's choice for that device, an EBS
+ * entry still merging member by member into the template's. The template's stored entries
+ * are never modified.
*/
public static List<BlockDeviceMapping> merge(List<BlockDeviceMapping> template,
List<BlockDeviceMapping> request) {
@@ -421,9 +429,15 @@ public class LaunchTemplateData {
private static BlockDeviceMapping overlaid(BlockDeviceMapping base, BlockDeviceMapping override) {
BlockDeviceMapping merged = new BlockDeviceMapping();
merged.deviceName = base.deviceName;
- merged.virtualName = override.virtualName != null ? override.virtualName : base.virtualName;
- merged.noDevice = override.noDevice != null ? override.noDevice : base.noDevice;
- merged.ebs = Ebs.overlaid(base.ebs, override.ebs);
+ if (override.ebs == null && !override.namesInstanceStore() && override.noDevice == null) {
+ merged.virtualName = base.virtualName;
+ merged.noDevice = base.noDevice;
+ merged.ebs = base.ebs;
+ } else {
+ merged.virtualName = override.virtualName;
+ merged.noDevice = override.noDevice;
+ merged.ebs = override.ebs == null ? null : Ebs.overlaid(base.ebs, override.ebs);
+ }
return merged;
}
}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Reservation.java b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Reservation.java
index f1347b846b..0f0009245c 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/model/Reservation.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/model/Reservation.java
@@ -1,5 +1,6 @@
package io.github.hectorvent.floci.services.ec2.model;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -14,6 +15,9 @@ public class Reservation {
private String ownerId;
private List<GroupIdentifier> groups = new ArrayList<>();
private List<Instance> instances = new ArrayList<>();
+ /** The volumes the launch created, kept for a caller that must undo it; never stored or returned. */
+ @JsonIgnore
+ private List<String> launchVolumeIds = new ArrayList<>();
public Reservation() {}
@@ -28,4 +32,7 @@ public class Reservation {
public List<Instance> getInstances() { return instances; }
public void setInstances(List<Instance> instances) { this.instances = instances; }
+
+ @JsonIgnore
+ public List<String> getLaunchVolumeIds() { return launchVolumeIds; }
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java
index 4ca0eb9af6..ebc141c004 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2CreateFleetRollbackTest.java
@@ -39,18 +39,20 @@ class Ec2CreateFleetRollbackTest {
template.setInstanceType("t3.micro");
when(service.resolveLaunchTemplateData(REGION, "lt-test", null, "1")).thenReturn(template);
+ Reservation first = reservation("i-first");
+ Reservation second = reservation("i-second");
when(service.runInstances(anyString(), anyString(), anyString(), anyInt(), anyInt(), nullable(String.class),
anyList(), nullable(String.class), nullable(String.class), anyList(), nullable(String.class),
nullable(String.class), nullable(Boolean.class), nullable(String.class), anyInt(),
nullable(String.class), nullable(LaunchTemplateData.MetadataOptions.class),
nullable(String.class), nullable(String.class), eq(false), nullable(String.class),
nullable(String.class), anyList(), eq(false), nullable(String.class), nullable(LaunchTemplateData.CapacityReservationSpecification.class)))
- .thenReturn(reservation("i-first"))
- .thenReturn(reservation("i-second"))
+ .thenReturn(first)
+ .thenReturn(second)
.thenThrow(new AwsException("InvalidSubnetID.NotFound", "launch failed", 400));
- when(service.terminateInstances(REGION, List.of("i-first")))
+ when(service.rollBackLaunch(REGION, first))
.thenThrow(new AwsException("InternalError", "cleanup failed", 500));
- when(service.terminateInstances(REGION, List.of("i-second"))).thenReturn(List.of());
+ when(service.rollBackLaunch(REGION, second)).thenReturn(List.of());
Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
@@ -62,8 +64,8 @@ class Ec2CreateFleetRollbackTest {
String responseBody = response.getEntity().toString();
assertTrue(responseBody.contains("<Code>InvalidSubnetID.NotFound</Code>"));
assertTrue(responseBody.contains("CreateFleet rollback incomplete for instance(s): i-first"));
- verify(service).terminateInstances(REGION, List.of("i-first"));
- verify(service).terminateInstances(REGION, List.of("i-second"));
+ verify(service).rollBackLaunch(REGION, first);
+ verify(service).rollBackLaunch(REGION, second);
}
private static Reservation reservation(String instanceId) {
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
index 25829b1b5b..3e07e1709c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2IntegrationTest.java
@@ -6029,6 +6029,7 @@ class Ec2IntegrationTest {
.extract().path("RunInstancesResponse.instancesSet.item.instanceId");
}
+ /** The type of the instance's root volume, ami-0abcdef1234567890's /dev/xvda; data devices also get volumes. */
private String rootVolumeType(String instanceId) {
return given()
.formParam("Action", "DescribeVolumes")
@@ -6039,6 +6040,7 @@ class Ec2IntegrationTest {
.post("/")
.then()
.statusCode(200)
- .extract().path("DescribeVolumesResponse.volumeSet.item.volumeType");
+ .extract().path("DescribeVolumesResponse.volumeSet.item.find { it.attachmentSet.item.device == '/dev/xvda' }"
+ + ".volumeType");
}
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2LaunchBlockDeviceMappingsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2LaunchBlockDeviceMappingsIntegrationTest.java
new file mode 100644
index 0000000000..4b13ee4a2e
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2LaunchBlockDeviceMappingsIntegrationTest.java
@@ -0,0 +1,586 @@
+package io.github.hectorvent.floci.services.ec2;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.path.xml.XmlPath;
+import io.restassured.response.ValidatableResponse;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * RunInstances creates one EBS volume for each device of the AMI's block device mapping with the
+ * launch's own entries over it, field by field, and DescribeInstances lists the volumes an
+ * instance has. Termination then keeps or deletes each one as its DeleteOnTermination says.
+ *
+ * @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-block-device-mapping.html">Add block device mappings to Amazon EC2 instance</a>
+ */
+@QuarkusTest
+class Ec2LaunchBlockDeviceMappingsIntegrationTest {
+
+ /** Each test works in an account of its own, so no other test's volumes show up in its answers. */
+ private static final AtomicLong ACCOUNTS = new AtomicLong(724600000000L);
+ private static final String INSTANCE = "DescribeInstancesResponse.reservationSet.item.instancesSet.item";
+ private static final String SNAPSHOT = "snap-0a1b2c3d4e5f60718";
+
+ @Test
+ void aLaunchCreatesTheAmiDevicesWithItsOwnEntriesOverThem() {
+ String account = account();
+ String image = registerImage(account);
+
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "20",
+ "BlockDeviceMapping.1.Ebs.DeleteOnTermination", "false",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdg",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "5",
+ "BlockDeviceMapping.2.Ebs.VolumeType", "gp3");
+
+ XmlPath described = ec2(account, "DescribeInstances", "InstanceId.1", instance);
+ assertEquals("/dev/sda1", described.getString(INSTANCE + ".rootDeviceName"));
+ assertEquals(List.of("/dev/sda1", "/dev/sdf", "/dev/sdg"),
+ described.getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ assertEquals(List.of("false", "false", "true"),
+ described.getList(INSTANCE + ".blockDeviceMapping.item.ebs.deleteOnTermination", String.class));
+ assertEquals(List.of("attached", "attached", "attached"),
+ described.getList(INSTANCE + ".blockDeviceMapping.item.ebs.status", String.class));
+
+ // The resized root keeps the AMI's snapshot and gp2, where a root mapping nothing would be
+ // gp3; the AMI's data device comes as it maps it.
+ volume(account, deviceVolume(account, instance, "/dev/sda1"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("20"))
+ .body("DescribeVolumesResponse.volumeSet.item.snapshotId", equalTo(SNAPSHOT))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp2"));
+ volume(account, deviceVolume(account, instance, "/dev/sdf"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("125"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("st1"))
+ .body("DescribeVolumesResponse.volumeSet.item.attachmentSet.item.deleteOnTermination",
+ equalTo("false"));
+ volume(account, deviceVolume(account, instance, "/dev/sdg"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("5"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp3"));
+ assertEquals(described.getString(INSTANCE + ".blockDeviceMapping.item[0].ebs.volumeId"),
+ deviceVolume(account, instance, "/dev/sda1"));
+
+ // The launch's entries change only this instance, never the AMI it came from.
+ XmlPath amiAfter = ec2(account, "DescribeImages", "ImageId.1", image);
+ assertEquals(List.of("16", "125"),
+ amiAfter.getList("DescribeImagesResponse.imagesSet.item.blockDeviceMapping.item.ebs.volumeSize",
+ String.class));
+ }
+
+ @Test
+ void terminationKeepsOrDeletesEachVolumeAsItsDeleteOnTerminationSays() {
+ String account = account();
+ String image = registerImage(account);
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.1.Ebs.DeleteOnTermination", "false",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdg",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "5");
+ String root = deviceVolume(account, instance, "/dev/sda1");
+ String data = deviceVolume(account, instance, "/dev/sdf");
+
+ ec2(account, "TerminateInstances", "InstanceId.1", instance);
+
+ // The root and /dev/sdf asked to be kept, /dev/sdg took the launch default and is deleted.
+ assertEquals(List.of(root, data).stream().sorted().toList(), volumeIds(account));
+ for (String kept : List.of(root, data)) {
+ volume(account, kept).body("DescribeVolumesResponse.volumeSet.item.status", equalTo("available"));
+ assertTrue(ec2(account, "DescribeVolumes", "VolumeId.1", kept)
+ .getList("DescribeVolumesResponse.volumeSet.item.attachmentSet.item").isEmpty());
+ }
+ assertTrue(ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item").isEmpty());
+ }
+
+ @Test
+ void noDeviceKeepsAnAmiDeviceFromBeingCreatedButNotTheRoot() {
+ String account = account();
+ String image = registerImage(account);
+
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.NoDevice", "",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.2.NoDevice", "");
+
+ assertEquals(List.of("/dev/sda1"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ assertEquals(1, volumeIds(account).size());
+ // The root stays, as the AMI maps it.
+ volume(account, deviceVolume(account, instance, "/dev/sda1"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("16"))
+ .body("DescribeVolumesResponse.volumeSet.item.snapshotId", equalTo(SNAPSHOT));
+ }
+
+ @Test
+ void aVolumeFromASnapshotTakesTheSnapshotsSizeWhenItsEntryNamesNone() {
+ String account = account();
+ String image = registerImage(account);
+
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdh",
+ "BlockDeviceMapping.1.Ebs.SnapshotId", SNAPSHOT);
+
+ volume(account, deviceVolume(account, instance, "/dev/sdh"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("16"))
+ .body("DescribeVolumesResponse.volumeSet.item.snapshotId", equalTo(SNAPSHOT))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp2"));
+ }
+
+ @Test
+ void aRootEntryNamingOnlyASnapshotTakesTheSnapshotsSizeWhenTheAmiMapsNoRoot() {
+ String account = account();
+ registerImage(account); // records the 16 GiB snapshot
+ String image = ec2(account, "RegisterImage", "Name", "no-root-mapping-" + account,
+ "RootDeviceName", "/dev/xvda")
+ .getString("RegisterImageResponse.imageId");
+
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.Ebs.SnapshotId", SNAPSHOT);
+
+ volume(account, deviceVolume(account, instance, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("16"))
+ .body("DescribeVolumesResponse.volumeSet.item.snapshotId", equalTo(SNAPSHOT));
+ }
+
+ @Test
+ void aLaunchTemplateDeviceKeepsItsSettingsUnderARequestEntryThatChangesItsType() {
+ String account = account();
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "launch-volumes-" + account,
+ "LaunchTemplateData.ImageId", "ami-0abcdef1234567890",
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "12",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeType", "gp3",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.Iops", "4000",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.Throughput", "250",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.Encrypted", "true")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ String asTemplated = launch(account, null, "LaunchTemplate.LaunchTemplateId", template);
+ volume(account, deviceVolume(account, asTemplated, "/dev/sdf"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("12"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp3"))
+ .body("DescribeVolumesResponse.volumeSet.item.iops", equalTo("4000"))
+ .body("DescribeVolumesResponse.volumeSet.item.throughput", equalTo("250"))
+ .body("DescribeVolumesResponse.volumeSet.item.encrypted", equalTo("true"));
+
+ // The new type takes neither of the old one's performance settings; the rest still apply.
+ String retyped = launch(account, null, "LaunchTemplate.LaunchTemplateId", template,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.Ebs.VolumeType", "gp2");
+ XmlPath gp2 = ec2(account, "DescribeVolumes", "VolumeId.1", deviceVolume(account, retyped, "/dev/sdf"));
+ assertEquals("gp2", gp2.getString("DescribeVolumesResponse.volumeSet.item.volumeType"));
+ assertEquals("12", gp2.getString("DescribeVolumesResponse.volumeSet.item.size"));
+ assertEquals("true", gp2.getString("DescribeVolumesResponse.volumeSet.item.encrypted"));
+ assertEquals("", gp2.getString("DescribeVolumesResponse.volumeSet.item.iops"));
+ assertEquals("", gp2.getString("DescribeVolumesResponse.volumeSet.item.throughput"));
+ }
+
+ @Test
+ void aVolumeAttachedAfterLaunchIsPartOfTheInstancesBlockDeviceMapping() {
+ String account = account();
+ String instance = launch(account, "ami-0abcdef1234567890");
+ String zone = ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getString(INSTANCE + ".placement.availabilityZone");
+ String attached = ec2(account, "CreateVolume", "Size", "4", "AvailabilityZone", zone)
+ .getString("CreateVolumeResponse.volumeId");
+
+ ec2(account, "AttachVolume", "VolumeId", attached, "InstanceId", instance, "Device", "/dev/sdj");
+
+ XmlPath described = ec2(account, "DescribeInstances", "InstanceId.1", instance);
+ assertEquals(List.of("/dev/xvda", "/dev/sdj"),
+ described.getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ assertEquals(attached, described.getString(INSTANCE + ".blockDeviceMapping.item[1].ebs.volumeId"));
+ assertEquals("false", described.getString(INSTANCE + ".blockDeviceMapping.item[1].ebs.deleteOnTermination"));
+ }
+
+ @Test
+ void anAmiEntryWithoutAnEbsBlockCreatesNoVolume() {
+ String account = account();
+ String image = ec2(account, "RegisterImage", "Name", "instance-store-" + account,
+ "RootDeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "8",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdb",
+ "BlockDeviceMapping.2.VirtualName", "ephemeral0",
+ "BlockDeviceMapping.3.DeviceName", "/dev/sdc",
+ "BlockDeviceMapping.3.NoDevice", "")
+ .getString("RegisterImageResponse.imageId");
+
+ String instance = launch(account, image);
+
+ assertEquals(List.of("/dev/xvda"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ assertEquals(1, volumeIds(account).size());
+ }
+
+ @Test
+ void aDeviceNameTheRootAlsoAnswersToChangesTheRoot() {
+ String account = account();
+
+ String instance = launch(account, "ami-0abcdef1234567890",
+ "BlockDeviceMapping.1.DeviceName", "xvda",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "30");
+
+ assertEquals(List.of("/dev/xvda"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ volume(account, deviceVolume(account, instance, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("30"));
+ }
+
+ @Test
+ void anAmiRootMappedUnderAnotherSpellingIsTheRoot() {
+ String account = account();
+ String image = ec2(account, "RegisterImage", "Name", "aliased-root-" + account,
+ "RootDeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.DeviceName", "xvda",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "30",
+ "BlockDeviceMapping.1.Ebs.VolumeType", "gp2")
+ .getString("RegisterImageResponse.imageId");
+
+ String asMapped = launch(account, image);
+ String resized = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "40");
+
+ for (String instance : List.of(asMapped, resized)) {
+ assertEquals(List.of("/dev/xvda"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ }
+ assertEquals(2, volumeIds(account).size());
+ volume(account, deviceVolume(account, asMapped, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("30"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp2"));
+ volume(account, deviceVolume(account, resized, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("40"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("gp2"));
+ }
+
+ @Test
+ void aRequestEntrySpellingATemplateDeviceDifferentlyChangesThatDevice() {
+ String account = account();
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "spelled-" + account,
+ "LaunchTemplateData.ImageId", "ami-0abcdef1234567890",
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/xvdf",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "10",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeType", "io1",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.Iops", "400")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ String instance = launch(account, null, "LaunchTemplate.LaunchTemplateId", template,
+ "BlockDeviceMapping.1.DeviceName", "xvdf",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "20");
+
+ assertEquals(List.of("/dev/xvda", "/dev/xvdf"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ volume(account, deviceVolume(account, instance, "/dev/xvdf"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("20"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("io1"))
+ .body("DescribeVolumesResponse.volumeSet.item.iops", equalTo("400"));
+ }
+
+ @Test
+ void anEntryNamingAnInstanceStoreVolumeTakesAnAmiDeviceOverButNotTheRoot() {
+ String account = account();
+ String image = registerImage(account);
+
+ String instance = launch(account, image,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.VirtualName", "ephemeral0",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.2.VirtualName", "ephemeral1");
+
+ assertEquals(List.of("/dev/sda1"), ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ assertEquals(1, volumeIds(account).size());
+ volume(account, deviceVolume(account, instance, "/dev/sda1"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("16"))
+ .body("DescribeVolumesResponse.volumeSet.item.snapshotId", equalTo(SNAPSHOT));
+ }
+
+ @Test
+ void aRequestEntryOfAnotherKindReplacesTheTemplatesForItsDevice() {
+ String account = account();
+ String image = registerImage(account);
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "kinds-" + account,
+ "LaunchTemplateData.ImageId", image,
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdb",
+ "LaunchTemplateData.BlockDeviceMapping.1.VirtualName", "ephemeral0",
+ "LaunchTemplateData.BlockDeviceMapping.2.DeviceName", "/dev/sdf",
+ "LaunchTemplateData.BlockDeviceMapping.2.NoDevice", "")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ // The template makes /dev/sdb an instance store volume and suppresses the AMI's /dev/sdf;
+ // the request asks for EBS volumes at both, and names an empty VirtualName at /dev/sdc.
+ String instance = launch(account, null, "LaunchTemplate.LaunchTemplateId", template,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdb",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "100",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "150",
+ "BlockDeviceMapping.3.DeviceName", "/dev/sdc",
+ "BlockDeviceMapping.3.VirtualName", "",
+ "BlockDeviceMapping.3.Ebs.VolumeSize", "7");
+
+ assertEquals(List.of("/dev/sda1", "/dev/sdb", "/dev/sdc", "/dev/sdf"),
+ ec2(account, "DescribeInstances", "InstanceId.1", instance)
+ .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class));
+ volume(account, deviceVolume(account, instance, "/dev/sdb"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("100"));
+ // /dev/sdf still merges into the AMI's device, so it keeps the AMI's type.
+ volume(account, deviceVolume(account, instance, "/dev/sdf"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("150"))
+ .body("DescribeVolumesResponse.volumeSet.item.volumeType", equalTo("st1"));
+ }
+
+ @Test
+ void aRootEntryWithAnEmptyVirtualNameOrOverATemplatesNoDeviceStillResizesTheRoot() {
+ String account = account();
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "root-kinds-" + account,
+ "LaunchTemplateData.ImageId", "ami-0abcdef1234567890",
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "LaunchTemplateData.BlockDeviceMapping.1.NoDevice", "")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ String blank = launch(account, "ami-0abcdef1234567890",
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.VirtualName", "",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "50");
+ String overNoDevice = launch(account, null, "LaunchTemplate.LaunchTemplateId", template,
+ "BlockDeviceMapping.1.DeviceName", "/dev/xvda",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "40");
+
+ volume(account, deviceVolume(account, blank, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("50"));
+ volume(account, deviceVolume(account, overNoDevice, "/dev/xvda"))
+ .body("DescribeVolumesResponse.volumeSet.item.size", equalTo("40"));
+ }
+
+ @Test
+ void eachInstanceOfALaunchGetsVolumesOfItsOwn() {
+ String account = account();
+
+ XmlPath launched = request(account, "RunInstances", "ImageId", "ami-0abcdef1234567890",
+ "InstanceType", "t3.micro", "MinCount", "2", "MaxCount", "2",
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "4")
+ .post("/").then().statusCode(200).extract().xmlPath();
+ List<String> instances = launched.getList("RunInstancesResponse.instancesSet.item.instanceId", String.class);
+
+ assertEquals(2, instances.size());
+ assertEquals(4, volumeIds(account).size());
+ assertNotEquals(deviceVolume(account, instances.get(0), "/dev/sdf"),
+ deviceVolume(account, instances.get(1), "/dev/sdf"));
+ }
+
+ @Test
+ void aDeviceNamedTwiceIsRefusedWithALaunchTemplateToo() {
+ String account = account();
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "twice-" + account,
+ "LaunchTemplateData.ImageId", "ami-0abcdef1234567890",
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdh",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "5")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+ String twiceInTemplate = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "twice-in-template-" + account,
+ "LaunchTemplateData.ImageId", "ami-0abcdef1234567890",
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdh",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "5",
+ "LaunchTemplateData.BlockDeviceMapping.2.DeviceName", "sdh",
+ "LaunchTemplateData.BlockDeviceMapping.2.Ebs.VolumeSize", "6")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ refused(account, null, "LaunchTemplate.LaunchTemplateId", template,
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdj",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "5",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdj",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "6")
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo("/dev/sdj is being assigned to multiple times."));
+ refused(account, null, "LaunchTemplate.LaunchTemplateId", twiceInTemplate)
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo("sdh is being assigned to multiple times."));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void aDeviceNameAttachVolumeWouldRefuseIsRefused() {
+ String account = account();
+
+ refused(account, "ami-0abcdef1234567890",
+ "BlockDeviceMapping.1.DeviceName", "/dev/sd;f",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "5")
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo("Invalid device name /dev/sd;f"));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void aRootSmallerThanItsSnapshotIsRefusedWithNothingLeftBehind() {
+ String account = account();
+ String image = registerImage(account);
+
+ refused(account, image, "BlockDeviceMapping.1.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "8")
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo(
+ "Volume of size 8GB is smaller than snapshot '" + SNAPSHOT + "', expect size>= 16GB"));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void anEntryNamingASnapshotThatDoesNotExistIsRefusedWithNothingLeftBehind() {
+ String account = account();
+ String image = registerImage(account);
+
+ refused(account, image, "BlockDeviceMapping.1.DeviceName", "/dev/sdh",
+ "BlockDeviceMapping.1.Ebs.SnapshotId", "snap-0fffffffffffffff1")
+ .body("Response.Errors.Error.Code", equalTo("InvalidSnapshot.NotFound"))
+ .body("Response.Errors.Error.Message", equalTo("The snapshot 'snap-0fffffffffffffff1' does not exist."));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void aDeviceNamedTwiceIsRefusedWithNothingLeftBehind() {
+ String account = account();
+
+ refused(account, "ami-0abcdef1234567890",
+ "BlockDeviceMapping.1.DeviceName", "/dev/sdh",
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "5",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdh",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "6")
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo("/dev/sdh is being assigned to multiple times."));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void anEntryWithoutADeviceNameIsRefusedWithNothingLeftBehind() {
+ String account = account();
+
+ refused(account, "ami-0abcdef1234567890", "BlockDeviceMapping.1.Ebs.VolumeSize", "5")
+ .body("Response.Errors.Error.Code", equalTo("InvalidBlockDeviceMapping"))
+ .body("Response.Errors.Error.Message", equalTo("Missing device name"));
+ assertNothingLaunched(account);
+ }
+
+ @Test
+ void aFleetRolledBackAfterALaunchLeavesNoVolumeOfThatLaunchBehind() {
+ String account = account();
+ String image = registerImage(account);
+ String reservation = ec2(account, "CreateCapacityReservation",
+ "InstanceType", "t3.micro", "InstancePlatform", "Linux/UNIX",
+ "AvailabilityZone", "us-east-1a", "InstanceCount", "1", "InstanceMatchCriteria", "targeted")
+ .getString("CreateCapacityReservationResponse.capacityReservation.capacityReservationId");
+ String template = ec2(account, "CreateLaunchTemplate",
+ "LaunchTemplateName", "launch-volumes-" + account,
+ "LaunchTemplateData.ImageId", image,
+ "LaunchTemplateData.InstanceType", "t3.micro",
+ "LaunchTemplateData.Placement.AvailabilityZone", "us-east-1a",
+ "LaunchTemplateData.CapacityReservationSpecification.CapacityReservationTarget.CapacityReservationId",
+ reservation,
+ "LaunchTemplateData.BlockDeviceMapping.1.DeviceName", "/dev/sdg",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.VolumeSize", "5",
+ "LaunchTemplateData.BlockDeviceMapping.1.Ebs.DeleteOnTermination", "false")
+ .getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId");
+
+ // The first launch takes the reservation's one slot and creates a root that is deleted on
+ // termination and two devices that are not; the second finds no capacity left, so the
+ // fleet undoes the first.
+ request(account, "CreateFleet", "Type", "instant",
+ "LaunchTemplateConfig.1.LaunchTemplateSpecification.LaunchTemplateId", template,
+ "TargetCapacitySpecification.TotalTargetCapacity", "2",
+ "TargetCapacitySpecification.DefaultTargetCapacityType", "on-demand")
+ .post("/").then().statusCode(400)
+ .body("Response.Errors.Error.Code", equalTo("ReservationCapacityExceeded"));
+
+ assertTrue(volumeIds(account).isEmpty());
+ }
+
+ /**
+ * A RegisterImage AMI rooted at /dev/sda1 on a 16 GiB gp2 snapshot, with an empty 125 GiB st1
+ * data device at /dev/sdf that is kept when an instance terminates.
+ */
+ private static String registerImage(String account) {
+ return ec2(account, "RegisterImage", "Name", "launch-volumes-" + account,
+ "RootDeviceName", "/dev/sda1",
+ "BlockDeviceMapping.1.DeviceName", "/dev/sda1",
+ "BlockDeviceMapping.1.Ebs.SnapshotId", SNAPSHOT,
+ "BlockDeviceMapping.1.Ebs.VolumeSize", "16",
+ "BlockDeviceMapping.1.Ebs.VolumeType", "gp2",
+ "BlockDeviceMapping.2.DeviceName", "/dev/sdf",
+ "BlockDeviceMapping.2.Ebs.VolumeSize", "125",
+ "BlockDeviceMapping.2.Ebs.VolumeType", "st1",
+ "BlockDeviceMapping.2.Ebs.DeleteOnTermination", "false")
+ .getString("RegisterImageResponse.imageId");
+ }
+
+ private static String launch(String account, String image, String... params) {
+ return launchRequest(account, image, params).post("/").then().statusCode(200).extract()
+ .path("RunInstancesResponse.instancesSet.item.instanceId");
+ }
+
+ private static ValidatableResponse refused(String account, String image, String... params) {
+ return launchRequest(account, image, params).post("/").then().statusCode(400);
+ }
+
+ private static RequestSpecification launchRequest(String account, String image, String... params) {
+ RequestSpecification request = request(account, "RunInstances", params)
+ .formParam("InstanceType", "t3.micro").formParam("MinCount", "1").formParam("MaxCount", "1");
+ return image == null ? request : request.formParam("ImageId", image);
+ }
+
+ private static void assertNothingLaunched(String account) {
+ assertTrue(ec2(account, "DescribeInstances").getList(INSTANCE + ".instanceId").isEmpty());
+ assertTrue(volumeIds(account).isEmpty());
+ assertTrue(ec2(account, "DescribeNetworkInterfaces")
+ .getList("DescribeNetworkInterfacesResponse.networkInterfaceSet.item.networkInterfaceId").isEmpty());
+ }
+
+ private static String deviceVolume(String account, String instance, String device) {
+ return ec2(account, "DescribeInstances", "InstanceId.1", instance).getString(
+ INSTANCE + ".blockDeviceMapping.item.find { it.deviceName == '" + device + "' }.ebs.volumeId");
+ }
+
+ private static ValidatableResponse volume(String account, String volumeId) {
+ return request(account, "DescribeVolumes", "VolumeId.1", volumeId).post("/").then().statusCode(200);
+ }
+
+ private static List<String> volumeIds(String account) {
+ return ec2(account, "DescribeVolumes").getList("DescribeVolumesResponse.volumeSet.item.volumeId", String.class)
+ .stream().sorted().toList();
+ }
+
+ private static String account() {
+ return Long.toString(ACCOUNTS.incrementAndGet());
+ }
+
+ private static XmlPath ec2(String account, String action, String... params) {
+ return request(account, action, params).post("/").then().statusCode(200).extract().xmlPath();
+ }
+
+ private static RequestSpecification request(String account, String action, String... params) {
+ RequestSpecification request = given()
+ .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account
+ + "/20261022/us-east-1/ec2/aws4_request, SignedHeaders=host, Signature=abc")
+ .formParam("Action", action);
+ for (int i = 0; i < params.length; i += 2) {
+ request = request.formParam(params[i], params[i + 1]);
+ }
+ return request;
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
index 5fe05fa960..556f04c8ef 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2ServiceTest.java
@@ -63,6 +63,7 @@ import io.github.hectorvent.floci.services.ec2.net.VpcNetworkManager;
import io.github.hectorvent.floci.services.ec2.portforward.Ec2PortForwardManager;
import io.github.hectorvent.floci.testing.ParkingStore;
import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.Response;
import org.awaitility.Awaitility;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
@@ -132,6 +133,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
@@ -140,9 +142,11 @@ import static org.mockito.Mockito.CALLS_REAL_METHODS;
import static org.mockito.Mockito.RETURNS_SELF;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -305,6 +309,91 @@ class Ec2ServiceTest {
assertTrue(service.describeVolumes(region, List.of(), Map.of()).isEmpty());
}
+ @Test
+ void aRollbackDeletesALaunchVolumeWhoseAttachmentEndedFirst() {
+ Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
+ new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory());
+ Reservation launch = launchKeepingADataVolume(service);
+ String instanceId = launch.getInstances().getFirst().getInstanceId();
+ String dataVolume = service.describeVolumes("us-east-1", List.of(), Map.of()).stream()
+ .filter(volume -> "/dev/sdf".equals(volume.getAttachments().getFirst().getDevice()))
+ .findFirst().orElseThrow().getVolumeId();
+ // A failed container's launch volumes are settled by whichever call reads them first, which
+ // can end this attachment before a later launch of the same fleet fails.
+ service.detachVolume("us-east-1", dataVolume, instanceId, null, false);
+
+ service.rollBackLaunch("us-east-1", launch);
+
+ assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).isEmpty());
+ }
+
+ @Test
+ void describeInstancesReadsTheVolumeAttachmentsOnceWhateverTheInstanceCount() {
+ Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
+ new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()));
+ service.runInstances("us-east-1", "ami-test", "t3.micro", 3, 3, null, List.of(), null, null,
+ List.of(), null, null);
+ Ec2QueryHandler handler = new Ec2QueryHandler(service, mock(EmulatorConfig.class),
+ mock(FlowLogService.class), mock(Ec2EbsEncryptionService.class),
+ mock(Ec2SnapshotBlockPublicAccessService.class), mock(Ec2IpamService.class),
+ new RegionResolver("us-east-1", "000000000000"));
+
+ Response response = handler.handle("DescribeInstances", new MultivaluedHashMap<>(), "us-east-1");
+
+ assertEquals(200, response.getStatus());
+ assertEquals(3, response.getEntity().toString().split("<volumeId>", -1).length - 1);
+ assertEquals(1, mockingDetails(service).getInvocations().stream()
+ .filter(invocation -> invocation.getMethod().getName().equals("instanceVolumeAttachments"))
+ .count());
+ }
+
+ @Test
+ void aLaunchRefusedWhenItTakesCapacityLeavesNoVolumeBehind() {
+ Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
+ new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()));
+ // Another launch took the capacity between this launch's check and its claim on it.
+ doThrow(new AwsException("InsufficientInstanceCapacity", "taken in the meantime", 400))
+ .when(service).acquireCapacity(anyString(), anyList());
+
+ assertThrows(AwsException.class, () -> launchKeepingADataVolume(service));
+
+ assertTrue(service.describeVolumes("us-east-1", List.of(), Map.of()).isEmpty());
+ }
+
+ @Test
+ void aRollbackWhoseTerminationFailsKeepsEachVolumesDeleteOnTermination() {
+ Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),
+ mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class),
+ new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory()));
+ Reservation launch = launchKeepingADataVolume(service);
+ doThrow(new AwsException("InternalError", "termination failed", 500))
+ .when(service).terminateInstances(anyString(), anyList());
+
+ assertThrows(AwsException.class, () -> service.rollBackLaunch("us-east-1", launch));
+
+ // The instance is still there, so a later TerminateInstances must keep what the launch asked to keep.
+ assertEquals(Set.of("/dev/xvda=true", "/dev/sdf=false"),
+ service.describeVolumes("us-east-1", List.of(), Map.of()).stream()
+ .map(volume -> volume.getAttachments().getFirst())
+ .map(a -> a.getDevice() + "=" + a.isDeleteOnTermination())
+ .collect(Collectors.toSet()));
+ }
+
+ private static Reservation launchKeepingADataVolume(Ec2Service service) {
+ LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs();
+ ebs.setVolumeSize(5);
+ ebs.setDeleteOnTermination(false);
+ LaunchTemplateData.BlockDeviceMapping data = new LaunchTemplateData.BlockDeviceMapping();
+ data.setDeviceName("/dev/sdf");
+ data.setEbs(ebs);
+ return service.runInstances("us-east-1", "ami-test", "t3.micro", 1, 1, null, List.of(), null, null,
+ List.of(), null, null, null, null, 0, null, null, null, null, false, null, null,
+ List.of(data), false, null, null);
+ }
+
@Test
void subnetAvailableAddressesTrackAllocationsAndRelease() {
Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class),