|
1 | 1 | package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
|
2 | 2 |
|
3 |
| -import java.util.Map; |
4 |
| - |
5 |
| -import org.junit.jupiter.api.BeforeEach; |
6 |
| -import org.junit.jupiter.api.Test; |
7 |
| - |
8 | 3 | import io.fabric8.kubernetes.api.model.ConfigMap;
|
9 | 4 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
10 | 5 | import io.fabric8.kubernetes.api.model.apps.Deployment;
|
| 6 | +import io.fabric8.kubernetes.api.model.apps.StatefulSet; |
11 | 7 | import io.javaoperatorsdk.operator.MockKubernetesClient;
|
12 | 8 | import io.javaoperatorsdk.operator.ReconcilerUtils;
|
13 | 9 | import io.javaoperatorsdk.operator.api.config.ConfigurationService;
|
14 | 10 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
|
15 | 11 | import io.javaoperatorsdk.operator.api.reconciler.Context;
|
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.ValueSource; |
| 16 | + |
| 17 | +import java.util.Map; |
16 | 18 |
|
17 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
18 | 20 | import static org.mockito.Mockito.mock;
|
19 | 21 | import static org.mockito.Mockito.when;
|
20 | 22 |
|
21 | 23 | class SSABasedGenericKubernetesResourceMatcherTest {
|
22 | 24 |
|
23 |
| - Context<?> mockedContext = mock(Context.class); |
| 25 | + private final Context<?> mockedContext = mock(); |
24 | 26 |
|
25 |
| - SSABasedGenericKubernetesResourceMatcher<HasMetadata> matcher = |
26 |
| - new SSABasedGenericKubernetesResourceMatcher<>(); |
| 27 | + private final SSABasedGenericKubernetesResourceMatcher<HasMetadata> matcher = |
| 28 | + SSABasedGenericKubernetesResourceMatcher.getInstance(); |
27 | 29 |
|
28 | 30 | @BeforeEach
|
29 | 31 | @SuppressWarnings("unchecked")
|
30 | 32 | void setup() {
|
31 |
| - var controllerConfiguration = mock(ControllerConfiguration.class); |
32 |
| - when(controllerConfiguration.fieldManager()).thenReturn("controller"); |
33 |
| - var configurationService = mock(ConfigurationService.class); |
34 |
| - |
35 | 33 | final var client = MockKubernetesClient.client(HasMetadata.class);
|
36 | 34 | when(mockedContext.getClient()).thenReturn(client);
|
37 | 35 |
|
| 36 | + final var configurationService = mock(ConfigurationService.class); |
| 37 | + final var controllerConfiguration = mock(ControllerConfiguration.class); |
38 | 38 | when(controllerConfiguration.getConfigurationService()).thenReturn(configurationService);
|
| 39 | + when(controllerConfiguration.fieldManager()).thenReturn("controller"); |
39 | 40 | when(mockedContext.getControllerConfiguration()).thenReturn(controllerConfiguration);
|
40 | 41 | }
|
41 | 42 |
|
@@ -116,9 +117,33 @@ void addedLabelInDesiredMakesMatchFail() {
|
116 | 117 | assertThat(matcher.matches(actualConfigMap, desiredConfigMap, mockedContext)).isFalse();
|
117 | 118 | }
|
118 | 119 |
|
119 |
| - private <R> R loadResource(String fileName, Class<R> clazz) { |
| 120 | + @ParameterizedTest |
| 121 | + @ValueSource(strings = {"sample-sts-volumeclaimtemplates-desired.yaml", |
| 122 | + "sample-sts-volumeclaimtemplates-desired-with-status.yaml", |
| 123 | + "sample-sts-volumeclaimtemplates-desired-with-volumemode.yaml"}) |
| 124 | + void testSanitizeState_statefulSetWithVolumeClaims(String desiredResourceFileName) { |
| 125 | + var desiredStatefulSet = loadResource(desiredResourceFileName, StatefulSet.class); |
| 126 | + var actualStatefulSet = loadResource("sample-sts-volumeclaimtemplates.yaml", |
| 127 | + StatefulSet.class); |
| 128 | + |
| 129 | + assertThat(matcher.matches(actualStatefulSet, desiredStatefulSet, mockedContext)).isTrue(); |
| 130 | + } |
| 131 | + |
| 132 | + @ParameterizedTest |
| 133 | + @ValueSource(strings = {"sample-sts-volumeclaimtemplates-desired-add.yaml", |
| 134 | + "sample-sts-volumeclaimtemplates-desired-update.yaml", |
| 135 | + "sample-sts-volumeclaimtemplates-desired-with-status-mismatch.yaml", |
| 136 | + "sample-sts-volumeclaimtemplates-desired-with-volumemode-mismatch.yaml"}) |
| 137 | + void testSanitizeState_statefulSetWithVolumeClaims_withMismatch(String desiredResourceFileName) { |
| 138 | + var desiredStatefulSet = loadResource(desiredResourceFileName, StatefulSet.class); |
| 139 | + var actualStatefulSet = loadResource("sample-sts-volumeclaimtemplates.yaml", |
| 140 | + StatefulSet.class); |
| 141 | + |
| 142 | + assertThat(matcher.matches(actualStatefulSet, desiredStatefulSet, mockedContext)).isFalse(); |
| 143 | + } |
| 144 | + |
| 145 | + private static <R> R loadResource(String fileName, Class<R> clazz) { |
120 | 146 | return ReconcilerUtils.loadYaml(clazz, SSABasedGenericKubernetesResourceMatcherTest.class,
|
121 | 147 | fileName);
|
122 | 148 | }
|
123 |
| - |
124 | 149 | }
|
0 commit comments