|
5 | 5 | import java.util.List;
|
6 | 6 | import java.util.Optional;
|
7 | 7 |
|
| 8 | +import org.assertj.core.api.Assertions; |
8 | 9 | import org.junit.jupiter.api.Test;
|
9 | 10 |
|
10 | 11 | import io.javaoperatorsdk.operator.AggregatedOperatorException;
|
|
13 | 14 | import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
|
14 | 15 | import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
|
15 | 16 | import io.javaoperatorsdk.operator.processing.dependent.workflow.builder.WorkflowBuilder;
|
| 17 | +import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReadyCondition; |
16 | 18 | import io.javaoperatorsdk.operator.processing.dependent.workflow.condition.ReconcileCondition;
|
17 | 19 | import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
|
18 | 20 |
|
|
21 | 23 |
|
22 | 24 | class WorkflowTest {
|
23 | 25 |
|
| 26 | + public static final String NOT_READY_YET = "NOT READY YET"; |
24 | 27 | private ReconcileCondition met_reconcile_condition =
|
25 | 28 | (dependentResource, primary, context) -> true;
|
26 | 29 | private ReconcileCondition not_met_reconcile_condition =
|
27 | 30 | (dependentResource, primary, context) -> false;
|
28 | 31 |
|
| 32 | + private ReadyCondition<String, TestCustomResource> metReadyCondition = |
| 33 | + (dependentResource, primary, context) -> true; |
| 34 | + private ReadyCondition<String, TestCustomResource> notMetReadyCondition = |
| 35 | + (dependentResource, primary, context) -> false; |
| 36 | + |
| 37 | + private ReadyCondition<String, TestCustomResource> notMetReadyConditionWithStatusUpdate = |
| 38 | + new ReadyCondition<>() { |
| 39 | + @Override |
| 40 | + public boolean isMet(DependentResource<String, TestCustomResource> dependentResource, |
| 41 | + TestCustomResource primary, Context<TestCustomResource> context) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void addNotReadyStatusInfo(TestCustomResource primary) { |
| 47 | + primary.getStatus().setConfigMapStatus(NOT_READY_YET); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
29 | 51 | public static final String VALUE = "value";
|
30 | 52 | private List<ReconcileRecord> executionHistory =
|
31 | 53 | Collections.synchronizedList(new ArrayList<>());
|
@@ -233,6 +255,59 @@ void oneDependsOnConditionNotMet() {
|
233 | 255 | assertThat(executionHistory).notReconciled(dr2);
|
234 | 256 | }
|
235 | 257 |
|
| 258 | + @Test |
| 259 | + void readyConditionTrivialCase() { |
| 260 | + var workflow = new WorkflowBuilder<TestCustomResource>() |
| 261 | + .addDependent(dr1).withReadyCondition(metReadyCondition).build() |
| 262 | + .addDependent(dr2).dependsOn(dr1).build() |
| 263 | + .build(); |
| 264 | + |
| 265 | + workflow.reconcile(new TestCustomResource(), null); |
| 266 | + |
| 267 | + assertThat(executionHistory).reconciledInOrder(dr1, dr2); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + void readyConditionNotMetTrivialCase() { |
| 272 | + var workflow = new WorkflowBuilder<TestCustomResource>() |
| 273 | + .addDependent(dr1).withReadyCondition(notMetReadyCondition).build() |
| 274 | + .addDependent(dr2).dependsOn(dr1).build() |
| 275 | + .build(); |
| 276 | + |
| 277 | + workflow.reconcile(new TestCustomResource(), null); |
| 278 | + |
| 279 | + assertThat(executionHistory).reconciled(dr1).notReconciled(dr2); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + void readyConditionNotMetStatusUpdates() { |
| 284 | + var workflow = new WorkflowBuilder<TestCustomResource>() |
| 285 | + .addDependent(dr1).withReadyCondition(notMetReadyConditionWithStatusUpdate).build() |
| 286 | + .addDependent(dr2).dependsOn(dr1).build() |
| 287 | + .build(); |
| 288 | + |
| 289 | + var cr = new TestCustomResource(); |
| 290 | + workflow.reconcile(cr, null); |
| 291 | + |
| 292 | + assertThat(executionHistory).reconciled(dr1).notReconciled(dr2); |
| 293 | + Assertions.assertThat(cr.getStatus().getConfigMapStatus()).isEqualTo(NOT_READY_YET); |
| 294 | + } |
| 295 | + |
| 296 | + @Test |
| 297 | + void readyConditionNotMetInOneParent() { |
| 298 | + TestDependent dr3 = new TestDependent("DR_3"); |
| 299 | + |
| 300 | + var workflow = new WorkflowBuilder<TestCustomResource>() |
| 301 | + .addDependent(dr1).withReadyCondition(notMetReadyCondition).build() |
| 302 | + .addDependent(dr2).build() |
| 303 | + .addDependent(dr3).dependsOn(dr1, dr2).build() |
| 304 | + .build(); |
| 305 | + |
| 306 | + workflow.reconcile(new TestCustomResource(), null); |
| 307 | + |
| 308 | + assertThat(executionHistory).reconciled(dr1, dr2).notReconciled(dr3); |
| 309 | + } |
| 310 | + |
236 | 311 | private class TestDependent implements DependentResource<String, TestCustomResource> {
|
237 | 312 |
|
238 | 313 | private String name;
|
|
0 commit comments