mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test(ecs): move the remaining ECS service tests onto EcsServiceFixtures (#8759)
This commit is contained in:
1 parent
9c9792a019
commit
783bd1a5fe
5 files changed
+70
-181
No files matched your search
@@ -1,14 +1,9 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.model.ContainerDefinition;
|
||||
import io.floci.aws.services.ecs.model.EcsTask;
|
||||
import io.floci.aws.services.ecs.model.LaunchType;
|
||||
@@ -18,12 +13,11 @@ import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
import io.floci.aws.services.eventbridge.TargetDispatcher;
|
||||
import io.floci.aws.services.eventbridge.model.RuleState;
|
||||
import io.floci.aws.services.eventbridge.model.Target;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -55,8 +49,8 @@ class EcsEventBridgeIntegrationTest {
|
||||
return null;
|
||||
}).when(dispatcher).dispatch(anyString(), any(), anyString(), anyString(), any());
|
||||
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, "000000000000");
|
||||
InMemoryStorageFactory storageFactory = new InMemoryStorageFactory();
|
||||
RegionResolver regionResolver = new RegionResolver(REGION, EcsServiceFixtures.ACCOUNT);
|
||||
InMemoryStorageFactory storageFactory = new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT);
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
@@ -75,15 +69,10 @@ class EcsEventBridgeIntegrationTest {
|
||||
|
||||
EcsEventPublisher eventPublisher = new EcsEventPublisher(eventBridgeService, regionResolver, objectMapper);
|
||||
|
||||
ecsService = new EcsService(
|
||||
regionResolver,
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storageFactory,
|
||||
eventPublisher
|
||||
);
|
||||
ecsService.initializeStorage();
|
||||
ecsService = EcsServiceFixtures.builder()
|
||||
.storageFactory(storageFactory)
|
||||
.eventPublisher(eventPublisher)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static TaskDefinition registerTaskDef(EcsService service, String family, String image) {
|
||||
@@ -193,21 +182,4 @@ class EcsEventBridgeIntegrationTest {
|
||||
&& taskArn.equals(evt.path("detail").path("taskArn").asText())
|
||||
), "Expected STOPPED ECS Task State Change event with matching taskArn in delivered events");
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,6 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
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.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.container.EcsTaskHandle;
|
||||
import io.floci.aws.services.ecs.model.AwsVpcConfiguration;
|
||||
@@ -24,7 +18,6 @@ import org.junit.jupiter.api.Test;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
@@ -41,7 +34,6 @@ 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.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -305,11 +297,7 @@ class EcsServiceAwaitStableTest {
|
||||
}
|
||||
|
||||
private EcsService mockModeService(EcsContainerManager containerManager) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
return track(new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
|
||||
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory(), null));
|
||||
return track(EcsServiceFixtures.builder().containerManager(containerManager).build());
|
||||
}
|
||||
|
||||
/** A container manager that runs {@code onStart} in {@code startTask}; its containers keep running. */
|
||||
@@ -327,15 +315,10 @@ class EcsServiceAwaitStableTest {
|
||||
}
|
||||
|
||||
private EcsService dockerModeService(EcsContainerManager containerManager) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(false);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
return track(new EcsService(new RegionResolver(REGION, "000000000000"), containerManager,
|
||||
config, mock(EcsLoadBalancerRegistrar.class), new InMemoryStorageFactory(), null));
|
||||
return track(EcsServiceFixtures.builder().containerManager(containerManager).dockerMode().build());
|
||||
}
|
||||
|
||||
private EcsService track(EcsService service) {
|
||||
service.initializeStorage();
|
||||
created.add(service);
|
||||
return service;
|
||||
}
|
||||
@@ -358,20 +341,4 @@ class EcsServiceAwaitStableTest {
|
||||
.filter(t -> TaskStatus.RUNNING.name().equals(t.getLastStatus()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
+17
-48
@@ -1,21 +1,14 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
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.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudwatch.metrics.CloudWatchMetricsService;
|
||||
import io.floci.aws.services.cloudwatch.metrics.model.MetricAlarm;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.container.EcsTaskHandle;
|
||||
import io.floci.aws.services.ecs.exec.EcsExecSessionRegistry;
|
||||
import io.floci.aws.services.ecs.model.ContainerDefinition;
|
||||
import io.floci.aws.services.ecs.model.CreateServiceRequest;
|
||||
import io.floci.aws.services.ecs.model.Deployment;
|
||||
@@ -26,6 +19,7 @@ import io.floci.aws.services.ecs.model.ServiceDeployment;
|
||||
import io.floci.aws.services.ecs.model.ServiceRevision;
|
||||
import io.floci.aws.services.ecs.model.TaskDefinition;
|
||||
import io.floci.aws.services.ecs.model.UpdateServiceRequest;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
@@ -48,7 +42,6 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -218,7 +211,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
alarm.setStateValue("ALARM");
|
||||
when(metricsService.describeAlarms(List.of("deployment-failure"), null, REGION))
|
||||
.thenReturn(List.of(alarm));
|
||||
EcsService service = newService(new InMemoryStorageFactory(), metricsService);
|
||||
EcsService service = newService(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT), metricsService);
|
||||
EcsServiceModel model = createServiceWithDeploymentConfiguration(service, "cb-alarm", 1,
|
||||
Map.of("alarms", Map.of(
|
||||
"enable", true,
|
||||
@@ -248,7 +241,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
alarm.setStateValue("ALARM");
|
||||
when(metricsService.describeAlarms(List.of("new-alarm"), null, REGION))
|
||||
.thenReturn(List.of(alarm));
|
||||
EcsService service = newService(new InMemoryStorageFactory(), metricsService);
|
||||
EcsService service = newService(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT), metricsService);
|
||||
EcsServiceModel model = createServiceWithDeploymentConfiguration(service, "cb-alarm-update", 1,
|
||||
Map.of("alarms", Map.of("enable", true, "rollback", false,
|
||||
"alarmNames", List.of("old-alarm"))));
|
||||
@@ -310,7 +303,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
@ValueSource(strings = {"LATEST", "1.3.0"})
|
||||
void rollbackRestoresTheMostRecentSuccessfulRevision(String requestedVersion) {
|
||||
EcsEventPublisher publisher = mock(EcsEventPublisher.class);
|
||||
EcsService service = newService(new InMemoryStorageFactory(), null, publisher);
|
||||
EcsService service = newService(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT), null, publisher);
|
||||
healthy = true;
|
||||
String successfulDeploymentId = createServiceWithDeploymentConfiguration(service, "cb-rollback", 1,
|
||||
Map.of("deploymentCircuitBreaker", breaker(true, true)), requestedVersion).getDeploymentId();
|
||||
@@ -392,7 +385,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
alarm.setStateValue("OK");
|
||||
when(metricsService.describeAlarms(List.of("rollback-drain-alarm"), null, REGION))
|
||||
.thenReturn(List.of(alarm));
|
||||
EcsService service = newService(new InMemoryStorageFactory(), metricsService);
|
||||
EcsService service = newService(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT), metricsService);
|
||||
healthy = true;
|
||||
EcsServiceModel model = createService(service, "cb-drain", 2, breaker(true, true));
|
||||
model.setDeploymentConfiguration(Map.of(
|
||||
@@ -597,7 +590,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
*/
|
||||
@Test
|
||||
void aFailedDeploymentStaysHaltedAcrossARestart() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT);
|
||||
EcsService before = newService(storage);
|
||||
createService(before, "cb-restart", 1, breaker(true, false));
|
||||
for (int i = 0; i < 3; i++) {
|
||||
@@ -615,7 +608,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
|
||||
@Test
|
||||
void anAlarmStillFailsAnInProgressDeploymentAfterARestart() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT);
|
||||
CloudWatchMetricsService metricsService = mock(CloudWatchMetricsService.class);
|
||||
MetricAlarm alarm = new MetricAlarm();
|
||||
alarm.setAlarmName("restart-alarm");
|
||||
@@ -640,7 +633,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
|
||||
@Test
|
||||
void aCompletedDeploymentIsNotFailedByAnAlarmAfterARestart() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT);
|
||||
CloudWatchMetricsService metricsService = mock(CloudWatchMetricsService.class);
|
||||
MetricAlarm alarm = new MetricAlarm();
|
||||
alarm.setAlarmName("completed-restart-alarm");
|
||||
@@ -669,7 +662,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
@CsvSource({"LATEST,false,LATEST", "1.3.0,false,1.3.0", "LATEST,true,1.4.0", "1.3.0,true,1.3.0"})
|
||||
void aRollbackAfterRestartUsesThePersistedSuccessfulRevision(String requestedVersion, boolean legacy,
|
||||
String expectedVersion) throws JsonProcessingException {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT);
|
||||
CloudWatchMetricsService metricsService = mock(CloudWatchMetricsService.class);
|
||||
MetricAlarm alarm = new MetricAlarm();
|
||||
alarm.setAlarmName("restart-rollback-alarm");
|
||||
@@ -787,7 +780,7 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
|
||||
/** Docker mode over a container manager whose launches succeed or throw as the test says. */
|
||||
private EcsService newService() {
|
||||
return newService(new InMemoryStorageFactory());
|
||||
return newService(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT));
|
||||
}
|
||||
|
||||
/** A service over {@code storage}; a second one over the same storage is a restart. */
|
||||
@@ -801,9 +794,6 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
|
||||
private EcsService newService(InMemoryStorageFactory storage, CloudWatchMetricsService metricsService,
|
||||
EcsEventPublisher eventPublisher) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(false);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
EcsContainerManager containerManager = mock(EcsContainerManager.class);
|
||||
when(containerManager.stopTaskAndCollectExitCodes(any())).thenAnswer(invocation -> {
|
||||
EcsTaskHandle handle = invocation.getArgument(0);
|
||||
@@ -822,34 +812,13 @@ class EcsServiceDeploymentCircuitBreakerTest {
|
||||
}
|
||||
return new EcsTaskHandle(task.getTaskArn(), Map.of("app", "docker-id"), Map.of());
|
||||
});
|
||||
service = new EcsService(
|
||||
new RegionResolver(REGION, "000000000000"),
|
||||
containerManager,
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage,
|
||||
eventPublisher,
|
||||
new EcsExecSessionRegistry(),
|
||||
null,
|
||||
metricsService);
|
||||
service.initializeStorage();
|
||||
service = EcsServiceFixtures.builder()
|
||||
.containerManager(containerManager)
|
||||
.storageFactory(storage)
|
||||
.cloudWatchMetricsService(metricsService)
|
||||
.eventPublisher(eventPublisher)
|
||||
.dockerMode()
|
||||
.build();
|
||||
return service;
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package io.floci.aws.services.ecs;
|
||||
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.cloudwatch.metrics.CloudWatchMetricsService;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.exec.EcsExecSessionRegistry;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
@@ -15,8 +17,9 @@ import static org.mockito.Mockito.when;
|
||||
/**
|
||||
* An {@link EcsService} over in-memory storage for account 000000000000 in us-east-1, for unit tests
|
||||
* that drive the service directly. It is in mock mode, and its containers and load balancer
|
||||
* registration are mocks, unless a test changes them through {@link #builder()}. Storage is
|
||||
* initialised; the reconciler is not scheduled, so tests call {@code reconcileServices()} themselves.
|
||||
* registration are mocks, unless a test changes them through {@link #builder()}. Storage is a fresh
|
||||
* {@link InMemoryStorageFactory} unless the test passes its own, and is initialised; the reconciler
|
||||
* is not scheduled, so tests call {@code reconcileServices()} themselves.
|
||||
*/
|
||||
final class EcsServiceFixtures {
|
||||
|
||||
@@ -39,6 +42,8 @@ final class EcsServiceFixtures {
|
||||
private EcsContainerManager containerManager = mock(EcsContainerManager.class);
|
||||
private EcsLoadBalancerRegistrar loadBalancerRegistrar = mock(EcsLoadBalancerRegistrar.class);
|
||||
private EcsEventPublisher eventPublisher;
|
||||
private StorageFactory storageFactory;
|
||||
private CloudWatchMetricsService cloudWatchMetricsService;
|
||||
private Clock clock = Clock.systemUTC();
|
||||
private boolean dockerMode;
|
||||
|
||||
@@ -60,6 +65,17 @@ final class EcsServiceFixtures {
|
||||
return this;
|
||||
}
|
||||
|
||||
/** Storage to build on; a second service over the same storage is a restart. */
|
||||
Builder storageFactory(StorageFactory storageFactory) {
|
||||
this.storageFactory = storageFactory;
|
||||
return this;
|
||||
}
|
||||
|
||||
Builder cloudWatchMetricsService(CloudWatchMetricsService cloudWatchMetricsService) {
|
||||
this.cloudWatchMetricsService = cloudWatchMetricsService;
|
||||
return this;
|
||||
}
|
||||
|
||||
Builder clock(Clock clock) {
|
||||
this.clock = clock;
|
||||
return this;
|
||||
@@ -76,8 +92,9 @@ final class EcsServiceFixtures {
|
||||
when(config.services().ecs().mock()).thenReturn(!dockerMode);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
EcsService service = new EcsService(new RegionResolver(REGION, ACCOUNT), containerManager, config,
|
||||
loadBalancerRegistrar, new InMemoryStorageFactory(ACCOUNT), eventPublisher,
|
||||
new EcsExecSessionRegistry(), null, null, clock);
|
||||
loadBalancerRegistrar,
|
||||
storageFactory != null ? storageFactory : new InMemoryStorageFactory(ACCOUNT), eventPublisher,
|
||||
new EcsExecSessionRegistry(), null, cloudWatchMetricsService, clock);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,16 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
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.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.ecs.container.EcsContainerManager;
|
||||
import io.floci.aws.services.ecs.model.ContainerDefinition;
|
||||
import io.floci.aws.services.ecs.model.TaskDefinition;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Task definitions persisted before their storage key carried the region ({@code family:revision})
|
||||
@@ -37,9 +27,9 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
|
||||
@Test
|
||||
void anotherRegionsRegistrationKeepsALegacyDefinition() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
storage.taskDefinitions().put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(storage);
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions = taskDefinitionsStore();
|
||||
taskDefinitions.put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(taskDefinitions);
|
||||
|
||||
TaskDefinition other = register(service, OTHER);
|
||||
|
||||
@@ -50,13 +40,13 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
|
||||
@Test
|
||||
void aLegacyDefinitionIsRefiledOnItsNextWriteAndListedOnce() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
storage.taskDefinitions().put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(storage);
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions = taskDefinitionsStore();
|
||||
taskDefinitions.put(FAMILY + ":1", legacyDefinition());
|
||||
EcsService service = newService(taskDefinitions);
|
||||
|
||||
service.deregisterTaskDefinition(FAMILY + ":1", REGION);
|
||||
|
||||
assertEquals(List.of(REGION + "::" + FAMILY + ":1"), List.copyOf(storage.taskDefinitions().keys()));
|
||||
assertEquals(List.of(REGION + "::" + FAMILY + ":1"), List.copyOf(taskDefinitions.keys()));
|
||||
assertEquals(List.of(LEGACY_ARN), service.listTaskDefinitions(REGION, FAMILY, "INACTIVE", null, null, null)
|
||||
.arns());
|
||||
assertEquals(2, register(service, REGION).getRevision());
|
||||
@@ -64,12 +54,12 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
|
||||
@Test
|
||||
void aStoredDefinitionWithoutAnArnBreaksNoLookup() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions = taskDefinitionsStore();
|
||||
TaskDefinition unreadable = legacyDefinition();
|
||||
unreadable.setFamily(FAMILY + "-unreadable");
|
||||
unreadable.setTaskDefinitionArn(null);
|
||||
storage.taskDefinitions().put(FAMILY + "-unreadable:1", unreadable);
|
||||
EcsService service = newService(storage);
|
||||
taskDefinitions.put(FAMILY + "-unreadable:1", unreadable);
|
||||
EcsService service = newService(taskDefinitions);
|
||||
|
||||
TaskDefinition registered = register(service, REGION);
|
||||
|
||||
@@ -82,11 +72,11 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
|
||||
@Test
|
||||
void aStoredDefinitionWithoutAnArnStaysInTheDefaultRegion() {
|
||||
InMemoryStorageFactory storage = new InMemoryStorageFactory();
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions = taskDefinitionsStore();
|
||||
TaskDefinition unreadable = legacyDefinition();
|
||||
unreadable.setTaskDefinitionArn(null);
|
||||
storage.taskDefinitions().put(FAMILY + ":1", unreadable);
|
||||
EcsService service = newService(storage);
|
||||
taskDefinitions.put(FAMILY + ":1", unreadable);
|
||||
EcsService service = newService(taskDefinitions);
|
||||
|
||||
TaskDefinition other = register(service, OTHER);
|
||||
|
||||
@@ -101,7 +91,7 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
// NullPointerException instead of the ClientException any unknown reference gets.
|
||||
@Test
|
||||
void aMissingReferenceInAPopulatedRegionIsAClientException() {
|
||||
EcsService service = newService(new InMemoryStorageFactory());
|
||||
EcsService service = EcsServiceFixtures.mockModeService();
|
||||
register(service, REGION);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.describeTaskDefinition(null, REGION));
|
||||
@@ -129,41 +119,15 @@ class EcsTaskDefinitionLegacyKeyTest {
|
||||
return service.listTaskDefinitions(region, FAMILY, null, null, null, null).arns();
|
||||
}
|
||||
|
||||
private static EcsService newService(StorageFactory storage) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
when(config.services().ecs().mock()).thenReturn(true);
|
||||
when(config.effectiveBaseUrl()).thenReturn("http://localhost:4566");
|
||||
EcsService service = new EcsService(
|
||||
new RegionResolver(REGION, "000000000000"),
|
||||
mock(EcsContainerManager.class),
|
||||
config,
|
||||
mock(EcsLoadBalancerRegistrar.class),
|
||||
storage,
|
||||
null);
|
||||
service.initializeStorage();
|
||||
return service;
|
||||
private static AccountAwareStorageBackend<TaskDefinition> taskDefinitionsStore() {
|
||||
return AccountAwareStorageBackend.inMemory(EcsServiceFixtures.ACCOUNT);
|
||||
}
|
||||
|
||||
private static final class InMemoryStorageFactory extends StorageFactory {
|
||||
private final Map<String, StorageBackend<String, ?>> stores = new HashMap<>();
|
||||
|
||||
private InMemoryStorageFactory() {
|
||||
super(null, null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
AccountAwareStorageBackend<TaskDefinition> taskDefinitions() {
|
||||
return (AccountAwareStorageBackend<TaskDefinition>) stores.computeIfAbsent("ecs-task-definitions.json",
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName,
|
||||
String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return (AccountAwareStorageBackend<V>) stores.computeIfAbsent(fileName,
|
||||
ignored -> AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
}
|
||||
/** A service whose task definitions start as {@code taskDefinitions} holds them. */
|
||||
private static EcsService newService(AccountAwareStorageBackend<TaskDefinition> taskDefinitions) {
|
||||
return EcsServiceFixtures.builder()
|
||||
.storageFactory(new InMemoryStorageFactory(EcsServiceFixtures.ACCOUNT)
|
||||
.withStore("ecs-task-definitions.json", taskDefinitions))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user