Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #255 from pjgg/feat/parallelize-openshift-setup
Browse files Browse the repository at this point in the history
Deploy in parallel several resources.
  • Loading branch information
Pablo Gonzalez Granados authored Apr 12, 2021
2 parents 2b1a7fb + 8083f76 commit 374a62e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ quarkus.my.property=${KEYCLOAK_HTTP_URL:default_value}

Further information about usage of configuration in [here](https://quarkus.io/guides/config-reference#combine-property-env-var).

**Note:** In case that you have several unrelated additional resources as Redis and postgres together in the same application, you can deploy those resources in parallel by adding `@ParallelAdditionalResourcesEnabled` annotation. Note that this label applies to all additional resources of this scenario.

### Running tests in ephemeral namespaces

By default, the test framework expects that the user is logged into an OpenShift project, and that project is used for all tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import io.quarkus.ts.openshift.common.util.AwaitUtil;
import io.quarkus.ts.openshift.common.util.OpenShiftUtil;
import io.restassured.RestAssured;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.fusesource.jansi.Ansi;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
Expand All @@ -39,10 +42,15 @@
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
Expand Down Expand Up @@ -139,18 +147,13 @@ public void beforeAll(ExtensionContext context) throws Exception {

private void doBeforeAll(ExtensionContext context) throws Exception {
System.out.println("---------- OpenShiftTest set up ----------");

createEphemeralNamespaceIfNecessary(context);

deployAdditionalResources(context);

CompletableFuture.allOf(deployAdditionalResources(context)).join();
runPublicStaticVoidMethods(CustomizeApplicationDeployment.class, context);

Map<String, String> envVars = resolveEnvVars(context);
getDeploymentStrategy(context).deploy(envVars);

setUpRestAssured(context);

getAwaitUtil(context).awaitAppRoute();
}

Expand Down Expand Up @@ -180,27 +183,68 @@ private void createEphemeralNamespaceIfNecessary(ExtensionContext context) throw
}
}

