-
Notifications
You must be signed in to change notification settings - Fork 220
Error status handler improvements #742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package io.javaoperatorsdk.operator.processing.event; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -11,6 +12,7 @@ | |
import io.fabric8.kubernetes.api.model.ObjectMeta; | ||
import io.fabric8.kubernetes.client.CustomResource; | ||
import io.javaoperatorsdk.operator.TestUtils; | ||
import io.javaoperatorsdk.operator.api.config.Cloner; | ||
import io.javaoperatorsdk.operator.api.config.ConfigurationService; | ||
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.monitoring.Metrics; | ||
|
@@ -56,9 +58,12 @@ private <R extends HasMetadata> ReconciliationDispatcher<R> init(R customResourc | |
when(configuration.getName()).thenReturn("EventDispatcherTestController"); | ||
when(configService.getMetrics()).thenReturn(Metrics.NOOP); | ||
when(configuration.getConfigurationService()).thenReturn(configService); | ||
when(configService.getResourceCloner()).thenReturn(ConfigurationService.DEFAULT_CLONER); | ||
when(reconciler.reconcile(eq(customResource), any())) | ||
.thenReturn(UpdateControl.updateResource(customResource)); | ||
when(configService.getResourceCloner()).thenReturn(new Cloner() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this changed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make sure to make easier to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but then you're not really testing the actual behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be understood as a form of a mocking of that clone method / cloner. The goal here is not to test the cloner but only if it is called in case it's relevant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand but the problem is what if the behavior changes when the instance is not actually cloned? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually not, is just what instance we move around, for some special cases. |
||
@Override | ||
public <R extends HasMetadata> R clone(R object) { | ||
return object; | ||
} | ||
}); | ||
when(reconciler.cleanup(eq(customResource), any())) | ||
.thenReturn(DeleteControl.defaultDelete()); | ||
when(customResourceFacade.replaceWithLock(any())).thenReturn(null); | ||
|
@@ -351,9 +356,9 @@ void callErrorStatusHandlerIfImplemented() { | |
|
||
when(reconciler.reconcile(any(), any())) | ||
.thenThrow(new IllegalStateException("Error Status Test")); | ||
when(((ErrorStatusHandler) reconciler).updateErrorStatus(any(), any())).then(a -> { | ||
when(((ErrorStatusHandler) reconciler).updateErrorStatus(any(), any(), any())).then(a -> { | ||
testCustomResource.getStatus().setConfigMapStatus(ERROR_MESSAGE); | ||
return testCustomResource; | ||
return Optional.of(testCustomResource); | ||
}); | ||
|
||
reconciliationDispatcher.handleExecution( | ||
|
@@ -373,7 +378,25 @@ public boolean isLastAttempt() { | |
|
||
verify(customResourceFacade, times(1)).updateStatus(testCustomResource); | ||
verify(((ErrorStatusHandler) reconciler), times(1)).updateErrorStatus(eq(testCustomResource), | ||
any()); | ||
any(), any()); | ||
} | ||
|
||
@Test | ||
void callErrorStatusHandlerEvenOnFirstError() { | ||
testCustomResource.addFinalizer(DEFAULT_FINALIZER); | ||
|
||
when(reconciler.reconcile(any(), any())) | ||
.thenThrow(new IllegalStateException("Error Status Test")); | ||
when(((ErrorStatusHandler) reconciler).updateErrorStatus(any(), any(), any())).then(a -> { | ||
testCustomResource.getStatus().setConfigMapStatus(ERROR_MESSAGE); | ||
return Optional.of(testCustomResource); | ||
}); | ||
reconciliationDispatcher.handleExecution( | ||
new ExecutionScope( | ||
testCustomResource, null)); | ||
verify(customResourceFacade, times(1)).updateStatus(testCustomResource); | ||
verify(((ErrorStatusHandler) reconciler), times(1)).updateErrorStatus(eq(testCustomResource), | ||
any(), any()); | ||
} | ||
|
||
private ObservedGenCustomResource createObservedGenCustomResource() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.javaoperatorsdk.operator; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService; | ||
import io.javaoperatorsdk.operator.junit.OperatorExtension; | ||
import io.javaoperatorsdk.operator.processing.retry.GenericRetry; | ||
import io.javaoperatorsdk.operator.sample.retry.RetryTestCustomReconciler; | ||
import io.javaoperatorsdk.operator.sample.retry.RetryTestCustomResource; | ||
|
||
import static io.javaoperatorsdk.operator.RetryIT.createTestCustomResource; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class RetryMaxAttemptIT { | ||
|
||
public static final int MAX_RETRY_ATTEMPTS = 3; | ||
public static final int RETRY_INTERVAL = 100; | ||
public static final int ALL_EXECUTION_TO_FAIL = 99; | ||
|
||
RetryTestCustomReconciler reconciler = new RetryTestCustomReconciler(ALL_EXECUTION_TO_FAIL); | ||
|
||
@RegisterExtension | ||
OperatorExtension operator = | ||
OperatorExtension.builder() | ||
.withConfigurationService(DefaultConfigurationService.instance()) | ||
.withReconciler(reconciler, | ||
new GenericRetry().setInitialInterval(RETRY_INTERVAL).withLinearRetry() | ||
.setMaxAttempts(MAX_RETRY_ATTEMPTS)) | ||
.build(); | ||
|
||
|
||
@Test | ||
public void retryFailedExecution() throws InterruptedException { | ||
RetryTestCustomResource resource = createTestCustomResource("max-retry"); | ||
|
||
operator.create(RetryTestCustomResource.class, resource); | ||
|
||
Thread.sleep((MAX_RETRY_ATTEMPTS + 2) * RETRY_INTERVAL); | ||
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(4); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
package io.javaoperatorsdk.operator.sample.errorstatushandler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ErrorStatusHandlerTestCustomResourceStatus { | ||
|
||
private String message; | ||
private List<String> messages; | ||
|
||
public String getMessage() { | ||
return message; | ||
public List<String> getMessages() { | ||
if (messages == null) { | ||
messages = new ArrayList<>(); | ||
} | ||
return messages; | ||
} | ||
|
||
public ErrorStatusHandlerTestCustomResourceStatus setMessage(String message) { | ||
this.message = message; | ||
public ErrorStatusHandlerTestCustomResourceStatus setMessages(List<String> messages) { | ||
this.messages = messages; | ||
return this; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.