|
| 1 | +/* |
| 2 | + * Copyright Java Operator SDK Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.javaoperatorsdk.operator.api.reconciler.matcher; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 21 | +import io.fabric8.kubernetes.api.model.apps.Deployment; |
| 22 | +import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; |
| 23 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 24 | +import io.javaoperatorsdk.operator.MockKubernetesClient; |
| 25 | +import io.javaoperatorsdk.operator.api.reconciler.Context; |
| 26 | +import io.javaoperatorsdk.operator.api.reconciler.DefaultContext; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | + |
| 31 | +class PatchMatchersTest { |
| 32 | + |
| 33 | + private static final Context<?> context = new TestContext(); |
| 34 | + |
| 35 | + private final JsonPatchMacher jsonPatchMatcher = JsonPatchMacher.getInstance(); |
| 36 | + private final JsonMergePatchMatcher mergePatchMatcher = JsonMergePatchMatcher.getInstance(); |
| 37 | + private final JsonPatchStatusMacher jsonPatchStatusMatcher = JsonPatchStatusMacher.getInstance(); |
| 38 | + private final JsonMergePatchStatusMatcher mergePatchStatusMatcher = |
| 39 | + JsonMergePatchStatusMatcher.getInstance(); |
| 40 | + |
| 41 | + // ---- JSON Patch (whole resource) ---- |
| 42 | + |
| 43 | + @Test |
| 44 | + void jsonPatchMatchesIdenticalResources() { |
| 45 | + assertThat(jsonPatchMatcher.matches(deployment(), deployment(), context)).isTrue(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void jsonPatchDoesNotMatchWhenActualHasAdditionalField() { |
| 50 | + var actual = deployment(); |
| 51 | + actual.getMetadata().getLabels().put("extra", "value"); |
| 52 | + assertThat(jsonPatchMatcher.matches(deployment(), actual, context)).isFalse(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void jsonPatchDoesNotMatchWhenDesiredHasAdditionalField() { |
| 57 | + var desired = deployment(); |
| 58 | + desired.getMetadata().getLabels().put("extra", "value"); |
| 59 | + assertThat(jsonPatchMatcher.matches(desired, deployment(), context)).isFalse(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void jsonPatchDoesNotMatchOnDifferentValue() { |
| 64 | + var desired = deployment(); |
| 65 | + desired.getSpec().setReplicas(5); |
| 66 | + assertThat(jsonPatchMatcher.matches(desired, deployment(), context)).isFalse(); |
| 67 | + } |
| 68 | + |
| 69 | + // ---- JSON Merge Patch (whole resource) ---- |
| 70 | + |
| 71 | + @Test |
| 72 | + void mergePatchMatchesIdenticalResources() { |
| 73 | + assertThat(mergePatchMatcher.matches(deployment(), deployment(), context)).isTrue(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void mergePatchMatchesWhenActualHasAdditionalField() { |
| 78 | + // a merge patch of the desired state leaves fields only present in the actual state untouched |
| 79 | + var actual = deployment(); |
| 80 | + actual.getMetadata().getLabels().put("extra", "value"); |
| 81 | + assertThat(mergePatchMatcher.matches(deployment(), actual, context)).isTrue(); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void mergePatchDoesNotMatchWhenDesiredHasAdditionalField() { |
| 86 | + var desired = deployment(); |
| 87 | + desired.getMetadata().getLabels().put("extra", "value"); |
| 88 | + assertThat(mergePatchMatcher.matches(desired, deployment(), context)).isFalse(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void mergePatchDoesNotMatchOnDifferentValue() { |
| 93 | + var desired = deployment(); |
| 94 | + desired.getSpec().setReplicas(5); |
| 95 | + assertThat(mergePatchMatcher.matches(desired, deployment(), context)).isFalse(); |
| 96 | + } |
| 97 | + |
| 98 | + // ---- Status matchers ---- |
| 99 | + |
| 100 | + @Test |
| 101 | + void statusMatchersIgnoreChangesOutsideStatus() { |
| 102 | + var desired = deploymentWithStatus(); |
| 103 | + desired.getSpec().setReplicas(5); |
| 104 | + var actual = deploymentWithStatus(); |
| 105 | + assertThat(jsonPatchStatusMatcher.matches(desired, actual, context)).isTrue(); |
| 106 | + assertThat(mergePatchStatusMatcher.matches(desired, actual, context)).isTrue(); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + void statusMatchersDoNotMatchOnDifferentStatus() { |
| 111 | + var desired = deploymentWithStatus(); |
| 112 | + desired.getStatus().setReplicas(9); |
| 113 | + var actual = deploymentWithStatus(); |
| 114 | + assertThat(jsonPatchStatusMatcher.matches(desired, actual, context)).isFalse(); |
| 115 | + assertThat(mergePatchStatusMatcher.matches(desired, actual, context)).isFalse(); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void statusMatchersDifferOnAdditionalActualStatusField() { |
| 120 | + var actual = deploymentWithStatus(); |
| 121 | + actual.getStatus().setAvailableReplicas(1); |
| 122 | + var desired = deploymentWithStatus(); |
| 123 | + |
| 124 | + // json patch sees the extra actual field as a removal, so it does not match |
| 125 | + assertThat(jsonPatchStatusMatcher.matches(desired, actual, context)).isFalse(); |
| 126 | + // merge patch tolerates fields only present in the actual state |
| 127 | + assertThat(mergePatchStatusMatcher.matches(desired, actual, context)).isTrue(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void statusMatchersMatchWhenBothStatusesAbsent() { |
| 132 | + assertThat(jsonPatchStatusMatcher.matches(deployment(), deployment(), context)).isTrue(); |
| 133 | + assertThat(mergePatchStatusMatcher.matches(deployment(), deployment(), context)).isTrue(); |
| 134 | + } |
| 135 | + |
| 136 | + private static Deployment deployment() { |
| 137 | + return new DeploymentBuilder() |
| 138 | + .withNewMetadata() |
| 139 | + .withName("test") |
| 140 | + .withNamespace("default") |
| 141 | + .addToLabels("app", "test") |
| 142 | + .endMetadata() |
| 143 | + .withNewSpec() |
| 144 | + .withReplicas(1) |
| 145 | + .endSpec() |
| 146 | + .build(); |
| 147 | + } |
| 148 | + |
| 149 | + private static Deployment deploymentWithStatus() { |
| 150 | + var deployment = deployment(); |
| 151 | + deployment.setStatus(new io.fabric8.kubernetes.api.model.apps.DeploymentStatus()); |
| 152 | + deployment.getStatus().setReplicas(1); |
| 153 | + return deployment; |
| 154 | + } |
| 155 | + |
| 156 | + private static class TestContext extends DefaultContext<HasMetadata> { |
| 157 | + private final KubernetesClient client = MockKubernetesClient.client(HasMetadata.class); |
| 158 | + |
| 159 | + TestContext() { |
| 160 | + super(mock(), mock(), null, false, false); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public KubernetesClient getClient() { |
| 165 | + return client; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments