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 waits to DistributedST.java to prevent flaky fails #130

Merged
merged 2 commits into from
Apr 17, 2024
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
34 changes: 34 additions & 0 deletions src/main/java/io/odh/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
Expand All @@ -30,6 +34,9 @@
import java.util.concurrent.TimeoutException;
import java.util.function.BooleanSupplier;

import static io.odh.test.TestConstants.GLOBAL_POLL_INTERVAL_SHORT;
import static io.odh.test.TestConstants.GLOBAL_TIMEOUT;

@SuppressWarnings({"checkstyle:ClassFanOutComplexity"})
public final class TestUtils {

Expand Down Expand Up @@ -122,6 +129,33 @@ public static long waitFor(String description, long pollIntervalMs, long timeout
}
}

/**
* Polls the given HTTP {@code url} until it gives != 503 status code
*/
public static void waitForServiceNotUnavailable(String url) {
final HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.followRedirects(HttpClient.Redirect.NORMAL)
.build();
waitForServiceNotUnavailable(httpClient, url);
}

public static void waitForServiceNotUnavailable(HttpClient httpClient, String url) {
TestUtils.waitFor("service to be not unavailable", GLOBAL_POLL_INTERVAL_SHORT, GLOBAL_TIMEOUT, () -> {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.GET()
.timeout(Duration.of(DEFAULT_TIMEOUT_DURATION, DEFAULT_TIMEOUT_UNIT.toChronoUnit()))
.build();
try {
HttpResponse<Void> response = httpClient.send(request, HttpResponse.BodyHandlers.discarding());
return response.statusCode() != 503; // Service Unavailable
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
});
}

private static final ExecutorService EXECUTOR = Executors.newCachedThreadPool(new ThreadFactory() {
final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();

Expand Down
50 changes: 38 additions & 12 deletions src/test/java/io/odh/test/e2e/standard/DistributedST.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.OpenShiftClient;
import io.odh.test.Environment;
import io.odh.test.OdhAnnotationsLabels;
import io.odh.test.TestUtils;
import io.odh.test.framework.manager.ResourceManager;
import io.odh.test.platform.KubeUtils;
import io.odh.test.platform.RayClient;
Expand All @@ -31,6 +34,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import static io.odh.test.TestConstants.GLOBAL_TIMEOUT;

@SuiteDoc(
description = @Desc("Verifies simple setup of ODH for distributed workloads by spin-up operator, setup DSCI, and setup DSC."),
beforeTestSteps = {
Expand Down Expand Up @@ -85,18 +93,27 @@ void testDistributedWorkload() {
final String projectName = "test-codeflare";

Allure.step("Setup resources", () -> {
Allure.step("Create namespace");
Namespace ns = new NamespaceBuilder()
.withNewMetadata()
.withName(projectName)
.addToLabels(OdhAnnotationsLabels.LABEL_DASHBOARD, "true")
.endMetadata()
.build();
ResourceManager.getInstance().createResourceWithWait(ns);

Allure.step("Create AppWrapper from yaml file");
AppWrapper koranteng = kubeClient.resources(AppWrapper.class).load(this.getClass().getResource("/codeflare/koranteng.yaml")).item();
ResourceManager.getInstance().createResourceWithWait(koranteng);
Allure.step("Create namespace", () -> {
Namespace ns = new NamespaceBuilder()
.withNewMetadata()
.withName(projectName)
.addToLabels(OdhAnnotationsLabels.LABEL_DASHBOARD, "true")
.endMetadata()
.build();
ResourceManager.getInstance().createResourceWithWait(ns);
});

Allure.step("Wait for AppWrapper CRD to be created", () -> {
CustomResourceDefinition appWrapperCrd = CustomResourceDefinitionContext.v1CRDFromCustomResourceType(AppWrapper.class).build();
kubeClient.apiextensions().v1().customResourceDefinitions()
.resource(appWrapperCrd)
.waitUntilCondition(crdIsEstablishedPredicate(), GLOBAL_TIMEOUT, TimeUnit.MILLISECONDS);
});

Allure.step("Create AppWrapper from yaml file", () -> {
AppWrapper koranteng = kubeClient.resources(AppWrapper.class).load(this.getClass().getResource("/codeflare/koranteng.yaml")).item();
ResourceManager.getInstance().createResourceWithWait(koranteng);
});
});

Allure.step("Wait for Ray API endpoint");
Expand All @@ -107,6 +124,9 @@ void testDistributedWorkload() {
Route route = kubeClient.routes().inNamespace(projectName).withName("ray-dashboard-koranteng").get();
String url = "http://" + route.getStatus().getIngress().get(0).getHost();

Allure.step("Wait for service availability");
TestUtils.waitForServiceNotUnavailable(url);

Allure.step("Run workload through Ray API", () -> {
RayClient ray = new RayClient(url);
String jobId = ray.submitJob("expr 3 + 4");
Expand All @@ -116,4 +136,10 @@ void testDistributedWorkload() {
Assertions.assertEquals("7\n", logs);
});
}

private static Predicate<CustomResourceDefinition> crdIsEstablishedPredicate() {
return c -> c != null && c.getStatus() != null && c.getStatus().getConditions() != null
&& c.getStatus().getConditions().stream()
.anyMatch(crdc -> crdc.getType().equals("Established") && crdc.getStatus().equals("True"));
}
}
17 changes: 1 addition & 16 deletions src/test/java/io/odh/test/e2e/standard/ModelServingST.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -66,8 +65,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.odh.test.TestConstants.GLOBAL_POLL_INTERVAL_SHORT;
import static io.odh.test.TestConstants.GLOBAL_TIMEOUT;
import static io.odh.test.TestUtils.DEFAULT_TIMEOUT_DURATION;
import static io.odh.test.TestUtils.DEFAULT_TIMEOUT_UNIT;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -250,19 +247,7 @@ void queryModelAndCheckMnistInference(String baseUrl, String modelInputPath, Str
.sslContext(sslContext)
.build();

TestUtils.waitFor("route to be available", GLOBAL_POLL_INTERVAL_SHORT, GLOBAL_TIMEOUT, () -> {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.GET()
.timeout(Duration.of(DEFAULT_TIMEOUT_DURATION, DEFAULT_TIMEOUT_UNIT.toChronoUnit()))
.build();
try {
HttpResponse<Void> response = httpClient.send(request, HttpResponse.BodyHandlers.discarding());
return response.statusCode() != 503; // Service Unavailable
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
});
TestUtils.waitForServiceNotUnavailable(httpClient, baseUrl);

HttpRequest inferRequest = HttpRequest.newBuilder()
.uri(new URI("%s/infer".formatted(baseUrl)))
Expand Down
Loading