|
| 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.baseapi.expectation.periodicclean; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 22 | + |
| 23 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 24 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 25 | + |
| 26 | +import static io.javaoperatorsdk.operator.baseapi.expectation.periodicclean.PeriodicCleanerExpectationReconciler.DEPLOYMENT_READY; |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.awaitility.Awaitility.await; |
| 29 | + |
| 30 | +/** |
| 31 | + * Integration test showcasing PeriodicCleanerExpectationManager usage. |
| 32 | + * |
| 33 | + * <p>This test demonstrates the key benefits of PeriodicCleanerExpectationManager: 1. Works without |
| 34 | + * requiring @ControllerConfiguration(triggerReconcilerOnAllEvents = true) 2. Automatically cleans |
| 35 | + * up stale expectations periodically 3. Maintains the same expectation API and functionality as the |
| 36 | + * regular ExpectationManager |
| 37 | + */ |
| 38 | +class PeriodicCleanerExpectationIT { |
| 39 | + |
| 40 | + public static final String TEST_1 = "test1"; |
| 41 | + public static final String TEST_2 = "test2"; |
| 42 | + |
| 43 | + @RegisterExtension |
| 44 | + LocallyRunOperatorExtension extension = |
| 45 | + LocallyRunOperatorExtension.builder() |
| 46 | + .withReconciler(new PeriodicCleanerExpectationReconciler()) |
| 47 | + .build(); |
| 48 | + |
| 49 | + @Test |
| 50 | + void testPeriodicCleanerExpectationBasicFlow() { |
| 51 | + extension |
| 52 | + .getReconcilerOfType(PeriodicCleanerExpectationReconciler.class) |
| 53 | + .setTimeout(Duration.ofSeconds(30)); |
| 54 | + var res = testResource(); |
| 55 | + extension.create(res); |
| 56 | + |
| 57 | + await() |
| 58 | + .untilAsserted( |
| 59 | + () -> { |
| 60 | + var actual = extension.get(PeriodicCleanerExpectationCustomResource.class, TEST_1); |
| 61 | + assertThat(actual.getStatus()).isNotNull(); |
| 62 | + assertThat(actual.getStatus().getMessage()).isEqualTo(DEPLOYMENT_READY); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void demonstratesNoTriggerReconcilerOnAllEventsNeededForCleanup() { |
| 68 | + // This test demonstrates that PeriodicCleanerExpectationManager works |
| 69 | + // without @ControllerConfiguration(triggerReconcilerOnAllEvents = true) |
| 70 | + |
| 71 | + // The PeriodicCleanerExpectationReconciler doesn't use triggerReconcilerOnAllEvents = true |
| 72 | + // yet expectations still work properly due to the periodic cleanup functionality |
| 73 | + |
| 74 | + var reconciler = extension.getReconcilerOfType(PeriodicCleanerExpectationReconciler.class); |
| 75 | + reconciler.setTimeout(Duration.ofSeconds(30)); |
| 76 | + |
| 77 | + var res = testResource("no-trigger-test"); |
| 78 | + var created = extension.create(res); |
| 79 | + |
| 80 | + await() |
| 81 | + .untilAsserted( |
| 82 | + () -> { |
| 83 | + assertThat(reconciler.getExpectationManager().getExpectation(created)).isPresent(); |
| 84 | + }); |
| 85 | + |
| 86 | + extension.delete(res); |
| 87 | + |
| 88 | + await() |
| 89 | + .untilAsserted( |
| 90 | + () -> { |
| 91 | + assertThat(reconciler.getExpectationManager().getExpectation(created)).isEmpty(); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + private PeriodicCleanerExpectationCustomResource testResource() { |
| 96 | + return testResource(TEST_1); |
| 97 | + } |
| 98 | + |
| 99 | + private PeriodicCleanerExpectationCustomResource testResource(String name) { |
| 100 | + var res = new PeriodicCleanerExpectationCustomResource(); |
| 101 | + res.setMetadata(new ObjectMetaBuilder().withName(name).build()); |
| 102 | + return res; |
| 103 | + } |
| 104 | +} |
0 commit comments