fix(elasticache): roll back proxy port and container on failed provisioning (#1618)

* fix(elasticache): roll back proxy port and container on failed provisioning

* fix(elasticache): stop container even if proxy rollback fails

* fix(elasticache): guard proxy stop and isolate rollback steps

* test(elasticache): cover rollback when container start fails

* fix: remove accidentally committed browse audit file

Signed-off-by: Ahmed khaled <125749397+ahmedk20@users.noreply.github.com>

* fix: remove accidentally committed browse audit file

Signed-off-by: Ahmed khaled <125749397+ahmedk20@users.noreply.github.com>

---------

Signed-off-by: Ahmed khaled <125749397+ahmedk20@users.noreply.github.com>
This commit is contained in:
Ahmed khaled authored and GitHub committed 2026-07-02 00:15:09 -05:00
1 parent 6c5f533f34
commit 8ba1e92713
2 files changed
+104 -18

No files matched your search

@@ -6,29 +6,38 @@ 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.ElastiCacheContainerManager;
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
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.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class ElastiCacheServiceTest {
private ElastiCacheService service;
private ElastiCacheContainerManager containerManager;
private ElastiCacheProxyManager proxyManager;
@BeforeEach
void setUp() {
ElastiCacheContainerManager containerManager = mock(ElastiCacheContainerManager.class);
ElastiCacheProxyManager proxyManager = mock(ElastiCacheProxyManager.class);
containerManager = mock(ElastiCacheContainerManager.class);
proxyManager = mock(ElastiCacheProxyManager.class);
StorageFactory storageFactory = mock(StorageFactory.class);
EmulatorConfig config = mock(EmulatorConfig.class);
@@ -95,4 +104,55 @@ class ElastiCacheServiceTest {
// Single-arg AUTH with wrong password should fail
assertFalse(service.validatePassword("grp", null, "wrong-token"));
}
@Test
void failedProvisioningRollsBackContainerAndReleasesProxyPort() {
ElastiCacheContainerHandle handle =
new ElastiCacheContainerHandle("cid", "grp", "localhost", 6379);
when(containerManager.start(anyString(), anyString())).thenReturn(handle);
// Proxy startup blows up after the port is reserved and the container is started.
doThrow(new RuntimeException("proxy boom"))
.when(proxyManager).startProxy(eq("grp"), any(), anyInt(), anyString(), anyInt(), any());
// The original failure must propagate to the caller (we clean up, then rethrow).
assertThrows(RuntimeException.class,
() -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
// Rollback stopped the proxy and the already-started container.
verify(proxyManager).stopProxy("grp");
verify(containerManager).stop(handle);
// The reserved proxy port was released: a subsequent successful create reuses the base port
// instead of skipping to the next one (which is what a leak would cause).
doNothing().when(proxyManager)
.startProxy(anyString(), any(), anyInt(), anyString(), anyInt(), any());
ReplicationGroup recovered =
service.createReplicationGroup("grp2", "test", AuthMode.PASSWORD, null);
assertEquals(16379, recovered.getProxyPort(),
"Port from the failed create must be released so the next group reuses it");
}
@Test
void failedContainerStartReleasesPortWithoutTouchingProxyOrContainer() {
// Container start blows up before any handle exists.
doThrow(new RuntimeException("container boom"))
.when(containerManager).start(eq("grp"), anyString());
// The original failure must propagate to the caller (we clean up, then rethrow).
assertThrows(RuntimeException.class,
() -> service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null));
// No container started and no proxy started, so rollback must not call either stop.
verify(proxyManager, never()).stopProxy(anyString());
verify(containerManager, never()).stop(any());
// The reserved proxy port was still released: a subsequent successful create reuses the base port.
when(containerManager.start(anyString(), anyString()))
.thenReturn(new ElastiCacheContainerHandle("cid", "grp2", "localhost", 6379));
ReplicationGroup recovered =
service.createReplicationGroup("grp2", "test", AuthMode.PASSWORD, null);
assertEquals(16379, recovered.getProxyPort(),
"Port from the failed create must be released so the next group reuses it");
}
}