From 747428ff36c53ee000460ad53c3b82d215e9554d Mon Sep 17 00:00:00 2001 From: Fabio Burzigotti Date: Fri, 22 Dec 2023 12:57:31 +0100 Subject: [PATCH] [issue 128] CI - Fixing test that are executed in simple build GitHub workflow --- .github/workflows/simple-build.yml | 2 +- .../operator/OperatorProvisioner.java | 16 +++++-- .../operator/resources/CatalogSource.java | 18 +++----- .../operator/resources/OpenShiftResource.java | 18 ++++++++ .../operator/resources/Subscription.java | 15 ++++--- .../operator/resources/SubscriptionTest.java | 1 + .../wildfly/WildFlyServerBuilder.java | 5 ++- ...ersmash.tools.provision.ProvisionerFactory | 5 ++- .../resources/OpenShiftResourceTestCase.java | 44 +++++++++++++++---- .../wildfly/WildFlyServersTestCase.java | 25 +---------- 10 files changed, 89 insertions(+), 60 deletions(-) diff --git a/.github/workflows/simple-build.yml b/.github/workflows/simple-build.yml index ec64769fd..5b6631cff 100644 --- a/.github/workflows/simple-build.yml +++ b/.github/workflows/simple-build.yml @@ -23,4 +23,4 @@ jobs: java-version: '17' distribution: 'temurin' - name: Build with Maven - run: mvn clean install -U -B -DskipTests ; mvn test -pl deployments/intersmash-deployments-provider,tools/intersmash-tools-core,tools/intersmash-tools-provisioners -am + run: mvn -version ; mvn clean install -U -B -DskipTests ; mvn test -pl deployments/intersmash-deployments-provider,tools/intersmash-tools-core,tools/intersmash-tools-provisioners -am diff --git a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/OperatorProvisioner.java b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/OperatorProvisioner.java index d4ee9242c..3c659305d 100644 --- a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/OperatorProvisioner.java +++ b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/OperatorProvisioner.java @@ -220,9 +220,15 @@ private CatalogSource initCatalogSource() { } } else { // load CatalogSource by name from OpenShift cluster + io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource existing = OpenShifts + .admin(IntersmashConfig.defaultOperatorCatalogSourceNamespace()).operatorHub() + .catalogSources().list().getItems() + .stream().filter(cs -> cs.getMetadata().getName().equalsIgnoreCase(operatorCatalogSource)) + .findFirst().orElseThrow( + () -> new IllegalStateException( + "Unable to retrieve CatalogSource " + operatorCatalogSource)); catalogSource = new CatalogSource(); - catalogSource.load(operatorCatalogSource, - IntersmashConfig.defaultOperatorCatalogSourceNamespace()); + catalogSource.load(existing); } return catalogSource; } @@ -305,9 +311,11 @@ public void subscribe(String installPlanApproval, Map envVariabl log.info("Subscribing the {} operator", packageManifestName); // oc get packagemanifest wildfly -o template --template {{.status.defaultChannel}} Subscription operatorSubscription = (envVariables == null || envVariables.isEmpty()) - ? new Subscription(getCatalogSourceNamespace(), getOperatorCatalogSource(), packageManifestName, + ? new Subscription(getCatalogSourceNamespace(), OpenShiftConfig.namespace(), getOperatorCatalogSource(), + packageManifestName, operatorChannel, installPlanApproval) - : new Subscription(getCatalogSourceNamespace(), getOperatorCatalogSource(), packageManifestName, + : new Subscription(getCatalogSourceNamespace(), OpenShiftConfig.namespace(), getOperatorCatalogSource(), + packageManifestName, operatorChannel, installPlanApproval, envVariables); try { adminBinary.execute("apply", "-f", operatorSubscription.save().getAbsolutePath()); diff --git a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/CatalogSource.java b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/CatalogSource.java index b7e30e59f..96c10f4d2 100644 --- a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/CatalogSource.java +++ b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/CatalogSource.java @@ -15,7 +15,6 @@ */ package org.jboss.intersmash.tools.provision.openshift.operator.resources; -import cz.xtf.core.openshift.OpenShifts; import io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSourceBuilder; public class CatalogSource extends io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource @@ -58,20 +57,13 @@ public CatalogSource load(CatalogSource loaded) { /** * Load CatalogSource by name from OpenShift cluster - * @param catalogSourceName name of the CatalogSource e.g. certified-operators, community-operators, - * redhat-marketplace, redhat-operators,... + * @param existing Existing instance of {@link io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource}, + * as loaded from the cluster * @return CatalogSource */ - public CatalogSource load(String catalogSourceName, String catalogSourceNamespace) { - io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource catalogSource = OpenShifts - .admin(catalogSourceNamespace).operatorHub() - .catalogSources().list().getItems() - .stream().filter(cs -> cs.getMetadata().getName().equalsIgnoreCase(catalogSourceName)) - .findFirst().orElseThrow( - () -> new IllegalStateException( - "Unable to retrieve CatalogSource " + catalogSourceName)); - this.setMetadata(catalogSource.getMetadata()); - this.setSpec(catalogSource.getSpec()); + public CatalogSource load(io.fabric8.openshift.api.model.operatorhub.v1alpha1.CatalogSource existing) { + this.setMetadata(existing.getMetadata()); + this.setSpec(existing.getSpec()); return this; } } diff --git a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResource.java b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResource.java index cec581730..3a06c4d85 100644 --- a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResource.java +++ b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResource.java @@ -23,6 +23,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import io.fabric8.kubernetes.client.CustomResource; + /** * Interface of common methods for OpenShift resources. */ @@ -41,6 +43,10 @@ default File save() throws IOException { return save(file); } + static File save(T data) throws IOException { + return save(File.createTempFile(data.getClass().getSimpleName() + "-", ".yaml"), data); + } + /** * Write the yaml representation of object into a file. * @@ -53,6 +59,11 @@ default File save(File file) throws IOException { return file; } + static File save(File file, O data) throws IOException { + mapper.writeValue(file, data); + return file; + } + /** * Read a yaml representation of object from a file. * @@ -66,6 +77,13 @@ default T load(File file) throws IOException { } + static CR load(File file, Class clazz, CR target) throws IOException { + CR loaded = mapper.readValue(file, clazz); + target.setMetadata(loaded.getMetadata()); + target.setSpec(loaded.getSpec()); + return target; + } + /** * Read a yaml representation of object from a input stream. * diff --git a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/Subscription.java b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/Subscription.java index ecaa52987..e1174f119 100644 --- a/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/Subscription.java +++ b/tools/intersmash-tools-core/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/Subscription.java @@ -20,7 +20,6 @@ import org.assertj.core.util.Strings; -import cz.xtf.core.config.OpenShiftConfig; import io.fabric8.kubernetes.api.model.EnvVar; import io.fabric8.openshift.api.model.operatorhub.v1alpha1.SubscriptionBuilder; import io.fabric8.openshift.api.model.operatorhub.v1alpha1.SubscriptionFluent; @@ -44,13 +43,13 @@ public Subscription() { } private SubscriptionFluent.SpecNested getConfiguredSubscriptionBuilder( - String sourceNamespace, + String sourceNamespace, String targetNamespace, String source, String name, String channel, String installPlanApproval) { return new SubscriptionBuilder() .withNewMetadata() .withName(name) - .withNamespace(OpenShiftConfig.namespace()) + .withNamespace(targetNamespace) .endMetadata() .withNewSpec() .withChannel(channel) @@ -60,11 +59,12 @@ private SubscriptionFluent.SpecNested .withInstallPlanApproval(Strings.isNullOrEmpty(installPlanApproval) ? "Automatic" : installPlanApproval); } - public Subscription(String sourceNamespace, String source, String name, String channel, String installPlanApproval, + public Subscription(String sourceNamespace, String targetNamespace, String source, String name, String channel, + String installPlanApproval, Map envVariables) { this(); io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription loaded = getConfiguredSubscriptionBuilder( - sourceNamespace, source, name, channel, installPlanApproval) + sourceNamespace, targetNamespace, source, name, channel, installPlanApproval) .withNewConfig() .addAllToEnv( envVariables.entrySet().stream() @@ -77,10 +77,11 @@ public Subscription(String sourceNamespace, String source, String name, String c this.setSpec(loaded.getSpec()); } - public Subscription(String sourceNamespace, String source, String name, String channel, String installPlanApproval) { + public Subscription(String sourceNamespace, String targetNamespace, String source, String name, String channel, + String installPlanApproval) { this(); io.fabric8.openshift.api.model.operatorhub.v1alpha1.Subscription loaded = getConfiguredSubscriptionBuilder( - sourceNamespace, source, name, channel, installPlanApproval) + sourceNamespace, targetNamespace, source, name, channel, installPlanApproval) .endSpec() .build(); this.setMetadata(loaded.getMetadata()); diff --git a/tools/intersmash-tools-core/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/SubscriptionTest.java b/tools/intersmash-tools-core/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/SubscriptionTest.java index c35a6b199..5b7a15466 100644 --- a/tools/intersmash-tools-core/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/SubscriptionTest.java +++ b/tools/intersmash-tools-core/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/SubscriptionTest.java @@ -35,6 +35,7 @@ public void testEnv() throws IOException { final String propertyValue = "dummy-prop-value"; Subscription subscription = new Subscription( "openshift-marketplace", + "my-namespace", "redhat-operators", "dummy-operator", "alpha", diff --git a/tools/intersmash-tools-provisioners/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServerBuilder.java b/tools/intersmash-tools-provisioners/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServerBuilder.java index cc55a4e27..dc22f716c 100644 --- a/tools/intersmash-tools-provisioners/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServerBuilder.java +++ b/tools/intersmash-tools-provisioners/src/main/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServerBuilder.java @@ -251,8 +251,9 @@ public WildFlyServer build() { WildFlyServer wildFlyServer = new WildFlyServer(); wildFlyServer.setMetadata(new ObjectMeta()); wildFlyServer.getMetadata().setName(name); - wildFlyServer.getMetadata().setLabels(labels); - + if (labels != null) { + wildFlyServer.getMetadata().setLabels(labels); + } WildFlyServerSpec wildFlyServerSpec = new WildFlyServerSpec(); wildFlyServerSpec.setApplicationImage(applicationImage); wildFlyServerSpec.setReplicas(replicas); diff --git a/tools/intersmash-tools-provisioners/src/main/resources/META-INF/services/org.jboss.intersmash.tools.provision.ProvisionerFactory b/tools/intersmash-tools-provisioners/src/main/resources/META-INF/services/org.jboss.intersmash.tools.provision.ProvisionerFactory index 60374f5bd..ada5200a8 100644 --- a/tools/intersmash-tools-provisioners/src/main/resources/META-INF/services/org.jboss.intersmash.tools.provision.ProvisionerFactory +++ b/tools/intersmash-tools-provisioners/src/main/resources/META-INF/services/org.jboss.intersmash.tools.provision.ProvisionerFactory @@ -3,11 +3,12 @@ org.jboss.intersmash.tools.provision.openshift.KafkaOperatorProvisionerFactory org.jboss.intersmash.tools.provision.openshift.WildflyBootableJarImageOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.WildflyImageOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.WildflyOperatorProvisionerFactory +org.jboss.intersmash.tools.provision.helm.WildflyHelmChartOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.MysqlImageOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.PostgreSQLImageOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.InfinispanOperatorProvisionerFactory org.jboss.intersmash.tools.provision.openshift.RhSsoOperatorProvisionerFactory -org.jboss.intersmash.tools.provision.helm.WildflyHelmChartOpenShiftProvisionerFactory -org.jboss.intersmash.tools.provision.openshift.Eap7ImageOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.RhSsoTemplateOpenShiftProvisionerFactory org.jboss.intersmash.tools.provision.openshift.PostgreSQLTemplateOpenShiftProvisionerFactory +org.jboss.intersmash.tools.provision.openshift.Eap7ImageOpenShiftProvisionerFactory +org.jboss.intersmash.tools.provision.openshift.Eap7LegacyS2iBuildTemplateProvisionerFactory diff --git a/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResourceTestCase.java b/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResourceTestCase.java index f5efc5990..2c42f0d94 100644 --- a/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResourceTestCase.java +++ b/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/resources/OpenShiftResourceTestCase.java @@ -17,27 +17,55 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.stream.Stream; +import org.jboss.intersmash.tools.provision.openshift.operator.OperatorProvisioner; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; /** * Verify the functionality provided by {@link OpenShiftResource} interface. */ public class OpenShiftResourceTestCase { + private static Stream resourceProvider() { + return Stream.of( + new OperatorGroup("my-namepace"), + new Subscription( + "openshift-marketplace", + "my-namespace", + "redhat-operators", + "dummy-operator", + "alpha", + OperatorProvisioner.INSTALLPLAN_APPROVAL_MANUAL), + new CatalogSource( + "my-custom-cs", + "my-namespace", + "grpc", + "quay.io/operatorhubio/catalog:latest", + "My Custom Operators", + "Intersmash")); + } + /** * Verify that object equals after serialization to file and deserialization back to object. */ - @Test - public void writeReadEqualsTest() throws IOException { + + @ParameterizedTest(name = "{displayName}#class({0})") + @MethodSource("resourceProvider") + public void writeReadEqualsTest(OpenShiftResource resource) throws IOException, NoSuchMethodException, + InvocationTargetException, InstantiationException, IllegalAccessException { // write test - File yaml = OperatorGroup.SINGLE_NAMESPACE.save(); + File yaml = resource.save(); // read test - OpenShiftResource testGroup = new OperatorGroup(); - testGroup.load(yaml); + + OpenShiftResource loaded = resource.getClass().getDeclaredConstructor().newInstance(); + loaded.load(yaml); // - Assertions.assertEquals(OperatorGroup.SINGLE_NAMESPACE, testGroup, - "OpenShift resource (OperatorGroup) does not equal after serialization into yaml file and deserialization back to an object."); + Assertions.assertEquals(resource, loaded, + "OpenShift resource (" + resource.getClass().getSimpleName() + + ") does not equal after serialization into yaml file and deserialization back to an object."); } } diff --git a/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServersTestCase.java b/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServersTestCase.java index c045629f7..953434e6a 100644 --- a/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServersTestCase.java +++ b/tools/intersmash-tools-provisioners/src/test/java/org/jboss/intersmash/tools/provision/openshift/operator/wildfly/WildFlyServersTestCase.java @@ -24,31 +24,11 @@ import org.junit.jupiter.api.Test; import org.wildfly.v1alpha1.WildFlyServer; -import io.fabric8.kubernetes.client.CustomResource; - /** * Basic verification of wildflyservers.wildfly.org resource. */ public class WildFlyServersTestCase { - class WildFlyServerSerializableResource - extends CustomResource - implements OpenShiftResource { - - private final WildFlyServer wildFlyServer; - - WildFlyServerSerializableResource(WildFlyServer wildFlyServer) { - this.wildFlyServer = wildFlyServer; - } - - @Override - public WildFlyServerSerializableResource load(WildFlyServerSerializableResource loaded) { - this.wildFlyServer.setMetadata(loaded.getMetadata()); - this.wildFlyServer.setSpec(loaded.getSpec()); - return this; - } - } - /** * Verify that object equals after serialization to file and deserialization back to object. */ @@ -60,11 +40,10 @@ public void writeReadEqualsTest() throws IOException { .build(); // write test - final WildFlyServerSerializableResource serde = new WildFlyServerSerializableResource(wildFlyServer); - File yaml = serde.save(); + File yaml = OpenShiftResource.save(wildFlyServer); // read test WildFlyServer testServer = new WildFlyServer(); - serde.load(yaml); + OpenShiftResource.load(yaml, WildFlyServer.class, testServer); // Assertions.assertEquals(wildFlyServer, testServer, "OpenShift resource (WildflyServer) does not equal after serialization into yaml file and deserialization back to an object.");