Skip to content

Commit

Permalink
[issue-1323] - Adding a requirement to mark tests tht need OLM to be …
Browse files Browse the repository at this point in the history
…deployed

Fix CI steps order to prevent OpenSHift setup step frm wiping the local dir contents
  • Loading branch information
fabiobrz committed Dec 19, 2024
1 parent 800ba2b commit 7d81b90
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ jobs:
with:
oc version: 'v3.10.0'
github token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup OLM on OpenShift v3.z
if: ${{ matrix.target == 'openshift' }}
run: |
set -x
# operator-sdk must be installed manually on OpenShift v3
oc version
oc create -f https://github.com/operator-framework/operator-lifecycle-manager/tree/master/deploy/upstream/manifests/0.6.0/
# - name: Setup OLM on OpenShift v3.z
# if: ${{ matrix.target == 'openshift' }}
# run: |
# set -x
# # operator-sdk must be installed manually on OpenShift v3
# oc version
# oc create -f https://github.com/operator-framework/operator-lifecycle-manager/tree/master/deploy/upstream/manifests/0.6.0/
# # OR...
# export ARCH=$(case $(uname -m) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(uname -m) ;; esac)
# export OS=$(uname | awk '{print tolower($0)}')
# export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/v1.37.0
# curl -LO ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH}
# chmod +x operator-sdk_${OS}_${ARCH} && sudo mv operator-sdk_${OS}_${ARCH} /usr/local/bin/operator-sdk
# operator-sdk olm install
- uses: actions/cache@v4
- name: Cache .m2 registry
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
key: cache-e2e-${{ github.sha }}-${{ github.run_id }}
- name: Set up JDK ${{ matrix.java }}
uses: actions/setup-java@v4
with:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.arquillian.cube.openshift.ftest;

import java.net.URL;

import org.arquillian.cube.olm.impl.requirement.RequiresOlm;
import org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift;
import org.arquillian.cube.remote.requirement.RequiresRemoteResource;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
Expand All @@ -14,8 +16,9 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@Category({RequiresOpenshift.class, RequiresRemoteResource.class})
@Category({RequiresOpenshift.class, RequiresOlm.class, RequiresRemoteResource.class})
@RequiresOpenshift
@RequiresOlm
@RunWith(ArquillianConditionalRunner.class)
public class HelloPodDeploymentOpenShiftITCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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.olm.impl.requirement.RequiresOlm;
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 OpenshiftOlmRequirement implements Constraint<RequiresOlm> {

@Override
public void check(RequiresOlm context) throws UnsatisfiedRequirementException {
final List<String> 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<String> 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.");
}
if (client.namespaces().withName("openshift-operators").get() == null) {
throw new UnsatisfiedRequirementException("OLM is not installed on Openshift.");
}
}
} catch (IOException | IllegalArgumentException | InterruptedException | ExecutionException e) {
throw new UnsatisfiedRequirementException(
"Error while checking Openshift version: [" + e.getMessage() + "]");
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.arquillian.cube.openshift.impl.requirement.OpenshiftRequirement
org.arquillian.cube.openshift.impl.requirement.OpenshiftOlmRequirement
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.arquillian.cube.olm.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;

/**
* Marks tests that require Operator Lifecycle Manager
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Requires(Constraint.class)
public @interface RequiresOlm {

String name() default "";
}

0 comments on commit 7d81b90

Please sign in to comment.