Skip to content

Commit c0007a8

Browse files
committed
Unit tests
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 8afa94e commit c0007a8

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/reconciler/ReconcileUtilsTest.java

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package io.javaoperatorsdk.operator.api.reconciler;
1717

18+
import java.util.Collections;
19+
import java.util.List;
1820
import java.util.function.UnaryOperator;
1921

2022
import org.junit.jupiter.api.BeforeEach;
@@ -33,7 +35,9 @@
3335
import io.javaoperatorsdk.operator.TestUtils;
3436
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
3537
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;
38+
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
3639
import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerEventSource;
40+
import io.javaoperatorsdk.operator.processing.event.source.informer.ManagedInformerEventSource;
3741
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;
3842

3943
import static org.assertj.core.api.Assertions.assertThat;
@@ -112,7 +116,7 @@ void validateAndCompareResourceVersionsTest() {
112116
}
113117

114118
@Test
115-
public void compareResourceVersionsWithStrings() {
119+
void compareResourceVersionsWithStrings() {
116120
// Test equal versions
117121
assertThat(ReconcileUtils.compareResourceVersions("1", "1")).isZero();
118122
assertThat(ReconcileUtils.compareResourceVersions("123", "123")).isZero();
@@ -355,6 +359,87 @@ void retriesFinalizerRemovalWithFreshResource() {
355359
verify(resourceOp, times(1)).get();
356360
}
357361

362+
@Test
363+
void resourcePatchWithSingleEventSource() {
364+
var resource = TestUtils.testCustomResource1();
365+
resource.getMetadata().setResourceVersion("1");
366+
367+
var updatedResource = TestUtils.testCustomResource1();
368+
updatedResource.getMetadata().setResourceVersion("2");
369+
370+
var eventSourceRetriever = mock(EventSourceRetriever.class);
371+
var managedEventSource = mock(ManagedInformerEventSource.class);
372+
373+
when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever);
374+
when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class))
375+
.thenReturn(List.of(managedEventSource));
376+
when(managedEventSource.eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class)))
377+
.thenReturn(updatedResource);
378+
379+
var result = ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity());
380+
381+
assertThat(result).isNotNull();
382+
assertThat(result.getMetadata().getResourceVersion()).isEqualTo("2");
383+
verify(managedEventSource, times(1))
384+
.eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class));
385+
}
386+
387+
@Test
388+
void resourcePatchThrowsWhenNoEventSourceFound() {
389+
var resource = TestUtils.testCustomResource1();
390+
var eventSourceRetriever = mock(EventSourceRetriever.class);
391+
392+
when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever);
393+
when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class))
394+
.thenReturn(Collections.emptyList());
395+
396+
var exception =
397+
assertThrows(
398+
IllegalStateException.class,
399+
() -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity()));
400+
401+
assertThat(exception.getMessage()).contains("No event source found for type");
402+
}
403+
404+
@Test
405+
void resourcePatchThrowsWhenMultipleEventSourcesFound() {
406+
var resource = TestUtils.testCustomResource1();
407+
var eventSourceRetriever = mock(EventSourceRetriever.class);
408+
var eventSource1 = mock(ManagedInformerEventSource.class);
409+
var eventSource2 = mock(ManagedInformerEventSource.class);
410+
411+
when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever);
412+
when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class))
413+
.thenReturn(List.of(eventSource1, eventSource2));
414+
415+
var exception =
416+
assertThrows(
417+
IllegalStateException.class,
418+
() -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity()));
419+
420+
assertThat(exception.getMessage()).contains("Multiple event sources found for");
421+
assertThat(exception.getMessage()).contains("please provide the target event source");
422+
}
423+
424+
@Test
425+
void resourcePatchThrowsWhenEventSourceIsNotManagedInformer() {
426+
var resource = TestUtils.testCustomResource1();
427+
var eventSourceRetriever = mock(EventSourceRetriever.class);
428+
var nonManagedEventSource = mock(EventSource.class);
429+
430+
when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever);
431+
when(eventSourceRetriever.getEventSourcesFor(TestCustomResource.class))
432+
.thenReturn(List.of(nonManagedEventSource));
433+
434+
var exception =
435+
assertThrows(
436+
IllegalStateException.class,
437+
() -> ReconcileUtils.resourcePatch(context, resource, UnaryOperator.identity()));
438+
439+
assertThat(exception.getMessage()).contains("Target event source must be a subclass off");
440+
assertThat(exception.getMessage()).contains("ManagedInformerEventSource");
441+
}
442+
358443
// naive performance test that compares the work case scenario for the parsing and non-parsing
359444
// variants
360445
@Test

sample-operators/mysql-schema/k8s/operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ spec:
3939
serviceAccountName: mysql-schema-operator # specify the ServiceAccount under which's RBAC persmissions the operator will be executed under
4040
containers:
4141
- name: operator
42-
image: mysql-schema-operator # TODO Change this to point to your pushed mysql-schema-operator image
42+
image: mysql-schema-operator # Change this to point to your pushed mysql-schema-operator image
4343
imagePullPolicy: IfNotPresent
4444
ports:
4545
- containerPort: 80

0 commit comments

Comments
 (0)