Skip to content

Commit fd0e92c

Browse files
committed
chore(tests): add condition with result tests
Signed-off-by: Chris Laprun <[email protected]>
1 parent 1c0f27f commit fd0e92c

File tree

5 files changed

+70
-23
lines changed

5 files changed

+70
-23
lines changed

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileExecutorTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
99

10+
import io.fabric8.kubernetes.api.model.ConfigMap;
1011
import io.fabric8.kubernetes.api.model.HasMetadata;
1112
import io.javaoperatorsdk.operator.AggregatedOperatorException;
1213
import io.javaoperatorsdk.operator.api.reconciler.Context;
@@ -184,8 +185,18 @@ void onlyOneDependsOnErroredResourceNotReconciled() {
184185

185186
@Test
186187
void simpleReconcileCondition() {
188+
final var result = "Some error message";
189+
final var unmetWithResult = new ResultCondition<ConfigMap, TestCustomResource, String>() {
190+
@Override
191+
public Result<String> detailedIsMet(
192+
DependentResource<ConfigMap, TestCustomResource> dependentResource,
193+
TestCustomResource primary, Context<TestCustomResource> context) {
194+
return Result.withResult(false, result);
195+
}
196+
};
197+
187198
var workflow = new WorkflowBuilder<TestCustomResource>()
188-
.addDependentResource(dr1).withReconcilePrecondition(notMetCondition)
199+
.addDependentResource(dr1).withReconcilePrecondition(unmetWithResult)
189200
.addDependentResource(dr2).withReconcilePrecondition(metCondition)
190201
.addDependentResource(drDeleter).withReconcilePrecondition(notMetCondition)
191202
.build();
@@ -196,6 +207,8 @@ void simpleReconcileCondition() {
196207
Assertions.assertThat(res.getErroredDependents()).isEmpty();
197208
Assertions.assertThat(res.getReconciledDependents()).containsExactlyInAnyOrder(dr2);
198209
Assertions.assertThat(res.getNotReadyDependents()).isEmpty();
210+
res.getDependentConditionResult(dr1, Condition.Type.RECONCILE, String.class)
211+
.ifPresentOrElse(s -> assertEquals(result, s), org.junit.jupiter.api.Assertions::fail);
199212
}
200213

201214

operator-framework/src/test/java/io/javaoperatorsdk/operator/WorkflowAllFeatureIT.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
1111
import io.fabric8.kubernetes.api.model.apps.Deployment;
1212
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
13+
import io.javaoperatorsdk.operator.sample.workflowallfeature.ConfigMapReconcileCondition;
1314
import io.javaoperatorsdk.operator.sample.workflowallfeature.WorkflowAllFeatureCustomResource;
1415
import io.javaoperatorsdk.operator.sample.workflowallfeature.WorkflowAllFeatureReconciler;
1516
import io.javaoperatorsdk.operator.sample.workflowallfeature.WorkflowAllFeatureSpec;
17+
import io.javaoperatorsdk.operator.sample.workflowallfeature.WorkflowAllFeatureStatus;
1618

1719
import static io.javaoperatorsdk.operator.sample.workflowallfeature.ConfigMapDependentResource.READY_TO_DELETE_ANNOTATION;
1820
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,6 +41,8 @@ void configMapNotReconciledUntilDeploymentReady() {
3941
.isPositive();
4042
assertThat(operator.get(Deployment.class, RESOURCE_NAME)).isNotNull();
4143
assertThat(operator.get(ConfigMap.class, RESOURCE_NAME)).isNull();
44+
assertThat(getPrimaryStatus().getMsgFromCondition())
45+
.isEqualTo(ConfigMapReconcileCondition.NOT_RECONCILED_YET);
4246
});
4347

4448
await().atMost(ONE_MINUTE).untilAsserted(() -> {
@@ -47,31 +51,36 @@ void configMapNotReconciledUntilDeploymentReady() {
4751
.getNumberOfReconciliationExecution())
4852
.isGreaterThan(1);
4953
assertThat(operator.get(ConfigMap.class, RESOURCE_NAME)).isNotNull();
50-
assertThat(operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME)
51-
.getStatus().getReady()).isTrue();
54+
final var primaryStatus = getPrimaryStatus();
55+
assertThat(primaryStatus.getReady()).isTrue();
56+
assertThat(primaryStatus.getMsgFromCondition())
57+
.isEqualTo(ConfigMapReconcileCondition.CREATE_SET);
5258
});
5359

5460
markConfigMapForDelete();
5561
}
5662

63+
private WorkflowAllFeatureStatus getPrimaryStatus() {
64+
return operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME)
65+
.getStatus();
66+
}
67+
5768

5869
@Test
5970
void configMapNotReconciledIfReconcileConditionNotMet() {
6071
var resource = operator.create(customResource(false));
6172

6273
await().atMost(ONE_MINUTE).untilAsserted(() -> {
6374
assertThat(operator.get(ConfigMap.class, RESOURCE_NAME)).isNull();
64-
assertThat(operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME)
65-
.getStatus().getReady()).isTrue();
75+
assertThat(getPrimaryStatus().getReady()).isTrue();
6676
});
6777

6878
resource.getSpec().setCreateConfigMap(true);
6979
operator.replace(resource);
7080

