Skip to content

Commit

Permalink
improve: resource creation api in JUnit extension
Browse files Browse the repository at this point in the history
Signed-off-by: Attila Mészáros <[email protected]>
  • Loading branch information
csviri committed Jan 31, 2024
1 parent 8342b91 commit 9b26d18
Show file tree
Hide file tree
Showing 42 changed files with 68 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void updateTestResources() {
IntStream.range(0, NUMBER_OF_RESOURCE_TO_TEST).forEach(i -> {
var cm = extension().get(ConfigMap.class, RESOURCE_NAME_PREFIX + i);
cm.getData().put(DATA_KEY, UPDATED_PREFIX + i);
extension().replace(cm);
extension().createOrUpdate(cm);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.NonDeletingOperation;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.utils.Utils;
Expand Down Expand Up @@ -107,6 +108,16 @@ public <T extends HasMetadata> T get(Class<T> type, String name) {
return kubernetesClient.resources(type).inNamespace(namespace).withName(name).get();
}

public <T extends HasMetadata> T createOr(T resource,
Function<NonDeletingOperation<T>, T> conflictAction) {
return kubernetesClient.resource(resource).inNamespace(namespace).createOr(conflictAction);
}

public <T extends HasMetadata> T createOrUpdate(T resource) {
return kubernetesClient.resource(resource).inNamespace(namespace)
.createOr(NonDeletingOperation::update);
}

public <T extends HasMetadata> T create(T resource) {
return kubernetesClient.resource(resource).inNamespace(namespace).create();
}
Expand All @@ -116,6 +127,10 @@ public <T extends HasMetadata> T create(Class<T> type, T resource) {
return create(resource);
}

/**
* Use createOrUpdate instead.
*/
@Deprecated(forRemoval = true)
public <T extends HasMetadata> T replace(T resource) {
return kubernetesClient.resource(resource).inNamespace(namespace).replace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void addNewAndRemoveOldNamespaceTest() {

ConfigMap firstMap = operator.get(ConfigMap.class, TEST_RESOURCE_NAME_1);
firstMap.setData(Map.of("data", "newdata"));
operator.replace(firstMap);
operator.createOrUpdate(firstMap);
assertReconciled(reconciler, defaultNamespaceResource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void cleanupRemovesFinalizerWithoutConflict() throws InterruptedException {
testResource = operator.get(CleanupConflictCustomResource.class, TEST_RESOURCE_NAME);
testResource.getMetadata().getFinalizers().remove(ADDITIONAL_FINALIZER);
testResource.getMetadata().setResourceVersion(null);
operator.replace(testResource);
operator.createOrUpdate(testResource);

await().pollDelay(Duration.ofMillis(WAIT_TIME * 2)).untilAsserted(
() -> assertThat(operator.getReconcilerOfType(CleanupConflictReconciler.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void crudOperationOnClusterScopedCustomResource() {
});

resource.getSpec().setData(UPDATED_DATA);
operator.replace(resource);
operator.createOrUpdate(resource);
await().untilAsserted(() -> {
var cm = operator.get(ConfigMap.class, TEST_NAME);
assertThat(cm).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void updateEventNotReceivedAfterCreateOrUpdate() {
operator.get(CreateUpdateEventFilterTestCustomResource.class,
resource.getMetadata().getName());
actualCreatedResource.getSpec().setValue("2");
operator.replace(actualCreatedResource);
operator.createOrUpdate(actualCreatedResource);

assertData(operator, actualCreatedResource, 2, 2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void mapsSecondaryByAnnotation() {
assertThat(configMap.getMetadata().getOwnerReferences()).isEmpty();

configMap.getData().put("additional_data", "data");
operator.replace(configMap);
operator.createOrUpdate(configMap);

await().pollDelay(Duration.ofMillis(150))
.untilAsserted(() -> assertThat(reconciler.getNumberOfExecutions()).isEqualTo(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void testCustomMappingAnnotationForDependent() {
assertConfigMapData(INITIAL_VALUE);

cr.getSpec().setValue(CHANGED_VALUE);
cr = extension.replace(cr);
cr = extension.createOrUpdate(cr);
assertConfigMapData(CHANGED_VALUE);

extension.delete(cr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void managesCRUDOperationsForDependentInDifferentNamespace() {
});

resource.getSpec().setValue(CHANGED_VALUE);
resource = extension.replace(resource);
resource = extension.createOrUpdate(resource);

await().untilAsserted(() -> {
var cm = getDependentConfigMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void filtersUpdateOnConfigMap() {

var configMap = operator.get(ConfigMap.class, RESOURCE_NAME);
configMap.setData(Map.of(CM_VALUE_KEY, CONFIG_MAP_FILTER_VALUE));
operator.replace(configMap);
operator.createOrUpdate(configMap);

await().pollDelay(Duration.ofMillis(150)).untilAsserted(() -> {
assertThat(operator.getReconcilerOfType(DependentFilterTestReconciler.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void reconcileNotTriggeredWithDependentResourceCreateOrUpdate() {
.containsEntry(ConfigMapDependentResource.KEY, SPEC_VAL_1);

resource.getSpec().setValue(SPEC_VAL_2);
operator.replace(resource);
operator.createOrUpdate(resource);

await().pollDelay(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(3))
.until(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void testMatchingAndUpdate() {
});

resource.getSpec().setValue(CHANGED_VALUE);
extension.replace(resource);
extension.createOrUpdate(resource);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, TEST_RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void registersEventSourcesDynamically() {
var cm = extension.get(ConfigMap.class, TEST_RESOURCE_NAME);
cm.getData().put("key2", "val2");

extension.replace(cm); // triggers the reconciliation
extension.createOrUpdate(cm); // triggers the reconciliation

await().untilAsserted(() -> {
assertThat(reconciler.getNumberOfExecutions() - executions).isEqualTo(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ void reconcilesResourceWithPersistentState() throws InterruptedException {
assertResources(resource, INITIAL_TEST_DATA, INITIAL_BULK_SIZE);

resource.getSpec().setData(UPDATED_DATA);
resource = operator.replace(resource);
resource = operator.createOrUpdate(resource);
assertResources(resource, UPDATED_DATA, INITIAL_BULK_SIZE);

resource.getSpec().setNumber(INCREASED_BULK_SIZE);
resource = operator.replace(resource);
resource = operator.createOrUpdate(resource);
assertResources(resource, UPDATED_DATA, INCREASED_BULK_SIZE);

resource.getSpec().setNumber(DECREASED_BULK_SIZE);
resource = operator.replace(resource);
resource = operator.createOrUpdate(resource);
assertResources(resource, UPDATED_DATA, DECREASED_BULK_SIZE);

operator.delete(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void reconcilesResourceWithPersistentState() {
assertResourcesCreated(resource, INITIAL_TEST_DATA);

resource.getSpec().setData(UPDATED_DATA);
operator.replace(resource);
operator.createOrUpdate(resource);
assertResourcesCreated(resource, UPDATED_DATA);

operator.delete(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void reconcilesResourceWithPersistentState() {
assertResourcesCreated(resource, INITIAL_TEST_DATA);

resource.getSpec().setData(UPDATED_DATA);
extension().replace(resource);
extension().createOrUpdate(resource);
assertResourcesCreated(resource, UPDATED_DATA);

extension().delete(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void filtersControllerResourceUpdate() {
.getNumberOfExecutions()).isEqualTo(2));

res.getSpec().setValue(FilterTestReconciler.CUSTOM_RESOURCE_FILTER_VALUE);
operator.replace(res);
operator.createOrUpdate(res);

// not more reconciliation with the filtered value
await().pollDelay(Duration.ofMillis(POLL_DELAY))
Expand All @@ -51,7 +51,7 @@ void filtersSecondaryResourceUpdate() {
.getNumberOfExecutions()).isEqualTo(2));

res.getSpec().setValue(CONFIG_MAP_FILTER_VALUE);
operator.replace(res);
operator.createOrUpdate(res);

// the CM event filtered out
await().pollDelay(Duration.ofMillis(POLL_DELAY))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void testReconciliation() {
});

resource.getSpec().setValue(CHANGED_DATA);
resource = extension().replace(resource);
resource = extension().createOrUpdate(resource);

await().untilAsserted(() -> {
var cm = extension().get(ConfigMap.class, TEST_RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void testGracefulStop(String resourceName, int stopTimeout, int expected
});

testRes.getSpec().setValue(2);
operator.replace(testRes);
operator.createOrUpdate(testRes);

await().pollDelay(Duration.ofMillis(50)).untilAsserted(
() -> assertThat(operator.getReconcilerOfType(GracefulStopTestReconciler.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void resourcesFoundAndReconciled() {
});

res.getSpec().setValue(CHANGED_SPEC_VALUE);
res = operator.replace(res);
res = operator.createOrUpdate(res);

await().untilAsserted(() -> {
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void testUsingInformerToWatchChangesOfConfigMap() {
waitForCRStatusValue(INITIAL_STATUS_MESSAGE);

configMap.getData().put(TARGET_CONFIG_MAP_KEY, UPDATE_STATUS_MESSAGE);
operator.replace(configMap);
operator.createOrUpdate(configMap);

waitForCRStatusValue(UPDATE_STATUS_MESSAGE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void deletesSecondaryResource() {
});

createdResources.getSpec().setCreateConfigMap(false);
operator.replace(createdResources);
operator.createOrUpdate(createdResources);

await().untilAsserted(() -> {
ConfigMap cm = operator.get(ConfigMap.class, TEST_RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void resourceNotDeletedUntilDependentDeleted() {

var secret = extension.get(Secret.class, RESOURCE_NAME);
secret.getMetadata().getFinalizers().add(CUSTOM_FINALIZER);
secret = extension.replace(secret);
secret = extension.createOrUpdate(secret);

extension.delete(resource);

Expand All @@ -58,7 +58,7 @@ void resourceNotDeletedUntilDependentDeleted() {
});

secret.getMetadata().getFinalizers().clear();
extension.replace(secret);
extension.createOrUpdate(secret);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void multiOwnerTriggeringAndManagement() {
});

res1.getSpec().setValue(NEW_VALUE_1);
extension.replace(res1);
extension.createOrUpdate(res1);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, MultipleOwnerDependentConfigMap.RESOURCE_NAME);
Expand All @@ -59,7 +59,7 @@ void multiOwnerTriggeringAndManagement() {
});

res2.getSpec().setValue(NEW_VALUE_2);
extension.replace(res2);
extension.createOrUpdate(res2);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, MultipleOwnerDependentConfigMap.RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void handlesCrudOperations() {

var updatedResource = testResource();
updatedResource.getSpec().setValue(UPDATED_SPEC_VALUE);
operator.replace(updatedResource);
operator.createOrUpdate(updatedResource);
assertConfigMapsPresent(UPDATED_SPEC_VALUE);

operator.delete(testResource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void handlesCrudOperations() {

var updatedResource = testResource();
updatedResource.getSpec().setValue(UPDATED_SPEC_VALUE);
operator.replace(updatedResource);
operator.createOrUpdate(updatedResource);
assertConfigMapsPresent(UPDATED_SPEC_VALUE);

operator.delete(testResource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void handlesExternalCrudOperations() {

var updatedResource = testResource();
updatedResource.getSpec().setValue(UPDATED_SPEC_VALUE);
operator.replace(updatedResource);
operator.createOrUpdate(updatedResource);
assertResourceCreatedWithData(UPDATED_SPEC_VALUE);

operator.delete(testResource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void updateConfigMap(MultipleSecondaryEventSourceCustomResource resource
number == 1 ? MultipleSecondaryEventSourceReconciler.getName1(resource)
: MultipleSecondaryEventSourceReconciler.getName2(resource));
map1.getData().put("value2", "value2");
operator.replace(map1);
operator.createOrUpdate(map1);
}

public MultipleSecondaryEventSourceCustomResource createTestCustomResource() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void updateEventReceivedAfterCreateOrUpdate() {
operator.get(CreateUpdateEventFilterTestCustomResource.class,
resource.getMetadata().getName());
actualCreatedResource.getSpec().setValue("2");
operator.replace(actualCreatedResource);
operator.createOrUpdate(actualCreatedResource);

CreateUpdateInformerEventSourceEventFilterIT.assertData(operator, actualCreatedResource, 2, 4);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testPrimaryToSecondaryInDependentResources() {

cm.setData(Map.of(DATA_KEY, TEST_DATA));
var executions = reconciler.getNumberOfExecutions();
operator.replace(cm);
operator.createOrUpdate(cm);

await().pollDelay(Duration.ofMillis(250)).untilAsserted(() -> {
assertThat(reconciler.getNumberOfExecutions()).isGreaterThan(executions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void rateLimitsExecution() {
log.debug("replacing resource version: {}", i);
var resource = createResource();
resource.getSpec().setNumber(i);
operator.replace(resource);
operator.createOrUpdate(resource);
});
await().pollInterval(Duration.ofMillis(100))
.pollDelay(Duration.ofMillis(REFRESH_PERIOD / 2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void testTheMatchingDoesNoTTriggersFurtherUpdates() {

// make an update to spec to reconcile again
resource.getSpec().setValue(2);
operator.replace(resource);
operator.createOrUpdate(resource);

await().pollDelay(Duration.ofMillis(300)).untilAsserted(() -> {
assertThat(operator.getReconcilerOfType(ServiceStrictMatcherTestReconciler.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void specialCRUDReconciler() {
});

resource.getSpec().setValue(CHANGED_VALUE);
extension.replace(resource);
extension.createOrUpdate(resource);

await().untilAsserted(() -> {
var sa = extension.get(ServiceAccount.class, RESOURCE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void executeUpdateForTestingCacheUpdateForGetResource() {

var clonedCr = cloner().clone(createdCR);
clonedCr.getSpec().setReplicaCount(2);
operator.replace(clonedCr);
operator.createOrUpdate(clonedCr);

awaitForDeploymentReadyReplicas(2);
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void testSSAMatcher() {
});
// make sure reconciliation happens at least once more
resource.getSpec().setValue("changed value");
extension.replace(resource);
extension.createOrUpdate(resource);

await().untilAsserted(
() -> assertThat(StatefulSetDesiredSanitizerDependentResource.nonMatchedAtLeastOnce)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void noOptimisticLockingDoneOnStatusUpdate() throws InterruptedException {
var resource = operator.create(createResource());
Thread.sleep(WAIT_TIME / 2);
resource.getMetadata().setAnnotations(Map.of("key", "value"));
operator.replace(resource);
operator.createOrUpdate(resource);

await().pollDelay(Duration.ofMillis(WAIT_TIME)).untilAsserted(() -> {
assertThat(
Expand Down Expand Up @@ -59,7 +59,7 @@ void valuesAreDeletedIfSetToNull() {
});

resource.getSpec().setMessageInStatus(false);
operator.replace(resource);
operator.createOrUpdate(resource);

await().untilAsserted(() -> {
var actual = operator.get(StatusPatchLockingCustomResource.class,
Expand Down
Loading

0 comments on commit 9b26d18

Please sign in to comment.