diff --git a/docs/services/autoscaling.md b/docs/services/autoscaling.md index d04c8bb844..c918ed3ec9 100644 --- a/docs/services/autoscaling.md +++ b/docs/services/autoscaling.md @@ -56,8 +56,22 @@ scale-in behavior, and do not complete an instance refresh. Unexpected runtime failures in ordinary scale-out, and save and cleanup failures, still propagate. Refresh launch failures retain the original replacement attempt and appear as failed scaling activities while other replacements can continue. Refresh and -ordinary launches share the same launch settings, including template block-device -mappings; accepted refresh launches keep their recorded source identity. +ordinary launches share the same launch settings, including the block device mappings +of the launch template or launch configuration; accepted refresh launches keep their +recorded source identity. A launch template or launch configuration a CloudFormation +stack creates carries no block device mappings yet, so its instances get the AMI's +devices alone. `CreateLaunchConfiguration` refuses a block device mapping without a +`DeviceName`, or with a Boolean or integer member it cannot parse, and stores the rest as +given; the mappings are otherwise checked only when a group launches from the +configuration, by the checks `RunInstances` applies. Neither step applies the ranges the +Auto Scaling model sets for `VolumeSize`, `Iops` and `Throughput`, and the volume types a +launch accepts are EC2's, `io2` included. A mapping EC2 refuses, such as an invalid device +name or a snapshot that does not exist, fails that launch and is recorded as a failed +scaling activity. + +An EBS mapping supplied by a launch configuration defaults `DeleteOnTermination` to +`true`, even when the AMI's mapping sets it to `false`. An explicit `false` in the +launch configuration keeps the volume after termination. For groups configured only with Availability Zones, Floci preserves its unpinned EC2 launch behavior and counts each member under the requested zone. This permits diff --git a/docs/services/ec2.md b/docs/services/ec2.md index a092ed6496..d71b0fb879 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -965,10 +965,9 @@ request's, field by field: a request entry for a `DeviceName` the template maps, 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 +Auto Scaling group launching from a template or a launch configuration gets its mappings the same +way. 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 diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java index b0d3621975..725e79d6b5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingReconciler.java @@ -9,6 +9,7 @@ import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefresh; import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshLaunch; import io.github.hectorvent.floci.services.autoscaling.model.InstanceRefreshReplacement; import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfiguration; +import io.github.hectorvent.floci.services.autoscaling.model.LaunchConfigurationBlockDeviceMapping; import io.github.hectorvent.floci.services.autoscaling.model.MixedInstancesPolicy; import io.github.hectorvent.floci.services.autoscaling.model.ScalingActivity; import io.github.hectorvent.floci.services.ec2.Ec2Service; @@ -943,7 +944,7 @@ public class AutoScalingReconciler { // subnet's MapPublicIpOnLaunch, and only null means // "fall back to the subnet default". lc.getAssociatePublicIpAddress(), - null, lc.getMetadataOptions(), null); + launchConfigurationBlockDevices(lc), lc.getMetadataOptions(), null); } LaunchTemplate launchTemplate = resolveLaunchTemplate(asg); @@ -1016,6 +1017,38 @@ public class AutoScalingReconciler { return null; } + /** + * A launch configuration's block device mappings in the shape RunInstances takes. Auto Scaling + * marks a suppressed device with a Boolean {@code NoDevice}, EC2 with an empty string. + */ + private static List launchConfigurationBlockDevices( + LaunchConfiguration configuration) { + List mappings = new ArrayList<>(); + if (configuration.getBlockDeviceMappings() == null) { + return mappings; + } + for (LaunchConfigurationBlockDeviceMapping source : configuration.getBlockDeviceMappings()) { + LaunchTemplateData.BlockDeviceMapping mapping = new LaunchTemplateData.BlockDeviceMapping(); + mapping.setDeviceName(source.getDeviceName()); + mapping.setVirtualName(source.getVirtualName()); + mapping.setNoDevice(Boolean.TRUE.equals(source.getNoDevice()) ? "" : null); + LaunchConfigurationBlockDeviceMapping.Ebs sourceEbs = source.getEbs(); + if (sourceEbs != null) { + LaunchTemplateData.Ebs ebs = new LaunchTemplateData.Ebs(); + ebs.setSnapshotId(sourceEbs.getSnapshotId()); + ebs.setVolumeSize(sourceEbs.getVolumeSize()); + ebs.setVolumeType(sourceEbs.getVolumeType()); + ebs.setDeleteOnTermination(!Boolean.FALSE.equals(sourceEbs.getDeleteOnTermination())); + ebs.setIops(sourceEbs.getIops()); + ebs.setThroughput(sourceEbs.getThroughput()); + ebs.setEncrypted(sourceEbs.getEncrypted()); + mapping.setEbs(ebs); + } + mappings.add(mapping); + } + return mappings; + } + private static String encodeLegacyUserData(String userData) { return userData == null ? null : Base64.getEncoder().encodeToString(userData.getBytes(StandardCharsets.UTF_8)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/LaunchConfigurationBlockDeviceMapping.java b/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/LaunchConfigurationBlockDeviceMapping.java index 5f06b6fc49..505f245d36 100644 --- a/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/LaunchConfigurationBlockDeviceMapping.java +++ b/src/main/java/io/github/hectorvent/floci/services/autoscaling/model/LaunchConfigurationBlockDeviceMapping.java @@ -5,8 +5,8 @@ import io.quarkus.runtime.annotations.RegisterForReflection; /** * One entry of a launch configuration's {@code BlockDeviceMappings}. The shape follows the - * Auto Scaling API rather than EC2's, which has no {@code Throughput}, {@code Iops}, - * {@code VirtualName} or {@code NoDevice} on its launch-time mapping. + * Auto Scaling API rather than EC2's: {@code NoDevice} is a Boolean here, where EC2's launch-time + * mapping marks a suppressed device with a string. */ @RegisterForReflection @JsonIgnoreProperties(ignoreUnknown = true) diff --git a/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingLaunchConfigurationBlockDevicesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingLaunchConfigurationBlockDevicesIntegrationTest.java new file mode 100644 index 0000000000..b5c2fc8cc5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/autoscaling/AutoScalingLaunchConfigurationBlockDevicesIntegrationTest.java @@ -0,0 +1,235 @@ +package io.github.hectorvent.floci.services.autoscaling; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.path.xml.XmlPath; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static io.github.hectorvent.floci.testing.QueryRequests.request; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +/** + * An Auto Scaling group launching from a launch configuration creates the volumes the + * configuration's block device mappings describe, as one launching from a template does, for + * the instances an instance refresh launches too; a mapping EC2 refuses fails the launch. + */ +@QuarkusTest +class AutoScalingLaunchConfigurationBlockDevicesIntegrationTest { + + /** Each test works in an account of its own, so no other test's images or volumes show up in it. */ + private static final String RESIZING_ACCOUNT = "724700000001"; + private static final String MEMBERS_ACCOUNT = "724700000002"; + private static final String REFRESH_ACCOUNT = "724700000003"; + private static final String REFUSED_ACCOUNT = "724700000004"; + private static final String GROUP = "DescribeAutoScalingGroupsResponse.DescribeAutoScalingGroupsResult" + + ".AutoScalingGroups.member"; + private static final String INSTANCE = "DescribeInstancesResponse.reservationSet.item.instancesSet.item"; + private static final String VOLUME = "DescribeVolumesResponse.volumeSet.item"; + private static final String ACTIVITY = "DescribeScalingActivitiesResponse.DescribeScalingActivitiesResult" + + ".Activities.member"; + private static final String SNAPSHOT = "snap-0724700000000a001"; + + @Inject + AutoScalingReconciler reconciler; + + /** The groups a test created, each named as its launch configuration and deleted with it. */ + private final List created = new ArrayList<>(); + + @AfterEach + void deleteTheGroups() { + for (Group group : created) { + call(group.account(), "autoscaling", "DeleteAutoScalingGroup", + "AutoScalingGroupName", group.name(), "ForceDelete", "true"); + call(group.account(), "autoscaling", "DeleteLaunchConfiguration", + "LaunchConfigurationName", group.name()); + } + } + + @Test + void aGroupLaunchesWithItsLaunchConfigurationsBlockDeviceMappings() { + String account = RESIZING_ACCOUNT; + String name = "lc-block-devices"; + // The AMI maps a data device the configuration suppresses, so a NoDevice the launch did + // not receive would show up as a /dev/sdf volume. + String image = call(account, "ec2", "RegisterImage", "Name", name, + "RootDeviceName", "/dev/xvda", + "BlockDeviceMapping.1.DeviceName", "/dev/xvda", + "BlockDeviceMapping.1.Ebs.VolumeSize", "8", + "BlockDeviceMapping.1.Ebs.VolumeType", "gp2", + "BlockDeviceMapping.1.Ebs.DeleteOnTermination", "false", + "BlockDeviceMapping.2.DeviceName", "/dev/sdf", + "BlockDeviceMapping.2.Ebs.VolumeSize", "10") + .getString("RegisterImageResponse.imageId"); + + String instance = launchFromConfiguration(account, name, image, + "BlockDeviceMappings.member.1.DeviceName", "/dev/xvda", + "BlockDeviceMappings.member.1.Ebs.VolumeSize", "30", + "BlockDeviceMappings.member.1.Ebs.VolumeType", "io1", + "BlockDeviceMappings.member.1.Ebs.Iops", "1500", + "BlockDeviceMappings.member.2.DeviceName", "/dev/sdf", + "BlockDeviceMappings.member.2.NoDevice", "true", + "BlockDeviceMappings.member.3.DeviceName", "/dev/sdg", + "BlockDeviceMappings.member.3.Ebs.VolumeSize", "12", + "BlockDeviceMappings.member.3.Ebs.VolumeType", "gp3", + "BlockDeviceMappings.member.3.Ebs.DeleteOnTermination", "false"); + + XmlPath described = call(account, "ec2", "DescribeInstances", "InstanceId.1", instance); + assertEquals(List.of("/dev/xvda", "/dev/sdg"), + described.getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class)); + // The configuration defaults to deleting the root even when the AMI keeps it. + assertEquals(List.of("true", "false"), + described.getList(INSTANCE + ".blockDeviceMapping.item.ebs.deleteOnTermination", String.class)); + XmlPath root = volume(account, described, 0); + assertEquals("30", root.getString(VOLUME + ".size")); + assertEquals("io1", root.getString(VOLUME + ".volumeType")); + assertEquals("1500", root.getString(VOLUME + ".iops")); + XmlPath data = volume(account, described, 1); + assertEquals("12", data.getString(VOLUME + ".size")); + assertEquals("gp3", data.getString(VOLUME + ".volumeType")); + } + + @Test + void aGroupLaunchesWithEveryMemberOfItsLaunchConfigurationsEntries() { + String account = MEMBERS_ACCOUNT; + String name = "lc-block-device-members"; + // The configuration names two data devices the AMI maps: /dev/sdh with NoDevice false, + // which keeps it, and /dev/sdj as an instance store volume, which leaves no EBS volume + // there. A NoDevice taken as set, or a VirtualName the launch did not receive, would + // change the devices the instance gets. + String image = call(account, "ec2", "RegisterImage", "Name", name, + "RootDeviceName", "/dev/xvda", + "BlockDeviceMapping.1.DeviceName", "/dev/xvda", + "BlockDeviceMapping.1.Ebs.SnapshotId", SNAPSHOT, + "BlockDeviceMapping.1.Ebs.VolumeSize", "16", + "BlockDeviceMapping.2.DeviceName", "/dev/sdh", + "BlockDeviceMapping.2.Ebs.VolumeSize", "10", + "BlockDeviceMapping.3.DeviceName", "/dev/sdj", + "BlockDeviceMapping.3.Ebs.VolumeSize", "10") + .getString("RegisterImageResponse.imageId"); + + String instance = launchFromConfiguration(account, name, image, + "BlockDeviceMappings.member.1.DeviceName", "/dev/sdh", + "BlockDeviceMappings.member.1.NoDevice", "false", + "BlockDeviceMappings.member.2.DeviceName", "/dev/sdj", + "BlockDeviceMappings.member.2.VirtualName", "ephemeral0", + "BlockDeviceMappings.member.3.DeviceName", "/dev/sdg", + "BlockDeviceMappings.member.3.Ebs.VolumeSize", "12", + "BlockDeviceMappings.member.3.Ebs.VolumeType", "gp3", + "BlockDeviceMappings.member.3.Ebs.Throughput", "250", + "BlockDeviceMappings.member.3.Ebs.Encrypted", "true", + "BlockDeviceMappings.member.4.DeviceName", "/dev/sdi", + "BlockDeviceMappings.member.4.Ebs.SnapshotId", SNAPSHOT); + + XmlPath described = call(account, "ec2", "DescribeInstances", "InstanceId.1", instance); + assertEquals(List.of("/dev/xvda", "/dev/sdg", "/dev/sdh", "/dev/sdi"), + described.getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class)); + // Auto Scaling's DeleteOnTermination default is true, for the devices the configuration adds too. + assertEquals(List.of("true", "true", "true", "true"), + described.getList(INSTANCE + ".blockDeviceMapping.item.ebs.deleteOnTermination", String.class)); + XmlPath added = volume(account, described, 1); + assertEquals("12", added.getString(VOLUME + ".size")); + assertEquals("gp3", added.getString(VOLUME + ".volumeType")); + assertEquals("250", added.getString(VOLUME + ".throughput")); + assertEquals("true", added.getString(VOLUME + ".encrypted")); + XmlPath fromSnapshot = volume(account, described, 3); + assertEquals(SNAPSHOT, fromSnapshot.getString(VOLUME + ".snapshotId")); + assertEquals("16", fromSnapshot.getString(VOLUME + ".size")); + } + + @Test + void anInstanceRefreshReplacesTheInstanceWithTheLaunchConfigurationsBlockDeviceMappings() { + String account = REFRESH_ACCOUNT; + String name = "lc-block-devices-refresh"; + String image = call(account, "ec2", "RegisterImage", "Name", name, + "RootDeviceName", "/dev/xvda", + "BlockDeviceMapping.1.DeviceName", "/dev/xvda", + "BlockDeviceMapping.1.Ebs.VolumeSize", "8", + "BlockDeviceMapping.2.DeviceName", "/dev/sdf", + "BlockDeviceMapping.2.Ebs.VolumeSize", "10") + .getString("RegisterImageResponse.imageId"); + String original = launchFromConfiguration(account, name, image, + "BlockDeviceMappings.member.1.DeviceName", "/dev/sdf", + "BlockDeviceMappings.member.1.NoDevice", "true", + "BlockDeviceMappings.member.2.DeviceName", "/dev/sdg", + "BlockDeviceMappings.member.2.Ebs.VolumeSize", "12"); + // The next pass puts the instance in service before the refresh replaces it. + reconciler.reconcileAll(); + + String refresh = call(account, "autoscaling", "StartInstanceRefresh", "AutoScalingGroupName", name) + .getString("StartInstanceRefreshResponse.StartInstanceRefreshResult.InstanceRefreshId"); + reconciler.reconcileAll(); + reconciler.reconcileAll(); + + assertEquals("Successful", call(account, "autoscaling", "DescribeInstanceRefreshes", + "AutoScalingGroupName", name, "InstanceRefreshIds.member.1", refresh) + .getString("DescribeInstanceRefreshesResponse.DescribeInstanceRefreshesResult" + + ".InstanceRefreshes.member.Status")); + String replacement = call(account, "autoscaling", "DescribeAutoScalingGroups", + "AutoScalingGroupNames.member.1", name).getString(GROUP + ".Instances.member.InstanceId"); + assertNotEquals(original, replacement); + assertEquals(List.of("/dev/xvda", "/dev/sdg"), + call(account, "ec2", "DescribeInstances", "InstanceId.1", replacement) + .getList(INSTANCE + ".blockDeviceMapping.item.deviceName", String.class)); + } + + @Test + void aGroupWhoseLaunchConfigurationMapsADeviceNameEc2RefusesRecordsAFailedLaunch() { + String account = REFUSED_ACCOUNT; + String name = "lc-block-devices-refused"; + String image = call(account, "ec2", "RegisterImage", "Name", name, + "RootDeviceName", "/dev/xvda", + "BlockDeviceMapping.1.DeviceName", "/dev/xvda", + "BlockDeviceMapping.1.Ebs.VolumeSize", "8") + .getString("RegisterImageResponse.imageId"); + + // CreateLaunchConfiguration stores the mapping and the launch refuses it, so the group gets + // no instance and records the failed launch. + launchFromConfiguration(account, name, image, + "BlockDeviceMappings.member.1.DeviceName", "/dev/sd;f", + "BlockDeviceMappings.member.1.Ebs.VolumeSize", "5"); + + assertEquals(List.of(), call(account, "autoscaling", "DescribeAutoScalingGroups", + "AutoScalingGroupNames.member.1", name).getList(GROUP + ".Instances.member.InstanceId", String.class)); + XmlPath activities = call(account, "autoscaling", "DescribeScalingActivities", "AutoScalingGroupName", name); + assertEquals("Failed", activities.getString(ACTIVITY + "[0].StatusCode")); + assertEquals("Invalid device name /dev/sd;f Launching EC2 instance failed.", + activities.getString(ACTIVITY + "[0].StatusMessage")); + } + + /** + * Creates a launch configuration with these block device mappings and a group of one instance + * launching from it, and returns the instance the group launches, if any. + */ + private String launchFromConfiguration(String account, String name, String image, String... mappings) { + List configuration = new ArrayList<>(List.of( + "LaunchConfigurationName", name, "ImageId", image, "InstanceType", "t3.micro")); + configuration.addAll(List.of(mappings)); + call(account, "autoscaling", "CreateLaunchConfiguration", configuration.toArray(String[]::new)); + call(account, "autoscaling", "CreateAutoScalingGroup", + "AutoScalingGroupName", name, + "LaunchConfigurationName", name, + "MinSize", "1", "MaxSize", "1", "DesiredCapacity", "1", + "AvailabilityZones.member.1", "us-east-1a"); + created.add(new Group(account, name)); + reconciler.reconcileAll(); + return call(account, "autoscaling", "DescribeAutoScalingGroups", + "AutoScalingGroupNames.member.1", name).getString(GROUP + ".Instances.member.InstanceId"); + } + + /** The volume an instance's block device mapping entry at this index names. */ + private static XmlPath volume(String account, XmlPath instance, int entry) { + return call(account, "ec2", "DescribeVolumes", "VolumeId.1", + instance.getString(INSTANCE + ".blockDeviceMapping.item[" + entry + "].ebs.volumeId")); + } + + private static XmlPath call(String account, String service, String action, String... params) { + return request(account, service, action, params).post("/").then().statusCode(200).extract().xmlPath(); + } + + private record Group(String account, String name) {} +} 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 index 4b13ee4a2e..3de23d20c0 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2LaunchBlockDeviceMappingsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2LaunchBlockDeviceMappingsIntegrationTest.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.ec2; +import io.github.hectorvent.floci.testing.QueryRequests; import io.quarkus.test.junit.QuarkusTest; import io.restassured.path.xml.XmlPath; import io.restassured.response.ValidatableResponse; @@ -9,7 +10,6 @@ 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; @@ -574,13 +574,6 @@ class Ec2LaunchBlockDeviceMappingsIntegrationTest { } 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; + return QueryRequests.request(account, "ec2", action, params); } } 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 166cf9a23c..0d791bd756 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 @@ -148,7 +148,6 @@ 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; @@ -331,24 +330,25 @@ class Ec2ServiceTest { } @Test - void describeInstancesReadsTheVolumeAttachmentsOnceWhateverTheInstanceCount() { - Ec2Service service = spy(new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), + void describeInstancesScansTheVolumeStoreOnceWhateverTheInstanceCount() { + InMemoryStorage volumeStore = spy(new InMemoryStorage()); + Ec2Service service = new Ec2Service(mockConfig(true), mock(Ec2ContainerManager.class), mock(Ec2PortForwardManager.class), mock(AmiImageResolver.class), mock(Ec2ImageCatalog.class), - new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory())); + new Ec2InstanceTypeCatalog(), new InMemoryStorageFactory(Map.of("ec2-volumes.json", + new AccountAwareStorageBackend<>(volumeStore, null, "000000000000")))); 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")); + clearInvocations(volumeStore); Response response = handler.handle("DescribeInstances", new MultivaluedHashMap<>(), "us-east-1"); assertEquals(200, response.getStatus()); assertEquals(3, response.getEntity().toString().split("", -1).length - 1); - assertEquals(1, mockingDetails(service).getInvocations().stream() - .filter(invocation -> invocation.getMethod().getName().equals("instanceVolumeAttachments")) - .count()); + verify(volumeStore, times(1)).scan(any()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/testing/QueryRequests.java b/src/test/java/io/github/hectorvent/floci/testing/QueryRequests.java new file mode 100644 index 0000000000..ffe25c473e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/testing/QueryRequests.java @@ -0,0 +1,20 @@ +package io.github.hectorvent.floci.testing; + +import io.restassured.specification.RequestSpecification; + +import static io.restassured.RestAssured.given; + +public final class QueryRequests { + private QueryRequests() {} + + public static RequestSpecification request(String account, String service, String action, String... params) { + RequestSpecification request = given() + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + account + + "/20261022/us-east-1/" + service + "/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; + } +}