From ea2f4c5a2c3a5d98897e4e326c0d67693a169427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Sun, 12 Jul 2026 22:24:23 +0200 Subject: [PATCH] test: finalizer adding without ssa MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../AddFinalizerNoSSACustomResource.java | 30 ++++++++ .../finalizernossa/AddFinalizerNoSSAIT.java | 73 +++++++++++++++++++ .../AddFinalizerNoSSAReconciler.java | 53 ++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSACustomResource.java create mode 100644 operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAIT.java create mode 100644 operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAReconciler.java diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSACustomResource.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSACustomResource.java new file mode 100644 index 0000000000..07936cff76 --- /dev/null +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSACustomResource.java @@ -0,0 +1,30 @@ +/* + * Copyright Java Operator SDK Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.javaoperatorsdk.operator.baseapi.finalizernossa; + +import io.fabric8.kubernetes.api.model.Namespaced; +import io.fabric8.kubernetes.client.CustomResource; +import io.fabric8.kubernetes.model.annotation.Group; +import io.fabric8.kubernetes.model.annotation.Kind; +import io.fabric8.kubernetes.model.annotation.ShortNames; +import io.fabric8.kubernetes.model.annotation.Version; + +@Group("sample.javaoperatorsdk") +@Version("v1") +@Kind("AddFinalizerNoSSACustomResource") +@ShortNames("afnossa") +public class AddFinalizerNoSSACustomResource extends CustomResource + implements Namespaced {} diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAIT.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAIT.java new file mode 100644 index 0000000000..0897c293db --- /dev/null +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAIT.java @@ -0,0 +1,73 @@ +/* + * Copyright Java Operator SDK Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.javaoperatorsdk.operator.baseapi.finalizernossa; + +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +class AddFinalizerNoSSAIT { + + public static final String TEST_RESOURCE_NAME = "add-finalizer-no-ssa-test1"; + + @RegisterExtension + LocallyRunOperatorExtension operator = + LocallyRunOperatorExtension.builder() + .withConfigurationService(o -> o.withUseSSAToPatchPrimaryResource(false)) + .withReconciler(new AddFinalizerNoSSAReconciler()) + .build(); + + @Test + void addsFinalizerWithoutSSAAndRemovesItOnCleanup() { + var reconciler = operator.getReconcilerOfType(AddFinalizerNoSSAReconciler.class); + + var testResource = createTestResource(); + operator.create(testResource); + + await("finalizer added") + .atMost(5, TimeUnit.SECONDS) + .untilAsserted( + () -> { + var actual = operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME); + assertThat(actual).isNotNull(); + assertThat(actual.getMetadata().getFinalizers()).hasSize(1); + }); + + operator.delete(testResource); + + await("resource deleted after finalizer removal") + .atMost(5, TimeUnit.SECONDS) + .until( + () -> operator.get(AddFinalizerNoSSACustomResource.class, TEST_RESOURCE_NAME) == null); + + assertThat(reconciler.getNumberOfExecutions()).isEqualTo(1); + assertThat(reconciler.getNumberOfCleanupExecutions()).isEqualTo(1); + } + + private AddFinalizerNoSSACustomResource createTestResource() { + AddFinalizerNoSSACustomResource cr = new AddFinalizerNoSSACustomResource(); + cr.setMetadata(new ObjectMeta()); + cr.getMetadata().setName(TEST_RESOURCE_NAME); + return cr; + } +} diff --git a/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAReconciler.java b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAReconciler.java new file mode 100644 index 0000000000..a9e29ec1f6 --- /dev/null +++ b/operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/finalizernossa/AddFinalizerNoSSAReconciler.java @@ -0,0 +1,53 @@ +/* + * Copyright Java Operator SDK Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.javaoperatorsdk.operator.baseapi.finalizernossa; + +import java.util.concurrent.atomic.AtomicInteger; + +import io.javaoperatorsdk.operator.api.reconciler.*; +import io.javaoperatorsdk.operator.support.TestExecutionInfoProvider; + +@ControllerConfiguration +public class AddFinalizerNoSSAReconciler + implements Reconciler, + Cleaner, + TestExecutionInfoProvider { + + private final AtomicInteger numberOfExecutions = new AtomicInteger(0); + private final AtomicInteger numberOfCleanupExecutions = new AtomicInteger(0); + + @Override + public UpdateControl reconcile( + AddFinalizerNoSSACustomResource resource, Context context) { + numberOfExecutions.addAndGet(1); + return UpdateControl.noUpdate(); + } + + @Override + public DeleteControl cleanup( + AddFinalizerNoSSACustomResource resource, Context context) { + numberOfCleanupExecutions.addAndGet(1); + return DeleteControl.defaultDelete(); + } + + public int getNumberOfExecutions() { + return numberOfExecutions.get(); + } + + public int getNumberOfCleanupExecutions() { + return numberOfCleanupExecutions.get(); + } +}