private void deployAdditionalResources(ExtensionContext context) throws IOException, InterruptedException {
private CompletableFuture<Void> deployAdditionalResources(ExtensionContext context) {
final String actionName = "deployAdditionalResources";
TestsStatus testsStatus = getTestsStatus(context);
OpenShiftClient oc = getOpenShiftClient(context);
AwaitUtil awaitUtil = getAwaitUtil(context);
Optional<AnnotatedElement> element = context.getElement();
List<CompletableFuture<Pair<String, Boolean>>> additionalResourceStatus = new ArrayList<>();
if (element.isPresent()) {
AnnotatedElement annotatedElement = element.get();
AdditionalResources[] annotations = annotatedElement.getAnnotationsByType(AdditionalResources.class);
boolean isParallelDeployment = isParallelDeploymentEnabled(annotatedElement);
for (AdditionalResources additionalResources : annotations) {
AdditionalResourcesDeployed deployed = AdditionalResourcesDeployed.deploy(additionalResources,
testsStatus, oc, awaitUtil);
Supplier<Pair<String, Boolean>> deployStatus = applyDeployment(context, testsStatus, oc, awaitUtil, additionalResources);
if (isParallelDeployment) {
additionalResourceStatus.add(CompletableFuture.supplyAsync(() -> deployStatus.get()));
} else {
additionalResourceStatus.add(CompletableFuture.completedFuture(deployStatus.get()));
}
}
}
CompletableFuture[] additionalResources = additionalResourceStatus.toArray(new CompletableFuture[0]);
return CompletableFuture.allOf(additionalResources).thenAccept(next -> printResults(actionName, additionalResourceStatus));
}

private boolean isParallelDeploymentEnabled(AnnotatedElement annotatedElement){
return annotatedElement.getAnnotationsByType(ParallelAdditionalResourcesEnabled.class).length > 0;
}

private Supplier<Pair<String, Boolean>> applyDeployment(ExtensionContext context, TestsStatus testsStatus, OpenShiftClient oc, AwaitUtil awaitUtil, AdditionalResources additionalResources) {
return () -> {
String resourceUrl = additionalResources.value();
Pair<String, Boolean> status = MutablePair.of(resourceUrl, true);
try {
AdditionalResourcesDeployed deployed = AdditionalResourcesDeployed.deploy(additionalResources, testsStatus, oc, awaitUtil);
if (EphemeralNamespace.isDisabled()) {
// when using ephemeral namespaces, we don't delete additional resources because:
// - when an ephemeral namespace is dropped, everything is destroyed anyway
// - when retain on failure is enabled and failure occurs,
// everything in the ephemeral namespace must be kept intact
getStore(context).put(new Object(), deployed);
}
return status;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
status.setValue(false);
return status;
}
};
}

private void printResults(String title, List<CompletableFuture<Pair<String, Boolean>>> results) {
System.out.println(ansi().a(String.format("--- Summary: %s ---", title)));
for (CompletableFuture<Pair<String, Boolean>> result : results) {
try {
Ansi content = ansi().a(result.get().getKey() + " ");
content = result.get().getValue() ? content.fgGreen().a("\u2713") : content.fgRed().a("\u2717");
System.out.println(content.reset());
} catch (InterruptedException | ExecutionException e) {
System.err.println(e.getMessage());
}
}
System.out.println(ansi().a("--- end ---"));
}

private void setUpRestAssured(ExtensionContext context) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.ts.openshift.common;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Allow you to deploy several additional resources at once.
* */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ParallelAdditionalResourcesEnabled {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.quarkus.ts.openshift.common.CustomAppMetadata;
import io.quarkus.ts.openshift.common.OnlyIfConfigured;
import io.quarkus.ts.openshift.common.OpenShiftTest;
import io.quarkus.ts.openshift.common.ParallelAdditionalResourcesEnabled;
import io.quarkus.ts.openshift.common.deploy.ManualDeploymentStrategy;
import io.quarkus.ts.openshift.common.injection.TestResource;
import org.junit.jupiter.api.MethodOrderer;
Expand Down Expand Up @@ -33,6 +34,7 @@
@AdditionalResources("classpath:hero.yaml")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@OnlyIfConfigured("ts.authenticated-registry")
@ParallelAdditionalResourcesEnabled
public class HeroesOpenShiftIT {

@TestResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.quarkus.ts.openshift.common.CustomAppMetadata;
import io.quarkus.ts.openshift.common.OnlyIfConfigured;
import io.quarkus.ts.openshift.common.OpenShiftTest;
import io.quarkus.ts.openshift.common.ParallelAdditionalResourcesEnabled;
import io.quarkus.ts.openshift.common.deploy.ManualDeploymentStrategy;
import io.quarkus.ts.openshift.common.injection.TestResource;
import org.hamcrest.core.Is;
Expand Down Expand Up @@ -34,6 +35,7 @@
@AdditionalResources("classpath:villain.yaml")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@OnlyIfConfigured("ts.authenticated-registry")
@ParallelAdditionalResourcesEnabled
public class VillainsOpenShiftIT {

@TestResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import io.quarkus.ts.openshift.common.AdditionalResources;
import io.quarkus.ts.openshift.common.OnlyIfConfigured;
import io.quarkus.ts.openshift.common.OpenShiftTest;
import io.quarkus.ts.openshift.common.ParallelAdditionalResourcesEnabled;

@OpenShiftTest
@AdditionalResources("classpath:deployments/kafka/amq-streams.yaml")
@AdditionalResources("classpath:deployments/kafka/apicurio.yaml")
@OnlyIfConfigured("ts.authenticated-registry")
@ParallelAdditionalResourcesEnabled
public class AmqStreamsOpenShiftIT extends AbstractKafkaTest{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import io.quarkus.ts.openshift.common.AdditionalResources;
import io.quarkus.ts.openshift.common.OpenShiftTest;
import io.quarkus.ts.openshift.common.ParallelAdditionalResourcesEnabled;

@OpenShiftTest
@AdditionalResources("classpath:deployments/kafka/strimzi.yaml")
@AdditionalResources("classpath:deployments/kafka/apicurio.yaml")
@ParallelAdditionalResourcesEnabled
public class StrimziKafkaAvroOpenShiftIT extends AbstractKafkaTest{
}

0 comments on commit 374a62e

Please sign in to comment.