|
| 1 | +package io.javaoperatorsdk.operator; |
| 2 | + |
| 3 | +import java.time.Duration; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 7 | + |
| 8 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 9 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 10 | +import io.javaoperatorsdk.operator.sample.gracefulstop.GracefulStopTestCustomResource; |
| 11 | +import io.javaoperatorsdk.operator.sample.gracefulstop.GracefulStopTestCustomResourceSpec; |
| 12 | +import io.javaoperatorsdk.operator.sample.gracefulstop.GracefulStopTestReconciler; |
| 13 | + |
| 14 | +import static io.javaoperatorsdk.operator.sample.gracefulstop.GracefulStopTestReconciler.RECONCILER_SLEEP; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.awaitility.Awaitility.await; |
| 17 | + |
| 18 | +public class GracefulStopIT { |
| 19 | + |
| 20 | + public static final String TEST_1 = "test1"; |
| 21 | + public static final String TEST_2 = "test2"; |
| 22 | + |
| 23 | + @RegisterExtension |
| 24 | + LocallyRunOperatorExtension operator = |
| 25 | + LocallyRunOperatorExtension.builder() |
| 26 | + .withConfigurationService(o -> o.withCloseClientOnStop(false)) |
| 27 | + .withReconciler(new GracefulStopTestReconciler()) |
| 28 | + .build(); |
| 29 | + |
| 30 | + @Test |
| 31 | + void stopsGracefullyWIthTimeout() { |
| 32 | + var testRes = operator.create(testResource1()); |
| 33 | + await().untilAsserted(() -> { |
| 34 | + var r = operator.get(GracefulStopTestCustomResource.class, TEST_1); |
| 35 | + assertThat(r.getStatus()).isNotNull(); |
| 36 | + assertThat(r.getStatus().getObservedGeneration()).isEqualTo(1); |
| 37 | + assertThat(operator.getReconcilerOfType(GracefulStopTestReconciler.class) |
| 38 | + .getNumberOfExecutions()).isEqualTo(1); |
| 39 | + }); |
| 40 | + |
| 41 | + testRes.getSpec().setValue(2); |
| 42 | + operator.replace(testRes); |
| 43 | + |
| 44 | + await().pollDelay(Duration.ofMillis(50)).untilAsserted( |
| 45 | + () -> assertThat(operator.getReconcilerOfType(GracefulStopTestReconciler.class) |
| 46 | + .getNumberOfExecutions()).isEqualTo(2)); |
| 47 | + |
| 48 | + operator.getOperator().stop(Duration.ofMillis(RECONCILER_SLEEP)); |
| 49 | + |
| 50 | + await().untilAsserted(() -> { |
| 51 | + var r = operator.get(GracefulStopTestCustomResource.class, TEST_1); |
| 52 | + assertThat(r.getStatus()).isNotNull(); |
| 53 | + assertThat(r.getStatus().getObservedGeneration()).isEqualTo(2); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void stopsGracefullyWithExpiredTimeout() { |
| 59 | + var testRes = operator.create(testResource2()); |
| 60 | + await().untilAsserted(() -> { |
| 61 | + var r = operator.get(GracefulStopTestCustomResource.class, TEST_2); |
| 62 | + assertThat(r.getStatus()).isNotNull(); |
| 63 | + assertThat(r.getStatus().getObservedGeneration()).isEqualTo(1); |
| 64 | + }); |
| 65 | + |
| 66 | + testRes.getSpec().setValue(2); |
| 67 | + operator.replace(testRes); |
| 68 | + |
| 69 | + await().pollDelay(Duration.ofMillis(50)).untilAsserted( |
| 70 | + () -> assertThat(operator.getReconcilerOfType(GracefulStopTestReconciler.class) |
| 71 | + .getNumberOfExecutions()).isEqualTo(2)); |
| 72 | + |
| 73 | + operator.getOperator().stop(Duration.ofMillis(RECONCILER_SLEEP / 5)); |
| 74 | + |
| 75 | + await().pollDelay(Duration.ofMillis(RECONCILER_SLEEP)).untilAsserted(() -> { |
| 76 | + var r = operator.get(GracefulStopTestCustomResource.class, TEST_2); |
| 77 | + assertThat(r.getStatus()).isNotNull(); |
| 78 | + assertThat(r.getStatus().getObservedGeneration()).isEqualTo(1); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + public GracefulStopTestCustomResource testResource1() { |
| 83 | + return testResource(TEST_1); |
| 84 | + } |
| 85 | + |
| 86 | + public GracefulStopTestCustomResource testResource2() { |
| 87 | + return testResource(TEST_2); |
| 88 | + } |
| 89 | + |
| 90 | + public GracefulStopTestCustomResource testResource(String name) { |
| 91 | + GracefulStopTestCustomResource resource = |
| 92 | + new GracefulStopTestCustomResource(); |
| 93 | + resource.setMetadata( |
| 94 | + new ObjectMetaBuilder() |
| 95 | + .withName(name) |
| 96 | + .withNamespace(operator.getNamespace()) |
| 97 | + .build()); |
| 98 | + resource.setSpec(new GracefulStopTestCustomResourceSpec()); |
| 99 | + resource.getSpec().setValue(1); |
| 100 | + return resource; |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments