Skip to content

Commit 6e5dbd5

Browse files
committed
tests
1 parent 30bddf1 commit 6e5dbd5

File tree

2 files changed

+80
-17
lines changed

2 files changed

+80
-17
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcher.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ static <R extends HasMetadata, P extends HasMetadata> Matcher<R, P> matcherFor(
3030
@Override
3131
public Result<R> match(R actualResource, P primary, Context<P> context) {
3232
var desired = dependentResource.desired(primary, context);
33-
return match(desired, actualResource, false, false);
33+
return match(desired, actualResource, true, false,
34+
false, Collections.emptyList());
3435
}
3536

3637
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
@@ -64,18 +65,27 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc
6465
*/
6566
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
6667
boolean considerMetadata, boolean equality) {
67-
return match(desired, actualResource, considerMetadata, equality, Collections.emptyList());
68+
return match(desired, actualResource, considerMetadata, false, equality,
69+
Collections.emptyList());
6870
}
6971

7072
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
7173
boolean considerMetadata, String... ignoreList) {
72-
return match(desired, actualResource, considerMetadata, false, Arrays.asList(ignoreList));
74+
return match(desired, actualResource, considerMetadata, false, false,
75+
Arrays.asList(ignoreList));
7376
}
7477

78+
public static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
79+
boolean considerMetadata, boolean metadataEquality, String... ignoreList) {
80+
return match(desired, actualResource, considerMetadata, metadataEquality, false,
81+
Arrays.asList(ignoreList));
82+
}
7583

7684
private static <R extends HasMetadata> Result<R> match(R desired, R actualResource,
77-
boolean considerMetadata, boolean equality, List<String> ignoreList) {
78-
if (equality && !ignoreList.isEmpty()) {
85+
boolean considerMetadata, boolean metadataEquality, boolean specEquality,
86+
List<String> ignoreList) {
87+
88+
if (specEquality && !ignoreList.isEmpty()) {
7989
throw new IllegalArgumentException(
8090
"Equality should be false in case of ignore list provided");
8191
}
@@ -86,10 +96,10 @@ private static <R extends HasMetadata> Result<R> match(R desired, R actualResour
8696
var actualNode = objectMapper.valueToTree(actualResource);
8797
var wholeDiffJsonPatch = JsonDiff.asJson(desiredNode, actualNode);
8898

89-
var considerIgnoreList = !equality && !ignoreList.isEmpty();
99+
var considerIgnoreList = !specEquality && !ignoreList.isEmpty();
90100

91101
if (considerMetadata) {
92-
if (equality) {
102+
if (metadataEquality) {
93103
final var desiredMetadata = desired.getMetadata();
94104
final var actualMetadata = actualResource.getMetadata();
95105

@@ -123,7 +133,7 @@ private static <R extends HasMetadata> Result<R> match(R desired, R actualResour
123133
// In case of equality is set to true, no diffs are allowed, so we return early if diffs exist
124134
// On contrary (if equality is false), "add" is allowed for cases when for some
125135
// resources Kubernetes fills-in values into spec.
126-
if (equality && !specDiffJsonPatch.isEmpty()) {
136+
if (specEquality && !specDiffJsonPatch.isEmpty()) {
127137
return Result.computed(false, desired);
128138
}
129139
if (considerIgnoreList) {
@@ -216,8 +226,11 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
216226
* @param <R> the type of resource we want to determine whether they match or not
217227
* @param <P> the type of primary resources associated with the secondary resources we want to
218228
* match
219-
* @param ignorePaths are paths in the resource that are ignored on matching. Anny related change
220-
* on a calculated JSON Patch between actual and desired will be ignored.
229+
* @param ignorePaths are paths in the resource that are ignored on matching (basically an ignore
230+
* list). All changes with a target prefix path on a calculated JSON Patch between actual
231+
* and desired will be ignored. If there are other changes, non-present on ignore list
232+
* match fails.
233+
*
221234
*/
222235
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
223236
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
@@ -226,10 +239,27 @@ public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
226239
return match(desired, actualResource, considerMetadata, ignorePaths);
227240
}
228241

242+
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
243+
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
244+
Context<P> context, boolean considerMetadata, boolean metadataEquality,
245+
String... ignorePaths) {
246+
final var desired = dependentResource.desired(primary, context);
247+
return match(desired, actualResource, considerMetadata, metadataEquality, ignorePaths);
248+
}
249+
229250
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
230251
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
231252
Context<P> context, boolean considerMetadata) {
232253
final var desired = dependentResource.desired(primary, context);
233254
return match(desired, actualResource, considerMetadata, false);
234255
}
256+
257+
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
258+
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
259+
Context<P> context, boolean considerMetadata, boolean metadataEquality,
260+
boolean strongEquality) {
261+
final var desired = dependentResource.desired(primary, context);
262+
return match(desired, actualResource, considerMetadata, metadataEquality, strongEquality,
263+
Collections.emptyList());
264+
}
235265
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcherTest.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.javaoperatorsdk.operator.ReconcilerUtils;
1212
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
1313
import io.javaoperatorsdk.operator.api.reconciler.Context;
14+
import io.javaoperatorsdk.operator.processing.dependent.Matcher;
1415

1516
import static org.assertj.core.api.Assertions.assertThat;
1617
import static org.mockito.Mockito.mock;
@@ -21,49 +22,81 @@ class GenericKubernetesResourceMatcherTest {
2122

2223
private static final Context context = mock(Context.class);
2324

25+
Deployment actual = createDeployment();
26+
Deployment desired = createDeployment();
27+
TestDependentResource dependentResource = new TestDependentResource(desired);
28+
Matcher matcher =
29+
GenericKubernetesResourceMatcher.matcherFor(Deployment.class, dependentResource);
30+
2431
@BeforeAll
2532
static void setUp() {
2633
final var controllerConfiguration = mock(ControllerConfiguration.class);
2734
when(context.getControllerConfiguration()).thenReturn(controllerConfiguration);
2835
}
2936

3037
@Test
31-
void checksIfDesiredValuesAreTheSame() {
32-
var actual = createDeployment();
33-
final var desired = createDeployment();
34-
final var dependentResource = new TestDependentResource(desired);
35-
final var matcher =
36-
GenericKubernetesResourceMatcher.matcherFor(Deployment.class, dependentResource);
38+
void matchesTrivialCases() {
3739
assertThat(matcher.match(actual, null, context).matched()).isTrue();
3840
assertThat(matcher.match(actual, null, context).computedDesired()).isPresent();
3941
assertThat(matcher.match(actual, null, context).computedDesired()).contains(desired);
42+
}
4043

44+
@Test
45+
void matchesAdditiveOnlyChanges() {
4146
actual.getSpec().getTemplate().getMetadata().getLabels().put("new-key", "val");
4247
assertThat(matcher.match(actual, null, context).matched())
4348
.withFailMessage("Additive changes should be ok")
4449
.isTrue();
50+
}
4551

52+
@Test
53+
void matchesWithStrongSpecEquality() {
54+
actual.getSpec().getTemplate().getMetadata().getLabels().put("new-key", "val");
4655
assertThat(GenericKubernetesResourceMatcher
4756
.match(dependentResource, actual, null, context, true, true).matched())
4857
.withFailMessage("Strong equality does not ignore additive changes on spec")
4958
.isFalse();
59+
}
5060

61+
@Test
62+
void notMatchesRemovedValues() {
5163
actual = createDeployment();
5264
assertThat(matcher.match(actual, createPrimary("removed"), context).matched())
5365
.withFailMessage("Removed value should not be ok")
5466
.isFalse();
67+
}
5568

69+
@Test
70+
void notMatchesChangedValues() {
5671
actual = createDeployment();
5772
actual.getSpec().setReplicas(2);
5873
assertThat(matcher.match(actual, null, context).matched())
5974
.withFailMessage("Changed values are not ok")
6075
.isFalse();
76+
}
6177

78+
@Test
79+
void notMatchesIgnoredPaths() {
80+
actual = createDeployment();
81+
actual.getSpec().setReplicas(2);
6282
assertThat(GenericKubernetesResourceMatcher
6383
.match(dependentResource, actual, null, context, false, "/spec/replicas").matched())
6484
.withFailMessage("Ignored paths are not matched")
6585
.isTrue();
86+
}
6687

88+
@Test
89+
void ignoresWholeSubPath() {
90+
actual = createDeployment();
91+
actual.getSpec().getTemplate().getMetadata().getLabels().put("additionak-key", "val");
92+
assertThat(GenericKubernetesResourceMatcher
93+
.match(dependentResource, actual, null, context, false, "/spec/template").matched())
94+
.withFailMessage("Ignored sub-paths are not matched")
95+
.isTrue();
96+
}
97+
98+
@Test
99+
void matchesMetadata() {
67100
actual = new DeploymentBuilder(createDeployment())
68101
.editOrNewMetadata()
69102
.addToAnnotations("test", "value")
@@ -75,7 +108,7 @@ void checksIfDesiredValuesAreTheSame() {
75108
.isTrue();
76109

77110
assertThat(GenericKubernetesResourceMatcher
78-
.match(dependentResource, actual, null, context, true, true).matched())
111+
.match(dependentResource, actual, null, context, true, true, true).matched())
79112
.withFailMessage("Annotations should matter when metadata is considered")
80113
.isFalse();
81114

0 commit comments

Comments
 (0)