Search in sources :

Example 1 with TestCustomResource

use of io.javaoperatorsdk.operator.sample.simple.TestCustomResource in project java-operator-sdk by java-operator-sdk.

the class ConcurrencyIT method manyResourcesGetCreatedUpdatedAndDeleted.

@Test
void manyResourcesGetCreatedUpdatedAndDeleted() throws InterruptedException {
    log.info("Creating {} new resources", NUMBER_OF_RESOURCES_CREATED);
    for (int i = 0; i < NUMBER_OF_RESOURCES_CREATED; i++) {
        TestCustomResource tcr = TestUtils.testCustomResourceWithPrefix(String.valueOf(i));
        operator.resources(TestCustomResource.class).create(tcr);
    }
    await().atMost(1, TimeUnit.MINUTES).untilAsserted(() -> {
        List<ConfigMap> items = operator.resources(ConfigMap.class).withLabel("managedBy", TestReconciler.class.getSimpleName()).list().getItems();
        assertThat(items).hasSize(NUMBER_OF_RESOURCES_CREATED);
    });
    log.info("Updating {} resources", NUMBER_OF_RESOURCES_UPDATED);
    // update some resources
    for (int i = 0; i < NUMBER_OF_RESOURCES_UPDATED; i++) {
        TestCustomResource tcr = operator.get(TestCustomResource.class, TestUtils.TEST_CUSTOM_RESOURCE_PREFIX + i);
        tcr.getSpec().setValue(i + UPDATED_SUFFIX);
        operator.resources(TestCustomResource.class).createOrReplace(tcr);
    }
    // sleep for a short time to make variability to the test, so some updates are not
    // executed before delete
    Thread.sleep(300);
    log.info("Deleting {} resources", NUMBER_OF_RESOURCES_DELETED);
    for (int i = 0; i < NUMBER_OF_RESOURCES_DELETED; i++) {
        TestCustomResource tcr = TestUtils.testCustomResourceWithPrefix(String.valueOf(i));
        operator.resources(TestCustomResource.class).delete(tcr);
    }
    await().atMost(1, TimeUnit.MINUTES).untilAsserted(() -> {
        List<ConfigMap> items = operator.resources(ConfigMap.class).withLabel("managedBy", TestReconciler.class.getSimpleName()).list().getItems();
        // reducing configmaps to names only - better for debugging
        List<String> itemDescs = items.stream().map(configMap -> configMap.getMetadata().getName()).collect(Collectors.toList());
        assertThat(itemDescs).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED);
        List<TestCustomResource> crs = operator.resources(TestCustomResource.class).list().getItems();
        assertThat(crs).hasSize(NUMBER_OF_RESOURCES_CREATED - NUMBER_OF_RESOURCES_DELETED);
    });
}
Also used : TestUtils(io.javaoperatorsdk.operator.support.TestUtils) Awaitility.await(org.awaitility.Awaitility.await) Logger(org.slf4j.Logger) DefaultConfigurationService(io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) TestReconciler(io.javaoperatorsdk.operator.sample.simple.TestReconciler) Collectors(java.util.stream.Collectors) OperatorExtension(io.javaoperatorsdk.operator.junit.OperatorExtension) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) List(java.util.List) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) TestReconciler(io.javaoperatorsdk.operator.sample.simple.TestReconciler) Test(org.junit.jupiter.api.Test)

Example 2 with TestCustomResource

use of io.javaoperatorsdk.operator.sample.simple.TestCustomResource in project java-operator-sdk by java-operator-sdk.

the class ControllerExecutionIT method configMapGetsCreatedForTestCustomResource.

@Test
void configMapGetsCreatedForTestCustomResource() {
    operator.getControllerOfType(TestReconciler.class).setUpdateStatus(true);
    TestCustomResource resource = TestUtils.testCustomResource();
    operator.create(TestCustomResource.class, resource);
    awaitResourcesCreatedOrUpdated();
    awaitStatusUpdated();
    assertThat(TestUtils.getNumberOfExecutions(operator)).isEqualTo(2);
}
Also used : TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) TestReconciler(io.javaoperatorsdk.operator.sample.simple.TestReconciler) Test(org.junit.jupiter.api.Test)

Example 3 with TestCustomResource

