diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 3d493f20d..57eaef894 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -87,7 +87,7 @@ jobs: - name: Build and run integration tests for OpenShift target (${{ matrix.java }}) if: ${{ matrix.target == 'openshift' }} run: | - ./mvnw verify -pl openshift -amd -Dfailsafe.groups=org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift -Dcontainerless.skip.tests=true + ./mvnw verify -pl openshift -amd -Dfailsafe.groups=org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift -Dfailsafe.excludedGroups=org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift4 -Dcontainerless.skip.tests=true - uses: actions/upload-artifact@v4 if: failure() with: diff --git a/openshift/ftest-openshift-routeurl/src/test/java/org/arquillian/cube/openshift/routeurl/RouteInOtherNamespaceIT.java b/openshift/ftest-openshift-routeurl/src/test/java/org/arquillian/cube/openshift/routeurl/RouteInOtherNamespaceIT.java index ef336b8ba..d6c412065 100644 --- a/openshift/ftest-openshift-routeurl/src/test/java/org/arquillian/cube/openshift/routeurl/RouteInOtherNamespaceIT.java +++ b/openshift/ftest-openshift-routeurl/src/test/java/org/arquillian/cube/openshift/routeurl/RouteInOtherNamespaceIT.java @@ -5,8 +5,8 @@ import java.net.URL; import org.arquillian.cube.openshift.impl.enricher.RouteURL; import org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift; +import org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift4; import org.arquillian.cube.requirement.ArquillianConditionalRunner; -import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; @@ -14,6 +14,7 @@ @Category(RequiresOpenshift.class) @RequiresOpenshift @RunWith(ArquillianConditionalRunner.class) +@RequiresOpenshift4 public class RouteInOtherNamespaceIT { @RouteURL(value = "prometheus-k8s", namespace = "openshift-monitoring") diff --git a/openshift/openshift/src/main/java/org/arquillian/cube/openshift/impl/requirement/Openshift4Requirement.java b/openshift/openshift/src/main/java/org/arquillian/cube/openshift/impl/requirement/Openshift4Requirement.java new file mode 100644 index 000000000..bd3c4e63e --- /dev/null +++ b/openshift/openshift/src/main/java/org/arquillian/cube/openshift/impl/requirement/Openshift4Requirement.java @@ -0,0 +1,66 @@ +package org.arquillian.cube.openshift.impl.requirement; + +import io.fabric8.kubernetes.client.Config; +import io.fabric8.kubernetes.client.DefaultKubernetesClient; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.http.HttpClient; +import io.fabric8.kubernetes.client.http.HttpRequest; +import io.fabric8.kubernetes.client.http.HttpResponse; +import io.fabric8.kubernetes.client.jdkhttp.JdkHttpClientFactory; +import io.fabric8.kubernetes.client.utils.URLUtils; +import io.fabric8.openshift.client.OpenShiftClient; +import org.arquillian.cube.kubernetes.impl.ClientConfigBuilder; +import org.arquillian.cube.kubernetes.impl.DefaultConfiguration; +import org.arquillian.cube.kubernetes.impl.ExtensionRegistrar; +import org.arquillian.cube.kubernetes.impl.utils.CommandExecutor; +import org.arquillian.cube.spi.requirement.Constraint; +import org.arquillian.cube.spi.requirement.UnsatisfiedRequirementException; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import static org.arquillian.cube.kubernetes.impl.DefaultConfigurationFactory.KUBERNETES_EXTENSION_NAME; +import static org.arquillian.cube.openshift.impl.client.CubeOpenShiftConfigurationFactory.OPENSHIFT_EXTENSION_NAME; + +public class Openshift4Requirement implements Constraint { + + @Override + public void check(RequiresOpenshift4 context) throws UnsatisfiedRequirementException { + final List extension = Arrays.asList(KUBERNETES_EXTENSION_NAME, OPENSHIFT_EXTENSION_NAME); + + final DefaultConfiguration config = new ExtensionRegistrar().loadExtension(extension); + + final Config httpClientConfig = new ClientConfigBuilder().configuration(config).build(); + try (KubernetesClient client = new DefaultKubernetesClient(httpClientConfig)) { + + HttpClient.Factory httpClientFactory = new JdkHttpClientFactory(); + HttpClient httpClient = httpClientFactory.newBuilder(httpClientConfig).build(); + + HttpRequest versionRequest = httpClient.newHttpRequestBuilder() + .url(new URL(URLUtils.join(client.getMasterUrl().toString(), "version").toString())) + .method("GET", "application/json", null) + .build(); + + HttpResponse response = httpClient.sendAsync(versionRequest, String.class).get(); + if (!response.isSuccessful()) { + throw new UnsatisfiedRequirementException( + "Failed to verify Openshift version, due to: [" + response.message() + "]"); + } else if (!client.isAdaptable(OpenShiftClient.class)) { + throw new UnsatisfiedRequirementException( + "A valid Kubernetes environment was found, but not Openshift."); + } + CommandExecutor ce = new CommandExecutor(); + List ocVersion = ce.execCommand("oc version"); + if (ocVersion.stream().noneMatch(o -> o.contains("Server Version: 4"))) { + throw new UnsatisfiedRequirementException( + "A valid OpenShift 4 environment was found, but not Openshift."); + } + } catch (IOException | IllegalArgumentException | InterruptedException | ExecutionException e) { + throw new UnsatisfiedRequirementException( + "Error while checking Openshift version: [" + e.getMessage() + "]"); + } + } +} diff --git a/openshift/openshift/src/main/resources/META-INF/services/org.arquillian.cube.spi.requirement.Constraint b/openshift/openshift/src/main/resources/META-INF/services/org.arquillian.cube.spi.requirement.Constraint index 2c46890f9..116c95bc8 100644 --- a/openshift/openshift/src/main/resources/META-INF/services/org.arquillian.cube.spi.requirement.Constraint +++ b/openshift/openshift/src/main/resources/META-INF/services/org.arquillian.cube.spi.requirement.Constraint @@ -1,2 +1,3 @@ org.arquillian.cube.openshift.impl.requirement.OpenshiftRequirement +org.arquillian.cube.openshift.impl.requirement.Openshift4Requirement org.arquillian.cube.openshift.impl.requirement.OpenshiftOlmRequirement diff --git a/requirement-spi/src/main/java/org/arquillian/cube/openshift/impl/requirement/RequiresOpenshift4.java b/requirement-spi/src/main/java/org/arquillian/cube/openshift/impl/requirement/RequiresOpenshift4.java new file mode 100644 index 000000000..b46a4136d --- /dev/null +++ b/requirement-spi/src/main/java/org/arquillian/cube/openshift/impl/requirement/RequiresOpenshift4.java @@ -0,0 +1,17 @@ +package org.arquillian.cube.openshift.impl.requirement; + +import org.arquillian.cube.spi.requirement.Constraint; +import org.arquillian.cube.spi.requirement.Requires; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Requires(Constraint.class) +public @interface RequiresOpenshift4 { + + String name() default ""; +}