mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
test: check the boot restores' wiring instead of booting two fresh applications (#8837)
EcsBootReconcilerIntegrationTest and EksBootRestoreIntegrationTest each took a Quarkus profile of their own, an application no other class had sent an ECS or EKS request to, only to see that EcsService and EksService get created at boot. EmulatorLifecycleTest already checks that onStart restores ECS and that onHttpStart restores EKS once the port listens; what the two classes added is that the container fires those observers at boot. One unit test now checks that wiring directly (onStart observes StartupEvent, onHttpStart observes HttpServerStart, on an @ApplicationScoped bean), and the two classes and their profiles go.
This commit is contained in:
1 parent
c9f00fee12
commit
cdb1cffa76
3 files changed
+17
-105
No files matched your search
@@ -50,6 +50,9 @@ import io.quarkus.runtime.ShutdownEvent;
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import io.quarkus.vertx.http.HttpServerStart;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.enterprise.event.ObservesAsync;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -63,6 +66,7 @@ import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
@@ -337,6 +341,19 @@ class EmulatorLifecycleTest {
|
||||
inOrder.verify(ecsService).restorePersistedRuntime();
|
||||
}
|
||||
|
||||
@Test
|
||||
void quarkusRunsTheBootRestoresWithoutWaitingForARequest() throws NoSuchMethodException {
|
||||
// EcsService and EksService are created lazily, and creating them is what restores their
|
||||
// persisted state; the tests above show onStart and onHttpStart do it. They only run at
|
||||
// boot because the container fires them: onStart observes StartupEvent and onHttpStart
|
||||
// observes HttpServerStart, on a bean the container manages.
|
||||
assertTrue(EmulatorLifecycle.class.isAnnotationPresent(ApplicationScoped.class));
|
||||
Method onStart = EmulatorLifecycle.class.getDeclaredMethod("onStart", StartupEvent.class);
|
||||
assertTrue(onStart.getParameters()[0].isAnnotationPresent(Observes.class));
|
||||
Method onHttpStart = EmulatorLifecycle.class.getDeclaredMethod("onHttpStart", HttpServerStart.class);
|
||||
assertTrue(onHttpStart.getParameters()[0].isAnnotationPresent(ObservesAsync.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoresPersistedEksClustersOnceTheHttpPortListens() throws Exception {
|
||||
stubStorageConfig();
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package io.floci.aws.services.ecs;
|
||||
|
||||
import io.quarkus.arc.Arc;
|
||||
import io.quarkus.arc.InjectableBean;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* EcsService is created lazily, and creating it is what loads the ECS stores and schedules the
|
||||
* reconciler. Boot has to create it, or persisted services stay without tasks after a restart
|
||||
* until the first ECS request. The profile of its own gives this test a fresh application that
|
||||
* no other test has sent an ECS request to.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(EcsBootReconcilerIntegrationTest.FreshBootProfile.class)
|
||||
class EcsBootReconcilerIntegrationTest {
|
||||
|
||||
public static final class FreshBootProfile implements QuarkusTestProfile {
|
||||
}
|
||||
|
||||
@Test
|
||||
void bootStartsTheReconcilerWithoutAnEcsRequest() {
|
||||
Map<InjectableBean<?>, Object> created = Arc.container().getActiveContext(ApplicationScoped.class)
|
||||
.getState().getContextualInstances();
|
||||
EcsService ecsService = created.entrySet().stream()
|
||||
.filter(entry -> entry.getKey().getBeanClass() == EcsService.class)
|
||||
.map(entry -> (EcsService) entry.getValue())
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
assertNotNull(ecsService, "EcsService must be created at boot, not on the first ECS request");
|
||||
assertFalse(ecsService.isReconcilerShutdown());
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package io.floci.aws.services.eks;
|
||||
|
||||
import io.floci.aws.testutil.FreePorts;
|
||||
import io.quarkus.arc.Arc;
|
||||
import io.quarkus.arc.InjectableBean;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* EksService is created lazily, and creating it is what restarts each persisted cluster's k3s
|
||||
* container. Boot has to create it, or persisted clusters stay down after a restart until the
|
||||
* first EKS request. The profile of its own gives this test a fresh application that no other
|
||||
* test has sent an EKS request to, with EKS out of mock mode as boot restores only real clusters.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(EksBootRestoreIntegrationTest.FreshBootProfile.class)
|
||||
class EksBootRestoreIntegrationTest {
|
||||
|
||||
public static final class FreshBootProfile implements QuarkusTestProfile {
|
||||
// Boot restores once quarkus.http.port listens; the suite's random port 0 never matches the bound one.
|
||||
private static final int TEST_PORT = freePort();
|
||||
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Map.of(
|
||||
"floci.services.eks.mock", "false",
|
||||
"quarkus.http.test-port", String.valueOf(TEST_PORT),
|
||||
"quarkus.http.port", String.valueOf(TEST_PORT),
|
||||
"floci.port", String.valueOf(TEST_PORT),
|
||||
"floci.base-url", "http://localhost:" + TEST_PORT);
|
||||
}
|
||||
|
||||
private static int freePort() {
|
||||
try {
|
||||
return FreePorts.anyInterfacePort();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException("No free port for the EKS boot test application", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void bootCreatesEksServiceWithoutAnEksRequest() {
|
||||
// Boot restores once the HTTP port listens, from an async observer, so this can run first.
|
||||
await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
|
||||
Map<InjectableBean<?>, Object> created = Arc.container().getActiveContext(ApplicationScoped.class)
|
||||
.getState().getContextualInstances();
|
||||
assertTrue(created.keySet().stream().anyMatch(bean -> bean.getBeanClass() == EksService.class),
|
||||
"EksService must be created at boot, not on the first EKS request");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user