mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(servicequotas): configure applied EC2 vCPU quota (#5926)
* feat(servicequotas): configure applied EC2 vCPU quota Report configured EC2 vCPU quotas separately from AWS defaults. * fix(servicequotas): reject negative applied quota settings Reject invalid quota settings before serving local requests. * docs(servicequotas): clarify negative quota settings * fix(servicequotas): reject invalid settings at startup
This commit is contained in:
1 parent
8298779ead
commit
1a80884f40
11 files changed
+288
-32
No files matched your search
+38
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ServiceQuotasStartupValidatorTest {
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, Integer.MIN_VALUE})
|
||||
void negativeQuotaIsRejectedByStartupObserver(int value) {
|
||||
ServiceQuotasStartupValidator validator = validator(value);
|
||||
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
||||
() -> validator.onStart(new StartupEvent()));
|
||||
assertEquals("floci.services.servicequotas.standard-on-demand-vcpus must be nonnegative",
|
||||
error.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, 5, 7, Integer.MAX_VALUE})
|
||||
void nonnegativeQuotaIsAcceptedByStartupObserver(int value) {
|
||||
ServiceQuotasStartupValidator validator = validator(value);
|
||||
assertDoesNotThrow(() -> validator.onStart(new StartupEvent()));
|
||||
}
|
||||
|
||||
private static ServiceQuotasStartupValidator validator(int value) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().servicequotas().standardOnDemandVcpus()).thenReturn(value);
|
||||
return new ServiceQuotasStartupValidator(config);
|
||||
}
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package io.github.hectorvent.floci.services.servicequotas;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.response.Response;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
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.assertNull;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ServiceQuotasAppliedValueIntegrationTest.AppliedQuotaProfile.class)
|
||||
class ServiceQuotasAppliedValueIntegrationTest {
|
||||
|
||||
private static final String CODE = "L-1216C47A";
|
||||
|
||||
public static class AppliedQuotaProfile implements QuarkusTestProfile {
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of("floci.services.servicequotas.standard-on-demand-vcpus", "7");
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
static void configureContentTypes() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"us-east-1,aws", "cn-north-1,aws-cn"})
|
||||
void appliedGetReportsConfiguredValue(String region, String partition) {
|
||||
quotaRequest("GetServiceQuota", "{\"ServiceCode\":\"ec2\",\"QuotaCode\":\"" + CODE + "\"}", region)
|
||||
.then().statusCode(200)
|
||||
.body("Quota.Value", equalTo(7.0f))
|
||||
.body("Quota.QuotaArn", equalTo("arn:" + partition + ":servicequotas:" + region
|
||||
+ ":111111111111:ec2/" + CODE));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"us-east-1,aws", "cn-north-1,aws-cn"})
|
||||
void appliedListReportsConfiguredValue(String region, String partition) {
|
||||
quotaRequest("ListServiceQuotas", "{\"ServiceCode\":\"ec2\",\"QuotaCode\":\"" + CODE + "\"}", region)
|
||||
.then().statusCode(200)
|
||||
.body("Quotas.size()", equalTo(1))
|
||||
.body("Quotas[0].Value", equalTo(7.0f))
|
||||
.body("Quotas[0].QuotaArn", equalTo("arn:" + partition + ":servicequotas:" + region
|
||||
+ ":111111111111:ec2/" + CODE));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"us-east-1,aws", "cn-north-1,aws-cn"})
|
||||
void awsDefaultReadsKeepFive(String region, String partition) {
|
||||
quotaRequest("GetAWSDefaultServiceQuota", "{\"ServiceCode\":\"ec2\",\"QuotaCode\":\"" + CODE + "\"}", region)
|
||||
.then().statusCode(200).body("Quota.Value", equalTo(5.0f));
|
||||
quotaRequest("ListAWSDefaultServiceQuotas", "{\"ServiceCode\":\"ec2\"}", region)
|
||||
.then().statusCode(200).body("Quotas.size()", equalTo(4))
|
||||
.body("Quotas.find { it.QuotaCode == 'L-1216C47A' }.Value", equalTo(5.0f));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"ListServiceQuotas,7", "ListAWSDefaultServiceQuotas,5"})
|
||||
void bothListsRetainPaginationAndGenericRows(String action, float expectedValue) {
|
||||
Response first = quotaRequest(action, "{\"ServiceCode\":\"ec2\",\"MaxResults\":1}", "us-east-1")
|
||||
.then().statusCode(200).body("Quotas.size()", equalTo(1))
|
||||
.body("Quotas[0].QuotaCode", equalTo(CODE)).body("Quotas[0].Value", equalTo(expectedValue))
|
||||
.extract().response();
|
||||
String token = first.path("NextToken");
|
||||
Response rest = quotaRequest(action,
|
||||
"{\"ServiceCode\":\"ec2\",\"NextToken\":\"" + token + "\"}", "us-east-1")
|
||||
.then().statusCode(200).body("Quotas.size()", equalTo(3)).extract().response();
|
||||
assertEquals(List.of(5000.0f, 5000.0f, 5000.0f), rest.path("Quotas.Value"));
|
||||
assertNull(rest.path("NextToken"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"GetServiceQuota", "GetAWSDefaultServiceQuota"})
|
||||
void unrelatedCuratedValuesAndErrorsRemainUnchanged(String action) {
|
||||
quotaRequest(action, "{\"ServiceCode\":\"vpc\",\"QuotaCode\":\"L-2AEEBF1A\"}", "us-east-1")
|
||||
.then().statusCode(200).body("Quota.Value", equalTo(20.0f));
|
||||
quotaRequest(action, "{\"ServiceCode\":\"ec2\",\"QuotaCode\":\"L-UNKNOWN\"}", "us-east-1")
|
||||
.then().statusCode(400).body("__type", equalTo("NoSuchResourceException"));
|
||||
}
|
||||
|
||||
private Response quotaRequest(String action, String body, String region) {
|
||||
return given().contentType("application/x-amz-json-1.1")
|
||||
.header("X-Amz-Target", "ServiceQuotasV20190624." + action)
|
||||
.header("Authorization", auth("111111111111", "20261018", region, "servicequotas"))
|
||||
.body(body).when().post("/");
|
||||
}
|
||||
}
|
||||
+45
-1
@@ -3,9 +3,13 @@ package io.github.hectorvent.floci.services.servicequotas;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -16,13 +20,53 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class ServiceQuotasServiceTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
|
||||
private final ServiceQuotasService service =
|
||||
new ServiceQuotasService(new InMemoryStorageFactory(ACCOUNT_ID), new ObjectMapper());
|
||||
new ServiceQuotasService(new InMemoryStorageFactory(ACCOUNT_ID), new ObjectMapper(), defaultConfig());
|
||||
|
||||
private static EmulatorConfig defaultConfig() {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().servicequotas().standardOnDemandVcpus()).thenReturn(5);
|
||||
return config;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {-1, Integer.MIN_VALUE})
|
||||
void rejectsNegativeVcpuSettingsBeforeOpeningStorage(int value) {
|
||||
EmulatorConfig config = defaultConfig();
|
||||
when(config.services().servicequotas().standardOnDemandVcpus()).thenReturn(value);
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
|
||||
() -> new ServiceQuotasService(storageFactory, new ObjectMapper(), config));
|
||||
assertTrue(error.getMessage().contains("floci.services.servicequotas.standard-on-demand-vcpus"));
|
||||
verifyNoInteractions(storageFactory);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, Integer.MAX_VALUE})
|
||||
void nonnegativeBoundarySettingsApplyWithoutChangingAwsDefaults(int value) {
|
||||
EmulatorConfig config = defaultConfig();
|
||||
when(config.services().servicequotas().standardOnDemandVcpus()).thenReturn(value);
|
||||
ServiceQuotasService configured = new ServiceQuotasService(
|
||||
new InMemoryStorageFactory(ACCOUNT_ID), new ObjectMapper(), config);
|
||||
String code = "L-1216C47A";
|
||||
assertEquals((double) value, configured.getServiceQuota("ec2", code, "us-east-1", ACCOUNT_ID)
|
||||
.path("Quota").path("Value").asDouble());
|
||||
assertEquals((double) value, configured.listServiceQuotas("ec2", code, null, null, "us-east-1", ACCOUNT_ID)
|
||||
.path("Quotas").get(0).path("Value").asDouble());
|
||||
assertEquals(5.0, configured.getAwsDefaultServiceQuota("ec2", code, "us-east-1", ACCOUNT_ID)
|
||||
.path("Quota").path("Value").asDouble());
|
||||
assertEquals(5.0, configured.listAwsDefaultServiceQuotas("ec2", code, null, null, "us-east-1", ACCOUNT_ID)
|
||||
.path("Quotas").get(0).path("Value").asDouble());
|
||||
}
|
||||
|
||||
@Test
|
||||
void syntheticQuotaCodesAreStableAndWellFormed() {
|
||||
|
||||
Reference in new issue
Block a user