7181
await().untilAsserted(() -> {
7282
assertThat(operator.get(ConfigMap.class, RESOURCE_NAME)).isNotNull();
73-
assertThat(operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME)
74-
.getStatus().getReady()).isTrue();
83+
assertThat(getPrimaryStatus().getReady()).isTrue();
7584
});
7685
}
7786

@@ -81,10 +90,9 @@ void configMapNotDeletedUntilNotMarked() {
8190
var resource = operator.create(customResource(true));
8291

8392
await().atMost(ONE_MINUTE).untilAsserted(() -> {
84-
assertThat(operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME).getStatus())
93+
assertThat(getPrimaryStatus())
8594
.isNotNull();
86-
assertThat(operator.get(WorkflowAllFeatureCustomResource.class, RESOURCE_NAME)
87-
.getStatus().getReady()).isTrue();
95+
assertThat(getPrimaryStatus().getReady()).isTrue();
8896
assertThat(operator.get(ConfigMap.class, RESOURCE_NAME)).isNotNull();
8997
});
9098

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/workflowallfeature/ConfigMapReconcileCondition.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import io.fabric8.kubernetes.api.model.ConfigMap;
44
import io.javaoperatorsdk.operator.api.reconciler.Context;
55
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
6-
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
6+
import io.javaoperatorsdk.operator.processing.dependent.workflow.ResultCondition;
77

88
public class ConfigMapReconcileCondition
9-
implements Condition<ConfigMap, WorkflowAllFeatureCustomResource> {
9+
implements ResultCondition<ConfigMap, WorkflowAllFeatureCustomResource, String> {
10+
11+
public static final String CREATE_SET = "create set";
12+
public static final String CREATE_NOT_SET = "create not set";
13+
public static final String NOT_RECONCILED_YET = "Not reconciled yet";
1014

1115
@Override
12-
public boolean isMet(
16+
public Result<String> detailedIsMet(
1317
DependentResource<ConfigMap, WorkflowAllFeatureCustomResource> dependentResource,
14-
WorkflowAllFeatureCustomResource primary,
15-
Context<WorkflowAllFeatureCustomResource> context) {
16-
17-
return primary.getSpec().isCreateConfigMap();
18+
WorkflowAllFeatureCustomResource primary, Context<WorkflowAllFeatureCustomResource> context) {
19+
final var createConfigMap = primary.getSpec().isCreateConfigMap();
20+
return Result.withResult(createConfigMap, createConfigMap ? CREATE_SET : CREATE_NOT_SET);
1821
}
1922
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/workflowallfeature/WorkflowAllFeatureReconciler.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
import java.util.concurrent.atomic.AtomicInteger;
44

5-
import io.javaoperatorsdk.operator.api.reconciler.*;
5+
import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
6+
import io.javaoperatorsdk.operator.api.reconciler.Context;
7+
import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
8+
import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
9+
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
10+
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
11+
import io.javaoperatorsdk.operator.api.reconciler.Workflow;
612
import io.javaoperatorsdk.operator.api.reconciler.dependent.Dependent;
13+
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
14+
import io.javaoperatorsdk.operator.processing.dependent.workflow.Condition;
715

816
import static io.javaoperatorsdk.operator.sample.workflowallfeature.WorkflowAllFeatureReconciler.DEPLOYMENT_NAME;
917

@@ -33,11 +41,15 @@ public UpdateControl<WorkflowAllFeatureCustomResource> reconcile(
3341
if (resource.getStatus() == null) {
3442
resource.setStatus(new WorkflowAllFeatureStatus());
3543
}
44+
final var reconcileResult = context.managedWorkflowAndDependentResourceContext()
45+
.getWorkflowReconcileResult();
46+
final var msgFromCondition = reconcileResult.getDependentConditionResult(
47+
DependentResource.defaultNameFor(ConfigMapDependentResource.class),
48+
Condition.Type.RECONCILE, String.class)
49+
.orElse(ConfigMapReconcileCondition.NOT_RECONCILED_YET);
3650
resource.getStatus()
37-
.setReady(
38-
context.managedWorkflowAndDependentResourceContext()
39-
.getWorkflowReconcileResult()
40-
.allDependentResourcesReady());
51+
.withReady(reconcileResult.allDependentResourcesReady())
52+
.withMsgFromCondition(msgFromCondition);
4153
return UpdateControl.patchStatus(resource);
4254
}
4355

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/workflowallfeature/WorkflowAllFeatureStatus.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@
33
public class WorkflowAllFeatureStatus {
44

55
private Boolean ready;
6+
private String msgFromCondition;
67

78
public Boolean getReady() {
89
return ready;
910
}
1011

11-
public WorkflowAllFeatureStatus setReady(Boolean ready) {
12+
public String getMsgFromCondition() {
13+
return msgFromCondition;
14+
}
15+
16+
public WorkflowAllFeatureStatus withReady(Boolean ready) {
1217
this.ready = ready;
1318
return this;
1419
}
20+
21+
@SuppressWarnings("UnusedReturnValue")
22+
public WorkflowAllFeatureStatus withMsgFromCondition(String msgFromCondition) {
23+
this.msgFromCondition = msgFromCondition;
24+
return this;
25+
}
1526
}

0 commit comments

Comments
 (0)