use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.
the class DefaultSimpleEntandoOperationsTest method shouldWatchInAnyNamespace.
@Test
@Description("Should watch in any namespace")
void shouldWatchInAnyNamespace() {
final Map<String, SerializedEntandoResource> resourcesObserved = new ConcurrentHashMap<>();
step("Given I have registered a Watcher for TestResource in any namespace", () -> {
getMyOperations().inAnyNamespace().watch((action, serializedEntandoResource) -> resourcesObserved.put(serializedEntandoResource.getMetadata().getName(), serializedEntandoResource));
});
step(format("When I create two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
});
step("Then both resources were observed", () -> {
await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> resourcesObserved.containsKey("my-test-resource2"));
assertThat(resourcesObserved).containsKeys("my-test-resource1", "my-test-resource2");
attachment("TestResources", objectMapper.writeValueAsString(resourcesObserved));
});
}
use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.
the class DefaultSimpleEntandoOperationsTest method shouldListInAnyNamespace.
@Test
@Description("Should list in any namespace")
void shouldListInAnyNamespace() {
step(format("Given I have created two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
});
List<SerializedEntandoResource> actual = new ArrayList<>();
step("When I list TestResources in any namespace", () -> {
actual.addAll(getMyOperations().inAnyNamespace().list());
});
step("Then both resources are found", () -> {
assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource1"));
assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource2"));
attachment("TestResources", objectMapper.writeValueAsString(actual));
});
}
use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.
the class DefaultSimpleEntandoOperationsTest method shouldListInSingleNamespace.
@Test
@Description("Should list in a single namespace")
void shouldListInSingleNamespace() {
step(format("Given I have created two TestResource in namespaces %s and %s", MY_APP_NAMESPACE_1, MY_APP_NAMESPACE_1 + "1"), () -> {
final TestResource testResource1 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, "my-test-resource1"));
attachment("TestResource 1", objectMapper.writeValueAsString(testResource1));
final TestResource testResource2 = createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_2, "my-test-resource2"));
attachment("TestResource 2", objectMapper.writeValueAsString(testResource2));
});
List<SerializedEntandoResource> actual = new ArrayList<>();
step("When I list TestResources in any namespace", () -> {
actual.addAll(getMyOperations().inNamespace(MY_APP_NAMESPACE_1).list());
});
step("Then both resources are found", () -> {
await().atMost(10, TimeUnit.SECONDS).ignoreExceptions().until(() -> actual.size() == 1);
assertThat(actual).size().isEqualTo(1);
assertThat(actual).anyMatch(r -> r.getMetadata().getName().equals("my-test-resource1"));
attachment("TestResource 1", objectMapper.writeValueAsString(actual));
});
}
use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.
the class PodManagementTests method shouldKillAllPreviousPods.
@Test
@Description("Should always kill existing pods running against the same resource to avoid racing conditions")
void shouldKillAllPreviousPods() {
step("Given the Coordinator observes its own namespace", () -> {
System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), clientDouble.getNamespace());
coordinator.onStartup(new StartupEvent());
});
step("And automatic controller pod garbage collections has been switched off", () -> System.setProperty(EntandoOperatorConfigProperty.ENTANDO_K8S_OPERATOR_GC_CONTROLLER_PODS.getJvmSystemProperty(), "false"));
ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
step("And I have created a new TestResource resource", () -> {
final TestResource entandoCustomResource = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(entandoCustomResource)));
attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
});
ValueHolder<Pod> firstPod = new ValueHolder<>();
step("And I have waited to see at least one controller pod", () -> {
await().ignoreExceptions().atMost(5, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())) != null);
firstPod.set(clientDouble.loadPod(clientDouble.getNamespace(), labelsFromResource(testResource.get())));
attachment("FirstPod", objectMapper.writeValueAsString(firstPod.get()));
});
step("When I force a second attempt at processing the resource", () -> {
final SerializedEntandoResource reloaded = clientDouble.reload(testResource.get());
reloaded.getMetadata().setAnnotations(Map.of(AnnotationNames.PROCESSING_INSTRUCTION.getName(), "force"));
clientDouble.createOrPatchEntandoResource(reloaded);
});
step("Then a new pod was created", () -> {
await().atMost(4, TimeUnit.SECONDS).ignoreExceptions().until(() -> {
final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
return !pods.get(pods.size() - 1).getMetadata().getUid().equals(firstPod.get().getMetadata().getUid());
});
final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
attachment("Second Pod", objectMapper.writeValueAsString(pods.get(pods.size() - 1)));
});
step("And the old pod was removed", () -> {
final List<Pod> pods = clientDouble.loadPods(clientDouble.getNamespace(), labelsFromResource(testResource.get()));
assertThat(pods.size()).isOne();
});
}
use of org.entando.kubernetes.fluentspi.TestResource in project entando-k8s-controller-coordinator by entando-k8s.
the class ControllerCoordinatorProcessingCriteriaTest method testDuplicatesIgnored.
@Test
@Description("Resource modification events should be ignored when Kubernetes it twice with the same resource version")
void testDuplicatesIgnored() {
step("Given the Coordinator observes this namespace", () -> {
System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), OBSERVED_NAMESPACE);
coordinator.onStartup(new StartupEvent());
});
final ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
step("And I have created an TestResource resource", () -> {
testResource.set(createTestResource(1L, Collections.emptyMap()));
attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
});
final ValueHolder<Pod> oldPod = new ValueHolder<>();
step("And its controller pod has been created", () -> {
await().ignoreExceptions().atMost(3, TimeUnit.SECONDS).until(() -> clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get())) != null);
oldPod.set(clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get())));
attachment("Controller Pod", objectMapper.writeValueAsString(oldPod.get()));
});
step("When a duplicate event is fired", () -> {
// NB we have to invoke the observer directly as our test double doesn't generate duplicate events
coordinator.getObserver(CustomResourceDefinitionContext.fromCustomResourceType(TestResource.class)).eventReceived(Action.ADDED, testResource.get());
coordinator.shutdownObservers(10, TimeUnit.SECONDS);
attachment("Duplicate Resource", objectMapper.writeValueAsString(testResource.get()));
});
step("Then no new pod gets created", () -> {
// NB This assertion assumes the ClientDouble is storing the objects without cloning/serialization
assertThat(clientDouble.loadPod(CONTROLLER_NAMESPACE, labelsFromResource(testResource.get()))).isSameAs(oldPod.get());
});
step("And the duplicate event was logged", () -> {
final Optional<String> logEntry = LogInterceptor.getLogEntries().stream().filter(s -> s.startsWith("Duplicate event")).findFirst();
assertThat(logEntry).isPresent();
});
}
Aggregations