Search in sources :

Example 1 with ValueHolder

use of org.entando.kubernetes.test.common.ValueHolder 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();
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Pod(io.fabric8.kubernetes.api.model.Pod) StartupEvent(io.quarkus.runtime.StartupEvent) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 2 with ValueHolder

use of org.entando.kubernetes.test.common.ValueHolder 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();
    });
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Allure.attachment(io.qameta.allure.Allure.attachment) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) DbmsVendor(org.entando.kubernetes.model.common.DbmsVendor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) Action(io.fabric8.kubernetes.client.Watcher.Action) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) CustomResourceDefinitionContext(io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext) EntandoOperatorConfigProperty(org.entando.kubernetes.controller.support.common.EntandoOperatorConfigProperty) Feature(io.qameta.allure.Feature) LabelNames(org.entando.kubernetes.controller.spi.common.LabelNames) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) LogInterceptor(org.entando.kubernetes.test.common.LogInterceptor) Map(java.util.Map) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ResourceUtils(org.entando.kubernetes.controller.spi.common.ResourceUtils) Tag(org.junit.jupiter.api.Tag) Issue(io.qameta.allure.Issue) CoordinatorTestUtils(org.entando.kubernetes.controller.coordinator.common.CoordinatorTestUtils) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) SimpleKubernetesClientDouble(org.entando.kubernetes.controller.coordinator.common.SimpleKubernetesClientDouble) CommonLabels(org.entando.kubernetes.test.common.CommonLabels) FluentIntegrationTesting(org.entando.kubernetes.controller.support.client.impl.integrationtesthelpers.FluentIntegrationTesting) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pod(io.fabric8.kubernetes.api.model.Pod) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CustomResourceDefinitionBuilder(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder) StandardCapability(org.entando.kubernetes.model.capability.StandardCapability) TestResource(org.entando.kubernetes.fluentspi.TestResource) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) EntandoResourceClientDouble(org.entando.kubernetes.controller.support.client.doubles.EntandoResourceClientDouble) AfterEach(org.junit.jupiter.api.AfterEach) Allure.step(io.qameta.allure.Allure.step) ProvidedCapabilityBuilder(org.entando.kubernetes.model.capability.ProvidedCapabilityBuilder) Optional(java.util.Optional) Tags(org.junit.jupiter.api.Tags) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) StartupEvent(io.quarkus.runtime.StartupEvent) Description(io.qameta.allure.Description) Collections(java.util.Collections) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition) EntandoDeploymentPhase(org.entando.kubernetes.model.common.EntandoDeploymentPhase) SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Pod(io.fabric8.kubernetes.api.model.Pod) StartupEvent(io.quarkus.runtime.StartupEvent) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 3 with ValueHolder

use of org.entando.kubernetes.test.common.ValueHolder in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleKubernetesClientTest method shouldUpdateStatusOfOpaqueCustomResource.

@Test
@Description("Should track phase updates on the status of opaque custom resources and in Kubernetes events")
void shouldUpdateStatusOfOpaqueCustomResource() throws IOException {
    ValueHolder<TestResource> testResource = new ValueHolder<>();
    step("Given I have created an instance of the CustomResourceDefinition TestResource", () -> {
        testResource.set(createTestResource(new TestResource().withNames(MY_APP_NAMESPACE_1, MY_APP).withSpec(new BasicDeploymentSpecBuilder().withReplicas(1).build())));
        attachResource("TestResource", testResource.get());
    });
    SerializedEntandoResource serializedEntandoResource = objectMapper.readValue(objectMapper.writeValueAsBytes(testResource.get()), SerializedEntandoResource.class);
    step("And it is represented in an opaque format using the SerializedEntandoResource class", () -> {
        serializedEntandoResource.setDefinition(CustomResourceDefinitionContext.fromCustomResourceType(TestResource.class));
        attachResource("Opaque Resource", serializedEntandoResource);
    });
    step("When I update its phase to 'successful'", () -> getMyClient().updatePhase(serializedEntandoResource, EntandoDeploymentPhase.SUCCESSFUL));
    step("Then the updated status reflects on the TestResource", () -> {
        final TestResource actual = getFabric8Client().customResources(TestResource.class).inNamespace(MY_APP_NAMESPACE_1).withName(MY_APP).get();
        assertThat(actual.getStatus().getPhase()).isEqualTo(EntandoDeploymentPhase.SUCCESSFUL);
        attachResource("TestResource", actual);
    });
    step("And a PHASE_CHANGE event has been issued to Kubernetes", () -> {
        final List<Event> events = getMyClient().listEventsFor(testResource.get());
        attachResources("Events", events);
        assertThat(events).allMatch(event -> event.getInvolvedObject().getName().equals(testResource.get().getMetadata().getName()));
        assertThat(events).anyMatch(event -> event.getAction().equals("PHASE_CHANGE"));
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) Event(io.fabric8.kubernetes.api.model.Event) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 4 with ValueHolder

use of org.entando.kubernetes.test.common.ValueHolder in project entando-k8s-controller-coordinator by entando-k8s.

the class DefaultSimpleKubernetesClientTest method shouldIssueDeathEvent.

@Test
@Description("Should issue Death events")
void shouldIssueDeathEvent() {
    ValueHolder<Pod> startedPod = new ValueHolder<>();
    step("Given I have started a dummy pod with the name 'my-pod'", () -> {
        startedPod.set(getMyClient().startPod(new PodBuilder().withNewMetadata().withName(MY_POD).withNamespace(MY_APP_NAMESPACE_1).endMetadata().withNewSpec().addNewContainer().withImage("busybox").withName("busybox").endContainer().endSpec().build()));
        System.setProperty(EntandoOperatorSpiConfigProperty.ENTANDO_CONTROLLER_POD_NAME.getJvmSystemProperty(), startedPod.get().getMetadata().getName());
        attachment("Started Pod", objectMapper.writeValueAsString(startedPod.get()));
    });
    Event event = new EventBuilder().withNewMetadata().withName(EntandoOperatorSpiConfig.getControllerPodName() + "-restart-" + NameUtils.randomNumeric(4)).addToLabels("entando-operator-restarted", "true").endMetadata().withCount(1).withFirstTimestamp(FormatUtils.format(LocalDateTime.now())).withLastTimestamp(FormatUtils.format(LocalDateTime.now())).withMessage("blah").build();
    step("When issue a death event against this pod", () -> myClient.issueOperatorDeathEvent(event));
    step("It reflects on the cluster", () -> {
        Event actual = kubernetesClient.v1().events().withName(event.getMetadata().getName()).fromServer().get();
        assertThat(actual).isNotNull();
        assertThat(actual.getInvolvedObject().getUid()).isEqualTo(startedPod.get().getMetadata().getUid());
    });
}
Also used : EventBuilder(io.fabric8.kubernetes.api.model.EventBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Event(io.fabric8.kubernetes.api.model.Event) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Example 5 with ValueHolder

use of org.entando.kubernetes.test.common.ValueHolder in project entando-k8s-controller-coordinator by entando-k8s.

the class LivenessTests method shouldCreateControllerPodPointingToResource.

@Test
@Description("Should create a  controller pod that points to the newly created resource")
void shouldCreateControllerPodPointingToResource() {
    System.setProperty(EntandoOperatorSpiConfigProperty.ENTANDO_CONTROLLER_POD_NAME.getJvmSystemProperty(), "my-pod");
    step("Given the Coordinator observes its own namespace", () -> {
        System.setProperty(EntandoOperatorConfigProperty.ENTANDO_NAMESPACES_TO_OBSERVE.getJvmSystemProperty(), clientDouble.getNamespace());
        coordinator.onStartup(new StartupEvent());
    });
    ValueHolder<SerializedEntandoResource> testResource = new ValueHolder<>();
    step("And I have created a new TestResource resource", () -> {
        final TestResource r = new TestResource().withNames(clientDouble.getNamespace(), "test-keycloak").withSpec(new BasicDeploymentSpecBuilder().withDbms(DbmsVendor.EMBEDDED).build());
        r.getMetadata().setGeneration(1L);
        testResource.set(clientDouble.createOrPatchEntandoResource(CoordinatorTestUtils.toSerializedResource(r)));
        attachment("TestResource", objectMapper.writeValueAsString(testResource.get()));
    });
    final File file = Paths.get("/tmp/EntandoControllerCoordinator.ready").toFile();
    clientDouble.getCluster().getResourceProcessor().getAllWatchers().forEach(watcher -> {
        Liveness.alive();
        step(format("When the watcher %s is closed", watcher.getClass().getSimpleName()), () -> {
            watcher.onClose(new WatcherException("Closed"));
        });
        step("Then the liveness probe will fail", () -> {
            assertThat(file).doesNotExist();
        });
    });
}
Also used : SerializedEntandoResource(org.entando.kubernetes.controller.spi.client.SerializedEntandoResource) StartupEvent(io.quarkus.runtime.StartupEvent) TestResource(org.entando.kubernetes.fluentspi.TestResource) ValueHolder(org.entando.kubernetes.test.common.ValueHolder) BasicDeploymentSpecBuilder(org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder) File(java.io.File) WatcherException(io.fabric8.kubernetes.client.WatcherException) Description(io.qameta.allure.Description) Test(org.junit.jupiter.api.Test)

Aggregations

Description (io.qameta.allure.Description)13 ValueHolder (org.entando.kubernetes.test.common.ValueHolder)13 Test (org.junit.jupiter.api.Test)13 SerializedEntandoResource (org.entando.kubernetes.controller.spi.client.SerializedEntandoResource)11 StartupEvent (io.quarkus.runtime.StartupEvent)10 TestResource (org.entando.kubernetes.fluentspi.TestResource)10 BasicDeploymentSpecBuilder (org.entando.kubernetes.fluentspi.BasicDeploymentSpecBuilder)9 Pod (io.fabric8.kubernetes.api.model.Pod)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)6 CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition)6 CustomResourceDefinitionBuilder (io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder)6 Allure.attachment (io.qameta.allure.Allure.attachment)5 Allure.step (io.qameta.allure.Allure.step)5 Feature (io.qameta.allure.Feature)5 Issue (io.qameta.allure.Issue)5 IOException (java.io.IOException)5 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5 TimeoutException (java.util.concurrent.TimeoutException)5