use of io.javaoperatorsdk.operator.sample.simple.TestCustomResource in project java-operator-sdk by java-operator-sdk.

the class ControllerExecutionIT method eventIsSkippedChangedOnMetadataOnlyUpdate.

@Test
void eventIsSkippedChangedOnMetadataOnlyUpdate() {
    operator.getControllerOfType(TestReconciler.class).setUpdateStatus(false);
    TestCustomResource resource = TestUtils.testCustomResource();
    operator.create(TestCustomResource.class, resource);
    awaitResourcesCreatedOrUpdated();
    assertThat(TestUtils.getNumberOfExecutions(operator)).isEqualTo(1);
}
Also used : TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) TestReconciler(io.javaoperatorsdk.operator.sample.simple.TestReconciler) Test(org.junit.jupiter.api.Test)

Example 4 with TestCustomResource

use of io.javaoperatorsdk.operator.sample.simple.TestCustomResource in project java-operator-sdk by java-operator-sdk.

the class EventProcessorTest method executesTheControllerInstantlyAfterErrorIfNewEventsReceived.

@Test
public void executesTheControllerInstantlyAfterErrorIfNewEventsReceived() {
    Event event = prepareCREvent();
    TestCustomResource customResource = testCustomResource();
    overrideData(event.getRelatedCustomResourceID(), customResource);
    PostExecutionControl postExecutionControl = PostExecutionControl.exceptionDuringExecution(new RuntimeException("test"));
    when(reconciliationDispatcherMock.handleExecution(any())).thenReturn(postExecutionControl).thenReturn(PostExecutionControl.defaultDispatch());
    // start processing an event
    eventProcessorWithRetry.handleEvent(event);
    // handle another event
    eventProcessorWithRetry.handleEvent(event);
    ArgumentCaptor<ExecutionScope> executionScopeArgumentCaptor = ArgumentCaptor.forClass(ExecutionScope.class);
    verify(reconciliationDispatcherMock, timeout(SEPARATE_EXECUTION_TIMEOUT).times(2)).handleExecution(executionScopeArgumentCaptor.capture());
    List<ExecutionScope> allValues = executionScopeArgumentCaptor.getAllValues();
    assertThat(allValues).hasSize(2);
    verify(retryTimerEventSourceMock, never()).scheduleOnce(eq(customResource), eq(GenericRetry.DEFAULT_INITIAL_INTERVAL));
}
Also used : TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) ResourceEvent(io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEvent) Test(org.junit.jupiter.api.Test)

Example 5 with TestCustomResource

use of io.javaoperatorsdk.operator.sample.simple.TestCustomResource in project java-operator-sdk by java-operator-sdk.

the class EventProcessorTest method schedulesAnEventRetryOnException.

@Test
public void schedulesAnEventRetryOnException() {
    TestCustomResource customResource = testCustomResource();
    ExecutionScope executionScope = new ExecutionScope(customResource, null);
    PostExecutionControl postExecutionControl = PostExecutionControl.exceptionDuringExecution(new RuntimeException("test"));
    eventProcessorWithRetry.eventProcessingFinished(executionScope, postExecutionControl);
    verify(retryTimerEventSourceMock, times(1)).scheduleOnce(eq(customResource), eq(GenericRetry.DEFAULT_INITIAL_INTERVAL));
}
Also used : TestCustomResource(io.javaoperatorsdk.operator.sample.simple.TestCustomResource) Test(org.junit.jupiter.api.Test)

Aggregations

TestCustomResource (io.javaoperatorsdk.operator.sample.simple.TestCustomResource)26 Test (org.junit.jupiter.api.Test)20 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)4 ResourceEvent (io.javaoperatorsdk.operator.processing.event.source.controller.ResourceEvent)3 TestCustomResourceSpec (io.javaoperatorsdk.operator.sample.simple.TestCustomResourceSpec)3 TestReconciler (io.javaoperatorsdk.operator.sample.simple.TestReconciler)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 DefaultConfigurationService (io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService)1 OperatorExtension (io.javaoperatorsdk.operator.junit.OperatorExtension)1 TestUtils (io.javaoperatorsdk.operator.support.TestUtils)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Awaitility.await (org.awaitility.Awaitility.await)1 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1