|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.processing.dependent.workflow |
| 17 | + |
| 18 | +import io.javaoperatorsdk.operator.AggregatedOperatorException |
| 19 | +import io.javaoperatorsdk.operator.api.reconciler.Context |
| 20 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource |
| 21 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult |
| 22 | +import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedWorkflowAndDependentResourceContext |
| 23 | +import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever |
| 24 | +import io.javaoperatorsdk.operator.sample.simple.TestCustomResource |
| 25 | +import java.util.concurrent.Executors |
| 26 | +import org.assertj.core.api.Assertions.assertThat |
| 27 | +import org.junit.jupiter.api.Test |
| 28 | +import org.junit.jupiter.api.assertThrows |
| 29 | +import org.mockito.Mockito.mock |
| 30 | +import org.mockito.Mockito.`when` |
| 31 | + |
| 32 | +/** |
| 33 | + * Smoke test verifying that JOSDK works properly when used from Kotlin. |
| 34 | + * |
| 35 | + * Unlike Java, Kotlin does not distinguish between checked and unchecked exceptions, so a Kotlin |
| 36 | + * [DependentResource] can throw a plain [java.lang.Exception] from `reconcile` without declaring |
| 37 | + * it, even though the Java `DependentResource.reconcile` method does not declare `throws |
| 38 | + * Exception`. Before https://github.com/operator-framework/java-operator-sdk/pull/2965 such an |
| 39 | + * exception was not caught by [NodeExecutor], since it only handled [RuntimeException], so it |
| 40 | + * would not have been recorded as an error and retries would not have been triggered. This test |
| 41 | + * makes sure such an exception is properly caught and reported, see |
| 42 | + * https://github.com/operator-framework/java-operator-sdk/issues/2967. |
| 43 | + */ |
| 44 | +class KotlinCheckedExceptionDependentResourceTest { |
| 45 | + |
| 46 | + private class CheckedException(message: String) : Exception(message) |
| 47 | + |
| 48 | + private class ThrowingDependentResource : |
| 49 | + DependentResource<String, TestCustomResource> { |
| 50 | + override fun reconcile( |
| 51 | + primary: TestCustomResource, |
| 52 | + context: Context<TestCustomResource> |
| 53 | + ): ReconcileResult<String> { |
| 54 | + throw CheckedException("checked exception thrown from Kotlin") |
| 55 | + } |
| 56 | + |
| 57 | + override fun resourceType(): Class<String> = String::class.java |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun checkedExceptionThrownFromKotlinDependentResourceTriggersRetry() { |
| 62 | + val dependentResource = ThrowingDependentResource() |
| 63 | + |
| 64 | + val workflow = |
| 65 | + WorkflowBuilder<TestCustomResource>() |
| 66 | + .addDependentResource(dependentResource) |
| 67 | + .withThrowExceptionFurther(false) |
| 68 | + .build() |
| 69 | + |
| 70 | + @Suppress("UNCHECKED_CAST") |
| 71 | + val context = mock(Context::class.java) as Context<TestCustomResource> |
| 72 | + `when`(context.managedWorkflowAndDependentResourceContext()) |
| 73 | + .thenReturn(mock(ManagedWorkflowAndDependentResourceContext::class.java)) |
| 74 | + `when`(context.workflowExecutorService).thenReturn(Executors.newCachedThreadPool()) |
| 75 | + @Suppress("UNCHECKED_CAST") |
| 76 | + `when`(context.eventSourceRetriever()) |
| 77 | + .thenReturn(mock(EventSourceRetriever::class.java) as EventSourceRetriever<TestCustomResource>) |
| 78 | + |
| 79 | + val result = workflow.reconcile(TestCustomResource(), context) |
| 80 | + |
| 81 | + // the checked exception was caught by the workflow executor, not left uncaught, so it is |
| 82 | + // reported as an error for the dependent resource, which is what allows JOSDK to retry the |
| 83 | + // reconciliation. |
| 84 | + assertThat(result.erroredDependents).containsOnlyKeys(dependentResource) |
| 85 | + assertThat(result.erroredDependents[dependentResource]).isInstanceOf(CheckedException::class.java) |
| 86 | + assertThrows<AggregatedOperatorException> { result.throwAggregateExceptionIfErrorsPresent() } |
| 87 | + } |
| 88 | +} |
0 commit comments