Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to specify resource readiness timeout #234

Merged
merged 3 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public interface TestFrameConstants {
*/
long GLOBAL_TIMEOUT = Duration.ofMinutes(10).toMillis();

/**
* Global timeout in milliseconds.
*/
long GLOBAL_TIMEOUT_SHORT = Duration.ofMinutes(5).toMillis();

/**
* Stability timeout in milliseconds
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.skodjob.testframe.TestFrameConstants;

/**
* Class for encapsulating methods related to {@link T} resource.
Expand All @@ -30,6 +31,16 @@ public interface ResourceType<T extends HasMetadata> {
*/
String getKind();

/**
* Timeout for resource readiness.
* Defaults to {@link TestFrameConstants#GLOBAL_TIMEOUT_SHORT}.
*
* @return timeout for resource readiness
*/
default Long getTimeoutForResourceReadiness() {
return TestFrameConstants.GLOBAL_TIMEOUT_SHORT;
}

/**
* Creates specific {@link T} resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Stack;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -386,8 +387,10 @@ private <T extends HasMetadata> void createOrUpdateResource(boolean async,
type.create(resource);
}
if (waitReady) {
long resourceTimeout = Objects.requireNonNullElse(type.getTimeoutForResourceReadiness(),
TestFrameConstants.GLOBAL_TIMEOUT_SHORT);
CompletableFuture<Void> c = CompletableFuture.runAsync(() ->
assertTrue(waitResourceCondition(resource, ResourceCondition.readiness(type)),
assertTrue(waitResourceCondition(resource, ResourceCondition.readiness(type), resourceTimeout),
String.format("Timed out waiting for %s/%s in %s to be ready", resource.getKind(),
resource.getMetadata().getName(), resource.getMetadata().getNamespace())));
if (async) {
Expand Down Expand Up @@ -474,12 +477,29 @@ public final <T extends HasMetadata> void updateResource(T... resources) {
/**
* Waits for a resource condition to be fulfilled.
*
* @param resource The resource to wait for.
* @param condition The condition to fulfill.
* @param <T> The type of the resource.
* @param resource The resource to wait for.
* @param condition The condition to fulfill.
* @param <T> The type of the resource.
* @return True if the condition is fulfilled, false otherwise.
*/
public final <T extends HasMetadata> boolean waitResourceCondition(T resource, ResourceCondition<T> condition) {
return waitResourceCondition(resource, condition, TestFrameConstants.GLOBAL_TIMEOUT);
}

/**
* Waits for a resource condition to be fulfilled.
*
* @param resource The resource to wait for.
* @param condition The condition to fulfill.
* @param <T> The type of the resource.
* @param resourceTimeout Timeout for resource condition
* @return True if the condition is fulfilled, false otherwise.
*/
public final <T extends HasMetadata> boolean waitResourceCondition(
T resource,
ResourceCondition<T> condition,
long resourceTimeout
) {
assertNotNull(resource);
assertNotNull(resource.getMetadata());
assertNotNull(resource.getMetadata().getName());
Expand All @@ -488,7 +508,7 @@ public final <T extends HasMetadata> boolean waitResourceCondition(T resource, R

Wait.until(String.format("Resource condition: %s to be fulfilled for resource %s/%s",
condition.conditionName(), resource.getKind(), resource.getMetadata().getName()),
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, TestFrameConstants.GLOBAL_TIMEOUT,
TestFrameConstants.GLOBAL_POLL_INTERVAL_MEDIUM, resourceTimeout,
() -> {
T res = kubeClient().getClient().resource(resource).get();
resourceReady[0] = condition.predicate().test(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.fabric8.kubernetes.api.model.apps.DeploymentList;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.RollableScalableResource;
import io.skodjob.testframe.TestFrameConstants;
import io.skodjob.testframe.interfaces.ResourceType;

/**
Expand All @@ -36,6 +37,16 @@ public String getKind() {
return "Deployment";
}

/**
* Timeout for resource readiness
*
* @return timeout for resource readiness
*/
@Override
public Long getTimeoutForResourceReadiness() {
return TestFrameConstants.GLOBAL_TIMEOUT;
}

/**
* Get specific client for resoruce
*
Expand Down