Skip to content

Commit

Permalink
[issue 128] CI - Fixing test that are executed in simple build GitHub…
Browse files Browse the repository at this point in the history
… workflow
  • Loading branch information
fabiobrz committed Jan 8, 2024
1 parent 0f3c5e0 commit 747428f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/simple-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -305,9 +311,11 @@ public void subscribe(String installPlanApproval, Map<String, String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -41,6 +43,10 @@ default File save() throws IOException {
return save(file);
}

static <T> File save(T data) throws IOException {
return save(File.createTempFile(data.getClass().getSimpleName() + "-", ".yaml"), data);
}

/**
* Write the yaml representation of object into a file.
*
Expand All @@ -53,6 +59,11 @@ default File save(File file) throws IOException {
return file;
}

static <O> File save(File file, O data) throws IOException {
mapper.writeValue(file, data);
return file;
}

/**
* Read a yaml representation of object from a file.
*
Expand All @@ -66,6 +77,13 @@ default T load(File file) throws IOException {

}

static <CR extends CustomResource> CR load(File file, Class<CR> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,13 +43,13 @@ public Subscription() {
}

private SubscriptionFluent<SubscriptionBuilder>.SpecNested<SubscriptionBuilder> 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)
Expand All @@ -60,11 +59,12 @@ private SubscriptionFluent<SubscriptionBuilder>.SpecNested<SubscriptionBuilder>
.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<String, String> 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()
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenShiftResource> 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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<org.wildfly.v1alpha1.WildFlyServerSpec, org.wildfly.v1alpha1.WildFlyServerStatus>
implements OpenShiftResource<WildFlyServerSerializableResource> {

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.
*/
Expand All @@ -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.");
Expand Down

0 comments on commit 747428f

Please sign in to comment.