Light, fluffy, and always free - The AWS Local Emulator alternative
feat(elasticache): add validation for cache parameter group existence in replication group operations (#2957)
Matheus André6c9cc95d5404
diff --git a/docs/services/elasticache.md b/docs/services/elasticache.mdindex 38a24cd8e7..5241582307 100644--- a/docs/services/elasticache.md+++ b/docs/services/elasticache.md@@ -113,6 +113,11 @@ than rejecting names a partial catalogue happens to be missing, which would refuAWS accepts. `DescribeCacheParameters` returns those parameters; a request for `system` or`engine-default` parameters returns none, and listings are unpaged.+A replication group that names a parameter group is refused with `CacheParameterGroupNotFound` when+no such group exists, and a parameter group still referenced by a replication group cannot be+deleted: `DeleteCacheParameterGroup` answers `InvalidCacheParameterGroupState` until that+replication group is gone.+## Configuration| Variable | Default | Description |diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.javaindex 21fcb40200..06eedf158f 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java@@ -144,6 +144,9 @@ public class ElastiCacheService implements ResourceProvider {// resolved with the other validations, before a port is taken or a container startedReplicationGroupSettings resolvedSettings =settings.withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), request.region()));+ if (request.cacheParameterGroupName() != null && !request.cacheParameterGroupName().isBlank()) {+ requireParameterGroup(request.cacheParameterGroupName());+ }if (groups.get(groupId).isPresent()) {throw new AwsException("ReplicationGroupAlreadyExistsFault","Replication group " + groupId + " already exists.", 400);@@ -1143,11 +1146,22 @@ public class ElastiCacheService implements ResourceProvider {throw new AwsException("CacheParameterGroupNotFound","CacheParameterGroupnot found: " + name, 404);}+ if (isParameterGroupInUse(name)) {+ throw new AwsException("InvalidCacheParameterGroupState",+ "One or more cache clusters are still members of this parameter group "+ + name + ", so the group cannot be deleted.", 400);+ }parameterGroups.delete(name);}LOG.infov("Deleted cache parameter group {0}", name);}+ /** Whether a stored replication group still references the parameter group by that name. */+ private boolean isParameterGroupInUse(String name) {+ return groups.scan(key -> true).stream()+ .anyMatch(group -> name.equals(group.getCacheParameterGroupName()));+ }+@Overridepublic List<ExplorerResource> getResources() {List<ExplorerResource> resources = new ArrayList<>();diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.javaindex 2b14160e37..0f37622dd9 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheIntegrationTest.java@@ -336,7 +336,7 @@ class ElastiCacheIntegrationTest {equalTo(GROUP_ID + "-reused"));}- private static boolean isDockerAvailable() {+ static boolean isDockerAvailable() {try {Process process = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}").redirectErrorStream(true)diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheParameterGroupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheParameterGroupIntegrationTest.javaindex 721769cd38..8fbe334d02 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheParameterGroupIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheParameterGroupIntegrationTest.java@@ -1,6 +1,7 @@package io.github.hectorvent.floci.services.elasticache;import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.Assumptions;import org.junit.jupiter.api.MethodOrderer;import org.junit.jupiter.api.Order;import org.junit.jupiter.api.Test;@@ -268,6 +269,75 @@ class ElastiCacheParameterGroupIntegrationTest {.body(containsString("CacheParameterGroupnot found: absent-pg"));}+ @Test+ @Order(4)+ void aReplicationGroupCannotReferenceAnUnknownParameterGroup() {+ // The lookup runs with the other validations, before any container is started, so the+ // refusal carries the parameter group's own not-found rather than a provisioning failure.+ query("CreateReplicationGroup")+ .formParam("ReplicationGroupId", "pg-absent-rg")+ .formParam("ReplicationGroupDescription", "references a missing group")+ .formParam("CacheParameterGroupName", "absent-pg")+ .when()+ .post("/")+ .then()+ .statusCode(404)+ .body(containsString("<Code>CacheParameterGroupNotFound</Code>"))+ .body(containsString("CacheParameterGroup absent-pg not found."));++ query("DescribeReplicationGroups")+ .formParam("ReplicationGroupId", "pg-absent-rg")+ .when()+ .post("/")+ .then()+ .statusCode(404);+ }++ @Test+ @Order(4)+ void aParameterGroupInUseByAReplicationGroupCannotBeDeleted() {+ // The replication group is a real one behind a container, so this case needs Docker+ // where the rest of the class does not.+ Assumptions.assumeTrue(ElastiCacheIntegrationTest.isDockerAvailable(),+ "Docker daemon must be available to create a replication group");+ String replicationGroupId = "pg-in-use-rg";+ query("CreateReplicationGroup")+ .formParam("ReplicationGroupId", replicationGroupId)+ .formParam("ReplicationGroupDescription", "holds the parameter group in use")+ .formParam("CacheParameterGroupName", GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ try {+ query("DeleteCacheParameterGroup")+ .formParam("CacheParameterGroupName", GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body(containsString("<Code>InvalidCacheParameterGroupState</Code>"))+ .body(containsString("One or more cache clusters are still members of this parameter group "+ + GROUP + ", so the group cannot be deleted."));++ query("DescribeCacheParameterGroups")+ .formParam("CacheParameterGroupName", GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(containsString("<CacheParameterGroupName>" + GROUP + "</CacheParameterGroupName>"));+ } finally {+ query("DeleteReplicationGroup")+ .formParam("ReplicationGroupId", replicationGroupId)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }+ }+@Test@Order(5)void deleteRemovesItAndTheSecondDeleteReportsItMissing() {diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.javaindex 2c96db9834..aaabb9df04 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java@@ -677,4 +677,41 @@ class ElastiCacheServiceTest {assertThrows(AwsException.class, () -> svc.getReplicationGroup("g1"),"the deleted group must not come back from the modify");}++ private static ElastiCacheService.CreateReplicationGroupRequest requestWithParameterGroup(+ String groupId, String parameterGroupName) {+ return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",+ AuthMode.NO_AUTH, null, "us-east-1", null, null, null,+ parameterGroupName, null, null, null, null,+ null, null, null, ReplicationGroupSettings.defaults(), Map.of());+ }++ @Test+ void createReferencingAnUnknownParameterGroupIsRefusedBeforeProvisioning() {+ AwsException ex = assertThrows(AwsException.class,+ () -> service.createReplicationGroup(requestWithParameterGroup("grp", "absent-pg")));++ assertEquals("CacheParameterGroupNotFound", ex.getErrorCode());+ assertEquals(404, ex.getHttpStatus());+ verify(containerManager, never()).start(anyString(), anyString());+ verify(containerManager, never()).start(anyString(), anyString(), any());+ assertThrows(AwsException.class, () -> service.getReplicationGroup("grp"));+ }++ @Test+ void aParameterGroupStillReferencedByAReplicationGroupCannotBeDeleted() {+ service.createCacheParameterGroup("custom-pg", "redis7", "in use", Map.of());+ service.createReplicationGroup(requestWithParameterGroup("grp", "custom-pg"));++ AwsException ex = assertThrows(AwsException.class,+ () -> service.deleteCacheParameterGroup("custom-pg"));+ assertEquals("InvalidCacheParameterGroupState", ex.getErrorCode());+ assertEquals(400, ex.getHttpStatus());+ assertTrue(service.findParameterGroup("custom-pg").isPresent(),+ "a refused delete must leave the group in place");++ service.deleteReplicationGroup("grp");+ service.deleteCacheParameterGroup("custom-pg");+ assertTrue(service.findParameterGroup("custom-pg").isEmpty());+ }}