feat(elasticache): add Memcached CreateCacheCluster/DescribeCacheClus… (#930)

* feat(elasticache): add Memcached CreateCacheCluster/DescribeCacheClusters/DeleteCacheCluster support

Closes #928

* fix(elasticache): stop Memcached containers on emulator shutdown

Wire ElastiCacheMemcachedContainerManager into EmulatorLifecycle so that
Memcached containers are stopped and removed when the emulator shuts down,
preventing orphaned containers.
This commit is contained in:
Lucas Costa authored and GitHub committed 2026-05-19 00:22:33 -05:00
1 parent c426289879
commit 6a9af6057c
12 files changed
+720 -5

No files matched your search

@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.lifecycle.InitLifecycleState;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHook;
import io.github.hectorvent.floci.lifecycle.inithook.InitializationHooksRunner;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheMemcachedContainerManager;
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
import io.github.hectorvent.floci.services.lambda.DynamoDbStreamsEventSourcePoller;
import io.github.hectorvent.floci.services.lambda.KinesisEventSourcePoller;
@@ -43,6 +44,7 @@ class EmulatorLifecycleTest {
@Mock private EmulatorConfig.ServicesConfig servicesConfig;
@Mock private EmulatorConfig.Ec2ServiceConfig ec2ServiceConfig;
@Mock private ElastiCacheContainerManager elastiCacheContainerManager;
@Mock private ElastiCacheMemcachedContainerManager elastiCacheMemcachedContainerManager;
@Mock private ElastiCacheProxyManager elastiCacheProxyManager;
@Mock private RdsContainerManager rdsContainerManager;
@Mock private RdsProxyManager rdsProxyManager;
@@ -67,10 +69,10 @@ class EmulatorLifecycleTest {
emulatorLifecycle = new EmulatorLifecycle(
storageFactory, serviceRegistry, config,
elastiCacheContainerManager, elastiCacheProxyManager,
rdsContainerManager, rdsProxyManager, initializationHooksRunner,
sqsPoller, kinesisPoller, dynamodbStreamsPoller, pipesService,
ec2MetadataServer, initLifecycleState);
elastiCacheContainerManager, elastiCacheMemcachedContainerManager,
elastiCacheProxyManager, rdsContainerManager, rdsProxyManager,
initializationHooksRunner, sqsPoller, kinesisPoller, dynamodbStreamsPoller,
pipesService, ec2MetadataServer, initLifecycleState);
}
private void stubStorageConfig() {
@@ -0,0 +1,186 @@
package io.github.hectorvent.floci.services.elasticache;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ElastiCacheMemcachedIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20260412/us-east-1/elasticache/aws4_request";
private static final String CLUSTER_ID = "it-memcached-cluster";
private static final int SOCKET_TIMEOUT_MS = 10_000;
private static int clusterPort;
@BeforeAll
static void requireDocker() {
Assumptions.assumeTrue(isDockerAvailable(), "Docker daemon must be available for Memcached integration tests");
}
@AfterAll
static void cleanup() {
try {
given()
.formParam("Action", "DeleteCacheCluster")
.formParam("CacheClusterId", CLUSTER_ID)
.header("Authorization", AUTH_HEADER)
.post("/");
} catch (Exception ignored) {}
}
@Test
@Order(1)
void createCacheCluster() {
clusterPort =
given()
.formParam("Action", "CreateCacheCluster")
.formParam("CacheClusterId", CLUSTER_ID)
.formParam("Engine", "memcached")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.contentType("application/xml")
.body("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.CacheClusterId", equalTo(CLUSTER_ID))
.body("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.CacheClusterStatus", equalTo("available"))
.body("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.Engine", equalTo("memcached"))
.body("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.ConfigurationEndpoint.Address", notNullValue())
.body("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.ConfigurationEndpoint.Port", notNullValue())
.extract()
.xmlPath()
.getInt("CreateCacheClusterResponse.CreateCacheClusterResult.CacheCluster.ConfigurationEndpoint.Port");
}
@Test
@Order(2)
void describeCacheClustersIncludesCreatedCluster() {
given()
.formParam("Action", "DescribeCacheClusters")
.formParam("CacheClusterId", CLUSTER_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DescribeCacheClustersResponse.DescribeCacheClustersResult.CacheClusters.CacheCluster.CacheClusterId",
equalTo(CLUSTER_ID))
.body("DescribeCacheClustersResponse.DescribeCacheClustersResult.CacheClusters.CacheCluster.Engine",
equalTo("memcached"));
}
@Test
@Order(3)
void createCacheClusterWithInvalidEngineReturnsError() {
given()
.formParam("Action", "CreateCacheCluster")
.formParam("CacheClusterId", "redis-attempt")
.formParam("Engine", "redis")
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(400);
}
@Test
@Order(4)
void memcachedAcceptsSetAndGet() throws Exception {
try (Socket socket = new Socket("localhost", clusterPort)) {
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
String setCmd = "set testkey 0 0 5\r\nhello\r\n";
out.write(setCmd.getBytes(StandardCharsets.UTF_8));
out.flush();
assertEquals("STORED", readLine(in));
out.write("get testkey\r\n".getBytes(StandardCharsets.UTF_8));
out.flush();
String valueLine = readLine(in);
assertTrue(valueLine.startsWith("VALUE testkey"), "Expected VALUE line, got: " + valueLine);
assertEquals("hello", readLine(in));
assertEquals("END", readLine(in));
}
}
@Test
@Order(5)
void versionCommandResponds() throws Exception {
try (Socket socket = new Socket("localhost", clusterPort)) {
socket.setSoTimeout(SOCKET_TIMEOUT_MS);
socket.getOutputStream().write("version\r\n".getBytes(StandardCharsets.UTF_8));
socket.getOutputStream().flush();
String response = readLine(socket.getInputStream());
assertTrue(response.startsWith("VERSION"), "Expected VERSION response, got: " + response);
}
}
@Test
@Order(6)
void deleteCacheCluster() {
given()
.formParam("Action", "DeleteCacheCluster")
.formParam("CacheClusterId", CLUSTER_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(200)
.body("DeleteCacheClusterResponse.DeleteCacheClusterResult.CacheCluster.CacheClusterId", equalTo(CLUSTER_ID));
given()
.formParam("Action", "DescribeCacheClusters")
.formParam("CacheClusterId", CLUSTER_ID)
.header("Authorization", AUTH_HEADER)
.when()
.post("/")
.then()
.statusCode(404);
}
private static String readLine(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
int b;
while ((b = in.read()) != -1) {
if (b == '\r') {
in.read(); // consume \n
break;
}
sb.append((char) b);
}
return sb.toString();
}
private static boolean isDockerAvailable() {
try {
Process process = new ProcessBuilder("docker", "version", "--format", "{{.Server.Version}}")
.redirectErrorStream(true)
.start();
return process.waitFor() == 0;
} catch (Exception e) {
return false;
}
}
}
@@ -0,0 +1,99 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheMemcachedContainerManager;
import io.github.hectorvent.floci.services.elasticache.model.CacheCluster;
import io.github.hectorvent.floci.services.elasticache.model.CacheClusterStatus;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class ElastiCacheMemcachedServiceTest {
private ElastiCacheMemcachedService service;
@BeforeEach
void setUp() {
ElastiCacheMemcachedContainerManager containerManager = mock(ElastiCacheMemcachedContainerManager.class);
StorageFactory storageFactory = mock(StorageFactory.class);
EmulatorConfig config = mock(EmulatorConfig.class);
EmulatorConfig.ServicesConfig servicesConfig = mock(EmulatorConfig.ServicesConfig.class);
EmulatorConfig.ElastiCacheServiceConfig ecConfig = mock(EmulatorConfig.ElastiCacheServiceConfig.class);
when(config.services()).thenReturn(servicesConfig);
when(servicesConfig.elasticache()).thenReturn(ecConfig);
when(ecConfig.defaultMemcachedImage()).thenReturn("memcached:1.6");
when(config.hostname()).thenReturn(Optional.of("localhost"));
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv -> new InMemoryStorage<>());
when(containerManager.start(anyString(), anyString()))
.thenReturn(new ElastiCacheContainerHandle("cid", "cluster", "localhost", 11211));
service = new ElastiCacheMemcachedService(containerManager, storageFactory, config);
}
@Test
void createClusterReturnsAvailableCluster() {
CacheCluster cluster = service.createCacheCluster("my-cluster");
assertEquals("my-cluster", cluster.getCacheClusterId());
assertEquals(CacheClusterStatus.AVAILABLE, cluster.getCacheClusterStatus());
assertEquals("memcached", cluster.getEngine());
assertEquals("localhost", cluster.getConfigurationEndpoint().address());
}
@Test
void createDuplicateClusterThrows() {
service.createCacheCluster("my-cluster");
AwsException ex = assertThrows(AwsException.class, () -> service.createCacheCluster("my-cluster"));
assertEquals("CacheClusterAlreadyExistsFault", ex.getErrorCode());
}
@Test
void getUnknownClusterThrows() {
AwsException ex = assertThrows(AwsException.class, () -> service.getCacheCluster("no-such-cluster"));
assertEquals("CacheClusterNotFound", ex.getErrorCode());
}
@Test
void listClustersReturnsAll() {
service.createCacheCluster("cluster-a");
service.createCacheCluster("cluster-b");
Collection<CacheCluster> list = service.listCacheClusters(null);
assertEquals(2, list.size());
}
@Test
void listClustersFiltersById() {
service.createCacheCluster("cluster-a");
service.createCacheCluster("cluster-b");
Collection<CacheCluster> list = service.listCacheClusters("cluster-a");
assertEquals(1, list.size());
assertEquals("cluster-a", list.iterator().next().getCacheClusterId());
}
@Test
void deleteClusterRemovesIt() {
service.createCacheCluster("my-cluster");
service.deleteCacheCluster("my-cluster");
AwsException ex = assertThrows(AwsException.class, () -> service.getCacheCluster("my-cluster"));
assertEquals("CacheClusterNotFound", ex.getErrorCode());
}
}