mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): keep a replication group's log delivery configurations (#8846)
* fix(elasticache): keep a replication group's log delivery configurations * fix(elasticache): refuse a log delivery configuration with an unknown or missing value * fix(elasticache): settle log delivery to a missing log group as an error
This commit is contained in:
1 parent
9e65c12f8c
commit
66a9d647fb
9 files changed
+287
-13
No files matched your search
+146
@@ -0,0 +1,146 @@
|
||||
package io.floci.aws.services.elasticache;
|
||||
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerManager;
|
||||
import io.quarkus.test.InjectMock;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
|
||||
import software.amazon.awssdk.services.elasticache.ElastiCacheClient;
|
||||
import software.amazon.awssdk.services.elasticache.model.DestinationType;
|
||||
import software.amazon.awssdk.services.elasticache.model.ElastiCacheException;
|
||||
import software.amazon.awssdk.services.elasticache.model.LogDeliveryConfiguration;
|
||||
import software.amazon.awssdk.services.elasticache.model.LogDeliveryConfigurationRequest;
|
||||
import software.amazon.awssdk.services.elasticache.model.LogDeliveryConfigurationStatus;
|
||||
import software.amazon.awssdk.services.elasticache.model.LogFormat;
|
||||
import software.amazon.awssdk.services.elasticache.model.LogType;
|
||||
import software.amazon.awssdk.services.elasticache.model.ReplicationGroupNotFoundException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/** A replication group reports the log delivery configurations it was created with. */
|
||||
@QuarkusTest
|
||||
class ElastiCacheLogDeliveryIntegrationTest {
|
||||
|
||||
@TestHTTPResource
|
||||
URI endpoint;
|
||||
|
||||
@InjectMock
|
||||
ElastiCacheContainerManager containerManager;
|
||||
|
||||
@BeforeEach
|
||||
void stubBackend() {
|
||||
when(containerManager.tryStart(anyString(), anyString(), anyString())).thenAnswer(invocation ->
|
||||
new ElastiCacheContainerHandle("stub-" + invocation.getArgument(0), invocation.getArgument(0),
|
||||
"127.0.0.1", 16379));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createAndDescribeReportTheRequestedLogDeliveryConfigurations() {
|
||||
String groupId = "log-delivery-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String logGroup = "/aws/ec/slow-" + groupId;
|
||||
try (ElastiCacheClient client = client(); CloudWatchLogsClient logs = logs()) {
|
||||
logs.createLogGroup(r -> r.logGroupName(logGroup));
|
||||
List<LogDeliveryConfiguration> created = client.createReplicationGroup(r -> r
|
||||
.replicationGroupId(groupId).replicationGroupDescription("log delivery").engine("redis")
|
||||
.logDeliveryConfigurations(
|
||||
c -> c.logType(LogType.SLOW_LOG).destinationType(DestinationType.CLOUDWATCH_LOGS)
|
||||
.destinationDetails(d -> d.cloudWatchLogsDetails(l -> l.logGroup(logGroup)))
|
||||
.logFormat(LogFormat.JSON).enabled(true),
|
||||
c -> c.logType(LogType.ENGINE_LOG).destinationType(DestinationType.KINESIS_FIREHOSE)
|
||||
.destinationDetails(d -> d.kinesisFirehoseDetails(k -> k.deliveryStream("engine")))
|
||||
.logFormat(LogFormat.TEXT).enabled(true)))
|
||||
.replicationGroup().logDeliveryConfigurations();
|
||||
|
||||
List<LogDeliveryConfiguration> expected = List.of(
|
||||
LogDeliveryConfiguration.builder().logType(LogType.SLOW_LOG)
|
||||
.destinationType(DestinationType.CLOUDWATCH_LOGS)
|
||||
.destinationDetails(d -> d.cloudWatchLogsDetails(l -> l.logGroup(logGroup)))
|
||||
.logFormat(LogFormat.JSON).status(LogDeliveryConfigurationStatus.ACTIVE).build(),
|
||||
LogDeliveryConfiguration.builder().logType(LogType.ENGINE_LOG)
|
||||
.destinationType(DestinationType.KINESIS_FIREHOSE)
|
||||
.destinationDetails(d -> d.kinesisFirehoseDetails(k -> k.deliveryStream("engine")))
|
||||
.logFormat(LogFormat.TEXT).status(LogDeliveryConfigurationStatus.ACTIVE).build());
|
||||
assertEquals(expected, created);
|
||||
assertEquals(expected, client.describeReplicationGroups(r -> r.replicationGroupId(groupId))
|
||||
.replicationGroups().getFirst().logDeliveryConfigurations());
|
||||
client.deleteReplicationGroup(r -> r.replicationGroupId(groupId));
|
||||
logs.deleteLogGroup(r -> r.logGroupName(logGroup));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMissingCloudWatchLogsGroupSettlesTheConfigurationToErrorWithoutCreatingIt() {
|
||||
String groupId = "log-missing-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String logGroup = "/aws/ec/missing-" + groupId;
|
||||
try (ElastiCacheClient client = client(); CloudWatchLogsClient logs = logs()) {
|
||||
client.createReplicationGroup(r -> r.replicationGroupId(groupId).replicationGroupDescription("missing")
|
||||
.engine("redis").logDeliveryConfigurations(c -> c.logType(LogType.SLOW_LOG)
|
||||
.destinationType(DestinationType.CLOUDWATCH_LOGS)
|
||||
.destinationDetails(d -> d.cloudWatchLogsDetails(l -> l.logGroup(logGroup)))
|
||||
.logFormat(LogFormat.JSON).enabled(true)));
|
||||
|
||||
LogDeliveryConfiguration expected = LogDeliveryConfiguration.builder().logType(LogType.SLOW_LOG)
|
||||
.destinationType(DestinationType.CLOUDWATCH_LOGS)
|
||||
.destinationDetails(d -> d.cloudWatchLogsDetails(l -> l.logGroup(logGroup)))
|
||||
.logFormat(LogFormat.JSON).status(LogDeliveryConfigurationStatus.ERROR)
|
||||
.message("Destination log group " + logGroup + " does not exist.").build();
|
||||
assertEquals(List.of(expected), client.describeReplicationGroups(r -> r.replicationGroupId(groupId))
|
||||
.replicationGroups().getFirst().logDeliveryConfigurations());
|
||||
assertEquals(List.of(), logs.describeLogGroups(r -> r.logGroupNamePrefix(logGroup)).logGroups());
|
||||
client.deleteReplicationGroup(r -> r.replicationGroupId(groupId));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"logType=audit-log", "destinationType=s3", "logFormat=xml", "logType="})
|
||||
void aLogDeliveryConfigurationWithAnUnknownOrMissingValueIsRefused(String override) {
|
||||
String groupId = "log-refused-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String[] field = override.split("=", -1);
|
||||
String value = field[1].isEmpty() ? null : field[1];
|
||||
LogDeliveryConfigurationRequest.Builder configuration = LogDeliveryConfigurationRequest.builder()
|
||||
.logType(LogType.SLOW_LOG).destinationType(DestinationType.CLOUDWATCH_LOGS)
|
||||
.destinationDetails(d -> d.cloudWatchLogsDetails(l -> l.logGroup("/aws/ec/slow")))
|
||||
.logFormat(LogFormat.JSON).enabled(true);
|
||||
switch (field[0]) {
|
||||
case "logType" -> configuration.logType(value);
|
||||
case "destinationType" -> configuration.destinationType(value);
|
||||
default -> configuration.logFormat(value);
|
||||
}
|
||||
try (ElastiCacheClient client = client()) {
|
||||
ElastiCacheException refused = assertThrows(ElastiCacheException.class, () -> client
|
||||
.createReplicationGroup(r -> r.replicationGroupId(groupId).replicationGroupDescription("refused")
|
||||
.engine("redis").logDeliveryConfigurations(configuration.build())));
|
||||
|
||||
assertEquals("InvalidParameterValue", refused.awsErrorDetails().errorCode());
|
||||
assertThrows(ReplicationGroupNotFoundException.class,
|
||||
() -> client.describeReplicationGroups(r -> r.replicationGroupId(groupId)));
|
||||
}
|
||||
}
|
||||
|
||||
private CloudWatchLogsClient logs() {
|
||||
return CloudWatchLogsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.build();
|
||||
}
|
||||
|
||||
private ElastiCacheClient client() {
|
||||
return ElastiCacheClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.ServiceConfigAccess;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.ec2.model.SecurityGroup;
|
||||
import io.floci.aws.services.ec2.model.Subnet;
|
||||
@@ -106,7 +107,7 @@ class ElastiCacheServerlessServiceTest {
|
||||
kms = mock(KmsService.class);
|
||||
when(kms.describeKey(anyString(), anyString())).thenThrow(new AwsException("NotFoundException", "Missing key", 400));
|
||||
runtime = new ElastiCacheService(containers, proxies, mock(ValkeyClusterFormation.class), factory,
|
||||
config, ec2, resolver, kms, new ElastiCacheProvisioningIds());
|
||||
config, ec2, resolver, kms, mock(CloudWatchLogsService.class), new ElastiCacheProvisioningIds());
|
||||
memcached = new ElastiCacheMemcachedService(mock(ElastiCacheMemcachedContainerManager.class), factory,
|
||||
config, new ElastiCacheProvisioningIds(), resolver);
|
||||
service = new ElastiCacheServerlessService(factory, resolver, runtime, memcached);
|
||||
@@ -390,7 +391,7 @@ class ElastiCacheServerlessServiceTest {
|
||||
|
||||
private ElastiCacheService newRuntime(StorageFactory activeFactory) {
|
||||
return new ElastiCacheService(containers, proxies, mock(ValkeyClusterFormation.class), activeFactory,
|
||||
config, ec2, resolver, kms, new ElastiCacheProvisioningIds());
|
||||
config, ec2, resolver, kms, mock(CloudWatchLogsService.class), new ElastiCacheProvisioningIds());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerManager;
|
||||
@@ -56,7 +57,7 @@ final class ElastiCacheServiceFixture {
|
||||
when(kmsService.describeKey(any(), any())).thenThrow(new AwsException("NotFoundException", "Key not found", 404));
|
||||
service = new ElastiCacheService(containerManager, proxyManager, mock(ValkeyClusterFormation.class),
|
||||
storageFactory, config, mock(Ec2Service.class), new RegionResolver("us-east-1", "000000000000"),
|
||||
kmsService, new ElastiCacheProvisioningIds());
|
||||
kmsService, mock(CloudWatchLogsService.class), new ElastiCacheProvisioningIds());
|
||||
}
|
||||
|
||||
/** The container the group {@code groupId} gets. */
|
||||
|
||||
@@ -8,6 +8,7 @@ import io.floci.aws.core.common.RequestContext;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsService;
|
||||
import io.floci.aws.services.ec2.Ec2Service;
|
||||
import io.floci.aws.services.ec2.model.Subnet;
|
||||
import io.floci.aws.services.elasticache.container.ElastiCacheContainerHandle;
|
||||
@@ -124,7 +125,7 @@ class ElastiCacheServiceTest {
|
||||
clusterFormation = mock(ValkeyClusterFormation.class);
|
||||
service = new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
storageFactory, config, ec2Service, new RegionResolver("us-east-1", "000000000000"),
|
||||
kmsService, provisioningIds);
|
||||
kmsService, mock(CloudWatchLogsService.class), provisioningIds);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -152,7 +153,7 @@ class ElastiCacheServiceTest {
|
||||
ElastiCacheService svc = new ElastiCacheService(cm, pm, mock(ValkeyClusterFormation.class),
|
||||
sf, cfg, mock(Ec2Service.class),
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
mock(KmsService.class), provisioningIds);
|
||||
mock(KmsService.class), mock(CloudWatchLogsService.class), provisioningIds);
|
||||
|
||||
svc.createReplicationGroup("g1", "d", AuthMode.PASSWORD, null, "us-east-1");
|
||||
|
||||
@@ -1210,7 +1211,7 @@ class ElastiCacheServiceTest {
|
||||
when(config.hostname()).thenReturn(Optional.of("localhost"));
|
||||
return new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
storageFactory, config, mock(Ec2Service.class),
|
||||
new RegionResolver("us-east-1", "000000000000"), mock(KmsService.class), new ElastiCacheProvisioningIds());
|
||||
new RegionResolver("us-east-1", "000000000000"), mock(KmsService.class), mock(CloudWatchLogsService.class), new ElastiCacheProvisioningIds());
|
||||
}
|
||||
|
||||
private static void stubPerNodeContainers(ElastiCacheContainerManager containerManager) {
|
||||
@@ -1962,7 +1963,7 @@ class ElastiCacheServiceTest {
|
||||
.thenAnswer(inv -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
ElastiCacheService svc = new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
factory, config, mock(Ec2Service.class),
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, provisioningIds);
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, mock(CloudWatchLogsService.class), provisioningIds);
|
||||
svc.createReplicationGroup("g1", "d", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
|
||||
pausing.pauseOn(PausingStorageBackend.Call.PUT, "g1");
|
||||
@@ -2081,7 +2082,7 @@ class ElastiCacheServiceTest {
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestContextInstance, "000000000000"));
|
||||
ElastiCacheService svc = new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
factory, config, mock(Ec2Service.class),
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, provisioningIds);
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, mock(CloudWatchLogsService.class), provisioningIds);
|
||||
|
||||
CountDownLatch startedLatch = new CountDownLatch(1);
|
||||
CountDownLatch releaseLatch = new CountDownLatch(1);
|
||||
@@ -2131,7 +2132,7 @@ class ElastiCacheServiceTest {
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestContextInstance, "000000000000"));
|
||||
ElastiCacheService svc = new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
factory, config, mock(Ec2Service.class),
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, provisioningIds);
|
||||
new RegionResolver("us-east-1", "000000000000"), kmsService, mock(CloudWatchLogsService.class), provisioningIds);
|
||||
|
||||
CountDownLatch startedLatch = new CountDownLatch(1);
|
||||
CountDownLatch releaseLatch = new CountDownLatch(1);
|
||||
@@ -2473,7 +2474,7 @@ class ElastiCacheServiceTest {
|
||||
private ElastiCacheService serviceAfterRestart() {
|
||||
return new ElastiCacheService(containerManager, proxyManager, clusterFormation,
|
||||
storageFactory, config, ec2Service, new RegionResolver("us-east-1", "000000000000"),
|
||||
kmsService, provisioningIds);
|
||||
kmsService, mock(CloudWatchLogsService.class), provisioningIds);
|
||||
}
|
||||
|
||||
private ElastiCacheMemcachedService memcachedService() {
|
||||
|
||||
Reference in new issue
Block a user