-
Notifications
You must be signed in to change notification settings - Fork 220
fix: only check for CRD if we're dealing with CustomResources #811
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...r-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/ControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.javaoperatorsdk.operator.processing; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.fabric8.kubernetes.api.model.Secret; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.V1ApiextensionAPIGroupDSL; | ||
import io.fabric8.kubernetes.client.dsl.ApiextensionsAPIGroupDSL; | ||
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation; | ||
import io.fabric8.kubernetes.client.dsl.Resource; | ||
import io.javaoperatorsdk.operator.MissingCRDException; | ||
import io.javaoperatorsdk.operator.api.config.ConfigurationService; | ||
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; | ||
import io.javaoperatorsdk.operator.api.reconciler.Reconciler; | ||
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ControllerTest { | ||
|
||
@Test | ||
void crdShouldNotBeCheckedForNativeResources() { | ||
final var client = mock(KubernetesClient.class); | ||
final var configurationService = mock(ConfigurationService.class); | ||
final var reconciler = mock(Reconciler.class); | ||
final var configuration = mock(ControllerConfiguration.class); | ||
when(configuration.getResourceClass()).thenReturn(Secret.class); | ||
when(configuration.getConfigurationService()).thenReturn(configurationService); | ||
|
||
final var controller = new Controller<Secret>(reconciler, configuration, client); | ||
controller.start(); | ||
verify(client, never()).apiextensions(); | ||
} | ||
|
||
@Test | ||
void crdShouldNotBeCheckedForCustomResourcesIfDisabled() { | ||
final var client = mock(KubernetesClient.class); | ||
final var configurationService = mock(ConfigurationService.class); | ||
when(configurationService.checkCRDAndValidateLocalModel()).thenReturn(false); | ||
final var reconciler = mock(Reconciler.class); | ||
final var configuration = mock(ControllerConfiguration.class); | ||
when(configuration.getResourceClass()).thenReturn(TestCustomResource.class); | ||
when(configuration.getConfigurationService()).thenReturn(configurationService); | ||
|
||
final var controller = new Controller<TestCustomResource>(reconciler, configuration, client); | ||
controller.start(); | ||
verify(client, never()).apiextensions(); | ||
} | ||
|
||
@Test | ||
void crdShouldBeCheckedForCustomResourcesByDefault() { | ||
final var client = mock(KubernetesClient.class); | ||
final var configurationService = mock(ConfigurationService.class); | ||
when(configurationService.checkCRDAndValidateLocalModel()).thenCallRealMethod(); | ||
final var reconciler = mock(Reconciler.class); | ||
final var configuration = mock(ControllerConfiguration.class); | ||
when(configuration.getResourceClass()).thenReturn(TestCustomResource.class); | ||
when(configuration.getConfigurationService()).thenReturn(configurationService); | ||
final var apiGroupDSL = mock(ApiextensionsAPIGroupDSL.class); | ||
when(client.apiextensions()).thenReturn(apiGroupDSL); | ||
final var v1 = mock(V1ApiextensionAPIGroupDSL.class); | ||
when(apiGroupDSL.v1()).thenReturn(v1); | ||
final var operation = mock(NonNamespaceOperation.class); | ||
when(v1.customResourceDefinitions()).thenReturn(operation); | ||
when(operation.withName(any())).thenReturn(mock(Resource.class)); | ||
|
||
final var controller = new Controller<TestCustomResource>(reconciler, configuration, client); | ||
// since we're not really connected to a cluster and the CRD wouldn't be deployed anyway, we | ||
// expect a MissingCRDException to be thrown | ||
assertThrows(MissingCRDException.class, controller::start); | ||
verify(client, times(1)).apiextensions(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.