-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResourceManager for OLM handling
Signed-off-by: Jakub Stejskal <[email protected]>
- Loading branch information
Showing
16 changed files
with
878 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.odh.test; | ||
|
||
import io.odh.test.framework.WaitException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.BooleanSupplier; | ||
|
||
@SuppressWarnings({"checkstyle:ClassFanOutComplexity"}) | ||
public final class TestUtils { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class); | ||
|
||
public static final String USER_PATH = System.getProperty("user.dir"); | ||
|
||
/** | ||
* Default timeout for asynchronous tests. | ||
*/ | ||
public static final int DEFAULT_TIMEOUT_DURATION = 30; | ||
|
||
/** | ||
* Default timeout unit for asynchronous tests. | ||
*/ | ||
public static final TimeUnit DEFAULT_TIMEOUT_UNIT = TimeUnit.SECONDS; | ||
|
||
private TestUtils() { | ||
// All static methods | ||
} | ||
|
||
/** | ||
* Poll the given {@code ready} function every {@code pollIntervalMs} milliseconds until it returns true, | ||
* or throw a WaitException if it doesn't return true within {@code timeoutMs} milliseconds. | ||
* @return The remaining time left until timeout occurs | ||
* (helpful if you have several calls which need to share a common timeout), | ||
* */ | ||
public static long waitFor(String description, long pollIntervalMs, long timeoutMs, BooleanSupplier ready) { | ||
return waitFor(description, pollIntervalMs, timeoutMs, ready, () -> { }); | ||
} | ||
|
||
public static long waitFor(String description, long pollIntervalMs, long timeoutMs, BooleanSupplier ready, Runnable onTimeout) { | ||
LOGGER.debug("Waiting for {}", description); | ||
long deadline = System.currentTimeMillis() + timeoutMs; | ||
|
||
String exceptionMessage = null; | ||
String previousExceptionMessage = null; | ||
|
||
// in case we are polling every 1s, we want to print exception after x tries, not on the first try | ||
// for minutes poll interval will 2 be enough | ||
int exceptionAppearanceCount = Duration.ofMillis(pollIntervalMs).toMinutes() > 0 ? 2 : Math.max((int) (timeoutMs / pollIntervalMs) / 4, 2); | ||
int exceptionCount = 0; | ||
int newExceptionAppearance = 0; | ||
|
||
StringWriter stackTraceError = new StringWriter(); | ||
|
||
while (true) { | ||
boolean result; | ||
try { | ||
result = ready.getAsBoolean(); | ||
} catch (Exception e) { | ||
exceptionMessage = e.getMessage(); | ||
|
||
if (++exceptionCount == exceptionAppearanceCount && exceptionMessage != null && exceptionMessage.equals(previousExceptionMessage)) { | ||
LOGGER.error("While waiting for {} exception occurred: {}", description, exceptionMessage); | ||
// log the stacktrace | ||
e.printStackTrace(new PrintWriter(stackTraceError)); | ||
} else if (exceptionMessage != null && !exceptionMessage.equals(previousExceptionMessage) && ++newExceptionAppearance == 2) { | ||
previousExceptionMessage = exceptionMessage; | ||
} | ||
|
||
result = false; | ||
} | ||
long timeLeft = deadline - System.currentTimeMillis(); | ||
if (result) { | ||
return timeLeft; | ||
} | ||
if (timeLeft <= 0) { | ||
if (exceptionCount > 1) { | ||
LOGGER.error("Exception waiting for {}, {}", description, exceptionMessage); | ||
|
||
if (!stackTraceError.toString().isEmpty()) { | ||
// printing handled stacktrace | ||
LOGGER.error(stackTraceError.toString()); | ||
} | ||
} | ||
onTimeout.run(); | ||
WaitException waitException = new WaitException("Timeout after " + timeoutMs + " ms waiting for " + description); | ||
waitException.printStackTrace(); | ||
throw waitException; | ||
} | ||
long sleepTime = Math.min(pollIntervalMs, timeLeft); | ||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace("{} not ready, will try again in {} ms ({}ms till timeout)", description, sleepTime, timeLeft); | ||
} | ||
try { | ||
Thread.sleep(sleepTime); | ||
} catch (InterruptedException e) { | ||
return deadline - System.currentTimeMillis(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.odh.test.framework; | ||
|
||
public class WaitException extends RuntimeException { | ||
public WaitException(String message) { | ||
super(message); | ||
} | ||
|
||
public WaitException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/io/odh/test/framework/manager/ResourceCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
|
||
package io.odh.test.framework.manager; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
public class ResourceCondition<T extends HasMetadata> { | ||
private final Predicate<T> predicate; | ||
private final String conditionName; | ||
|
||
public ResourceCondition(Predicate<T> predicate, String conditionName) { | ||
this.predicate = predicate; | ||
this.conditionName = conditionName; | ||
} | ||
|
||
public String getConditionName() { | ||
return conditionName; | ||
} | ||
|
||
public Predicate<T> getPredicate() { | ||
return predicate; | ||
} | ||
|
||
public static <T extends HasMetadata> ResourceCondition<T> readiness(ResourceType<T> type) { | ||
return new ResourceCondition<>(type::waitForReadiness, "readiness"); | ||
} | ||
|
||
public static <T extends HasMetadata> ResourceCondition<T> deletion() { | ||
return new ResourceCondition<>(Objects::isNull, "deletion"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/io/odh/test/framework/manager/ResourceItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright Skodjob authors. | ||
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). | ||
*/ | ||
package io.odh.test.framework.manager; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
|
||
public final class ResourceItem<T extends HasMetadata> { | ||
|
||
ThrowableRunner throwableRunner; | ||
T resource; | ||
|
||
public ResourceItem(ThrowableRunner throwableRunner, T resource) { | ||
this.throwableRunner = throwableRunner; | ||
this.resource = resource; | ||
} | ||
|
||
public ResourceItem(ThrowableRunner throwableRunner) { | ||
this.throwableRunner = throwableRunner; | ||
} | ||
|
||
public ThrowableRunner getThrowableRunner() { | ||
return throwableRunner; | ||
} | ||
public T getResource() { | ||
return resource; | ||
} | ||
} |
Oops, something went wrong.