fix(appconfig): support deleting environments (#5337)

* fix(appconfig): support deleting environments

* fix(appconfig): cascade environment deletion

* fix(appconfig): preserve deployment numbering

* fix(appconfig): resolve delete identifiers
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-10 13:45:28 -04:00
1 parent c6068e8cf5
commit 2015d01864
10 files changed
+197 -28

No files matched your search

@@ -0,0 +1,21 @@
package io.github.hectorvent.floci.core.storage;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class StorageOperationsTest {
@Test
void deleteByPrefixDeletesOnlyMatchingKeys() {
InMemoryStorage<String, String> storage = new InMemoryStorage<>();
storage.put("app::env::1", "first");
storage.put("app::env::2", "second");
storage.put("app::other::1", "other");
StorageOperations.deleteByPrefix(storage, "app::env::");
assertEquals(1, storage.keys().size());
assertEquals("other", storage.get("app::other::1").orElseThrow());
}
}
@@ -1084,4 +1084,32 @@ class AppConfigIntegrationTest {
.extract().asByteArray();
assertArrayEquals(stored, body);
}
@Test @Order(51)
void deleteEnvironmentRemovesIt() {
String token = given()
.contentType(ContentType.JSON)
.body("{\"ApplicationIdentifier\":\"" + appId + "\",\"EnvironmentIdentifier\":\""
+ envId + "\",\"ConfigurationProfileIdentifier\":\"" + profileId + "\"}")
.when().post("/configurationsessions")
.then().statusCode(201)
.extract().path("InitialConfigurationToken");
given()
.when().delete("/applications/test-app/environments/test-env")
.then().statusCode(204);
given()
.when().get("/applications/" + appId + "/environments/" + envId)
.then().statusCode(404);
given()
.when().get("/applications/" + appId + "/environments/" + envId + "/deployments/1")
.then().statusCode(404);
given()
.queryParam("configuration_token", token)
.when().get("/configuration")
.then().statusCode(404);
}
}
@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.services.appconfig;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.appconfig.model.Application;
import io.github.hectorvent.floci.services.appconfig.model.ConfigurationProfile;
import io.github.hectorvent.floci.services.appconfig.model.Deployment;
import io.github.hectorvent.floci.services.appconfig.model.DeploymentSummary;
import io.github.hectorvent.floci.services.appconfig.model.Environment;
@@ -15,6 +17,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -23,30 +32,40 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class AppConfigServiceTest {
private AccountAwareStorageBackend<Application> applicationStore;
private AccountAwareStorageBackend<Environment> environmentStore;
private AccountAwareStorageBackend<Deployment> deploymentStore;
private AccountAwareStorageBackend<String> activeConfigStore;
private AppConfigService service;
@BeforeEach
void setUp() {
applicationStore = mock(AccountAwareStorageBackend.class);
environmentStore = mock(AccountAwareStorageBackend.class);
AccountAwareStorageBackend<ConfigurationProfile> profileStore = mock(AccountAwareStorageBackend.class);
deploymentStore = mock(AccountAwareStorageBackend.class);
activeConfigStore = mock(AccountAwareStorageBackend.class);
Map<String, Deployment> deployments = new HashMap<>();
doAnswer(invocation -> {
deployments.put(invocation.getArgument(0, String.class), invocation.getArgument(1, Deployment.class));
return null;
}).when(deploymentStore).put(anyString(), any(Deployment.class));
when(deploymentStore.scan(any())).thenAnswer(invocation -> List.copyOf(deployments.values()));
when(deploymentStore.keys()).thenAnswer(invocation -> Set.copyOf(deployments.keySet()));
doAnswer(invocation -> deployments.remove(invocation.getArgument(0, String.class)))
.when(deploymentStore).delete(anyString());
when(activeConfigStore.keys()).thenReturn(Set.of());
StorageFactory storageFactory = mock(StorageFactory.class);
doAnswer(invocation -> switch (invocation.getArgument(1, String.class)) {
case "appconfig-applications.json" -> applicationStore;
case "appconfig-environments.json" -> environmentStore;
case "appconfig-profiles.json" -> profileStore;
case "appconfig-deployments.json" -> deploymentStore;
case "appconfig-active-configs.json" -> activeConfigStore;
default -> mock(AccountAwareStorageBackend.class);
}).when(storageFactory).create(anyString(), anyString(), any(TypeReference.class));
@@ -59,6 +78,10 @@ class AppConfigServiceTest {
environment.setApplicationId("app");
environmentStore.put("env", environment);
when(environmentStore.get("env")).thenReturn(Optional.of(environment));
ConfigurationProfile profile = new ConfigurationProfile();
profile.setId("profile");
profile.setApplicationId("app");
when(profileStore.get("profile")).thenReturn(Optional.of(profile));
service = new AppConfigService(storageFactory, mock(EmulatorConfig.class));
}
@@ -111,6 +134,75 @@ class AppConfigServiceTest {
assertThrows(RuntimeException.class, () -> service.listDeployments("app", "env", 1, "unknown"));
}
@Test
void deleteEnvironmentRemovesRuntimeState() {
deploymentStore.put("app::env::1", deployment("app", "env", 1));
when(activeConfigStore.keys()).thenReturn(Set.of("env::profile"));
service.deleteEnvironment("app", "env");
verify(deploymentStore).delete("app::env::1");
verify(activeConfigStore).delete("env::profile");
verify(environmentStore).delete("env");
}
@Test
void startDeploymentUsesHighestNumberAfterAnotherEnvironmentIsDeleted() {
Environment otherEnvironment = new Environment();
otherEnvironment.setId("other-env");
otherEnvironment.setApplicationId("app");
when(environmentStore.get("other-env")).thenReturn(Optional.of(otherEnvironment));
deploymentStore.put("app::env::1", deployment("app", "env", 1));
deploymentStore.put("app::other-env::2", deployment("app", "other-env", 2));
deploymentStore.put("app::env::3", deployment("app", "env", 3));
service.deleteEnvironment("app", "other-env");
Deployment deployment = service.startDeployment("app", "env", Map.of(
"ConfigurationProfileId", "profile",
"ConfigurationVersion", "version",
"DeploymentStrategyId", "AppConfig.AllAtOnce"));
assertEquals(4, deployment.getDeploymentNumber());
assertEquals(Set.of("app::env::1", "app::env::3", "app::env::4"), deploymentStore.keys());
}
@Test
void concurrentEnvironmentDeletesDoNotBothSucceed() throws Exception {
AtomicReference<Environment> stored = new AtomicReference<>(environmentStore.get("env").orElseThrow());
CountDownLatch reads = new CountDownLatch(2);
when(environmentStore.get("env")).thenAnswer(invocation -> {
Environment environment = stored.get();
reads.countDown();
reads.await(200, TimeUnit.MILLISECONDS);
return Optional.ofNullable(environment);
});
doAnswer(invocation -> {
stored.set(null);
return null;
}).when(environmentStore).delete("env");
ExecutorService executor = Executors.newFixedThreadPool(2);
try {
Future<Boolean> first = executor.submit(this::deleteEnvironment);
Future<Boolean> second = executor.submit(this::deleteEnvironment);
long successes = List.of(first.get(), second.get()).stream()
.filter(Boolean::booleanValue)
.count();
assertEquals(1, successes);
} finally {
executor.shutdownNow();
}
}
private boolean deleteEnvironment() {
try {
service.deleteEnvironment("app", "env");
return true;
} catch (AwsException e) {
return false;
}
}
private static Deployment deployment(String applicationId, String environmentId, int number) {
Deployment deployment = new Deployment();
deployment.setApplicationId(applicationId);