Skip to content

Commit d500347

Browse files
committed
test: test retry with max-attemps set to zero
1 parent 31502ce commit d500347

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ void testErrorMessageSetEventually() {
4242
assertThat(res.getStatus()).isNotNull();
4343
for (int i = 0; i < MAX_RETRY_ATTEMPTS + 1; i++) {
4444
assertThat(res.getStatus().getMessages())
45-
.contains(ErrorStatusHandlerTestReconciler.ERROR_STATUS_MESSAGE + i);
45+
.contains(ErrorStatusHandlerTestReconciler.ERROR_STATUS_MESSAGE + i
46+
+ (i == MAX_RETRY_ATTEMPTS));
4647
}
4748
});
4849
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.javaoperatorsdk.operator;
2+
3+
import java.util.concurrent.TimeUnit;
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.processing.retry.GenericRetry;
11+
import io.javaoperatorsdk.operator.sample.errorstatushandler.ErrorStatusHandlerTestCustomResource;
12+
import io.javaoperatorsdk.operator.sample.errorstatushandler.ErrorStatusHandlerTestReconciler;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.awaitility.Awaitility.await;
16+
17+
class ErrorStatusHandlerLastAttemptIT {
18+
19+
public static final int MAX_RETRY_ATTEMPTS = 0;
20+
ErrorStatusHandlerTestReconciler reconciler = new ErrorStatusHandlerTestReconciler();
21+
22+
@RegisterExtension
23+
LocallyRunOperatorExtension operator =
24+
LocallyRunOperatorExtension.builder()
25+
.withReconciler(reconciler,
26+
new GenericRetry().setMaxAttempts(MAX_RETRY_ATTEMPTS).withLinearRetry())
27+
.build();
28+
29+
@Test
30+
void testErrorMessageSetEventually() {
31+
ErrorStatusHandlerTestCustomResource resource =
32+
operator.create(ErrorStatusHandlerTestCustomResource.class, createCustomResource());
33+
34+
await()
35+
.atMost(10, TimeUnit.SECONDS)
36+
.pollInterval(250, TimeUnit.MICROSECONDS)
37+
.untilAsserted(
38+
() -> {
39+
ErrorStatusHandlerTestCustomResource res =
40+
operator.get(ErrorStatusHandlerTestCustomResource.class,
41+
resource.getMetadata().getName());
42+
assertThat(res.getStatus()).isNotNull();
43+
assertThat(res.getStatus().getMessages())
44+
.contains(ErrorStatusHandlerTestReconciler.ERROR_STATUS_MESSAGE + 0 + true);
45+
});
46+
}
47+
48+
public ErrorStatusHandlerTestCustomResource createCustomResource() {
49+
ErrorStatusHandlerTestCustomResource resource = new ErrorStatusHandlerTestCustomResource();
50+
resource.setMetadata(
51+
new ObjectMetaBuilder()
52+
.withName("error-status-test")
53+
.withNamespace(operator.getNamespace())
54+
.build());
55+
return resource;
56+
}
57+
58+
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/errorstatushandler/ErrorStatusHandlerTestReconciler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public ErrorStatusUpdateControl<ErrorStatusHandlerTestCustomResource> updateErro
5050
log.info("Setting status.");
5151
ensureStatusExists(resource);
5252
resource.getStatus().getMessages()
53-
.add(ERROR_STATUS_MESSAGE + context.getRetryInfo().orElseThrow().getAttemptCount());
53+
.add(ERROR_STATUS_MESSAGE + context.getRetryInfo().orElseThrow().getAttemptCount()
54+
+ context.getRetryInfo().get().isLastAttempt());
5455
return ErrorStatusUpdateControl.updateStatus(resource);
5556
}
5657
}

0 commit comments

Comments
 (0)