fix(elasticache): honor the requested Port on CreateReplicationGroup (#3276)

* fix(elasticache): honor the requested Port on CreateReplicationGroup

Port is a modeled optional input on CreateReplicationGroup ("the port number
on which each member of the replication group accepts connections"), but
allocateProxyPort took no argument and always handed out the next free port
from the base of the range. A caller that pinned a port read back a different
one, which Terraform reports as a replacement-forcing change and therefore as
permanent drift.

allocateProxyPort now takes the requested port and honors it when it is free
and inside the proxy range, matching NeptuneService.allocateProxyPort. Floci
multiplexes every group's proxy onto one host, so two groups genuinely cannot
share a port; when the requested one is taken or out of range this logs why
and falls back to the next free port rather than failing the create. In
cluster mode only the first node can take the requested port, because the
group reports its port from that node.

The compatibility fixtures pinned 6379, the bottom of the range and the port
floci would have allocated anyway, so they asserted nothing and passed by
coincidence. They now pin 6395, which fails without this fix.

* fix(elasticache): refuse a requested Port that cannot be honored

Honoring Port left one path that quietly ignored it: a requested port that was
already held by another replication group, or outside the range this emulator
serves, fell back to the next free one. The describe then reported the allocated
port, so a caller who pinned one still read back a different value and still saw
the replacement-forcing drift that honoring Port exists to remove. The narrower
case reproduced the bug the change was written to fix.

The fallback could only ever hit a caller who did ask for a port, since one who
does not care passes no Port at all and never reaches that branch, so it served
nobody. An explicit port is now either honored or refused with
InvalidParameterValue, the sender fault CreateReplicationGroup declares, naming
the port and the reason so the caller can pick another. Unpinned creates still
scan the range exactly as before, and the restore path for persisted groups is
unaffected because it asks for no particular port.

The two tests that pinned the old fallback are inverted rather than dropped, so
the new contract is held just as firmly, and one more proves an unpinned create
still falls back when the base port is taken.

NeptuneService.allocateProxyPort carries the same substitution and RdsService
ignores a requested port entirely; both are left for their own changes.

Raised by the review on this PR.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-08 22:42:22 -05:00
1 parent d322d48bd5
commit 67198c7064
6 files changed
+173 -7

No files matched your search

@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
import io.github.hectorvent.floci.core.common.AwsException;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentCaptor;
@@ -238,6 +239,42 @@ class ElastiCacheQueryHandlerTest {
assertEquals(Map.of("Name", "g1"), request.tags());
}
@Test
void createReplicationGroup_passesTheRequestedPortToService() {
// Port is a modeled optional input on CreateReplicationGroup; the handler dropped it,
// so a caller that pinned one always read back whatever port floci allocated.
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
.thenReturn(group("g1"));
MultivaluedMap<String, String> p = params();
p.add("ReplicationGroupId", "g1");
p.add("ReplicationGroupDescription", "d");
p.add("Port", "6395");
assertEquals(200, handler.handle("CreateReplicationGroup", p, "us-east-1").getStatus());
ArgumentCaptor<ElastiCacheService.CreateReplicationGroupRequest> captor =
ArgumentCaptor.forClass(ElastiCacheService.CreateReplicationGroupRequest.class);
verify(service).createReplicationGroup(captor.capture());
assertEquals(6395, captor.getValue().port());
}
@Test
void createReplicationGroup_leavesPortNullWhenAbsent() {
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
.thenReturn(group("g1"));
MultivaluedMap<String, String> p = params();
p.add("ReplicationGroupId", "g1");
p.add("ReplicationGroupDescription", "d");
assertEquals(200, handler.handle("CreateReplicationGroup", p, "us-east-1").getStatus());
ArgumentCaptor<ElastiCacheService.CreateReplicationGroupRequest> captor =
ArgumentCaptor.forClass(ElastiCacheService.CreateReplicationGroupRequest.class);
verify(service).createReplicationGroup(captor.capture());
assertNull(captor.getValue().port(),
"An absent Port must stay null so the service allocates as before");
}
@Test
void createReplicationGroup_readsTheEncryptionFlagAsAwsDoes() {
when(service.createReplicationGroup(any(ElastiCacheService.CreateReplicationGroupRequest.class)))
@@ -226,7 +226,86 @@ class ElastiCacheServiceTest {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
"default.valkey8.cluster.on", null, null, numNodeGroups, replicasPerNodeGroup,
null, true, null, ReplicationGroupSettings.defaults(), Map.of());
null, true, null, null, ReplicationGroupSettings.defaults(), Map.of());
}
private static ElastiCacheService.CreateReplicationGroupRequest singleNodeRequest(
String groupId, Integer port) {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", null, null, null, null, null, null,
null, null, null, null, null, port, ReplicationGroupSettings.defaults(), Map.of());
}
// botocore models Port as an optional input on CreateReplicationGroup ("the port number on
// which each member of the replication group accepts connections"). Ignoring it made every
// caller that pins a port read back a different one, which Terraform treats as
// replacement-forcing and reports as permanent drift.
@Test
void requestedPortIsHonoredWhenFreeAndInRange() {
ReplicationGroup group = service.createReplicationGroup(singleNodeRequest("grp", 16390));
assertEquals(16390, group.getProxyPort(),
"A free, in-range requested Port must be the port the group reports");
}
@Test
void unpinnedCreateStillAllocatesFromTheBasePort() {
ReplicationGroup group = service.createReplicationGroup(singleNodeRequest("grp", null));
assertEquals(16379, group.getProxyPort(),
"A create with no Port keeps the previous behavior of taking the base port");
}
@Test
void requestedPortAlreadyInUseIsRejected() {
// floci multiplexes every group's proxy onto one host, so two groups cannot share a port.
// Substituting a different one would hand back the drift honoring Port exists to remove,
// and it could only ever hit a caller who did pin a port.
service.createReplicationGroup(singleNodeRequest("grp1", 16390));
AwsException thrown = assertThrows(AwsException.class,
() -> service.createReplicationGroup(singleNodeRequest("grp2", 16390)));
assertEquals("InvalidParameterValue", thrown.getErrorCode());
assertTrue(thrown.getMessage().contains("16390"));
}
@Test
void requestedPortOutsideTheProxyRangeIsRejected() {
AwsException thrown = assertThrows(AwsException.class,
() -> service.createReplicationGroup(singleNodeRequest("grp", 9999)));
assertEquals("InvalidParameterValue", thrown.getErrorCode());
assertTrue(thrown.getMessage().contains("9999"));
}
@Test
void anUnpinnedCreateStillFallsBackWhenTheBasePortIsTaken() {
// The fallback survives for callers that named no port: only an explicit one is refused.
service.createReplicationGroup(singleNodeRequest("grp1", 16379));
ReplicationGroup second = service.createReplicationGroup(singleNodeRequest("grp2", null));
assertEquals(16380, second.getProxyPort());
}
@Test
void clusterModeHonorsTheRequestedPortOnTheFirstNode() {
// The group's Port is reported from the first node's proxy port, so that is the only
// node whose port a caller can pin; the remaining members take whatever is free.
stubPerNodeContainers();
ReplicationGroup group = service.createReplicationGroup(
new ElastiCacheService.CreateReplicationGroupRequest("grp", "test",
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
"default.valkey8.cluster.on", null, null, 2, 1,
null, true, null, 16390, ReplicationGroupSettings.defaults(), Map.of()));
assertEquals(16390, group.getConfigurationEndpoint().port());
assertEquals(16390, group.getClusterNodes().getFirst().getProxyPort());
assertEquals(4, group.getClusterNodes().stream()
.map(ClusterNode::getProxyPort).distinct().count(),
"Each node must still own its own proxy port");
}
private void stubPerNodeContainers() {
@@ -687,7 +766,7 @@ class ElastiCacheServiceTest {
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());
null, null, null, null, ReplicationGroupSettings.defaults(), Map.of());
}
@Test